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}
42
44{
45 gStyle->SetOptStat(0);
46 gStyle->SetStatStyle(1);
47 gStyle->SetOptDate(22);// Date and Time in Bottom Right, does no work
48
49 for (auto& it : m_histlist) {
50 if (it.size() != 11) {
51 B2WARNING("Histolist with wrong nr of parameters (" << it.size() << "), histo=" << (it.size() > 0 ? it.at(0) : std::string("")));
52 continue;
53 }
54
55 auto n = new CMPNODE;
56 n->histName = it.at(0);
57 n->refName = it.at(1) == "" ? n->histName : it.at(1) ; // only use if set
58 n->canvas = new TCanvas(it.at(2).c_str());
59
60 n->deltaflag = it.at(3) != "";
61 n->colorflag = it.at(4) != "";
62 n->min_entries = atoi(it.at(5).c_str()); // if delta hist is used, value must be lower or equal delta entries value
63 if (it.at(6) == "KS") n->algo = 1;
64 else n->algo = 0; // chi as default
65 n->warning = atof(it.at(7).c_str());
66 n->error = atof(it.at(8).c_str());
67
68 n->pvfit = it.at(9);
69 if (n->pvfit != "") registerEpicsPV(n->pvfit);
70 n->pvstatus = it.at(10);
71 if (n->pvstatus != "") registerEpicsPV(n->pvstatus);
72 m_pnode.push_back(n);
73 }
74
75 B2DEBUG(20, "DQMHistComparitor: initialized.");
76}
77
78
80{
81 B2DEBUG(20, "DQMHistComparitor: beginRun called.");
82}
83
85{
86 gStyle->SetOptStat(0);
87 gStyle->SetStatStyle(0);
88 gStyle->SetOptDate(22);// Date and Time in Bottom Right, does no work
89
90 for (auto& it : m_pnode) {
91 TH1* histo = nullptr;
92 B2DEBUG(20, "== Search for " << it->histName << " with ref " << it->refName << "==");
93 if (it->deltaflag) histo = getDelta(it->histName); // default: last delta
94 if (!histo) histo = findHist(it->histName, true); // only if changed
95 if (!histo) {
96 // B2DEBUG(20, "NOT Found " << it->histName);
97 // Dont compare if missing or not updated ...
98 continue;
99 }
100 // if compare normalized ... need configuration
101 auto refhist = findRefHist(it->refName, ERefScaling::c_RefScaleEntries, histo);
102 if (!refhist) {
103 B2DEBUG(20, "NOT Found " << it->refName);
104 // Dont compare if any of hists is missing ...
105 continue;
106 }
107 B2DEBUG(20, "Compare " << it->histName << " with ref " << it->refName);
108
109 it->canvas->UseCurrentStyle();
110 it->canvas->Clear();
111 it->canvas->cd();
112
113 double data = 0;
114 if (it->algo == 1) {
115 data = histo->KolmogorovTest(refhist, ""); // returns p value (0 bad, 1 good), N - do not compare normalized
116 B2DEBUG(20, "KS: " << data << " , " << it->warning << " , " << it->error);
117 } else { // default
118 data = histo->Chi2Test(refhist, "NORM,UU"); // return p value (0 bad, 1 good), ignores normalization
119 B2DEBUG(20, "Chi: " << data << " , " << it->warning << " , " << it->error);
120 }
121 // TODO other type?
122 // data= BinByBinTest(hits1,refhist);// user function (like Peters test)
123 // B2DEBUG(20, "User: " << data<< " , " <<it->warning<< " , " <<it->error);
124 if (it->pvfit != "") setEpicsPV(it->pvfit, data);
125
126 refhist->SetLineStyle(3);// 2 or 3
127 refhist->SetLineColor(3);
128
129 /*
130 // I think no need for this anymore
131 TIter nextkey(it->canvas->GetListOfPrimitives());
132 TObject* obj = NULL;
133 while ((obj = (TObject*)nextkey())) {
134 if (obj->IsA()->InheritsFrom("TH1")) {
135 if (string(obj->GetName()) == string(refhist->GetName())) {
136 delete obj;
137 }
138 }
139 }
140 */
141
142 refhist->Draw("hist");
143 refhist->SetStats(kFALSE);
144 if (refhist->GetMaximum() > histo->GetMaximum())
145 histo->SetMaximum(1.1 * refhist->GetMaximum());
146 histo->Draw("hist");
147 refhist->Draw("hist,same");
148 // think about the order, reference should be behind histogram?
149
150 auto status_data = makeStatus(histo->GetEntries() >= it->min_entries, data < it->warning,
151 data < it->error); // this must be double for EPICS below!
152 if (it->pvstatus != "") setEpicsPV(it->pvstatus, status_data);
153 if (it->colorflag) colorizeCanvas(it->canvas, status_data);
154
155 //auto tt = new TPaveText(.80, 1, 1, .90, "NDC");
156 //tt->SetFillColor(kWhite);
157 //tt->AddText(("Prob: " + std::to_string(data)).data());
158 //tt->Draw();
159 // delete tt;// do not delete it, otherwise it vanishes from the Canvas!
160
161 it->canvas->Modified();
162 it->canvas->Update();
163 }
164}
165
167{
168 B2DEBUG(20, "DQMHistComparitor: endRun called");
169}
170
172{
173 B2DEBUG(20, "DQMHistComparitor: terminate called");
174}
The base class for the histogram analysis module.
static TH1 * findRefHist(const std::string &histname, ERefScaling scaling=ERefScaling::c_RefScaleNone, const TH1 *hist=nullptr)
Get referencehistogram from list (no other search).
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.
void setEpicsPV(std::string keyname, double value)
Write value to a EPICS PV.
@ c_RefScaleEntries
to number of entries (integral)
EStatus makeStatus(bool enough, bool warn_flag, bool error_flag)
Helper function to judge the status for coloring and EPICS.
int registerEpicsPV(std::string pvname, std::string keyname="")
EPICS related Functions.
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.