Belle II Software development
DQMHistComparitor.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// File : DQMHistComparitor.cc
10// Description : DQM Histogram analysis module, compares histo with references
11//-
12
13
14#include <framework/core/ModuleParam.templateDetails.h>
15#include <dqm/analysis/modules/DQMHistComparitor.h>
16#include <TStyle.h>
17
18using namespace std;
19using namespace Belle2;
20
21//-----------------------------------------------------------------
22// Register the Module
23//-----------------------------------------------------------------
24REG_MODULE(DQMHistComparitor);
25
26//-----------------------------------------------------------------
27// Implementation
28//-----------------------------------------------------------------
29
32{
33 //Parameter definition
34 addParam("HistoList", m_histlist,
35 "['histname', 'refhistname', 'canvas', 'delta', 'color', 'minentry', 'Chi/KS', 'warnlvl', 'errlvl', 'fitresult_pvname', 'status_pvname'],[...],...");
36 B2DEBUG(1, "DQMHistComparitor: Constructor done.");
37}
38
40{
41 gStyle->SetOptStat(0);
42 gStyle->SetStatStyle(1);
43 gStyle->SetOptDate(22);// Date and Time in Bottom Right, does no work
44
45 for (auto& it : m_histlist) {
46 if (it.size() != 11) {
47 B2WARNING("Histolist with wrong nr of parameters (" << it.size() << "), histo=" << (it.size() > 0 ? it.at(0) : std::string("")));
48 continue;
49 }
50
51 auto n = new CMPNODE;
52 n->histName = it.at(0);
53 n->refName = it.at(1) == "" ? n->histName : it.at(1) ; // only use if set
54 n->canvas = new TCanvas(it.at(2).c_str());
55
56 n->deltaflag = it.at(3) != "";
57 n->colorflag = it.at(4) != "";
58 n->min_entries = atoi(it.at(5).c_str()); // if delta hist is used, value must be lower or equal delta entries value
59 if (it.at(6) == "KS") n->algo = 1;
60 else n->algo = 0; // chi as default
61 n->warning = atof(it.at(7).c_str());
62 n->error = atof(it.at(8).c_str());
63
64 n->pvfit = it.at(9);
65 if (n->pvfit != "") registerEpicsPV(n->pvfit);
66 n->pvstatus = it.at(10);
67 if (n->pvstatus != "") registerEpicsPV(n->pvstatus);
68 m_pnode.push_back(n);
69 }
70
71 B2DEBUG(20, "DQMHistComparitor: initialized.");
72}
73
74
76{
77 B2DEBUG(20, "DQMHistComparitor: beginRun called.");
78}
79
81{
82 gStyle->SetOptStat(0);
83 gStyle->SetStatStyle(0);
84 gStyle->SetOptDate(22);// Date and Time in Bottom Right, does no work
85
86 for (auto& it : m_pnode) {
87 TH1* histo = nullptr;
88 B2DEBUG(20, "== Search for " << it->histName << " with ref " << it->refName << "==");
89 if (it->deltaflag) histo = getDelta(it->histName); // default: last delta
90 if (!histo) histo = findHist(it->histName, true); // only if changed
91 if (!histo) {
92 // B2DEBUG(20, "NOT Found " << it->histName);
93 // Dont compare if missing or not updated ...
94 continue;
95 }
96 // if compare normalized ... need configuration
97 auto refhist = findRefHist(it->refName, ERefScaling::c_RefScaleEntries, histo);
98 if (!refhist) {
99 B2DEBUG(20, "NOT Found " << it->refName);
100 // Dont compare if any of hists is missing ...
101 continue;
102 }
103 B2DEBUG(20, "Compare " << it->histName << " with ref " << it->refName);
104
105 it->canvas->UseCurrentStyle();
106 it->canvas->Clear();
107 it->canvas->cd();
108
109 double data = 0;
110 if (it->algo == 1) {
111 data = histo->KolmogorovTest(refhist, ""); // returns p value (0 bad, 1 good), N - do not compare normalized
112 B2DEBUG(20, "KS: " << data << " , " << it->warning << " , " << it->error);
113 } else { // default
114 data = histo->Chi2Test(refhist, "NORM,UU"); // return p value (0 bad, 1 good), ignores normalization
115 B2DEBUG(20, "Chi: " << data << " , " << it->warning << " , " << it->error);
116 }
117 // TODO other type?
118 // data= BinByBinTest(hits1,refhist);// user function (like Peters test)
119 // B2DEBUG(20, "User: " << data<< " , " <<it->warning<< " , " <<it->error);
120 if (it->pvfit != "") setEpicsPV(it->pvfit, data);
121
122 refhist->SetLineStyle(3);// 2 or 3
123 refhist->SetLineColor(3);
124
125 /*
126 // I think no need for this anymore
127 TIter nextkey(it->canvas->GetListOfPrimitives());
128 TObject* obj = NULL;
129 while ((obj = (TObject*)nextkey())) {
130 if (obj->IsA()->InheritsFrom("TH1")) {
131 if (string(obj->GetName()) == string(refhist->GetName())) {
132 delete obj;
133 }
134 }
135 }
136 */
137
138 refhist->Draw("hist");
139 refhist->SetStats(kFALSE);
140 if (refhist->GetMaximum() > histo->GetMaximum())
141 histo->SetMaximum(1.1 * refhist->GetMaximum());
142 histo->Draw("hist");
143 refhist->Draw("hist,same");
144 // think about the order, reference should be behind histogram?
145
146 auto status_data = makeStatus(histo->GetEntries() >= it->min_entries, data < it->warning,
147 data < it->error); // this must be double for EPICS below!
148 if (it->pvstatus != "") setEpicsPV(it->pvstatus, status_data);
149 if (it->colorflag) colorizeCanvas(it->canvas, status_data);
150
151 //auto tt = new TPaveText(.80, 1, 1, .90, "NDC");
152 //tt->SetFillColor(kWhite);
153 //tt->AddText(("Prob: " + std::to_string(data)).data());
154 //tt->Draw();
155 // delete tt;// do not delete it, otherwise it vanishes from the Canvas!
156
157 it->canvas->Modified();
158 it->canvas->Update();
159 }
160}
161
163{
164 B2DEBUG(20, "DQMHistComparitor: endRun called");
165}
166
168{
169 B2DEBUG(20, "DQMHistComparitor: terminate called");
170}
static TH1 * findRefHist(const std::string &histname, ERefScaling scaling=ERefScaling::c_RefScaleNone, const TH1 *hist=nullptr)
Get referencehistogram from list (no other search).
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
static void colorizeCanvas(TCanvas *canvas, EStatus status)
Helper function for Canvas colorization.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
TH1 * getDelta(const std::string &fullname, int n=0, bool onlyIfUpdated=true)
Get Delta histogram.
DQMHistAnalysisModule()
Constructor / Destructor.
@ c_RefScaleEntries
to number of entries (integral)
static EStatus makeStatus(bool enough, bool warn_flag, bool error_flag)
Helper function to judge the status for coloring and EPICS.
void setEpicsPV(const std::string &keyname, double value)
Write value to a EPICS PV.
void initialize() override final
Initializer.
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.
std::vector< std::vector< std::string > > m_histlist
Parameter list for histograms.
void beginRun() override final
Called when entering a new run.
std::vector< CMPNODE * > m_pnode
Struct for extracted parameters + EPICS PV.
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.
The struct for reference histogram comparison.