Belle II Software development
DQMHistAnalysisPeak.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 : DQMHistAnalysisPeak.cc
10// Description : Simple Peak Analysis (Mean/Media/Width) for simple peaked distributions with delta histogramming
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisPeak.h>
15
16using namespace std;
17using namespace Belle2;
18
19//-----------------------------------------------------------------
20// Register the Module
21//-----------------------------------------------------------------
22REG_MODULE(DQMHistAnalysisPeak);
23
24//-----------------------------------------------------------------
25// Implementation
26//-----------------------------------------------------------------
27
30{
31 // This module CAN NOT be run in parallel!
32 setDescription("Modify and analyze the peaking distributions in data quality histograms");
33
34 //Parameter definition
35 addParam("HistoName", m_histoName, "Name of Histogram (excl dir)", std::string(""));
36 addParam("HistoDirectory", m_histoDirectory, "Name of Histogram dir", std::string(""));
37 addParam("PVName", m_pvPrefix, "PV Prefix and Name", std::string(""));
38 addParam("MonitorPrefix", m_monPrefix, "Monitor Prefix", std::string(""));
39 addParam("MonitorObjectName", m_monObjectName, "Monitor Object Name", std::string(""));
40 addParam("minEntries", m_minEntries, "minimum number of new Entries for a fit", m_minEntries);
41 addParam("plot", m_plot, "plot histogram in canvas", m_plot);
42 addParam("zoom", m_zoom, "zoom on peak with +-(zoom*rms); 0 disable", m_zoom);
43 addParam("mean", m_mean, "extract and plot mean", m_mean);
44 addParam("median", m_median, "extract and plot median", m_median);
45 addParam("rms", m_rms, "extract rms", m_rms);
46 addParam("PlotLimits", m_plotLimits, "plot limits from PV", m_plotLimits);
47 B2DEBUG(20, "DQMHistAnalysisPeak: Constructor done.");
48}
49
51{
52 B2DEBUG(20, "DQMHistAnalysisPeak: initialized.");
53
54 if (m_monObjectName != "") {
56 }
57
58 // register delta
60
61 // prefer to change canvas name to monitorPrefix, but then changes on the web gui are needed :-(
62 if (m_plot) {
63 m_canvas = new TCanvas((m_histoDirectory + "/c_" + m_histoName + "_fit").data());
64
65 if (m_mean) {
66 m_lineMean = new TLine(0, 10, 0, 0);
67 m_lineMean->SetVertical(true);
68 m_lineMean->SetLineColor(8);
69 m_lineMean->SetLineWidth(3);
70 }
71 if (m_median) {
72 m_lineMedian = new TLine(0, 10, 0, 0);
73 m_lineMedian->SetVertical(true);
74 m_lineMedian->SetLineColor(9);
75 m_lineMedian->SetLineWidth(3);
76 }
77 }
78
80
81 if (m_pvPrefix != "") {
82 if (m_mean) registerEpicsPV(m_pvPrefix + "Mean", "Mean");
83 if (m_median) registerEpicsPV(m_pvPrefix + "Median", "Median");
84 if (m_rms) registerEpicsPV(m_pvPrefix + "RMS", "RMS");
85 }
86
87 double xvalues[2] = {-1000, 1000};
88 double yvalues[2] = {0, 0};
89 m_g_alarmlo = new TGraph(2, xvalues, yvalues);
90 m_g_warnlo = new TGraph(2, xvalues, yvalues);
91 m_g_good = new TGraph(2, xvalues, yvalues);
92 m_g_warnhi = new TGraph(2, xvalues, yvalues);
93 m_g_alarmhi = new TGraph(2, xvalues, yvalues);
94
95 m_g_alarmlo->SetLineColor(c_ColorError);
96 m_g_alarmlo->SetLineWidth(-902);
97 m_g_alarmlo->SetFillStyle(3002);
98 m_g_alarmlo->SetFillColor(c_ColorError);
99
100 m_g_warnlo->SetLineColor(c_ColorWarning);
101 m_g_warnlo->SetLineWidth(-602);
102 m_g_warnlo->SetFillStyle(3002);
103 m_g_warnlo->SetFillColor(c_ColorWarning);
104
105 m_g_good->SetLineColor(c_ColorGood);
106 m_g_good->SetLineWidth(-302);
107 m_g_good->SetFillStyle(3002);
108 m_g_good->SetFillColor(c_ColorGood);
109
110 m_g_warnhi->SetLineColor(c_ColorWarning);
111 m_g_warnhi->SetLineWidth(-602);
112 m_g_warnhi->SetFillStyle(3002);
113 m_g_warnhi->SetFillColor(c_ColorWarning);
114
115 m_g_alarmhi->SetLineColor(c_ColorError);
116 m_g_alarmhi->SetLineWidth(-902);
117 m_g_alarmhi->SetFillStyle(3002);
118 m_g_alarmhi->SetFillColor(c_ColorError);
119}
120
122{
123 if (m_lineMean) delete m_lineMean;
124 if (m_lineMedian) delete m_lineMedian;
125 if (m_canvas) delete m_canvas;
126}
127
129{
130 B2DEBUG(20, "DQMHistAnalysisPeak: beginRun called.");
131 if (m_canvas) m_canvas->Clear();
132
138
139 m_g_good->SetPoint(0, -9e9, 0);
140 m_g_good->SetPoint(1, 9e9, 0);
141
142 m_lowarnlevel = NAN, m_hiwarnlevel = NAN, m_loerrorlevel = NAN, m_hierrorlevel = NAN;
144 m_g_alarmlo->SetPoint(0, -9e9, 0);
146 if (m_valid_alarmlo) {
147 m_g_alarmlo->SetPoint(1, m_loerrorlevel, 0);
148 m_g_warnlo->SetPoint(0, m_loerrorlevel, 0);
149 m_g_good->SetPoint(0, m_loerrorlevel, 0);
150 } else {
151 m_g_warnlo->SetPoint(0, -9e9, 0);
152 m_loerrorlevel = -9e9;
153 }
154
156 m_g_warnlo->SetPoint(1, m_lowarnlevel, 0);
157 if (m_valid_warnlo) m_g_good->SetPoint(0, m_lowarnlevel, 0);
158
159 m_g_alarmhi->SetPoint(1, 9e9, 0);
161 if (m_valid_alarmhi) {
162 m_g_warnhi->SetPoint(1, m_hierrorlevel, 0);
163 m_g_alarmhi->SetPoint(0, m_hierrorlevel, 0);
164 m_g_good->SetPoint(1, m_hierrorlevel, 0);
165 } else {
166 m_g_warnhi->SetPoint(1, 9e9, 0);
167 m_hierrorlevel = 9e9;
168 }
169
171 m_g_warnhi->SetPoint(0, m_hiwarnlevel, 0);
172 if (m_valid_warnhi) m_g_good->SetPoint(1, m_hiwarnlevel, 0);
173 } else {
174 m_valid_alarmlo = false;
175 m_valid_warnlo = false;
176 m_valid_good = false;
177 m_valid_warnhi = false;
178 m_valid_alarmhi = false;
179 }
180}
181
183{
185 // do not care about initial filling handling. we won't show or update unless we reach the min req entries
186 UpdateCanvas(m_canvas, delta != nullptr);
187 if (delta != nullptr) {
188
189 // we modify the current delta histogram, that is bad habit
190 // but as long as no one else uses it, it may be o.k.
191 // for more severe changes, maybe better work on a clone?)
192 delta->ResetStats(); // kills the Mean from filling, now only use bin values excl over/underflow
193 double mean = delta->GetMean();// must be double bc of EPICS below
194 double rms = delta->GetRMS();// must be double bc of EPICS below
195 double q = 0.5; // array size one for quantiles
196 double median = 0; // array of size 1 for result = median
197 delta->ComputeIntegral(); // precaution
198 delta->GetQuantiles(1, &median, &q);
199 double y1 = delta->GetMaximum();
200 double y2 = delta->GetMinimum();
201 B2DEBUG(20, "Fit " << mean << "," << rms << "," << y1 << "," << y2);
202
203 // Now plot
204 if (m_canvas) {
205 m_canvas->Clear();
206 m_canvas->cd();// necessary!
207 delta->Draw("hist");
208
209 if (m_zoom > 0) delta->GetXaxis()->SetRangeUser(mean - m_zoom * rms, mean + m_zoom * rms);
210 if (m_lineMean) {
211 m_lineMean->SetY1(y1 + (y1 - y2) * 0.05);
212 m_lineMean->SetX1(mean);
213 m_lineMean->SetX2(mean);
214 m_lineMean->Draw();
215 }
216 if (m_lineMedian) {
217 m_lineMedian->SetY1(y1 + (y1 - y2) * 0.05);
218 m_lineMedian->SetX1(median);
219 m_lineMedian->SetX2(median);
220 m_lineMedian->Draw();
221 }
222 m_canvas->Paint(); // needed, otherwise Graph overlay doesn't work
223 if (m_g_good and m_valid_good) m_g_good->Draw("RY");
224 if (m_g_warnlo and m_valid_warnlo) m_g_warnlo->Draw("RY");
225 if (m_g_warnhi and m_valid_warnhi) m_g_warnhi->Draw("RY");
226 if (m_g_alarmlo and m_valid_alarmlo) m_g_alarmlo->Draw("RY");
227 if (m_g_alarmhi and m_valid_alarmhi) m_g_alarmhi->Draw("RY");
228
229// m_canvas->Print((m_histoName + ".pdf").c_str());
230 m_canvas->Modified();
231 m_canvas->Update();
232 }
233 if (m_monObj and m_monPrefix != "") {
234 if (m_mean) m_monObj->setVariable(m_monPrefix + "_mean", mean);
235 if (m_median) m_monObj->setVariable(m_monPrefix + "_median", median);
236 if (m_rms) m_monObj->setVariable(m_monPrefix + "_width", rms);
237 }
238
239 B2DEBUG(20, "Now update Epics PVs");
240 if (m_pvPrefix != "") {
241 if (m_mean) setEpicsPV("Mean", mean);
242 if (m_median) setEpicsPV("Median", median);
243 if (m_rms) setEpicsPV("RMS", rms);
244 }
245 }
246}
The base class for the histogram analysis module.
bool hasDeltaPar(const std::string &dirname, const std::string &histname)
Check if Delta histogram parameters exist for histogram.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
void addDeltaPar(const std::string &dirname, const std::string &histname, HistDelta::EDeltaType t, int p, unsigned int a=1)
Add Delta histogram parameters.
@ c_ColorWarning
Analysis result: Warning, there may be minor issues.
@ c_ColorError
Analysis result: Severe issue found.
@ c_ColorGood
Analysis result: Good.
TH1 * getDelta(const std::string &fullname, int n=0, bool onlyIfUpdated=true)
Get Delta histogram.
void setEpicsPV(std::string keyname, double value)
Write value to a EPICS PV.
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)
bool requestLimitsFromEpicsPVs(chid id, double &lowerAlarm, double &lowerWarn, double &upperWarn, double &upperAlarm)
Get Alarm Limits from EPICS PV.
TLine * m_lineMedian
The line for the median result.
bool m_valid_alarmlo
flag for valid limit/graph
bool m_valid_warnlo
flag for valid limit/graph
int m_minEntries
Update entry interval.
void initialize() override final
Initializer.
TGraph * m_g_warnlo
Graph for Limit plot.
std::string m_histoName
The name of the histogram.
bool m_median
Flag: extract median.
bool m_valid_warnhi
flag for valid limit/graph
TLine * m_lineMean
The line for the mean result.
std::string m_pvPrefix
The prefix of PV.
bool m_plotLimits
Flag for plotting limits from PV.
TCanvas * m_canvas
The drawing canvas.
MonitoringObject * m_monObj
Monitoring Object.
TGraph * m_g_alarmhi
Graph for Limit plot.
TGraph * m_g_alarmlo
Graph for Limit plot.
void terminate() override final
Terminate.
bool m_plot
Flag: plot into canvas.
std::string m_monPrefix
The prefix for MonitoringObj.
void event() override final
This method is called for each event.
bool m_valid_alarmhi
flag for valid limit/graph
TGraph * m_g_warnhi
Graph for Limit plot.
std::string m_monObjectName
The Name for MonitoringObj.
float m_zoom
Zoom on peak with range +- zoom*rms ; 0 disable.
void beginRun() override final
Called when entering a new run.
bool m_valid_good
flag for valid limit/graph
std::string m_histoDirectory
The name of the histogram dir.
TGraph * m_g_good
Graph for Limit plot.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setVariable(const std::string &var, float val, float upErr=-1., float dwErr=-1)
set value to float variable (new variable is made if not yet existing)
void addCanvas(TCanvas *canv)
Add Canvas to monitoring object.
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:560
#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.