48 dbgPrint(
const debugLevel_t setLvl = none) : m_currLvl(setLvl) {};
49 inline void operator()(
const debugLevel_t level,
const std::string&
msg)
const {
50 if (level <= m_currLvl)
51 std::cout <<
msg << std::endl;
53 void setLvl(
const debugLevel_t lvl) { m_currLvl = lvl; }
54 debugLevel_t getLvl()
const {
return m_currLvl; }
57 debugLevel_t m_currLvl;
60 static dbgPrint s_dbg;
61 static bool s_checkEquality=
false;
63 std::optional<std::regex> checkRegEx(
const std::string&
re) {
70 std::string test(
"Test String");
71 std::regex_match(test, reNew);
72 }
catch (std::exception& e) {
73 std::cout <<
"ERROR: Invalid RegEx string \"" <<
re <<
"\"." << std::endl;
74 std::cout <<
"See https://en.cppreference.com/w/cpp/regex.html for allowed regular expression syntax" << std::endl;
84 explicit histCollection(TFile* out,
bool skipExisting =
false) : m_out{out}, m_skipExisting(skipExisting) {};
89 void addDirectory(TDirectory* dir,
const std::string& dirName,
const std::string& filename =
"");
90 size_t size() {
return m_data.size(); };
94 void addDirExclusion(
const std::optional<std::regex>& dirEx);
95 void addHistExclusion(
const std::optional<std::regex>& histEx);
97 unsigned size()
const;
100 fileLBMap_t getFileLBMapAndClear() {
102 return std::move(m_fileLBMap);
110 histPerDir_t(
const std::string& nameIn, std::unique_ptr<TObject>&& objIn, TTree* md);
111 histPerDir_t(dqutils::histCollection::histPerDir_t&& other)
112 : name(std::move(other.name)), obj(std::move(other.obj)), metadata(std::move(other.metadata)), mergeMethod(other.mergeMethod) {}
115 std::unique_ptr<TObject> obj;
116 std::array<std::string, 3> metadata{
"unset",
"",
"<default>"};
117 std::clock_t cpuSum = 0;
118 void (*mergeMethod)(TObject*
a,
const TObject* b) =
nullptr;
119 void merge(TObject* other);
122 bool fillMD(TTree* mdTree);
126 std::unordered_map<std::string, histPerDir_t> histos;
127 void writeMD(TDirectory* outDir)
const;
133 std::unordered_map<std::string, histDir_t> m_data;
134 std::optional<std::regex> m_dirExclusion;
135 std::optional<std::regex> m_histExclusion;
136 fileLBMap_t m_fileLBMap;
139 void histCollection::clear() {
144 void histCollection::printTiming() {
145 std::vector<std::pair<std::string, clock_t>> cpuPerHistVec;
146 for (
auto& [
dirname, histDir] : m_data) {
147 for (
auto& [histname, histo] : histDir.histos) {
148 cpuPerHistVec.emplace_back(
dirname +
"/" + histname, histo.cpuSum);
151 auto ordering = [](std::pair<std::string, clock_t>
a, std::pair<std::string, clock_t> b) {
return a.second < b.second; };
152 std::sort(cpuPerHistVec.begin(), cpuPerHistVec.end(), ordering);
153 for (
const auto& [name, time] : cpuPerHistVec) {
154 const double tSec = double(time) / CLOCKS_PER_SEC;
155 std::cout << std::format(
"{:<30} : {:10.3f}", name, tSec) << std::endl;
160 bool histCollection::histPerDir_t::fillMD(TTree * md) {
161 TTreeReader reader(md);
162 TTreeReaderArray<char> i_name(reader,
"Name");
163 TTreeReaderArray<char> i_interval(reader,
"Interval");
164 TTreeReaderArray<char> i_chain(reader,
"TriggerChain");
165 TTreeReaderArray<char> i_merge(reader,
"MergeMethod");
168 while (reader.Next()) {
169 const std::string nameStr(
static_cast<char*
>(i_name.GetAddress()));
170 if (name == nameStr) {
171 metadata = {
static_cast<char*
>(i_interval.GetAddress()),
static_cast<char*
>(i_chain.GetAddress()),
static_cast<char*
>(i_merge.GetAddress())};
179 void histCollection::histPerDir_t::merge(TObject * other) {
181 const std::clock_t cpuStart = std::clock();
182 this->mergeMethod(obj.get(), other);
183 cpuSum += std::clock() - cpuStart;
188 void histCollection::histDir_t::writeMD(TDirectory * out)
const {
191 std::map<std::string, std::array<std::string, 3>> metadatamap;
192 std::unique_ptr<TTree> oldMD((TTree*)out->Get(
"metadata"));
194 TTreeReader reader(oldMD.get());
195 TTreeReaderArray<char> i_name(reader,
"Name");
196 TTreeReaderArray<char> i_interval(reader,
"Interval");
197 TTreeReaderArray<char> i_chain(reader,
"TriggerChain");
198 TTreeReaderArray<char> i_merge(reader,
"MergeMethod");
200 while (reader.Next()) {
201 const std::string name(
static_cast<char*
>(i_name.GetAddress()));
202 metadatamap[name] = {
static_cast<char*
>(i_interval.GetAddress()),
static_cast<char*
>(i_chain.GetAddress()),
static_cast<char*
>(i_merge.GetAddress())};
206 for (
const auto& [key,
h] : histos) {
207 if (
h.metadata[0]!=
"unset")
208 metadatamap[key] =
h.metadata;
211 if (metadatamap.empty())
return;
212 std::string interval, chain,
merge;
214 std::unique_ptr<TTree> mdTree = std::make_unique<TTree>(
"metadata",
"Monitoring Metadata");
215 mdTree->SetDirectory(out);
217 mdTree->Branch(
"Name", (
void*)
nullptr,
"Name/C");
218 mdTree->Branch(
"Interval", interval.data(),
"Interval/C");
219 mdTree->Branch(
"TriggerChain", chain.data(),
"TriggerChain/C");
220 mdTree->Branch(
"MergeMethod",
merge.data(),
"MergeMethod/C");
222 mdTree->SetBranchAddress(
"Name", histname);
224 for (
auto& [key,
h] : metadatamap) {
225 strncpy(histname, key.c_str(), 1023);
231 mdTree->Write(0, TObject::kOverwrite);
234 histCollection::~histCollection() {}
236 void histCollection::addDirExclusion(
const std::optional<std::regex>& dir) {
237 m_dirExclusion = dir;
241 void histCollection::addHistExclusion(
const std::optional<std::regex>& dir) {
242 m_histExclusion = dir;
248 for (
const auto& it : m_data) {
249 s += it.second.histos.size();
255 for (
const auto& it : m_data) {
256 const histDir_t& hd = it.second;
257 std::cout <<
"Dir: " << it.first <<
" has " << hd.histos.size() <<
" histos" << std::endl;
258 for (
const auto& it1 : hd.histos)
259 std::cout <<
"\t" << it1.second.name << std::endl;
264 template <
class HIST>
266 static_cast<HIST*
>(
a)->Add(
static_cast<const HIST*
>(b));
271 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
272 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
274 std::cout <<
"ERROR in weightedAverageTH1: Object not of type TH1" << std::endl;
276 if (b1->GetEntries()==0)
return;
283 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
284 const TH1* b1 = (
dynamic_cast<const TH1*
>(b));
286 std::cout <<
"ERROR in weightedEff: Object not of type TH1" << std::endl;
288 if (b1->GetEntries()==0)
return;
295 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
296 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
298 std::cout <<
"ERROR in mergeRMS: Object not of type TH1" << std::endl;
300 if (b1->GetEntries()==0)
return;
307 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
308 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
310 std::cout <<
"ERROR in RMSpercentDeviation: Object not of type TH1" << std::endl;
312 if (b1->GetEntries()==0)
return;
319 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
320 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
322 std::cout <<
"ERROR in getBinEffPerCent: Object not of type TH1" << std::endl;
324 if (b1->GetEntries()==0)
return;
331 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
332 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
334 std::cout <<
"ERROR in lowerLB: Object not of type TH1" << std::endl;
340 template <
class HIST>
341 void identical(TObject *
a,
const TObject* b) {
342 if (!s_checkEquality)
344 HIST* a1 = (
dynamic_cast<HIST*
>(
a));
345 const HIST* b1 =
dynamic_cast<const HIST*
>(b);
347 std::cout <<
"ERROR in identical: Object not of correct type" << std::endl;
354 void merge_rebinned(TObject *
a,
const TObject* b) {
355 TH1* a1 = (
dynamic_cast<TH1*
>(
a));
356 const TH1* b1 =
dynamic_cast<const TH1*
>(b);
358 std::cout <<
"ERROR, in merge_rebinned: Object not of type TH1";
361 TH1* b2 =
const_cast<TH1*
>(b1);
366 void merge_eventSample(TObject *
a,
const TObject* b) {
367 TH2* a1 = (
dynamic_cast<TH2*
>(
a));
368 const TH2* b1 =
dynamic_cast<const TH2*
>(b);
370 std::cout <<
"ERROR in merge_eventSample: Object not of type TH2" << std::endl;
376 void merge_TEfficency(TObject *
a,
const TObject* b) {
377 TEfficiency* a1 =
dynamic_cast<TEfficiency*
>(
a);
378 const TEfficiency* b1 =
dynamic_cast<const TEfficiency*
>(b);
379 TEfficiency* b2 =
const_cast<TEfficiency*
>(b1);
381 std::cout <<
"ERROR in merge_TEfficiency: Object not of type TEfficiency" << std::endl;
390 void merge_TTree(TObject *
a,
const TObject* b) {
391 TTree* a1 =
dynamic_cast<TTree*
>(
a);
392 const TTree* b1 =
dynamic_cast<const TTree*
>(b);
394 std::cout <<
"ERROR in merge_TTree: Object not of type TTree" << std::endl;
397 TTree* b2 =
const_cast<TTree*
>(b1);
405 : name(nameIn), obj(std::move(objIn)), mergeMethod(
nullptr) {
408 std::cout <<
"ERROR while adding " << nameIn <<
": Histogram pointer is NULL" << std::endl;
416 s_dbg(
VERBOSE,
"No matadata found for " + name +
", use defaults");
418 const std::string& howToMerge = metadata[2];
419 s_dbg(
VERBOSE,
"Name: " + name +
" mergeMethod=" + howToMerge);
421 TH1* th1 =
dynamic_cast<TH1*
>(obj.get());
422 TH2* th2 =
dynamic_cast<TH2*
>(obj.get());
423 TEfficiency* teff =
dynamic_cast<TEfficiency*
>(obj.get());
425 th1->SetDirectory(
nullptr);
426 if (howToMerge ==
"<default>") {
433 else if (howToMerge ==
"weightedAverage" || howToMerge==
"weightedAverage2D")
434 mergeMethod = &weightedAverage;
435 else if (howToMerge ==
"weightedEff")
436 mergeMethod = &weightedEff;
437 else if (howToMerge ==
"mergeRMS")
438 mergeMethod = &mergeRMS;
439 else if (howToMerge ==
"RMSpercentDeviation")
440 mergeMethod = &RMSpercentDeviation;
441 else if (howToMerge ==
"perBinEffPerCent")
442 mergeMethod = &perBinEffPerCent;
443 else if (howToMerge ==
"lowerLB")
444 mergeMethod = &lowerLB;
445 else if (howToMerge ==
"identical")
447 mergeMethod = &identical<TH2>;
449 mergeMethod = &identical<TH1>;
451 else if ((howToMerge ==
"mergeRebinned") || (howToMerge ==
"merge"))
452 mergeMethod = &merge_rebinned;
454 std::cout <<
"ERROR: Unknown merging method (" << howToMerge <<
") for object of type TH1 named " << nameIn << std::endl;
459 teff->SetDirectory(
nullptr);
460 if (howToMerge ==
"<default>")
461 mergeMethod = &merge_TEfficency;
463 std::cout <<
"ERROR: Unknown merging method (" << howToMerge <<
") for object of type TEfficiency named " << nameIn << std::endl;
465 else if (
nullptr !=
dynamic_cast<TTree*
>(obj.get())) {
466 mergeMethod = &merge_TTree;
468 std::cout <<
"ERROR Object " << name <<
" has unkown type" << std::endl;
475 s_dbg(
VERBOSE,
"Working on directory " + dirName);
476 if (m_dirExclusion && !std::regex_search(dirName, *m_dirExclusion)) {
477 s_dbg(
DEBUG,
"Path " + dirName +
" is excluded");
481 for (TObject* oKey : *dir->GetListOfKeys()) {
482 TKey* key =
static_cast<TKey*
>(oKey);
483 const std::string name = key->GetName();
484 const std::string classname = key->GetClassName();
485 if ((classname ==
"TTree") && (name ==
"metadata")) {
489 s_dbg(
VERBOSE,
"Found name " + name +
", classname=" + classname);
491 const std::string newName = dirName.empty() ? name : dirName +
"/" + name;
492 auto itDir = m_data.find(dirName);
494 if (classname.starts_with(
"TH") || classname.starts_with(
"TProfile") || classname.starts_with(
"TEfficiency") || classname ==
"TTree") {
495 if (m_histExclusion && !std::regex_search(name, *m_histExclusion)) {
496 s_dbg(
DEBUG,
"Histogram with name " + name +
" is excluded");
501 if (m_skipExisting) {
503 std::unique_ptr<TObject> existingObj(m_out->Get(newName.c_str()));
508 std::unique_ptr<TTree> md;
509 if (itDir == m_data.end()) {
511 itDir = m_data.emplace(dirName, histDir_t()).first;
512 s_dbg(
VERBOSE,
"Registering new directory " + dirName);
516 auto itH = itDir->second.histos.find(name);
517 if (itH == itDir->second.histos.end()) {
521 md.reset((TTree*)dir->Get(
"metadata"));
524 std::unique_ptr<TObject> obj{key->ReadObj()};
525 TTree* treeObj =
dynamic_cast<TTree*
>(obj.get());
527 TDirectory* outDir = m_out->GetDirectory(dirName.c_str());
529 outDir = m_out->mkdir(dirName.c_str());
531 TDirectory* currentDir = gDirectory;
533 TTree* cloneTree = treeObj->CloneTree();
535 obj.reset(cloneTree);
538 histPerDir_t histo(name, std::move(obj), md.get());
539 itH = itDir->second.histos.emplace(name, std::move(histo)).first;
540 s_dbg(
VERBOSE,
"Cloning histogram " + name +
" in dir " + dirName);
543 std::unique_ptr<TObject> other(key->ReadObj());
545 std::cout <<
"ERROR, got NULL key";
547 itH->second.merge(other.get());
548 s_dbg(
VERBOSE,
"Merging histogram " + name +
" in dir " + dirName);
551 }
else if (classname.starts_with(
"TDirectory")) {
552 std::unique_ptr<TObject> obj(key->ReadObj());
553 TDirectory* subdir =
dynamic_cast<TDirectory*
>(obj.get());
555 if (filename.empty()) {
556 this->addDirectory(subdir, newName, filename);
558 if (!name.starts_with(
"lb_") && !name.starts_with(
"lowStat_LB")) {
559 this->addDirectory(subdir, newName, filename);
561 m_fileLBMap[newName].insert(filename);
566 std::cout <<
"Ignored objects '" << name <<
"' of type " << classname << std::endl;
573 unsigned nWritten = 0;
574 unsigned nIgnored = 0;
576 for (
auto& it : m_data) {
577 const std::string fulldir = it.first;
578 TDirectory* histDir = m_out->GetDirectory(fulldir.c_str());
579 if (histDir ==
nullptr) {
580 histDir = m_out->mkdir(fulldir.c_str());
581 if (histDir ==
nullptr) {
582 std::cout <<
"ERROR, failed to create directory " << fulldir << std::endl;
585 s_dbg(
VERBOSE,
"Created directory " + fulldir +
" in file " + m_out->GetName());
588 m_out->cd(fulldir.c_str());
590 for (
auto& [name, histo] : it.second.histos) {
595 std::cout <<
"NOT writing " << name <<
". Invalid." << std::endl;
599 it.second.writeMD(histDir);
601 std::cout <<
"Wrote " << nWritten <<
" histograms to " << nDirs <<
" directories in output file " << m_out->GetName() << std::endl;
603 std::cout <<
" Omitting " << nIgnored <<
" histograms." << std::endl;
611 makeBranch(
"Name",
"Name/C");
612 makeBranch(
"Interval",
"Interval/C");
613 makeBranch(
"TriggerChain",
"TriggerChain/C");
614 makeBranch(
"MergeMethod",
"MergeMethod/C");
619 m_metadata->Branch(branchName, (
void*)
nullptr, branchstr);
624 const std::string& theMerge) {
625 std::string name = theName;
626 std::string interval = theInterval;
627 std::string chain = theChain;
628 std::string
merge = theMerge;
629 m_metadata->SetBranchAddress(
"Name", name.data());
630 m_metadata->SetBranchAddress(
"Interval", interval.data());
631 m_metadata->SetBranchAddress(
"TriggerChain", chain.data());
632 m_metadata->SetBranchAddress(
"MergeMethod",
merge.data());
644 m_file = TFile::Open(fileName.c_str());
678 DirMap_t::value_type dirmapVal(dirName, dir);
679 dirmap.insert(std::move(dirmapVal));
682 TIter next(dir->GetListOfKeys());
684 while ((key =
dynamic_cast<TKey*
>(next())) != 0) {
686 TObject* obj = key->ReadObj();
687 TDirectory* subdir =
dynamic_cast<TDirectory*
>(obj);
689 std::string subdirName(subdir->GetName());
690 std::string fName(
"");
707 TDirectory* subdir(0);
708 DirMap_t::const_iterator diter;
709 std::string::size_type i = path.find_first_of(
'/');
710 std::string fName(
"");
716 if (i != std::string::npos) {
717 std::string dName(path, 0, i);
718 std::string pName(path, i + 1, std::string::npos);
721 diter = dirmap.find(fName);
722 if (diter != dirmap.end()) {
723 subdir = diter->second;
725 subdir = dir->mkdir(dName.c_str());
726 DirMap_t::value_type dirmapVal(fName, subdir);
727 dirmap.insert(std::move(dirmapVal));
732 return createDir(dirmap, subdir, fName, pName);
737 diter = dirmap.find(fName);
738 if (diter != dirmap.end()) {
739 return diter->second;
742 subdir = dir->mkdir(path.c_str());
743 DirMap_t::value_type dirmapVal(fName, subdir);
744 dirmap.insert(std::move(dirmapVal));
754 std::string::size_type i = path.find_first_of(
'/');
755 if (i != std::string::npos) {
756 std::string dName(path, 0, i);
757 std::string pName(path, i + 1, std::string::npos);
759 key = dir->FindKey(dName.c_str());
761 TDirectory* subDir =
dynamic_cast<TDirectory*
>(key->ReadObj());
771 return dir->FindKey(path.c_str());
777 TTree* md =
dynamic_cast<TTree*
>(dir->Get(
"metadata"));
781 TTreeReader reader(md);
782 TTreeReaderArray<char> i_name(reader,
"Name");
783 TTreeReaderArray<char> i_interval(reader,
"Interval");
784 TTreeReaderArray<char> i_chain(reader,
"TriggerChain");
785 TTreeReaderArray<char> i_merge(reader,
"MergeMethod");
787 while (reader.Next()) {
788 const std::string nameStr(
static_cast<char*
>(i_name.GetAddress()));
789 if (mdMap.find(nameStr) == mdMap.end()) {
790 MetaData md(nameStr,
static_cast<char*
>(i_interval.GetAddress()),
static_cast<char*
>(i_chain.GetAddress()),
static_cast<char*
>(i_merge.GetAddress()));
791 std::map<std::string, MetaData>::value_type mdVal(nameStr, md);
792 mdMap.insert(std::move(mdVal));
800 std::cout <<
"Writing file: " << outFileName << std::endl;
801 std::cout <<
"Start merging [" <<
files.size() <<
"] histogram files" << std::endl;
803 TH1::AddDirectory(
false);
805 std::cout <<
" ========== Using regular expressions for selective merging ========== " << std::endl;
808 std::cout <<
"CPU time measurement activated " << std::endl;
811 const size_t nFiles =
files.size();
817 std::cout <<
"Got exactly one input file. Will copy input -> output" << std::endl;
819 std::cout <<
"regular expressions for selective merging will have no effect!" << std::endl;
822 std::filesystem::path inPath(
files[0]);
823 std::filesystem::path outPath(outFileName);
824 std::filesystem::copy_file(inPath, outPath, std::filesystem::copy_options::overwrite_existing);
828 std::unique_ptr<TFile> outfile(TFile::Open(outFileName.c_str(),
"RECREATE", outFileName.c_str(),
m_fileCompressionLevel));
829 if (outfile.get() == 0) {
830 std::cout <<
" ERROR, cound not open output file " << outFileName << std::endl;
833 std::cout <<
"Opened/created output file " << outFileName << std::endl;
840 std::unique_ptr<TFile> in1(TFile::Open(
files[0].c_str()));
842 std::cout <<
"ERROR, could not open input file " <<
files[0] << std::endl;
845 std::cout <<
"Working on file 1/" << nFiles <<
": " <<
files[0] << std::endl;
846 std::string runDir, runDirFwd;
847 const std::regex runDirPattern(
"run_[0-9]*");
848 TIter next(in1->GetListOfKeys());
850 while ((key = (TKey*)next())) {
851 const char* name = key->GetName();
852 if (std::regex_match(name, runDirPattern)) {
853 if (runDir.size() > 0) {
854 std::cout <<
"ERROR More than one run_XXX directory found! Ignoring " << name << std::endl;
859 if (runDir.empty()) {
860 std::cout <<
"No run-directory found, start with '/'" << std::endl;
865 std::cout <<
"Found run directory " << runDir << std::endl;
870 TDirectory* dir(
dynamic_cast<TDirectory*
>(in1->GetDirectory(runDir.c_str())));
872 std::cout <<
"ERROR, can't access directory " << runDir;
881 for (
size_t i = 1; i <
files.size(); ++i) {
882 std::cout <<
"Working on file " << 1+i <<
"/" << nFiles <<
": " <<
files[i] << std::endl;
883 std::unique_ptr<TFile> in(TFile::Open(
files[i].c_str()));
885 std::cout <<
"ERROR, could not open input file " <<
files[i] << std::endl;
888 TDirectory* dir(
dynamic_cast<TDirectory*
>(in->GetDirectory(runDir.c_str())));
890 std::cout <<
"ERROR, could not cast to directory" << std::endl;
896 std::cout <<
"Accumulated a total of " << hc.
size() <<
" histograms." << std::endl;
898 std::cout <<
"Start writing output ..." << std::endl;
902 std::cout <<
"CPU time for histogram merging: (regular histograms)" << std::endl;
906 auto newlbmap = hc.getFileLBMapAndClear();
909 for (
auto& [lbname, newfileset] : newlbmap) {
910 auto& fileset = lbmap[lbname];
911 fileset.merge(newfileset);
914 if (!lbmap.empty() && fillLBDirs) {
915 std::cout <<
"Start merging lb_nnn and lowStat_LB directories (" << lbmap.size() <<
" in total)" << std::endl;
923 std::vector<std::pair<std::string, std::vector<std::string>>> lbToFiles;
924 for (
const auto& [
lb,fileSet] : lbmap) {
925 if (fileSet.size() > 0)
926 lbToFiles.emplace_back(
lb,std::vector<std::string>(fileSet.begin(),fileSet.end()));
930 std::sort(lbToFiles.begin(), lbToFiles.end(),
931 [](
const decltype(lbToFiles)
::value_type&
a,
const decltype(lbToFiles)
::value_type& b) { return a.second[0] < b.second[0]; });
934 std::unique_ptr<TFile> in;
935 for (
const auto& [dir, filenames] : lbToFiles) {
936 std::cout <<
"Merging/copying directory " << dir <<
" from " << filenames.size() <<
" input file(s) (" << ++counter <<
"/" << lbToFiles.size() <<
")"
938 for (
const std::string& fName : filenames) {
939 if (!in || strcmp(in->GetName(), fName.c_str()) != 0) {
940 in.reset(TFile::Open(fName.c_str()));
941 s_dbg(
DEBUG,
"Opening input file " + fName);
943 s_dbg(
DEBUG,
"Input file " + fName +
" already open");
947 std::cout <<
"ERROR, could not open input file " << fName << std::endl;
950 TDirectory* tDir = (
dynamic_cast<TDirectory*
>(in->Get(dir.c_str())));
952 std::cout <<
"ERROR, failed to get directory " << dir <<
" from file " << fName << std::endl;
959 std::cout <<
"CPU time for histogram merging: (lumiblock-histograms)" << std::endl;
970 typedef std::vector<std::string> FileList_t;
972 const unsigned int nFilesAtOnce = 50;
977 std::cout <<
"ERROR Failed ot read list of input files" << std::endl;
983 if (allFiles.size() <= nFilesAtOnce) {
984 return mergeFiles(outFileName, allFiles, fileLBMap,
true);
987 FileList_t procFiles, tmpIntermediateFiles;
989 FileList_t::const_iterator filesEnd = allFiles.end();
990 FileList_t::const_iterator fi = allFiles.begin();
992 unsigned int counter = 0;
993 std::string tmpInputFile(
"");
994 std::string tmpOutputFile(
"");
997 while (fi != filesEnd) {
999 procFiles.push_back(*fi);
1002 if (counter % nFilesAtOnce == 0 || fi == filesEnd) {
1003 std::ostringstream nameStream;
1004 nameStream <<
"tmp_merge_" << counter <<
".root";
1005 tmpOutputFile = nameStream.str();
1006 tmpIntermediateFiles.push_back(tmpOutputFile);
1007 int stat=
mergeFiles(tmpOutputFile, procFiles, fileLBMap,
false);
1008 if (stat)
return stat;
1013 int stat=
mergeFiles(outFileName, tmpIntermediateFiles,fileLBMap,
true);
1014 if (stat)
return stat;
1016 for (
const auto& tmpFile : tmpIntermediateFiles) {
1019 std::cerr<<
"MonitoringFile::mergeFiles: tmpFile "<<tmpFile<<
" could not be removed\n";
1027 std::cerr <<
"MonitoringFile::printStatistics(): "
1028 <<
"No input file is open\n";
1036 DirMap_t::const_iterator idirend = indirmap.end();
1037 for (DirMap_t::const_iterator idir = indirmap.begin(); idir != idirend; ++idir) {
1038 std::string idirName = idir->first;
1040 GatherStatistics stat_shift(idirName);
1041 GatherStatistics stat_all(idirName);
1046 std::cout.setf(std::ios_base::left, std::ios_base::adjustfield);
1047 std::cout.width(80);
1048 std::cout << idirName <<
" ";
1050 std::cout.setf(std::ios_base::right, std::ios_base::adjustfield);
1051 std::cout <<
" shift: ";
1053 std::cout << stat_shift.m_nHist1D <<
" ";
1055 std::cout << stat_shift.m_nHist1DBins <<
" ";
1057 std::cout << stat_shift.m_nHist2D <<
" ";
1059 std::cout << stat_shift.m_nHist2DBins <<
" ";
1061 std::cout << stat_shift.m_nGraph <<
" ";
1063 std::cout << stat_shift.m_nGraphPoints <<
" ";
1065 std::cout <<
" all: ";
1066 std::cout << stat_all.m_nHist1D <<
" ";
1068 std::cout << stat_all.m_nHist1DBins <<
" ";
1070 std::cout << stat_all.m_nHist2D <<
" ";
1072 std::cout << stat_all.m_nHist2DBins <<
" ";
1074 std::cout << stat_all.m_nGraph <<
" ";
1076 std::cout << stat_all.m_nGraphPoints <<
"\n";
1078 std::cout << std::flush;
1088 std::cerr <<
"MonitoringFile::copyHistograms(): "
1089 <<
"No input file is open\n";
1097 if (dirName !=
"all") {
1100 std::cerr <<
"MonitoringFile::copyHistograms(): "
1101 <<
"Directory \'" << dirName <<
"\' not found in input file\n";
1105 TDirectory* fromDir =
dynamic_cast<TDirectory*
>(dkey->ReadObj());
1107 DirMap_t::value_type dirmapVal(dirName, fromDir);
1108 indirmap.insert(std::move(dirmapVal));
1110 std::cout <<
"Building list of all TDirectories in file...\n" << std::flush;
1114 DirMap_t::const_iterator idirend = indirmap.end();
1115 for (DirMap_t::const_iterator idir = indirmap.begin(); idir != idirend; ++idir) {
1117 std::string idirName = idir->first;
1118 std::cout <<
"Checking " << idirName <<
"\n" << std::flush;
1130 reducedmap.insert(*idir);
1133 std::unique_ptr<TFile> outfile(TFile::Open(outFileName.c_str(),
"RECREATE", outFileName.c_str(),
m_fileCompressionLevel));
1134 if (outfile.get() == 0) {
1135 std::cerr <<
"MonitoringFile::copyHistograms(): "
1136 <<
"Output file not opened\n";
1140 idirend = reducedmap.end();
1141 for (DirMap_t::const_iterator idir = reducedmap.begin(); idir != idirend; ++idir) {
1143 std::string idirName = idir->first;
1144 std::cout <<
"Processing " << idirName <<
"\n" << std::flush;
1146 TDirectory* toDir =
createDir(outdirmap, outfile.get(),
"", idirName);
1148 std::cerr <<
"MonitoringFile::copyHistograms(): "
1149 <<
"Directory \'" << idirName <<
"\' not created in output file\n";
1153 CopyHistogram copyFcn(toDir, idirName);
1164 const std::string& hcfg_lowStat,
const std::string& hcfg_medStat) {
1167 std::cout <<
"\nUsing han configurations:\n"
1168 <<
" entire run: " << hcfg <<
"\n"
1169 <<
" low stat interval: " << hcfg_lowStat <<
"\n"
1170 <<
" medium stat interval: " << hcfg_medStat <<
"\n\n"
1173 TFile* infile = TFile::Open(input.c_str());
1175 std::cerr <<
"MonitoringFile::getHanResults(): "
1176 <<
"Cannot open input file \"" << input <<
"\"\n";
1180 std::vector<std::string> run_dirs;
1181 std::vector<std::string> lowStat_dirs;
1182 std::vector<std::string> medStat_dirs;
1184 TIter next_run(infile->GetListOfKeys());
1186 while ((key_run =
dynamic_cast<TKey*
>(next_run())) != 0) {
1187 TObject* obj_run = key_run->ReadObj();
1188 TDirectory* tdir_run =
dynamic_cast<TDirectory*
>(obj_run);
1189 if (tdir_run != 0) {
1190 std::string tdir_run_name(tdir_run->GetName());
1191 if (tdir_run_name.find(
"run") != std::string::npos) {
1192 run_dirs.push_back(tdir_run_name);
1193 TIter next_minutes(tdir_run->GetListOfKeys());
1194 TKey* key_minutes(0);
1195 while ((key_minutes =
dynamic_cast<TKey*
>(next_minutes())) != 0) {
1196 TObject* obj_minutes = key_minutes->ReadObj();
1197 TDirectory* tdir_minutes =
dynamic_cast<TDirectory*
>(obj_minutes);
1198 if (tdir_minutes != 0) {
1199 std::string tdir_minutes_name(tdir_minutes->GetName());
1200 if (tdir_minutes_name.find(
"lowStat") != std::string::npos) {
1201 lowStat_dirs.push_back(tdir_run_name +
'/' + tdir_minutes_name);
1202 }
else if (tdir_minutes_name.find(
"medStat") != std::string::npos) {
1203 medStat_dirs.push_back(tdir_run_name +
'/' + tdir_minutes_name);
1217 std::string fileList =
" ";
1218 std::vector<std::string>::const_iterator dirs_end;
1219 std::vector<std::string>::const_iterator dir;
1221 dirs_end = run_dirs.end();
1222 for (dir = run_dirs.begin(); dir != dirs_end; ++dir) {
1223 const std::string& tdir_run_name = *dir;
1224 std::string han_output_run = hanResultsDir +
'/' + tdir_run_name +
"_han.root";
1225 std::cout <<
"Calling han( " << hcfg <<
", " << input <<
", " << tdir_run_name <<
", " << han_output_run <<
" ):\n" << std::flush;
1226 han.Analyze(hcfg, input, han_output_run, tdir_run_name);
1228 fileList += han_output_run +
" " + tdir_run_name +
"\n";
1231 dirs_end = lowStat_dirs.end();
1232 for (dir = lowStat_dirs.begin(); dir != dirs_end; ++dir) {
1233 const std::string& tdir_minutes_path = *dir;
1235 std::string tdir_minutes_underscore = tdir_minutes_path;
1236 std::string::size_type tdir_minutes_i = tdir_minutes_underscore.find(
'/');
1237 tdir_minutes_underscore.replace(tdir_minutes_i, 1,
"_");
1239 std::string han_output_lowStat = hanResultsDir +
'/' + tdir_minutes_underscore +
"_han.root";
1240 std::cout <<
"Running han, writing to " << han_output_lowStat <<
":\n" << std::flush;
1241 han.Analyze(hcfg_lowStat, input, han_output_lowStat, tdir_minutes_path);
1243 std::string subdirname(tdir_minutes_path, tdir_minutes_i + 1, std::string::npos);
1244 std::string
dirname(tdir_minutes_path, 0, tdir_minutes_i);
1245 fileList += han_output_lowStat +
" " + subdirname +
" " +
dirname +
" " + subdirname +
"\n";
1248 dirs_end = medStat_dirs.end();
1249 for (dir = medStat_dirs.begin(); dir != dirs_end; ++dir) {
1250 const std::string& tdir_minutes_path = *dir;
1252 std::string tdir_minutes_underscore = tdir_minutes_path;
1253 std::string::size_type tdir_minutes_i = tdir_minutes_underscore.find(
'/');
1254 tdir_minutes_underscore.replace(tdir_minutes_i, 1,
"_");
1256 std::string han_output_medStat = hanResultsDir +
'/' + tdir_minutes_underscore +
"_han.root";
1257 std::cout <<
"Running han, writing to " << han_output_medStat <<
":\n" << std::flush;
1258 han.Analyze(hcfg_medStat, input, han_output_medStat, tdir_minutes_path);
1260 std::string subdirname(tdir_minutes_path, tdir_minutes_i + 1, std::string::npos);
1261 std::string
dirname(tdir_minutes_path, 0, tdir_minutes_i);
1262 fileList += han_output_medStat +
" " + subdirname +
" " +
dirname +
" " + subdirname +
"\n";
1270 std::cerr <<
"MonitoringFile::printHanConfig(): "
1271 <<
"No input file is open\n";
1279 std::string indent, indent_p, indent_c;
1280 std::string idirName_p;
1281 DirMap_t::const_iterator idirend = indirmap.end();
1282 for (DirMap_t::const_iterator idir = indirmap.begin(); idir != idirend; ++idir) {
1283 std::string idirName = idir->first;
1284 std::string::size_type shortNameIndex = idirName.rfind(
'/');
1285 std::string shortName = idirName.substr(shortNameIndex + 1, std::string::npos);
1287 std::string::size_type fsIndex = idirName.find(
'/');
1288 std::string shortPath;
1289 if (fsIndex != shortNameIndex)
1290 shortPath = idirName.substr(fsIndex + 1, shortNameIndex);
1292 shortPath = idirName.substr(fsIndex + 1, std::string::npos);
1294 std::cout << idirName <<
"\n";
1295 std::cout << shortPath <<
", " << shortName <<
"\n";
1334 std::string space = leadingSpace;
1335 std::string::size_type i = pathName.find_first_of(
'/');
1336 if (i != std::string::npos) {
1337 std::string subPath(pathName, i + 1, std::string::npos);
1345 int length = (name1.size() < name2.size()) ? name1.size() : name2.size();
1355 return (name1.substr(0,
count));
1363 m_metadata =
new TTree(
"metadata",
"Monitoring Metadata");
1364 m_metadata->SetDirectory(0);
1365 m_metadata->Branch(
"Name", (
void*)
nullptr,
"Name/C");
1366 m_metadata->Branch(
"Interval", (
void*)
nullptr,
"Interval/C");
1367 m_metadata->Branch(
"TriggerChain", (
void*)
nullptr,
"TriggerChain/C");
1368 m_metadata->Branch(
"MergeMethod", (
void*)
nullptr,
"MergeMethod/C");
1373 m_metadata->SetDirectory(m_target);
1374 m_metadata->Write();
1380 hist->SetDirectory(m_target);
1400 std::string name(md.name);
1401 std::string interval(md.interval);
1402 std::string chain(md.chain);
1403 std::string
merge(md.merge);
1404 m_metadata->SetBranchAddress(
"Name", name.data());
1405 m_metadata->SetBranchAddress(
"Interval", interval.data());
1406 m_metadata->SetBranchAddress(
"TriggerChain", chain.data());
1407 m_metadata->SetBranchAddress(
"MergeMethod",
merge.data());
1413 hist->SetDirectory(m_target);
1438 : m_dirName(dirName), m_nHist1D(0), m_nHist1DBins(0), m_nGraph(0), m_nGraphPoints(0), m_nHist2D(0), m_nHist2DBins(0) {}
1441 TH2* hist2d =
dynamic_cast<TH2*
>(hist);
1444 m_nHist2DBins += (hist2d->GetNbinsX() * hist2d->GetNbinsY());
1448 m_nHist1DBins += hist->GetNbinsX();
1454 m_nGraphPoints += graph->GetMaxSize();
1461 TH1* h_total = eff->GetCopyPassedHisto();
1462 TH2* h_total2D =
dynamic_cast<TH2*
>(h_total);
1464 if (h_total2D != 0) {
1465 m_nEfficiencyBins += (h_total2D->GetNbinsX() * h_total2D->GetNbinsY());
1468 m_nEfficiencyBins += h_total->GetNbinsX();
1476 m_names.push_back(std::string(hist->GetName()));
1481 m_names.push_back(std::string(graph->GetName()));
1486 m_names.push_back(std::string(eff->GetName()));
1502 TKey* mdKey = dir->FindKey(
"metadata");
1507 TTree* md =
dynamic_cast<TTree*
>(mdKey->ReadObj());
1512 int nEntries = int(md->GetEntries());
1517 }
catch (
const std::exception& e) {
1518 std::cerr <<
"Exception: \"" << e.what() <<
"\" in directory \"" << dir->GetName() <<
"\"\n" << std::flush;
1529 TIter next(dir->GetListOfKeys());
1531 while ((key =
dynamic_cast<TKey*
>(next())) != 0) {
1532 TObject* obj = key->ReadObj();
1536 if ((
h =
dynamic_cast<TH1*
>(obj))) {
1538 }
else if ((g =
dynamic_cast<TGraph*
>(obj))) {
1540 }
else if ((e =
dynamic_cast<TEfficiency*
>(obj))) {
1549 TKey* mdKey = dir->FindKey(
"metadata");
1554 TTree* md =
dynamic_cast<TTree*
>(mdKey->ReadObj());
1561 TTreeReader reader(md);
1562 TTreeReaderArray<char> i_name(reader,
"Name");
1563 TTreeReaderArray<char> i_interval(reader,
"Interval");
1564 TTreeReaderArray<char> i_chain(reader,
"TriggerChain");
1565 TTreeReaderArray<char> i_merge(reader,
"MergeMethod");
1567 while (reader.Next()) {
1568 const std::string nameStr(
static_cast<char*
>(i_name.GetAddress()));
1570 i_key = dir->FindKey(
static_cast<char*
>(i_name.GetAddress()));
1572 std::cerr <<
"MonitoringFile::loopOnHistogramsInMetadata(): "
1573 <<
"No \'" << nameStr <<
"\' object found\n";
1576 MetaData md(nameStr,
static_cast<char*
>(i_interval.GetAddress()),
static_cast<char*
>(i_chain.GetAddress()),
static_cast<char*
>(i_merge.GetAddress()));
1577 TObject* obj = i_key->ReadObj();
1578 TH1*
h =
dynamic_cast<TH1*
>(obj);
1580 fcn.executeMD(
h, md);
1582 TGraph* g =
dynamic_cast<TGraph*
>(obj);
1584 fcn.executeMD(g, md);
1596 using namespace std;
1600 ifstream listfile(listFileName.c_str());
1602 cerr <<
"MonitoringFile::setListFromFile(): "
1603 <<
"cannot read from file: " << listFileName <<
"\n";
1610 while (getline(listfile, line)) {
1611 istringstream linestream(line);
1612 while (linestream.get(c)) {
1619 linestream.putback(c);
1620 linestream >> filename;
1622 cerr <<
"MonitoringFile::setListFromFile(): "
1623 <<
"badly formatted line: " << line <<
"\n";
1627 filelist.push_back(filename);
1637 std::cout <<
"Running mergeLBintervals on " << inFilename << std::endl;
1639 std::unique_ptr<TFile> f(TFile::Open(inFilename.c_str(),
"UPDATE"));
1641 std::cout <<
"ERROR, could not open file " << inFilename <<
" for update" << std::endl;
1644 std::string runDirName;
1645 const std::regex runDirPattern(
"run_[0-9]*");
1646 TIter next(f->GetListOfKeys());
1648 while ((key = (TKey*)next())) {
1649 const char* name = key->GetName();
1650 if (std::regex_match(name, runDirPattern)) {
1651 if (runDirName.size() > 0) {
1652 std::cout <<
"ERROR More than one run_XXX directory found! Ignoring " << name << std::endl;
1659 TDirectory* runDir = f->GetDirectory(runDirName.c_str());
1662 if (s_dbg.getLvl() ==
VERBOSE) {
1663 std::cout <<
"LB directory mapping:" << std::endl;
1664 for (
const auto& i1 : mapping) {
1665 std::cout << i1.first;
1666 for (
const auto& i2 : i1.second) {
1667 std::cout <<
"\t" << i2 << std::endl;
1672 for (
const auto& [outDir, inDIrs] : mapping) {
1674 if (stat)
return stat;
1678 f.reset(TFile::Open(inFilename.c_str(),
"UPDATE"));
1679 runDir = f->GetDirectory(runDirName.c_str());
1681 std::cout <<
"merging lowStat_LB dirs into run-dir" << std::endl;
1682 std::vector<std::string> lowStatDirs;
1683 for (TObject* oKey : *runDir->GetListOfKeys()) {
1684 TKey* key =
static_cast<TKey*
>(oKey);
1685 const std::string name = key->GetName();
1686 const std::string classname = key->GetClassName();
1687 if (classname.starts_with(
"TDirectory") and name.starts_with(
"lowStat_LB")) {
1688 lowStatDirs.push_back(runDirName +
"/" + name);
1689 s_dbg(
VERBOSE,
"Found input: " + runDirName +
"/" + name);
1701 std::map<std::string, std::vector<std::string>> ranges;
1704 const std::string runDirName = runDir->GetName();
1705 for (TObject* oKey : *runDir->GetListOfKeys()) {
1706 TKey* key =
static_cast<TKey*
>(oKey);
1707 const std::string name = key->GetName();
1708 const std::string classname = key->GetClassName();
1709 if (!classname.starts_with(
"TDirectory"))
1711 if (name.starts_with(
"lb_")) {
1712 unsigned lumiBlock = 0;
1714 lumiBlock = std::stol(name.substr(3));
1715 }
catch (std::invalid_argument& e) {
1716 std::cout <<
"ERROR, unexpected directory name " << name <<
". Can't parse lb number" << std::endl;
1717 std::cout << e.what() << std::endl;
1721 const unsigned lbBase = lumiBlock - (((int64_t)lumiBlock - 1) % 20);
1722 const std::string lbString = runDirName +
"/lowStat_LB" + std::to_string(lbBase) +
"-" + std::to_string(lbBase + 19);
1723 ranges[lbString].push_back(runDirName +
"/" + name);
1731 TDirectory* outDir =
file->GetDirectory(outputDirName.c_str());
1733 outDir =
file->mkdir(outputDirName.c_str());
1736 std::cout <<
"ERROR, can't obtain nor create directory " << outputDirName <<
" in file " <<
file->GetName() << std::endl;
1744 for (
const std::string& inDirName : inputDirNames) {
1745 TDirectory* inDir =
file->GetDirectory(inDirName.c_str());
1748 if (hc.
size() == 0) {
1749 std::cout <<
"mergeLB_processLBinterval: No new objects found for " << outputDirName << std::endl;
1757 std::unique_ptr<TObject> obj(f->Get(HistoName));
1766 return s_dbg.getLvl();
1783 std::string path = dir->GetPath();
1784 if (path.find(
':') != std::string::npos)
1785 path = path.substr(path.rfind(
':') + 1);