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