14 #include <framework/core/ModuleParam.templateDetails.h>
15 #include <dqm/analysis/modules/DQMHistComparitor.h>
16 #include <daq/slc/base/StringUtil.h>
20 #include <TDirectory.h>
41 addParam(
"HistoList", m_histlist,
"['histname', 'refhistname', 'canvas', 'minentry', 'warnlvl', 'errlvl', 'pvname'],[...],...");
42 addParam(
"RefHistoFile", m_refFileName,
"Reference histrogram file name", std::string(
"refHisto.root"));
43 addParam(
"ColorAlert", m_color,
"Whether to show the color alert",
true);
44 B2DEBUG(1,
"DQMHistComparitor: Constructor done.");
47 DQMHistComparitorModule::~DQMHistComparitorModule()
50 if (ca_current_context()) ca_context_destroy();
54 TH1* DQMHistComparitorModule::find_histo_in_canvas(TString histo_name)
56 StringList s = StringUtil::split(histo_name.Data(),
'/');
57 std::string dirname = s[0];
58 std::string hname = s[1];
59 std::string canvas_name = dirname +
"/c_" + hname;
61 TIter nextckey(gROOT->GetListOfCanvases());
64 while ((cobj = (TObject*)nextckey())) {
65 if (cobj->IsA()->InheritsFrom(
"TCanvas")) {
66 if (cobj->GetName() == canvas_name)
70 if (cobj == NULL)
return NULL;
72 TIter nextkey(((TCanvas*)cobj)->GetListOfPrimitives());
75 while ((obj = (TObject*)nextkey())) {
76 if (obj->IsA()->InheritsFrom(
"TH1")) {
77 if (obj->GetName() == histo_name)
84 TH1* DQMHistComparitorModule::GetHisto(TString histoname)
88 hh1 = findHist(histoname.Data());
90 B2DEBUG(20,
"findHisto failed " << histoname <<
" not in memfile");
93 if (m_refFile && m_refFile->IsOpen()) {
94 TDirectory* d = m_refFile;
95 TString myl = histoname;
99 while (myl.Tokenize(tok, from,
"/")) {
103 if (myl.Tokenize(dummy, f,
"/")) {
104 auto e = d->GetDirectory(tok);
106 B2DEBUG(20,
"Cd Dir " << tok <<
" from " << d->GetPath());
109 B2DEBUG(20,
"cd failed " << tok <<
" from " << d->GetPath());
115 TObject* obj = d->FindObject(tok);
117 if (obj->IsA()->InheritsFrom(
"TH1")) {
118 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file");
121 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file but wrong type");
125 TIter next(d->GetListOfKeys());
127 while ((key = (TKey*)next())) {
128 TObject* obj2 = key->ReadObj() ;
129 if (obj2->InheritsFrom(
"TH1")) {
130 if (obj2->GetName() == tok) {
132 B2DEBUG(20,
"Histo " << histoname <<
" found as key -> readobj");
137 if (hh1 == NULL) B2DEBUG(20,
"Histo " << histoname <<
" NOT found in ref file " << tok);
142 B2DEBUG(20,
"Histo " << histoname <<
" not in memfile or ref file");
144 TDirectory* d = gROOT;
145 TString myl = histoname;
148 while (myl.Tokenize(tok, from,
"/")) {
152 if (myl.Tokenize(dummy, f,
"/")) {
153 auto e = d->GetDirectory(tok);
155 B2DEBUG(20,
"Cd Dir " << tok);
157 }
else B2DEBUG(20,
"cd failed " << tok);
163 TObject* obj = d->FindObject(tok);
165 if (obj->IsA()->InheritsFrom(
"TH1")) {
166 B2DEBUG(20,
"Histo " << histoname <<
" found in mem");
170 B2DEBUG(20,
"Histo " << histoname <<
" NOT found in mem");
176 B2DEBUG(20,
"Histo " << histoname <<
" not found");
182 void DQMHistComparitorModule::initialize()
185 if (m_refFileName !=
"") {
186 m_refFile =
new TFile(m_refFileName.data());
189 gStyle->SetOptStat(0);
190 gStyle->SetStatStyle(1);
191 gStyle->SetOptDate(22);
194 if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback),
"ca_context_create");
196 for (
auto& it : m_histlist) {
197 if (it.size() != 7) {
198 B2WARNING(
"Histolist with wrong nr of parameters (" << it.size() <<
"), hist1=" << it.at(0));
204 TString a = it.at(2).c_str();
207 n->histo1 = it.at(0);
208 n->histo2 = it.at(1);
209 TCanvas* c =
new TCanvas(a);
212 n->min_entries = atoi(it.at(3).c_str());
213 n->warning = atof(it.at(4).c_str());
214 n->error = atof(it.at(5).c_str());
215 n->epicsflag =
false;
218 if (it.at(6) !=
"") {
219 SEVCHK(ca_create_channel(it.at(6).c_str(), NULL, NULL, 10, &n->mychid),
"ca_create_channel failure");
223 m_pnode.push_back(n);
226 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
229 B2DEBUG(20,
"DQMHistComparitor: initialized.");
233 void DQMHistComparitorModule::beginRun()
235 B2DEBUG(20,
"DQMHistComparitor: beginRun called.");
238 void DQMHistComparitorModule::event()
240 for (
auto& it : m_pnode) {
244 B2DEBUG(20,
"== Search for " << it->histo1 <<
" with ref " << it->histo2 <<
"==");
245 hist1 = find_histo_in_canvas(it->histo1);
246 if (!hist1) B2DEBUG(20,
"NOT Found " << it->histo1);
247 hist2 = GetHisto(it->histo2);
248 if (!hist2) B2DEBUG(20,
"NOT Found " << it->histo2);
250 if (!hist1)
continue;
251 if (!hist2)
continue;
253 B2DEBUG(20,
"Compare " << it->histo1 <<
" with ref " << it->histo2);
257 double data = hist1->KolmogorovTest(hist2,
"");
262 if (it->epicsflag) SEVCHK(ca_put(DBR_DOUBLE, it->mychid, (
void*)&data),
"ca_set failure");
265 hist2->SetLineStyle(3);
266 hist2->SetLineColor(3);
268 TIter nextkey(it->canvas->GetListOfPrimitives());
270 while ((obj = (TObject*)nextkey())) {
271 if (obj->IsA()->InheritsFrom(
"TH1")) {
272 if (
string(obj->GetName()) ==
string(hist2->GetName())) {
281 h = (TH1*)hist2->Clone();
282 if (abs(hist2->Integral()) > 0)
283 h->Scale(hist1->Integral() / hist2->Integral());
290 hist1->SetFillColor(0);
291 if (h->GetMaximum() > hist1->GetMaximum())
292 hist1->SetMaximum(1.1 * h->GetMaximum());
294 h->Draw(
"hist,same");
296 it->canvas->Pad()->SetFrameFillColor(10);
298 if (hist1->GetEntries() < it->min_entries) {
300 it->canvas->Pad()->SetFillColor(6);
302 if (data < it->error) {
303 it->canvas->Pad()->SetFillColor(2);
304 }
else if (data < it->warning) {
305 it->canvas->Pad()->SetFillColor(5);
307 it->canvas->Pad()->SetFillColor(0);
311 it->canvas->Pad()->SetFillColor(0);
313 it->canvas->Modified();
314 it->canvas->Update();
317 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
321 void DQMHistComparitorModule::endRun()
323 B2DEBUG(20,
"DQMHistComparitor: endRun called");
327 void DQMHistComparitorModule::terminate()
329 B2DEBUG(20,
"DQMHistComparitor: terminate called");
330 if (m_refFile)
delete m_refFile;
The base class for the histogram analysis module.
Class definition for the reference histogram display.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
The struct for reference histogram comparison.