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 refhist->Draw("hist");
126 refhist->SetStats(kFALSE);
127 if (refhist->GetMaximum() > histo->GetMaximum())
128 histo->SetMaximum(1.1 * refhist->GetMaximum());
129 histo->Draw("hist");
130 refhist->Draw("hist,same");
131 // think about the order, reference should be behind histogram?
132
133 auto status_data = makeStatus(histo->GetEntries() >= it->min_entries, data < it->warning,
134 data < it->error); // this must be double for EPICS below!
135 if (it->pvstatus != "") setEpicsPV(it->pvstatus, status_data);
136 if (it->colorflag) colorizeCanvas(it->canvas, status_data);
137
138 //auto tt = new TPaveText(.80, 1, 1, .90, "NDC");
139 //tt->SetFillColor(kWhite);
140 //tt->AddText(("Prob: " + std::to_string(data)).data());
141 //tt->Draw();
142 // delete tt;// do not delete it, otherwise it vanishes from the Canvas!
143
144 it->canvas->Modified();
145 it->canvas->Update();
146 }
147}
148
150{
151 B2DEBUG(20, "DQMHistComparitor: endRun called");
152}
153
155{
156 B2DEBUG(20, "DQMHistComparitor: terminate called");
157}
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
TH1 * getDelta(const std::string &dirname, const std::string &histname="", bool onlyIfUpdated=true, int n=0)
Get Delta histogram.
static void colorizeCanvas(TCanvas *canvas, EStatus status)
Helper function for Canvas colorization.
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.
static TH1 * findRefHist(const std::string &dirname, const std::string &histname="", ERefScaling scaling=ERefScaling::c_RefScaleNone, const TH1 *hist=nullptr)
Find reference histogram.
void setEpicsPV(const std::string &keyname, double value)
Write value to a EPICS PV.
static TH1 * findHist(const std::string &dirname, const std::string &histname="", bool onlyIfUpdated=false)
Find histogram.
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.