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 TH1::AddDirectory(false); // do not store any histograms
136 gStyle->SetOptStat(0);
137 gStyle->SetStatStyle(1);
138 gStyle->SetOptDate(22);// Date and Time in Bottom Right, does no work
139
140 char mbstr[100];
141
142 time_t now = time(0);
143 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
144 B2INFO("[" << mbstr << "] before ref loop");
145
146 for (auto& it : getRefList()) {
147 TH1* ref = it.second.getRefHist();
148 if (!ref) continue; // No reference, continue
149 TCanvas* canvas = it.second.getCanvas();
150 TH1* hist1 = findHistInCanvas(it.second.m_orghist_name, &(canvas));
151
152 // if there is no histogram on canvas we plot the reference anyway.
153 if (!hist1) {
154 B2DEBUG(1, "Canvas is without histogram -> no display " << it.second.m_orghist_name);
155 // Display something could be confusing for shifters
156// B2DEBUG(1, "Canvas is without histogram -> displaying only reference " << it.second.orghist_name);
157// canvas->cd();
158// hist2->Draw();
159// canvas->Modified();
160// canvas->Update();
161 continue;
162 }
163 if (!canvas) {
164 B2DEBUG(1, "No canvas found for reference histogram " << it.second.m_orghist_name);
165 continue;
166 }
167 if (hist1->Integral() == 0) continue; // empty histogram -> continue
168
169 /* consider adding coloring option....
170 double data = 0;
171 if (m_color) {
172 data = hist1->KolmogorovTest(hist2, ""); // returns p value (0 bad, 1 good), N - do not compare normalized
173 }
174 */
175
176 //TODO: Remove me when and move me to AutoCanvas (after debugging)
177 if (abs(ref->Integral()) > 0) { // only if we have entries in reference
178 auto refCopy = scaleReference(1, hist1, it.second.getReference());
179
180 //Adjust the y scale to cover the reference
181 if (refCopy->GetMaximum() > hist1->GetMaximum())
182 hist1->SetMaximum(1.1 * refCopy->GetMaximum());
183
184 canvas->cd();
185 refCopy->Draw("hist,same");
186
187 canvas->Modified();
188 canvas->Update();
189 B2DEBUG(2, "Adding ref: " << it.second.m_orghist_name << " " << ref->GetName() << " " << ref);
190 //addRef("", it.second.m_orghist_name, ref);
191 }
192
193
194 }
195
196 now = time(0);
197 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
198 B2INFO("[" << mbstr << "] after ref loop");
199}
200
202{
203 B2DEBUG(1, "DQMHistReference: endRun called");
204}
205
207{
208 B2DEBUG(1, "DQMHistReference: terminate called");
209}
210
The base class for the histogram analysis module.
static TH1 * scaleReference(int scaling, const TH1 *hist, TH1 *ref)
Using the original and reference, create scaled version.
static const std::string & getRunType(void)
Get the Run Type.
static RefList & getRefList()
Get the list of the reference histograms.
TH1 * findHistInCanvas(const std::string &hname, TCanvas **canvas=nullptr)
Find histogram in corresponding canvas.
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.