Skip to content

Commit 95b2b92

Browse files
committed
open file and get object with nullptr check
1 parent 70011eb commit 95b2b92

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

PWGHF/D2H/Macros/runMassFitter.C

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@
5151

5252
using namespace rapidjson;
5353

54-
int runMassFitter(const std::string& configFileName = "config_massfitter.json");
54+
void runMassFitter(const std::string& configFileName = "config_massfitter.json");
55+
56+
TFile* openFileWithNullptrCheck(const std::string& fileName, const std::string& option = "read");
57+
58+
template <typename T>
59+
T* getObjectWithNullPtrCheck(TFile* fileIn, const std::string& objectName);
5560

5661
template <typename ValueType>
5762
void readArray(const Value& jsonArray, std::vector<ValueType>& output)
@@ -75,7 +80,7 @@ void parseStringArray(const Value& jsonArray, std::vector<std::string>& output)
7580
void divideCanvas(TCanvas* c, int nSliceVarBins);
7681
void setHistoStyle(TH1* histo, Color_t color = kBlack, Size_t markerSize = 1);
7782

78-
int runMassFitter(const std::string& configFileName)
83+
void runMassFitter(const std::string& configFileName)
7984
{
8085
// load config
8186
FILE* configFile = fopen(configFileName.c_str(), "r");
@@ -231,17 +236,11 @@ int runMassFitter(const std::string& configFileName)
231236
const std::vector<std::string> plotLabels = {std::get<2>(particleTuple), collisionSystem.c_str()};
232237

233238
// load inv-mass histograms
234-
auto* inputFile = TFile::Open(inputFileName.c_str());
235-
if ((inputFile == nullptr) || !inputFile->IsOpen()) {
236-
return -1;
237-
}
239+
auto* inputFile = openFileWithNullptrCheck(inputFileName);
238240

239241
TFile* inputFileRefl = nullptr;
240242
if (enableRefl) {
241-
inputFileRefl = TFile::Open(reflFileName.c_str());
242-
if ((inputFileRefl == nullptr) || !inputFileRefl->IsOpen()) {
243-
return -1;
244-
}
243+
inputFileRefl = openFileWithNullptrCheck(reflFileName);
245244
}
246245

247246
std::vector<TH1*> hMassSgn(nSliceVarBins);
@@ -250,32 +249,26 @@ int runMassFitter(const std::string& configFileName)
250249

251250
for (int iSliceVar = 0; iSliceVar < nSliceVarBins; iSliceVar++) {
252251
if (!isMc) {
253-
hMass[iSliceVar] = inputFile->Get<TH1>(inputHistoName[iSliceVar].data());
252+
hMass[iSliceVar] = getObjectWithNullPtrCheck<TH1>(inputFile, inputHistoName[iSliceVar]);
254253
if (enableRefl) {
255-
hMassRefl[iSliceVar] = inputFileRefl->Get<TH1>(reflHistoName[iSliceVar].data());
256-
if (hMassRefl[iSliceVar] == nullptr) {
257-
throw std::runtime_error("ERROR: MC reflection histogram not found! Exit!");
258-
}
259-
hMassSgn[iSliceVar] = inputFileRefl->Get<TH1>(fdHistoName[iSliceVar].data());
260-
if (hMassSgn[iSliceVar] == nullptr) {
261-
throw std::runtime_error("ERROR: MC prompt or FD histogram not found! Exit!");
262-
}
263-
hMassSgn[iSliceVar]->Add(inputFileRefl->Get<TH1>(promptHistoName[iSliceVar].data()));
254+
hMassRefl[iSliceVar] = getObjectWithNullPtrCheck<TH1>(inputFileRefl, reflHistoName[iSliceVar]);
255+
hMassSgn[iSliceVar] = getObjectWithNullPtrCheck<TH1>(inputFileRefl, fdHistoName[iSliceVar]);
256+
hMassSgn[iSliceVar]->Add(getObjectWithNullPtrCheck<TH1>(inputFileRefl, promptHistoName[iSliceVar]));
264257
}
265258
} else {
266-
hMass[iSliceVar] = inputFile->Get<TH1>(promptHistoName[iSliceVar].data());
267-
hMass[iSliceVar]->Add(inputFile->Get<TH1>(fdHistoName[iSliceVar].data()));
259+
hMass[iSliceVar] = getObjectWithNullPtrCheck<TH1>(inputFile, promptHistoName[iSliceVar]);
260+
hMass[iSliceVar]->Add(getObjectWithNullPtrCheck<TH1>(inputFile, fdHistoName[iSliceVar]));
268261
if (includeSecPeak) {
269-
hMass[iSliceVar]->Add(inputFile->Get<TH1>(promptSecPeakHistoName[iSliceVar].data()));
270-
hMass[iSliceVar]->Add(inputFile->Get<TH1>(fdSecPeakHistoName[iSliceVar].data()));
262+
hMass[iSliceVar]->Add(getObjectWithNullPtrCheck<TH1>(inputFile, promptSecPeakHistoName[iSliceVar]));
263+
hMass[iSliceVar]->Add(getObjectWithNullPtrCheck<TH1>(inputFile, fdSecPeakHistoName[iSliceVar]));
271264
}
272265
}
273-
if (hMass[iSliceVar] == nullptr) {
274-
throw std::runtime_error("ERROR: input histogram for fit not found! Exit!");
275-
}
276266
hMass[iSliceVar]->SetDirectory(nullptr);
277267
}
278268
inputFile->Close();
269+
if (enableRefl) {
270+
inputFileRefl->Close();
271+
}
279272

280273
// define output histos
281274
auto* hRawYieldsSignal = new TH1D("hRawYieldsSignal", ";" + sliceVarName + "(" + sliceVarUnit + ");raw yield", nSliceVarBins, sliceVarLimits.data());
@@ -327,12 +320,9 @@ int runMassFitter(const std::string& configFileName)
327320
TH1* histToFix = nullptr;
328321
if (isFix) {
329322
if (fixManual.empty()) {
330-
auto* fixInputFile = TFile::Open(fixFileName.data());
331-
if (fixInputFile == nullptr) {
332-
throw std::runtime_error("Cannot open file for fixed " + var);
333-
}
323+
auto* fixInputFile = openFileWithNullptrCheck(fixFileName);
334324
const std::string histName = "hRawYields" + var;
335-
histToFix = fixInputFile->Get<TH1>(histName.data());
325+
histToFix = getObjectWithNullPtrCheck<TH1>(fixInputFile, histName);
336326
histToFix->SetDirectory(nullptr);
337327
if (histToFix->GetNbinsX() != nSliceVarBins) {
338328
throw std::runtime_error("Different number of bins for this analysis and histo for fixed " + var);
@@ -568,7 +558,6 @@ int runMassFitter(const std::string& configFileName)
568558
canvasResiduals[iCanvas]->Print(Form("%s%s", outputFileNameResidual.Data(), printingBracket.c_str()), "pdf");
569559
}
570560
}
571-
return 0;
572561
}
573562

574563
void setHistoStyle(TH1* histo, Color_t color, Size_t markerSize)
@@ -588,6 +577,25 @@ void divideCanvas(TCanvas* canvas, int nSliceVarBins)
588577
canvas->Divide(nCols, nRows);
589578
}
590579

580+
TFile* openFileWithNullptrCheck(const std::string& fileName, const std::string& option)
581+
{
582+
TFile* file = TFile::Open(fileName.c_str(), option.c_str());
583+
if (file == nullptr || file->IsZombie()) {
584+
throw std::runtime_error("openFileWithNullptrCheck(): Cannot open file " + fileName);
585+
}
586+
return file;
587+
}
588+
589+
template <typename T>
590+
T* getObjectWithNullPtrCheck(TFile* fileIn, const std::string& objectName)
591+
{
592+
T* ptr = fileIn->Get<T>(objectName.c_str());
593+
if (ptr == nullptr) {
594+
throw std::runtime_error("getObjectWithNullptrCheck() - object " + objectName + " in file " + fileIn->GetName() + " is missing");
595+
}
596+
return ptr;
597+
}
598+
591599
int main(int argc, const char* argv[])
592600
{
593601
if (argc == 1) {

0 commit comments

Comments
 (0)