Belle II Software development
DQMHistReferenceModule.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <dqm/analysis/modules/DQMHistReferenceModule.h>
10#include <TROOT.h>
11#include <TKey.h>
12
13using namespace std;
14using namespace Belle2;
15
16//-----------------------------------------------------------------
17// Register the Module
18//-----------------------------------------------------------------
19REG_MODULE(DQMHistReference);
20
21//-----------------------------------------------------------------
22// Implementation
23//-----------------------------------------------------------------
24
26{
27 //Parameter definition
28 addParam("ReferenceFile", m_referenceFileName, "Name of the reference histrogram files", string(""));
29 B2DEBUG(1, "DQMHistReference: Constructor done.");
30}
31
33{
34 B2DEBUG(1, "DQMHistReference: beginRun called.");
35
37}
38
40{
41 TH1::AddDirectory(false); // do not store any histograms
42
43 B2DEBUG(1, "DQMHistReference: reading references from input root file");
44
45 string run_type = getRunType();
46 if (run_type == "") run_type = "default";
47
48 B2INFO("DQMHistReference: run_type " << run_type);
49
50 TFile* refFile = new TFile(m_referenceFileName.c_str(), "READ");
51
52 if (refFile->IsZombie()) {
53 B2INFO("DQMHistReference: reference file " << m_referenceFileName << " does not exist. No references will be used!");
54 refFile->Close();
55 delete refFile;
56 return;
57 }
58
59 B2INFO("DQMHistReference: use reference file " << m_referenceFileName);
60
61 TIter nextRefDirKey(refFile->GetListOfKeys());
62 TKey* refDirKey;
63 while ((refDirKey = dynamic_cast<TKey*>(nextRefDirKey()))) {
64 if (refDirKey->IsFolder() && string(refDirKey->GetName()) == string("ref")) {
65 TDirectory* refDir = static_cast<TDirectory*>(refDirKey->ReadObj()); // ReadObj -> I own it, delete later
66 TIter nextDetDirKey(refDir->GetListOfKeys());
67 TKey* detDirKey;
68 // detector folders
69 while ((detDirKey = dynamic_cast<TKey*>(nextDetDirKey()))) {
70 if (!detDirKey->IsFolder()) continue;
71 TDirectory* detDir = static_cast<TDirectory*>(detDirKey->ReadObj());
72 TIter nextRunTypeDirKey(detDir->GetListOfKeys()); // ReadObj -> Now I own this, so delete later
73 TKey* runtypeDirKey;
74 TDirectory* runtypeDir = nullptr;
75 // run type folders (get the run type corresponding folder or use default one)
76 while ((runtypeDirKey = dynamic_cast<TKey*>(nextRunTypeDirKey()))) {
77 if (!runtypeDirKey->IsFolder()) continue;
78 if (string(runtypeDirKey->GetName()) == run_type) {
79 if (runtypeDir) delete runtypeDir; // if default was loaded before
80 runtypeDir = static_cast<TDirectory*>(runtypeDirKey->ReadObj()); // ReadObj -> I own it, delete later
81 break; // break directly, otherwise "default" could overwrite it
82 }
83 // else we would check if default, which we load as backup
84 // ReadObj -> I own it
85 if (string(runtypeDirKey->GetName()) == "default") runtypeDir = static_cast<TDirectory*>(runtypeDirKey->ReadObj());
86 }
87 string detName = detDir->GetName();
88 // Attention, runtypeDir and runtypeDirKey could be zero here
89 if (!runtypeDir) {
90 B2INFO("No run type specific or default references available for " << detName);
91 } else {
92 B2INFO("Reading reference histograms for " << detName << " from run type folder: " << runtypeDir->GetName());
93
94 TIter nextHistkey(runtypeDir->GetListOfKeys());
95 TKey* histKey;
96 // now read histograms
97 while ((histKey = dynamic_cast<TKey*>(nextHistkey()))) {
98 if (histKey->IsFolder()) continue;
99 if (gROOT->GetClass(histKey->GetClassName())->InheritsFrom("TH1")) { // maybe not needed with the dynamic cast check below
100 auto h = dynamic_cast<TH1*>(histKey->ReadObj());
101 if (h) addRefHist(detName, h); // ReadObj -> I own it, tranfer ownership to function
102 }
103 }
104 delete runtypeDir; // always non-zero as checked above ... runtype or "default"
105 }
106 delete detDir; // always non-zero ... detector subdir name
107 }
108 delete refDir; // always non-zero ... "ref" folder
109 }
110 }
111
112 B2INFO("DQMHistReference: read references done");
113 refFile->Close();
114 delete refFile;
115}
static void addRefHist(const std::string &dirname, TH1 *hist)
Add reference histogram.
static const std::string & getRunType(void)
Get the list of the reference histograms.
DQMHistAnalysisModule()
Constructor / Destructor.
void loadReferenceHistos()
Reads reference histograms from input root file.
void beginRun() override final
Called when entering a new run.
std::string m_referenceFileName
Reference Histogram Root file name.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.