Belle II Software development
DQMHistAnalysis.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 : DQMHistAnalysisModule.cc
10// Description : Baseclass for DQM histogram analysis module
11//-
12
13#include <dqm/core/DQMHistAnalysis.h>
14#include <boost/algorithm/string.hpp>
15#include <TROOT.h>
16#include <TClass.h>
17
18using namespace std;
19using namespace Belle2;
20
21//-----------------------------------------------------------------
22// Register the Module
23//-----------------------------------------------------------------
24REG_MODULE(DQMHistAnalysis);
25
26//-----------------------------------------------------------------
27// Implementation
28//-----------------------------------------------------------------
29
35#ifdef _BELLE2_EPICS
36std::vector <chid> DQMHistAnalysisModule::m_epicsChID;
37#endif
38
39bool DQMHistAnalysisModule::m_useEpics = false; // default to false, to enable EPICS, add special EPICS Module class into chain
41 false; // special for second "online" use (reading limits). default to false, to enable EPICS, add special EPICS Module parameter
42std::string DQMHistAnalysisModule::m_PVPrefix = "TEST:"; // default to "TEST:", for production, set in EPICS enabler to e.g. "DQM:"
43
45{
46 //Set module properties
47 setDescription("Histogram Analysis module base class");
48}
49
51{
52 s_histList.clear();
53 s_refList.clear();
54// s_monObjList;
55 s_deltaList.clear();
56 s_canvasUpdatedList.clear();
57}
58
59bool DQMHistAnalysisModule::addHist(const std::string& dirname, const std::string& histname, TH1* h)
60{
61 std::string fullname;
62 if (dirname.size() > 0) {
63 fullname = dirname + "/" + histname;
64 } else {
65 fullname = histname;
66 }
67
68 if (s_histList[fullname].update(h)) {
69 // only if histogram changed, check if delta histogram update needed
70 auto it = s_deltaList.find(fullname);
71 if (it != s_deltaList.end()) {
72 B2DEBUG(20, "Found Delta" << fullname);
73 it->second.update(h); // update
74 }
75 return true; // histogram changed
76 }
77
78 return false; // histogram didn't change
79}
80
81// void DQMHistAnalysisModule::addRef(const std::string& dirname, const std::string& histname, TH1* ref)
82// {
83// std::string fullname;
84// if (dirname.size() > 0) {
85// fullname = dirname + "/" + histname;
86// } else {
87// fullname = histname;
88// }
89// auto it = s_refList.find(fullname);
90// if (it == s_refList.end()) {
91// B2DEBUG(1, "Did not find histogram " << fullname << "in s_refList, so inserting now.");
92// s_refList.insert({fullname, ref});
93// }
94// }
95
96void DQMHistAnalysisModule::addDeltaPar(const std::string& dirname, const std::string& histname, HistDelta::EDeltaType t, int p,
97 unsigned int a)
98{
99 std::string fullname;
100 if (dirname.size() > 0) {
101 fullname = dirname + "/" + histname;
102 } else {
103 fullname = histname;
104 }
105 s_deltaList[fullname].set(t, p, a);
106}
107
108bool DQMHistAnalysisModule::hasDeltaPar(const std::string& dirname, const std::string& histname)
109{
110 std::string fullname;
111 if (dirname.size() > 0) {
112 fullname = dirname + "/" + histname;
113 } else {
114 fullname = histname;
115 }
116 return s_deltaList.find(fullname) != s_deltaList.end(); // contains() if we switch to C++20
117}
118
119TH1* DQMHistAnalysisModule::getDelta(const std::string& dirname, const std::string& histname, int n, bool onlyIfUpdated)
120{
121 std::string fullname;
122 if (dirname.size() > 0) {
123 fullname = dirname + "/" + histname;
124 } else {
125 fullname = histname;
126 }
127 return getDelta(fullname, n, onlyIfUpdated);
128}
129
130TH1* DQMHistAnalysisModule::getDelta(const std::string& fullname, int n, bool onlyIfUpdated)
131{
132 auto it = s_deltaList.find(fullname);
133 if (it != s_deltaList.end()) {
134 return it->second.getDelta(n, onlyIfUpdated);
135 }
136 B2WARNING("Delta hist " << fullname << " not found");
137 return nullptr;
138}
139
141{
142 auto obj = &s_monObjList[objName];
143 obj->SetName(objName.c_str());
144 return obj;
145}
146
147TCanvas* DQMHistAnalysisModule::findCanvas(TString canvas_name)
148{
149 TIter nextkey(gROOT->GetListOfCanvases());
150 TObject* obj{};
151
152 while ((obj = dynamic_cast<TObject*>(nextkey()))) {
153 if (obj->IsA()->InheritsFrom("TCanvas")) {
154 if (obj->GetName() == canvas_name)
155 return dynamic_cast<TCanvas*>(obj);
156 }
157 }
158 return nullptr;
159}
160
161TH1* DQMHistAnalysisModule::findHist(const std::string& histname, bool was_updated)
162{
163 if (s_histList.find(histname) != s_histList.end()) {
164 if (was_updated && !s_histList[histname].isUpdated()) return nullptr;
165 if (s_histList[histname].getHist()) {
166 return s_histList[histname].getHist();
167 } else {
168 B2ERROR("Histogram " << histname << " in histogram list but nullptr.");
169 }
170 }
171 B2INFO("Histogram " << histname << " not in list.");
172 return nullptr;
173}
174
175TH1* DQMHistAnalysisModule::findHist(const std::string& dirname, const std::string& histname, bool updated)
176{
177 if (dirname.size() > 0) {
178 return findHist(dirname + "/" + histname, updated);
179 }
180 return findHist(histname, updated);
181}
182
183TH1* DQMHistAnalysisModule::scaleReference(int scaling, const TH1* hist, TH1* ref)
184{
185 // if hist/ref is nullptr, nothing to do
186 if (!hist || !ref)
187 return ref;
188
189 switch (scaling) {
190 // default: do nothing
191 // case 0: do nothing
192 case 1: // Integral
193 // only if we have entries in reference
194 if (hist->Integral() != 0 and ref->Integral() != 0) {
195 ref->Scale(hist->Integral() / ref->Integral());
196 }
197 break;
198 case 2: // Maximum
199 // only if we have entries in reference
200 if (hist->GetMaximum() != 0 and ref->GetMaximum() != 0) {
201 ref->Scale(hist->GetMaximum() / ref->GetMaximum());
202 }
203 break;
204 }
205 return ref;
206}
207
208TH1* DQMHistAnalysisModule::findRefHist(const std::string& histname, int scaling, const TH1* hist)
209{
210 if (s_refList.find(histname) != s_refList.end()) {
211 // get a copy of the reference which we can modify
212 // (it is still owned and managed by the framework)
213 // then do the scaling
214 return scaleReference(scaling, hist, s_refList[histname].getReference());
215 }
216 B2INFO("Ref Histogram " << histname << " not in list.");
217 return nullptr;
218}
219
220TH1* DQMHistAnalysisModule::findRefHist(const std::string& dirname, const std::string& histname, int scaling, const TH1* hist)
221{
222 if (dirname.size() > 0) {
223 return findRefHist(dirname + "/" + histname, scaling, hist);
224 }
225 return findRefHist(histname, scaling, hist);
226}
227
228TH1* DQMHistAnalysisModule::findHistInCanvas(const std::string& histo_name, TCanvas** cobj)
229{
230 TCanvas* cnv = nullptr;
231 // try to get canvas from outside
232 if (cobj) cnv = *cobj;
233 // if no canvas search for it
234 if (cnv == nullptr) {
235 // parse the dir+histo name and create the corresponding canvas name
236 auto s = StringSplit(histo_name, '/');
237 if (s.size() != 2) {
238 B2ERROR("findHistInCanvas: histoname not valid (missing dir?), should be 'dirname/histname': " << histo_name);
239 return nullptr;
240 }
241 auto dirname = s.at(0);
242 auto hname = s.at(1);
243 std::string canvas_name = dirname + "/c_" + hname;
244 cnv = findCanvas(canvas_name);
245 // set canvas pointer for outside
246 if (cnv && cobj) *cobj = cnv;
247 }
248
249 // get histogram pointer
250 if (cnv != nullptr) {
251 TIter nextkey(cnv->GetListOfPrimitives());
252 TObject* obj{};
253 while ((obj = dynamic_cast<TObject*>(nextkey()))) {
254 if (obj->IsA()->InheritsFrom("TH1")) {
255 if (obj->GetName() == histo_name)
256 return dynamic_cast<TH1*>(obj);
257 }
258 }
259 }
260 return nullptr;
261}
262
263TH1* DQMHistAnalysisModule::findHistInFile(TFile* file, const std::string& histname)
264{
265 // find histogram by name in file, histname CAN contain directory!
266 // will return nullptr if file is zeroptr, not found or not correct type
267 if (file && file->IsOpen()) {
268 auto obj = file->Get(histname.data());
269 if (obj != nullptr) {
270 // check class type
271 if (obj->IsA()->InheritsFrom("TH1")) {
272 B2DEBUG(20, "Histogram " << histname << " found in file");
273 return dynamic_cast<TH1*>(obj);
274 } else {
275 B2INFO("Found Object " << histname << " in file is not a histogram");
276 }
277 } else {
278 B2INFO("Histogram " << histname << " not found in file");
279 }
280 }
281 return nullptr;
282}
283
285{
286 if (s_monObjList.find(objName) != s_monObjList.end()) {
287 return &s_monObjList[objName];
288 }
289 B2INFO("MonitoringObject " << objName << " not in memfile.");
290 return NULL;
291}
292
294{
295 double probs[2] = {0.16, 1 - 0.16};
296 double quant[2] = {0, 0};
297 h->GetQuantiles(2, quant, probs);
298 const double sigma68 = (-quant[0] + quant[1]) / 2;
299 return sigma68;
300}
301
302std::vector <std::string> DQMHistAnalysisModule::StringSplit(const std::string& in, const char delim)
303{
304 std::vector <std::string> out;
305 boost::split(out, in, [delim](char c) {return c == delim;});
306 return out;
307}
308
310{
311 TIter nextckey(gROOT->GetListOfCanvases());
312 TObject* cobj = nullptr;
313
314 while ((cobj = dynamic_cast<TObject*>(nextckey()))) {
315 if (cobj->IsA()->InheritsFrom("TCanvas")) {
316 TCanvas* cnv = dynamic_cast<TCanvas*>(cobj);
317 cnv->Clear();
319 }
320 }
321}
322
324{
325 for (auto& it : s_histList) {
326 // attention, we must use reference, otherwise we work on a copy
327 it.second.resetBeforeEvent();
328 }
329 for (auto& it : s_deltaList) {
330 // attention, we must use reference, otherwise we work on a copy
331 it.second.setNotUpdated();
332 }
333
334 s_canvasUpdatedList.clear();
335}
336
338{
339 s_histList.clear();
340}
341
343{
344 s_refList.clear();
345}
346
348{
349 for (auto& d : s_deltaList) {
350 d.second.reset();
351 }
352}
353
354void DQMHistAnalysisModule::UpdateCanvas(std::string name, bool updated)
355{
356 s_canvasUpdatedList[name] = updated;
357}
358
359void DQMHistAnalysisModule::UpdateCanvas(TCanvas* c, bool updated)
360{
361 if (c) UpdateCanvas(c->GetName(), updated);
362}
363
364void DQMHistAnalysisModule::ExtractRunType(std::vector <TH1*>& hs)
365{
366 s_runType = "";
367 for (size_t i = 0; i < hs.size(); i++) {
368 if (hs[i]->GetName() == std::string("DQMInfo/rtype")) {
369 s_runType = hs[i]->GetTitle();
370 return;
371 }
372 }
373 B2ERROR("ExtractRunType: Histogram \"DQMInfo/rtype\" missing");
374}
375
376void DQMHistAnalysisModule::ExtractEvent(std::vector <TH1*>& hs)
377{
379 for (size_t i = 0; i < hs.size(); i++) {
380 if (hs[i]->GetName() == std::string("DAQ/Nevent")) {
381 s_eventProcessed = hs[i]->GetEntries();
382 return;
383 }
384 }
385 B2ERROR("ExtractEvent: Histogram \"DAQ/Nevent\" missing");
386}
387
388int DQMHistAnalysisModule::registerEpicsPV(std::string pvname, std::string keyname)
389{
390 return registerEpicsPVwithPrefix(m_PVPrefix, pvname, keyname);
391}
392
393int DQMHistAnalysisModule::registerExternalEpicsPV(std::string pvname, std::string keyname)
394{
395 return registerEpicsPVwithPrefix(std::string(""), pvname, keyname);
396}
397
398int DQMHistAnalysisModule::registerEpicsPVwithPrefix(std::string prefix, std::string pvname, std::string keyname)
399{
400 if (!m_useEpics) return -1;
401#ifdef _BELLE2_EPICS
402 if (m_epicsNameToChID[pvname] != nullptr) {
403 B2ERROR("Epics PV " << pvname << " already registered!");
404 return -1;
405 }
406 if (keyname != "" && m_epicsNameToChID[keyname] != nullptr) {
407 B2ERROR("Epics PV with key " << keyname << " already registered!");
408 return -1;
409 }
410
411 m_epicsChID.emplace_back();
412 auto ptr = &m_epicsChID.back();
413 if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback), "ca_context_create");
414 // the subscribed name includes the prefix, the map below does *not*
415 CheckEpicsError(ca_create_channel((prefix + pvname).data(), NULL, NULL, 10, ptr), "ca_create_channel failure", pvname);
416
417 m_epicsNameToChID[pvname] = *ptr;
418 if (keyname != "") m_epicsNameToChID[keyname] = *ptr;
419 return m_epicsChID.size() - 1; // return index to last added item
420#else
421 return -1;
422#endif
423}
424
425void DQMHistAnalysisModule::setEpicsPV(std::string keyname, double value)
426{
427 if (!m_useEpics || m_epicsReadOnly) return;
428#ifdef _BELLE2_EPICS
429 if (m_epicsNameToChID[keyname] == nullptr) {
430 B2ERROR("Epics PV " << keyname << " not registered!");
431 return;
432 }
433 CheckEpicsError(ca_put(DBR_DOUBLE, m_epicsNameToChID[keyname], (void*)&value), "ca_set failure", keyname);
434#endif
435}
436
437void DQMHistAnalysisModule::setEpicsPV(std::string keyname, int value)
438{
439 if (!m_useEpics || m_epicsReadOnly) return;
440#ifdef _BELLE2_EPICS
441 if (m_epicsNameToChID[keyname] == nullptr) {
442 B2ERROR("Epics PV " << keyname << " not registered!");
443 return;
444 }
445 CheckEpicsError(ca_put(DBR_SHORT, m_epicsNameToChID[keyname], (void*)&value), "ca_set failure", keyname);
446#endif
447}
448
449void DQMHistAnalysisModule::setEpicsStringPV(std::string keyname, std::string value)
450{
451 if (!m_useEpics || m_epicsReadOnly) return;
452#ifdef _BELLE2_EPICS
453 if (m_epicsNameToChID[keyname] == nullptr) {
454 B2ERROR("Epics PV " << keyname << " not registered!");
455 return;
456 }
457 if (value.length() > 40) {
458 B2ERROR("Epics string PV " << keyname << " too long (>40 characters)!");
459 return;
460 }
461 char text[40];
462 strcpy(text, value.c_str());
463 CheckEpicsError(ca_put(DBR_STRING, m_epicsNameToChID[keyname], text), "ca_set failure", keyname);
464#endif
465}
466
467void DQMHistAnalysisModule::setEpicsPV(int index, double value)
468{
469 if (!m_useEpics || m_epicsReadOnly) return;
470#ifdef _BELLE2_EPICS
471 if (index < 0 || index >= (int)m_epicsChID.size()) {
472 B2ERROR("Epics PV with " << index << " not registered!");
473 return;
474 }
475 CheckEpicsError(ca_put(DBR_DOUBLE, m_epicsChID[index], (void*)&value), "ca_set failure", m_epicsChID[index]);
476#endif
477}
478
479void DQMHistAnalysisModule::setEpicsPV(int index, int value)
480{
481 if (!m_useEpics || m_epicsReadOnly) return;
482#ifdef _BELLE2_EPICS
483 if (index < 0 || index >= (int)m_epicsChID.size()) {
484 B2ERROR("Epics PV with " << index << " not registered!");
485 return;
486 }
487 CheckEpicsError(ca_put(DBR_SHORT, m_epicsChID[index], (void*)&value), "ca_set failure", m_epicsChID[index]);
488#endif
489}
490
491void DQMHistAnalysisModule::setEpicsStringPV(int index, std::string value)
492{
493 if (!m_useEpics || m_epicsReadOnly) return;
494#ifdef _BELLE2_EPICS
495 if (index < 0 || index >= (int)m_epicsChID.size()) {
496 B2ERROR("Epics PV with " << index << " not registered!");
497 return;
498 }
499 char text[41];
500 strncpy(text, value.c_str(), 40);
501 text[40] = 0;
502 CheckEpicsError(ca_put(DBR_STRING, m_epicsChID[index], text), "ca_set failure", m_epicsChID[index]);
503#endif
504}
505
506double DQMHistAnalysisModule::getEpicsPV(std::string keyname)
507{
508 double value{NAN};
509 if (!m_useEpics) return value;
510#ifdef _BELLE2_EPICS
511 if (m_epicsNameToChID[keyname] == nullptr) {
512 B2ERROR("Epics PV " << keyname << " not registered!");
513 return value;
514 }
515 // From EPICS doc. When ca_get or ca_array_get are invoked the returned channel value can't be assumed to be stable
516 // in the application supplied buffer until after ECA_NORMAL is returned from ca_pend_io. If a connection is lost
517 // outstanding get requests are not automatically reissued following reconnect.
518 auto r = ca_get(DBR_DOUBLE, m_epicsNameToChID[keyname], (void*)&value);
519 if (r == ECA_NORMAL) r = ca_pend_io(5.0); // this is needed!
520 if (r == ECA_NORMAL) {
521 return value;
522 } else {
523 CheckEpicsError(r, "Read PV failed in ca_get or ca_pend_io failure", keyname);
524 }
525#endif
526 return NAN;
527}
528
530{
531 double value{NAN};
532 if (!m_useEpics) return value;
533#ifdef _BELLE2_EPICS
534 if (index < 0 || index >= (int)m_epicsChID.size()) {
535 B2ERROR("Epics PV with " << index << " not registered!");
536 return value;
537 }
538 // From EPICS doc. When ca_get or ca_array_get are invoked the returned channel value can't be assumed to be stable
539 // in the application supplied buffer until after ECA_NORMAL is returned from ca_pend_io. If a connection is lost
540 // outstanding get requests are not automatically reissued following reconnect.
541 auto r = ca_get(DBR_DOUBLE, m_epicsChID[index], (void*)&value);
542 if (r == ECA_NORMAL) r = ca_pend_io(5.0); // this is needed!
543 if (r == ECA_NORMAL) {
544 return value;
545 } else {
546 CheckEpicsError(r, "Read PV failed in ca_get or ca_pend_io failure", m_epicsChID[index]);
547 }
548#endif
549 return NAN;
550}
551
552std::string DQMHistAnalysisModule::getEpicsStringPV(std::string keyname, bool& status)
553{
554 status = false;
555 char value[40] = "";
556 if (!m_useEpics) return std::string(value);
557#ifdef _BELLE2_EPICS
558 if (m_epicsNameToChID[keyname] == nullptr) {
559 B2ERROR("Epics PV " << keyname << " not registered!");
560 return std::string(value);
561 }
562 // From EPICS doc. When ca_get or ca_array_get are invoked the returned channel value can't be assumed to be stable
563 // in the application supplied buffer until after ECA_NORMAL is returned from ca_pend_io. If a connection is lost
564 // outstanding get requests are not automatically reissued following reconnect.
565 auto r = ca_get(DBR_STRING, m_epicsNameToChID[keyname], value);
566 if (r == ECA_NORMAL) r = ca_pend_io(5.0); // this is needed!
567 if (r == ECA_NORMAL) {
568 status = true;
569 return std::string(value);
570 } else {
571 CheckEpicsError(r, "Read PV (string) failed in ca_get or ca_pend_io failure", keyname);
572 }
573#endif
574 return std::string(value);
575}
576
577std::string DQMHistAnalysisModule::getEpicsStringPV(int index, bool& status)
578{
579 status = false;
580 char value[40] = "";
581 if (!m_useEpics) return std::string(value);
582#ifdef _BELLE2_EPICS
583 if (index < 0 || index >= (int)m_epicsChID.size()) {
584 B2ERROR("Epics PV with " << index << " not registered!");
585 return std::string(value);
586 }
587 // From EPICS doc. When ca_get or ca_array_get are invoked the returned channel value can't be assumed to be stable
588 // in the application supplied buffer until after ECA_NORMAL is returned from ca_pend_io. If a connection is lost
589 // outstanding get requests are not automatically reissued following reconnect.
590 auto r = ca_get(DBR_DOUBLE, m_epicsChID[index], value);
591 if (r == ECA_NORMAL) r = ca_pend_io(5.0); // this is needed!
592 if (r == ECA_NORMAL) {
593 status = true;
594 return std::string(value);
595 } else {
596 CheckEpicsError(r, "Read PV (string) failed in ca_get or ca_pend_io failure", m_epicsChID[index]);
597 }
598#endif
599 return std::string(value);
600}
601
603{
604#ifdef _BELLE2_EPICS
605 if (m_useEpics) {
606 if (m_epicsNameToChID[keyname] != nullptr) {
607 return m_epicsNameToChID[keyname];
608 } else {
609 B2ERROR("Epics PV " << keyname << " not registered!");
610 }
611 }
612#endif
613 return nullptr;
614}
615
617{
618#ifdef _BELLE2_EPICS
619 if (m_useEpics) {
620 if (index >= 0 && index < (int)m_epicsChID.size()) {
621 return m_epicsChID[index];
622 } else {
623 B2ERROR("Epics PV with " << index << " not registered!");
624 }
625 }
626#endif
627 return nullptr;
628}
629
631{
632 int state = ECA_NORMAL;
633 if (!m_useEpics) return state;
634#ifdef _BELLE2_EPICS
635 if (wait > 0.) {
636 state = ca_pend_io(wait);
637 SEVCHK(state, "ca_pend_io failure");
638 }
639#endif
640 return state;
641}
642
644{
645 // this should be called in terminate function of analysis modules
646#ifdef _BELLE2_EPICS
647 if (getUseEpics()) {
648 for (auto& it : m_epicsChID) CheckEpicsError(ca_clear_channel(it), "ca_clear_channel failure", it);
649 updateEpicsPVs(5.0);
650 // Make sure we clean up both afterwards!
651 m_epicsChID.clear();
652 m_epicsNameToChID.clear();
653 }
654#endif
655}
656
657bool DQMHistAnalysisModule::requestLimitsFromEpicsPVs(std::string name, double& lowerAlarm, double& lowerWarn, double& upperWarn,
658 double& upperAlarm)
659{
660 return requestLimitsFromEpicsPVs(getEpicsPVChID(name), lowerAlarm, lowerWarn, upperWarn, upperAlarm);
661}
662
663bool DQMHistAnalysisModule::requestLimitsFromEpicsPVs(int index, double& lowerAlarm, double& lowerWarn, double& upperWarn,
664 double& upperAlarm)
665{
666 return requestLimitsFromEpicsPVs(getEpicsPVChID(index), lowerAlarm, lowerWarn, upperWarn, upperAlarm);
667}
668
669bool DQMHistAnalysisModule::requestLimitsFromEpicsPVs(chid pv, double& lowerAlarm, double& lowerWarn, double& upperWarn,
670 double& upperAlarm)
671{
672 // get warn and error limit only if pv exists
673 // overwrite only if limit is defined (not NaN)
674 // user should initialize with NaN before calling, unless
675 // some "default" values should be set otherwise
676 if (pv != nullptr) {
677 struct dbr_ctrl_double tPvData;
678 // From EPICS doc. When ca_get or ca_array_get are invoked the returned channel value can't be assumed to be stable
679 // in the application supplied buffer until after ECA_NORMAL is returned from ca_pend_io. If a connection is lost
680 // outstanding get requests are not automatically reissued following reconnect.
681 auto r = ca_get(DBR_CTRL_DOUBLE, pv, &tPvData);
682 if (r == ECA_NORMAL) r = ca_pend_io(5.0); // this is needed!
683 if (r == ECA_NORMAL) {
684 if (!std::isnan(tPvData.lower_alarm_limit)) {
685 lowerAlarm = tPvData.lower_alarm_limit;
686 }
687 if (!std::isnan(tPvData.lower_warning_limit)) {
688 lowerWarn = tPvData.lower_warning_limit;
689 }
690 if (!std::isnan(tPvData.upper_warning_limit)) {
691 upperWarn = tPvData.upper_warning_limit;
692 }
693 if (!std::isnan(tPvData.upper_alarm_limit)) {
694 upperAlarm = tPvData.upper_alarm_limit;
695 }
696 return true;
697 } else {
698 CheckEpicsError(r, "Reading PV Limits failed in ca_get or ca_pend_io failure", pv);
699 }
700 }
701 return false;
702}
703
704DQMHistAnalysisModule::EStatus DQMHistAnalysisModule::makeStatus(bool enough, bool warn_flag, bool error_flag)
705{
706 // white color is the default, if no colorize
707 if (!enough) {
708 return (c_StatusTooFew);
709 } else {
710 if (error_flag) {
711 return (c_StatusError);
712 } else if (warn_flag) {
713 return (c_StatusWarning);
714 } else {
715 return (c_StatusGood);
716 }
717 }
718
719 return (c_StatusDefault); // default, but should not be reached
720}
721
723{
724 // white color is the default, if no colorize
726 switch (stat) {
727 case c_StatusTooFew:
728 color = c_ColorTooFew; // Magenta or Gray
729 break;
730 case c_StatusDefault:
731 color = c_ColorDefault; // default no colors
732 break;
733 case c_StatusGood:
734 color = c_ColorGood; // Good
735 break;
736 case c_StatusWarning:
737 color = c_ColorWarning; // Warning
738 break;
739 case c_StatusError:
740 color = c_ColorError; // Severe
741 break;
742 default:
743 color = c_ColorDefault; // default no colors
744 break;
745 }
746 return color;
747}
748
750{
751 if (!canvas) return;
752 auto color = getStatusColor(stat);
753
754 canvas->Pad()->SetFillColor(color);
755
756 canvas->Pad()->SetFrameFillColor(10); // White (kWhite is not used since it results in transparent!)
757 canvas->Pad()->SetFrameFillStyle(1001);// White
758 canvas->Pad()->Modified();
759 canvas->Pad()->Update();
760}
761
763{
764 B2INFO("Check PV Connections");
765
766 for (auto& it : m_epicsChID) {
767 printPVStatus(it);
768 }
769 B2INFO("Check PVs done");
770}
771
772void DQMHistAnalysisModule::printPVStatus(chid pv, bool onlyError)
773{
774 if (pv == nullptr) {
775 B2WARNING("PV chid was nullptr");
776 return;
777 }
778 auto state = ca_state(pv);
779 switch (state) {
780 case cs_never_conn: /* valid chid, server not found or unavailable */
781 B2WARNING("Channel never connected " << ca_name(pv));
782 break;
783 case cs_prev_conn: /* valid chid, previously connected to server */
784 B2WARNING("Channel was connected, but now is not " << ca_name(pv));
785 break;
786 case cs_closed: /* channel deleted by user */
787 B2WARNING("Channel deleted already " << ca_name(pv));
788 break;
789 case cs_conn: /* valid chid, connected to server */
790 if (!onlyError) B2INFO("Channel connected and OK " << ca_name(pv));
791 break;
792 default:
793 B2WARNING("Undefined status for channel " << ca_name(pv));
794 break;
795 }
796}
797
798void DQMHistAnalysisModule::CheckEpicsError(int state, const std::string& message, const std::string& name)
799{
800 if (state != ECA_NORMAL) {
801 B2WARNING(message << ": " << name);
802 printPVStatus(m_epicsNameToChID[name], false);
803 }
804}
805
806void DQMHistAnalysisModule::CheckEpicsError(int state, const std::string& message, chid id = nullptr)
807{
808 if (state != ECA_NORMAL) {
809 std::string name;
810 if (id) name = ca_name(id);
811 B2WARNING(message << ": " << name);
812 printPVStatus(id, false);
813 }
814}
815
static MonObjList s_monObjList
The list of MonitoringObjects.
TCanvas * findCanvas(TString cname)
Find canvas by name.
void printPVStatus(chid pv, bool onlyError=true)
check the status of a PVs and report if disconnected or not found
bool hasDeltaPar(const std::string &dirname, const std::string &histname)
Check if Delta histogram parameters exist for histogram.
std::map< std::string, HistObject > HistList
The type of list of histograms.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
std::map< std::string, MonitoringObject > MonObjList
The type of list of MonitoringObjects.
void addDeltaPar(const std::string &dirname, const std::string &histname, HistDelta::EDeltaType t, int p, unsigned int a=1)
Add Delta histogram parameters.
static TH1 * findHistInFile(TFile *file, const std::string &histname)
Find histogram in specific TFile (e.g.
EStatusColor getStatusColor(EStatus status)
Return color for canvas state.
void colorizeCanvas(TCanvas *canvas, EStatus status)
Helper function for Canvas colorization.
static TH1 * scaleReference(int scaling, const TH1 *hist, TH1 *ref)
Using the original and reference, create scaled version.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
static MonitoringObject * findMonitoringObject(const std::string &objName)
Find MonitoringObject.
void clearlist(void)
Clear all static global lists.
double getSigma68(TH1 *h) const
Helper function to compute half of the central interval covering 68% of a distribution.
EStatusColor
Status colors of histogram/canvas (corresponding to status)
@ c_ColorWarning
Analysis result: Warning, there may be minor issues.
@ c_ColorError
Analysis result: Severe issue found.
@ c_ColorTooFew
Not enough entries/event to judge.
@ c_ColorGood
Analysis result: Good.
@ c_ColorDefault
default for non-coloring
double getEpicsPV(std::string keyname)
Read value from a EPICS PV.
static int s_eventProcessed
Number of Events processed to fill histograms.
std::map< std::string, bool > CanvasUpdatedList
The type of list of canvas updated status.
std::string getEpicsStringPV(std::string keyname, bool &status)
Read value from a EPICS PV.
int registerExternalEpicsPV(std::string pvname, std::string keyname="")
Register a PV with its name and a key name.
static HistList s_histList
The list of Histograms.
static RefList s_refList
The list of references.
static std::string s_runType
The Run type.
static void clearHistList(void)
Clears the list of histograms.
TH1 * getDelta(const std::string &fullname, int n=0, bool onlyIfUpdated=true)
Get Delta histogram.
std::vector< std::string > StringSplit(const std::string &s, const char delim)
Helper function for string token split.
static void clearRefList(void)
Clears the list of ref histograms.
std::map< std::string, HistDelta > DeltaList
The type of list of delta settings and histograms.
static TH1 * findRefHist(const std::string &histname, int scaling=0, const TH1 *hist=nullptr)
Get referencehistogram from list (no other search).
void setEpicsPV(std::string keyname, double value)
Write value to a EPICS PV.
static DeltaList s_deltaList
The list of Delta Histograms and settings.
DQMHistAnalysisModule()
Constructor / Destructor.
void checkPVStatus(void)
Check the status of all PVs and report if disconnected or not found.
std::map< std::string, RefHistObject > RefList
The type of list of references.
static bool m_epicsReadOnly
Flag if to use EPICS in ReadOnly mode (for reading limits) do not set by yourself,...
EStatus
Status flag of histogram/canvas.
@ c_StatusDefault
default for non-coloring
@ c_StatusTooFew
Not enough entries/event to judge.
@ c_StatusError
Analysis result: Severe issue found.
@ c_StatusWarning
Analysis result: Warning, there may be minor issues.
@ c_StatusGood
Analysis result: Good.
static bool addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
bool getUseEpics(void)
Getter for EPICS usage.
void ExtractRunType(std::vector< TH1 * > &hs)
Extract Run Type from histogram title, called from input module.
void CheckEpicsError(int state, const std::string &message, const std::string &name)
check the return status and check PV in case of error
TH1 * findHistInCanvas(const std::string &hname, TCanvas **canvas=nullptr)
Find histogram in corresponding canvas.
static std::string m_PVPrefix
The Prefix for EPICS PVs.
void cleanupEpicsPVs(void)
Unsubscribe from EPICS PVs on terminate.
void clearCanvases(void)
Clear content of all Canvases.
EStatus makeStatus(bool enough, bool warn_flag, bool error_flag)
Helper function to judge the status for coloring and EPICS.
static bool m_useEpics
Flag if to use EPICS do not set by yourself, use EpicsEnable module to set.
static void initHistListBeforeEvent(void)
Reset the list of histograms.
void ExtractEvent(std::vector< TH1 * > &hs)
Extract event processed from daq histogram, called from input module.
int registerEpicsPV(std::string pvname, std::string keyname="")
EPICS related Functions.
void UpdateCanvas(std::string name, bool updated=true)
Mark canvas as updated (or not)
void resetDeltaList(void)
Reset Delta.
chid getEpicsPVChID(std::string keyname)
Get EPICS PV Channel Id.
bool requestLimitsFromEpicsPVs(chid id, double &lowerAlarm, double &lowerWarn, double &upperWarn, double &upperAlarm)
Get Alarm Limits from EPICS PV.
int registerEpicsPVwithPrefix(std::string prefix, std::string pvname, std::string keyname="")
Register a PV with its name and a key name.
void setEpicsStringPV(std::string keyname, std::string value)
Write string to a EPICS PV.
int updateEpicsPVs(float timeout)
Update all EPICS PV (flush to network)
static CanvasUpdatedList s_canvasUpdatedList
The list of canvas updated status.
EDeltaType
enum definition for delta algo Disabled: nothing Entries: use nr histogram entries Underflow: use ent...
Definition: HistDelta.h:32
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
MonitoringObject is a basic object to hold data for the run-dependency monitoring Run summary TCanvas...
#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.