19 #include "reportingUtils.hpp"
20 #include "fileUtils.hpp"
33 convertTFitResultTree(
const string& inFileNamePattern =
"./*.root",
34 const string& outDirName =
"",
35 const unsigned long int maxNmbEvents = -1,
36 const string& inTreeName =
"pwa",
37 const string& outTreeName =
"pwa",
38 const string& inBranchName =
"fitResult",
39 const string& outBranchName =
"fitResult_v2")
43 string _outDirName = outDirName;
44 if (outDirName ==
"") {
45 char* s = strdup(inFileNamePattern.c_str());
46 _outDirName = dirname(s);
49 printInfo <<
"converting TFitResult trees in '" << inFileNamePattern <<
"' "
50 <<
"into fitResult trees." << endl;
51 cout <<
" writing to '" << _outDirName <<
"'" << endl;
54 const vector<string> inFileNames = filesMatchingGlobPattern(inFileNamePattern);
55 const unsigned int nmbInFiles = inFileNames.size();
56 cout <<
" found " << nmbInFiles <<
" files." << endl;
57 if (inFileNames.size() == 0) {
58 printErr <<
"no files do match pattern '" << inFileNamePattern <<
"'. exiting." << endl;
63 unsigned long int countEntriesWritten = 0;
64 for (
unsigned int fileIndex = 0; fileIndex < nmbInFiles; ++fileIndex) {
65 const string outFileName = _outDirName +
"/" + basename(inFileNames[fileIndex].c_str());
66 const bool updateMode = (outFileName == inFileNames[fileIndex]) ?
true :
false;
69 cout <<
" reading from input file '" << inFileNames[fileIndex] <<
"' "
70 <<
"[" << fileIndex <<
"/" << nmbInFiles <<
"]" << endl;
71 TFile* inFile = TFile::Open(inFileNames[fileIndex].c_str(), (updateMode) ?
"UPDATE" :
"READ");
72 if (!inFile || inFile->IsZombie()) {
73 printWarn <<
"cannot open file '" << inFileNames[fileIndex] <<
"'. skipping." << endl;
78 cout <<
" writing to output file '" << outFileName <<
"'" << endl;
83 outFile = TFile::Open(outFileName.c_str(),
"RECREATE");
84 if (!outFile || outFile->IsZombie()) {
85 printWarn <<
"cannot open file '" << outFileName <<
"'. skipping." << endl;
91 inFile->GetObject(inTreeName.c_str(), inTree);
93 printWarn <<
"cannot find tree '" << inTreeName <<
"'. skipping." << endl;
96 TFitResult* inResult = 0;
97 TBranch* inBranch = 0;
98 inTree->SetBranchAddress(inBranchName.c_str(), &inResult, &inBranch);
111 outTree =
new TTree(outTreeName.c_str(), outTreeName.c_str());
113 string _outBranchName = outBranchName;
114 if (updateMode && (outBranchName == inBranchName))
115 _outBranchName +=
"_v2";
116 if (outTree->FindBranch(_outBranchName.c_str())) {
117 printWarn <<
"branch '" << _outBranchName <<
"' already exists in output file. "
118 <<
"skipping." << endl;
121 TBranch* outBranch = outTree->Branch(_outBranchName.c_str(), &outResult);
124 const unsigned long int nmbEvents = inTree->GetEntriesFast();
125 for (
unsigned long int eventIndex = 0; eventIndex < nmbEvents; ++eventIndex) {
126 inTree->GetEntry(eventIndex);
128 printWarn <<
"zero pointer for TFitResult at index " << eventIndex <<
". skipping." << endl;
133 if (updateMode && (outTreeName == inTreeName))
139 ++countEntriesWritten;
140 if (countEntriesWritten >= maxNmbEvents)
144 outTree->Write(
"", TObject::kOverwrite);
147 if (inFile != outFile) {
152 if (countEntriesWritten >= maxNmbEvents)
155 printInfo <<
"wrote " << countEntriesWritten <<
" entries to file." << endl;
160 #endif // USE_TFITRESULT