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 <TStyle.h>
12#include <TClass.h>
13#include <TKey.h>
14
15using namespace std;
16using namespace Belle2;
17
18//-----------------------------------------------------------------
19// Register the Module
20//-----------------------------------------------------------------
21REG_MODULE(DQMHistReference);
22
23//-----------------------------------------------------------------
24// Implementation
25//-----------------------------------------------------------------
26
28{
29 //Parameter definition
30 addParam("ReferenceFile", m_referenceFileName, "Name of the reference histrogram files", string(""));
31 B2DEBUG(1, "DQMHistReference: Constructor done.");
32}
33
34
36
38{
39 B2DEBUG(1, "DQMHistReference: initialized.");
40}
41
43{
44 B2DEBUG(1, "DQMHistReference: beginRun called.");
45
47}
48
50{
51 TH1::AddDirectory(false); // do not store any histograms
52
53 B2DEBUG(1, "DQMHistReference: reading references from input root file");
54
55 string run_type = getRunType();
56 if (run_type == "") run_type = "default";
57
58 B2INFO("DQMHistReference: run_type " << run_type);
59
60 TFile* refFile = new TFile(m_referenceFileName.c_str(), "READ");
61
62 if (refFile->IsZombie()) {
63 B2INFO("DQMHistReference: reference file " << m_referenceFileName << " does not exist. No references will be used!");
64 refFile->Close();
65 delete refFile;
66 return;
67 }
68
69 B2INFO("DQMHistReference: use reference file " << m_referenceFileName);
70
71 TIter nextkey(refFile->GetListOfKeys());
72 TKey* key;
73 while ((key = (TKey*)nextkey())) {
74 if (key->IsFolder() && string(key->GetName()) == string("ref")) {
75 TDirectory* refdir = (TDirectory*)key->ReadObj(); // ReadObj -> I own it
76 TIter nextDetDir(refdir->GetListOfKeys());
77 TKey* detDir;
78 // detector folders
79 while ((detDir = (TKey*)nextDetDir())) {
80 if (!detDir->IsFolder()) continue;
81 TIter nextTypeDir(((TDirectory*)detDir->ReadObj())->GetListOfKeys());
82 TKey* typeDir;
83 TDirectory* foundDir = NULL;
84 // run type folders (get the run type corresponding folder or use default one)
85 while ((typeDir = (TKey*)nextTypeDir())) {
86 if (!typeDir->IsFolder()) continue;
87 if (string(typeDir->GetName()) == run_type) {
88 foundDir = (TDirectory*)typeDir->ReadObj(); // ReadObj -> I own it
89 break;
90 }
91 if (string(typeDir->GetName()) == "default") foundDir = (TDirectory*)typeDir->ReadObj(); // ReadObj -> I own it
92 }
93 string dirname = detDir->GetName();
94 if (!foundDir) {
95 B2INFO("No run type specific or default references available for " << dirname);
96 } else {
97 B2INFO("Reading reference histograms for " << dirname << " from run type folder: " << foundDir->GetName());
98
99 TIter next(foundDir->GetListOfKeys());
100 TKey* hh;
101
102 while ((hh = (TKey*)next())) {
103 if (hh->IsFolder()) continue;
104 TObject* obj = hh->ReadObj(); // ReadObj -> I own it
105 if (obj->IsA()->InheritsFrom("TH1")) {
106 TH1* h = (TH1*)obj;
107 string histname = h->GetName();
108 std::string name = dirname + "/" + histname;
109 auto& n = getRefList()[name];
110 n.m_orghist_name = name;
111 n.m_refhist_name = "ref/" + name;
112 h->SetName((n.m_refhist_name).c_str());
113 h->SetDirectory(0);
114 n.setRefHist(h); // transfer ownership!
115 n.setRefCopy(nullptr);
116 n.setCanvas(nullptr);
117 } else {
118 delete obj;
119 }
120 }
121 delete foundDir; // always non-zero
122 }
123 }
124 delete refdir; // always non-zero
125 }
126 }
127
128 B2INFO("DQMHistReference: read references done");
129 refFile->Close();
130 delete refFile;
131}
132
134{
135 B2DEBUG(1, "DQMHistReference: event called");
136}
137
139{
140 B2DEBUG(1, "DQMHistReference: endRun called");
141}
142
144{
145 B2DEBUG(1, "DQMHistReference: terminate called");
146}
147
The base class for the histogram analysis module.
static const std::string & getRunType(void)
Get the Run Type.
static RefList & getRefList()
Get the list of the reference histograms.
void initialize() override final
Initializer.
void loadReferenceHistos()
Reads reference histograms from input root file.
void terminate() override final
This method is called at the end of the event processing.
void event() override final
This method is called for each event.
void endRun() override final
This method is called if the current run ends.
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:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
STL namespace.