13#include <dqm/core/DQMHistAnalysis.h>
14#include <boost/algorithm/string.hpp>
36std::vector <chid> DQMHistAnalysisModule::m_epicsChID;
62 if (dirname.size() > 0) {
63 fullname = dirname +
"/" + histname;
72 B2DEBUG(20,
"Found Delta" << fullname);
83 string histname = hist->GetName();
84 std::string name = dirname +
"/" + histname;
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);
91 n.setRefCopy(
nullptr);
99 if (dirname.size() > 0) {
100 fullname = dirname +
"/" + histname;
109 std::string fullname;
110 if (dirname.size() > 0) {
111 fullname = dirname +
"/" + histname;
120 std::string fullname;
121 if (dirname.size() > 0) {
122 fullname = dirname +
"/" + histname;
126 return getDelta(fullname, n, onlyIfUpdated);
133 return it->second.getDelta(n, onlyIfUpdated);
135 B2WARNING(
"Delta hist " << fullname <<
" not found");
142 obj->SetName(objName.c_str());
148 TIter nextkey(gROOT->GetListOfCanvases());
151 while ((obj =
dynamic_cast<TObject*
>(nextkey()))) {
152 if (obj->IsA()->InheritsFrom(
"TCanvas")) {
153 if (obj->GetName() == canvas_name)
154 return dynamic_cast<TCanvas*
>(obj);
163 if (was_updated && !
s_histList[histname].isUpdated())
return nullptr;
167 B2ERROR(
"Histogram " << histname <<
" in histogram list but nullptr.");
170 B2INFO(
"Histogram " << histname <<
" not in list.");
176 if (dirname.size() > 0) {
177 return findHist(dirname +
"/" + histname, updated);
194 if (hist->Integral() != 0 and ref->Integral() != 0) {
195 ref->Scale(hist->Integral() / ref->Integral());
200 if (hist->GetMaximum() != 0 and ref->GetMaximum() != 0) {
201 ref->Scale(hist->GetMaximum() / ref->GetMaximum());
222 if (dirname.size() > 0) {
223 return findRefHist(dirname +
"/" + histname, scaling, hist);
230 TCanvas* cnv =
nullptr;
232 if (cobj) cnv = *cobj;
234 if (cnv ==
nullptr) {
238 B2ERROR(
"findHistInCanvas: histoname not valid (missing dir?), should be 'dirname/histname': " << histo_name);
241 auto dirname = s.at(0);
242 auto hname = s.at(1);
243 std::string canvas_name = dirname +
"/c_" + hname;
246 if (cnv && cobj) *cobj = cnv;
250 if (cnv !=
nullptr) {
251 TIter nextkey(cnv->GetListOfPrimitives());
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);
267 if (file && file->IsOpen()) {
268 auto obj = file->Get(histname.data());
269 if (obj !=
nullptr) {
271 if (obj->IsA()->InheritsFrom(
"TH1")) {
272 B2DEBUG(20,
"Histogram " << histname <<
" found in file");
273 return dynamic_cast<TH1*
>(obj);
275 B2INFO(
"Found Object " << histname <<
" in file is not a histogram");
278 B2INFO(
"Histogram " << histname <<
" not found in file");
289 B2INFO(
"MonitoringObject " << objName <<
" not in memfile.");
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;
304 std::vector <std::string> out;
305 boost::split(out, in, [delim](
char c) {
return c == delim;});
311 TIter nextckey(gROOT->GetListOfCanvases());
312 TObject* cobj =
nullptr;
314 while ((cobj =
dynamic_cast<TObject*
>(nextckey()))) {
315 if (cobj->IsA()->InheritsFrom(
"TCanvas")) {
316 TCanvas* cnv =
dynamic_cast<TCanvas*
>(cobj);
327 it.second.resetBeforeEvent();
331 it.second.setNotUpdated();
367 for (
size_t i = 0; i < hs.size(); i++) {
368 if (hs[i]->GetName() == std::string(
"DQMInfo/rtype")) {
373 B2ERROR(
"ExtractRunType: Histogram \"DQMInfo/rtype\" missing");
379 for (
size_t i = 0; i < hs.size(); i++) {
380 if (hs[i]->GetName() == std::string(
"DAQ/Nevent")) {
385 B2ERROR(
"ExtractEvent: Histogram \"DAQ/Nevent\" missing");
402 if (m_epicsNameToChID[pvname] !=
nullptr) {
403 B2ERROR(
"Epics PV " << pvname <<
" already registered!");
406 if (keyname !=
"" && m_epicsNameToChID[keyname] !=
nullptr) {
407 B2ERROR(
"Epics PV with key " << keyname <<
" already registered!");
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");
415 CheckEpicsError(ca_create_channel((prefix + pvname).data(), NULL, NULL, 10, ptr),
"ca_create_channel failure", pvname);
417 m_epicsNameToChID[pvname] = *ptr;
418 if (keyname !=
"") m_epicsNameToChID[keyname] = *ptr;
419 return m_epicsChID.size() - 1;
429 if (m_epicsNameToChID[keyname] ==
nullptr) {
430 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
433 CheckEpicsError(ca_put(DBR_DOUBLE, m_epicsNameToChID[keyname], (
void*)&value),
"ca_set failure", keyname);
441 if (m_epicsNameToChID[keyname] ==
nullptr) {
442 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
445 CheckEpicsError(ca_put(DBR_SHORT, m_epicsNameToChID[keyname], (
void*)&value),
"ca_set failure", keyname);
453 if (m_epicsNameToChID[keyname] ==
nullptr) {
454 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
457 if (value.length() > 40) {
458 B2ERROR(
"Epics string PV " << keyname <<
" too long (>40 characters)!");
462 strcpy(text, value.c_str());
463 CheckEpicsError(ca_put(DBR_STRING, m_epicsNameToChID[keyname], text),
"ca_set failure", keyname);
471 if (index < 0 || index >= (
int)m_epicsChID.size()) {
472 B2ERROR(
"Epics PV with " << index <<
" not registered!");
475 CheckEpicsError(ca_put(DBR_DOUBLE, m_epicsChID[index], (
void*)&value),
"ca_set failure", m_epicsChID[index]);
483 if (index < 0 || index >= (
int)m_epicsChID.size()) {
484 B2ERROR(
"Epics PV with " << index <<
" not registered!");
487 CheckEpicsError(ca_put(DBR_SHORT, m_epicsChID[index], (
void*)&value),
"ca_set failure", m_epicsChID[index]);
495 if (index < 0 || index >= (
int)m_epicsChID.size()) {
496 B2ERROR(
"Epics PV with " << index <<
" not registered!");
500 strncpy(text, value.c_str(), 40);
502 CheckEpicsError(ca_put(DBR_STRING, m_epicsChID[index], text),
"ca_set failure", m_epicsChID[index]);
511 if (m_epicsNameToChID[keyname] ==
nullptr) {
512 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
518 auto r = ca_get(DBR_DOUBLE, m_epicsNameToChID[keyname], (
void*)&value);
519 if (r == ECA_NORMAL) r = ca_pend_io(5.0);
520 if (r == ECA_NORMAL) {
523 CheckEpicsError(r,
"Read PV failed in ca_get or ca_pend_io failure", keyname);
534 if (index < 0 || index >= (
int)m_epicsChID.size()) {
535 B2ERROR(
"Epics PV with " << index <<
" not registered!");
541 auto r = ca_get(DBR_DOUBLE, m_epicsChID[index], (
void*)&value);
542 if (r == ECA_NORMAL) r = ca_pend_io(5.0);
543 if (r == ECA_NORMAL) {
546 CheckEpicsError(r,
"Read PV failed in ca_get or ca_pend_io failure", m_epicsChID[index]);
558 if (m_epicsNameToChID[keyname] ==
nullptr) {
559 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
560 return std::string(value);
565 auto r = ca_get(DBR_STRING, m_epicsNameToChID[keyname], value);
566 if (r == ECA_NORMAL) r = ca_pend_io(5.0);
567 if (r == ECA_NORMAL) {
569 return std::string(value);
571 CheckEpicsError(r,
"Read PV (string) failed in ca_get or ca_pend_io failure", keyname);
574 return std::string(value);
583 if (index < 0 || index >= (
int)m_epicsChID.size()) {
584 B2ERROR(
"Epics PV with " << index <<
" not registered!");
585 return std::string(value);
590 auto r = ca_get(DBR_DOUBLE, m_epicsChID[index], value);
591 if (r == ECA_NORMAL) r = ca_pend_io(5.0);
592 if (r == ECA_NORMAL) {
594 return std::string(value);
596 CheckEpicsError(r,
"Read PV (string) failed in ca_get or ca_pend_io failure", m_epicsChID[index]);
599 return std::string(value);
606 if (m_epicsNameToChID[keyname] !=
nullptr) {
607 return m_epicsNameToChID[keyname];
609 B2ERROR(
"Epics PV " << keyname <<
" not registered!");
620 if (index >= 0 && index < (
int)m_epicsChID.size()) {
621 return m_epicsChID[index];
623 B2ERROR(
"Epics PV with " << index <<
" not registered!");
632 int state = ECA_NORMAL;
636 state = ca_pend_io(wait);
637 SEVCHK(state,
"ca_pend_io failure");
648 for (
auto& it : m_epicsChID)
CheckEpicsError(ca_clear_channel(it),
"ca_clear_channel failure", it);
652 m_epicsNameToChID.clear();
677 struct dbr_ctrl_double tPvData;
681 auto r = ca_get(DBR_CTRL_DOUBLE, pv, &tPvData);
682 if (r == ECA_NORMAL) r = ca_pend_io(5.0);
683 if (r == ECA_NORMAL) {
684 if (!std::isnan(tPvData.lower_alarm_limit)) {
685 lowerAlarm = tPvData.lower_alarm_limit;
687 if (!std::isnan(tPvData.lower_warning_limit)) {
688 lowerWarn = tPvData.lower_warning_limit;
690 if (!std::isnan(tPvData.upper_warning_limit)) {
691 upperWarn = tPvData.upper_warning_limit;
693 if (!std::isnan(tPvData.upper_alarm_limit)) {
694 upperAlarm = tPvData.upper_alarm_limit;
698 CheckEpicsError(r,
"Reading PV Limits failed in ca_get or ca_pend_io failure", pv);
712 }
else if (warn_flag) {
754 canvas->Pad()->SetFillColor(color);
756 canvas->Pad()->SetFrameFillColor(10);
757 canvas->Pad()->SetFrameFillStyle(1001);
758 canvas->Pad()->Modified();
759 canvas->Pad()->Update();
764 B2INFO(
"Check PV Connections");
766 for (
auto& it : m_epicsChID) {
769 B2INFO(
"Check PVs done");
775 B2WARNING(
"PV chid was nullptr");
778 auto state = ca_state(pv);
781 B2WARNING(
"Channel never connected " << ca_name(pv));
784 B2WARNING(
"Channel was connected, but now is not " << ca_name(pv));
787 B2WARNING(
"Channel deleted already " << ca_name(pv));
790 if (!onlyError) B2INFO(
"Channel connected and OK " << ca_name(pv));
793 B2WARNING(
"Undefined status for channel " << ca_name(pv));
800 if (state != ECA_NORMAL) {
801 B2WARNING(message <<
": " << name);
808 if (state != ECA_NORMAL) {
810 if (
id) name = ca_name(
id);
811 B2WARNING(message <<
": " << name);
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
static TH1 * findRefHist(const std::string &histname, ERefScaling scaling=ERefScaling::c_RefScaleNone, const TH1 *hist=nullptr)
Get referencehistogram from list (no other search).
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.
static TH1 * scaleReference(ERefScaling scaling, const TH1 *hist, TH1 *ref)
Using the original and reference, create scaled version.
void addDeltaPar(const std::string &dirname, const std::string &histname, HistDelta::EDeltaType t, int p, unsigned int a=1)
Add Delta histogram parameters.
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.
EStatusColor getStatusColor(EStatus status)
Return color for canvas state.
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).
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.
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.
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.
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.
ERefScaling
Reference plot scaling type.
@ c_RefScaleEntries
to number of entries (integral)
@ c_RefScaleMax
to maximum (bin entry)
@ c_RefScaleNone
no scaling
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 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...
void setDescription(const std::string &description)
Sets the description of the module.
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.
Abstract base class for different kinds of events.