Belle II Software development
DQMHistAnalysisEpicsExample.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#include <dqm/analysis/modules/DQMHistAnalysisEpicsExample.h>
10#include <TROOT.h>
11#include <TClass.h>
12
13using namespace std;
14using namespace Belle2;
15
16//-----------------------------------------------------------------
17// Register the Module
18//-----------------------------------------------------------------
19REG_MODULE(DQMHistAnalysisEpicsExample);
20
21//-----------------------------------------------------------------
22// Implementation
23//-----------------------------------------------------------------
24
27{
28 // This module CAN NOT be run in parallel!
29 setDescription("Example module for EPICS");
30
31 //Parameter definition
32 addParam("HistoName", m_histoname, "Name of Histogram (incl dir)", std::string(""));
33 addParam("Function", m_function, "Fit function definition", std::string("gaus"));
34 addParam("Parameters", m_parameters, "Fit function parameters for EPICS", 3);
35 addParam("PVName", m_pvPrefix, "PV Prefix", std::string("DQM:TEST:hist:"));
36 B2DEBUG(20, "DQMHistAnalysisEpicsExample: Constructor done.");
37}
38
39
41{
42}
43
45{
46 B2DEBUG(20, "DQMHistAnalysisEpicsExample: initialized.");
47
48 TString a;
49 a = m_histoname;
50 a.ReplaceAll("/", "_");
51 m_c1 = new TCanvas("c_" + a);
52 m_f1 = new TF1("f_" + a, TString(m_function), -30, 300);
53 m_f1->SetParameter(0, 1000);
54 m_f1->SetParameter(1, 0);
55 m_f1->SetParameter(2, 10);
56 m_f1->SetLineColor(4);
57 m_f1->SetNpx(512);
58 m_f1->SetNumberFitPoints(512);
59
60 m_line = new TLine(0, 10, 0, 0);
61 m_line->SetVertical(true);
62 m_line->SetLineColor(8);
63 m_line->SetLineWidth(3);
64
65 m_line_lo = new TLine(0, 10, 0, 0);
66 m_line_lo->SetVertical(true);
67 m_line_lo->SetLineColor(2);
68 m_line_lo->SetLineWidth(3);
69
70 m_line_hi = new TLine(0, 10, 0, 0);
71 m_line_hi->SetVertical(true);
72 m_line_hi->SetLineColor(2);
73 m_line_hi->SetLineWidth(3);
74
75 m_line_lo->SetX1(5);// get from epics
76 m_line_lo->SetX2(5);
77
78 m_line_hi->SetX1(50);// get from epics
79 m_line_hi->SetX2(50);
80
81 // need the function to get parameter names
82 if (m_parameters > 0) {
83 if (m_parameters > 10) m_parameters = 10; // hard limit
84 for (auto i = 0; i < m_parameters; i++) {
85 std::string aa;
86 aa = m_f1->GetParName(i);
87 if (aa == "") aa = string("par") + string(TString::Itoa(i, 10).Data());
88 mypv.push_back(aa);
90 // Read LO and HI limits from EPICS if needed, like
91 // requestLimitsFromEpicsPVs("mean", m_meanLowerAlarm, m_meanLowerWarn, m_meanUpperWarn, m_meanUpperAlarm);
92 }
93 } else {
94 m_parameters = 0;
95 }
96}
97
98
100{
101 //m_serv->SetTimer(100, kFALSE);
102 B2DEBUG(20, "DQMHistAnalysisEpicsExample: beginRun called.");
103 m_c1->Clear();
104
105 TH1* hh1;
106 hh1 = findHist(m_histoname);
107
108 if (hh1 == NULL) {
109 B2DEBUG(20, "Histo " << m_histoname << " not in memfile");
110 TDirectory* d = gROOT;
111 TString myl = m_histoname;
112 TString tok;
113 Ssiz_t from = 0;
114 while (myl.Tokenize(tok, from, "/")) {
115 TString dummy;
116 Ssiz_t f;
117 f = from;
118 if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
119 auto e = d->GetDirectory(tok);
120 if (e) {
121 B2DEBUG(20, "Cd Dir " << tok);
122 d = e;
123 }
124 d->cd();
125 } else {
126 break;
127 }
128 }
129 TObject* obj = d->FindObject(tok);
130 if (obj != NULL) {
131 if (obj->IsA()->InheritsFrom("TH1")) {
132 B2DEBUG(20, "Histo " << m_histoname << " found in mem");
133 hh1 = (TH1*)obj;
134 }
135 } else {
136 B2DEBUG(20, "Histo " << m_histoname << " NOT found in mem");
137 }
138 }
139
140 if (hh1 != NULL) {
141 m_c1->cd();
142 hh1->Draw();
143 m_line->Draw();
144 m_line_lo->Draw();
145 m_line_hi->Draw();
146 } else {
147 B2DEBUG(20, "Histo " << m_histoname << " not found");
148 }
149}
150
152{
153 TH1* hh1;
154 bool flag = false;
155
156 hh1 = findHist(m_histoname);
157 if (hh1 == NULL) {
158 B2DEBUG(20, "Histo " << m_histoname << " not in memfile");
159 TDirectory* d = gROOT;
160 TString myl = m_histoname;
161 TString tok;
162 Ssiz_t from = 0;
163 while (myl.Tokenize(tok, from, "/")) {
164 TString dummy;
165 Ssiz_t f;
166 f = from;
167 if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
168 auto e = d->GetDirectory(tok);
169 if (e) {
170 B2DEBUG(20, "Cd Dir " << tok);
171 d = e;
172 }
173 d->cd();
174 } else {
175 break;
176 }
177 }
178 TObject* obj = d->FindObject(tok);
179 if (obj != NULL) {
180 if (obj->IsA()->InheritsFrom("TH1")) {
181 B2DEBUG(20, "Histo " << m_histoname << " found in mem");
182 hh1 = (TH1*)obj;
183 flag = true;
184 }
185 } else {
186 B2DEBUG(20, "Histo " << m_histoname << " NOT found in mem");
187 }
188 }
189 if (hh1 != NULL) {
190 m_c1->cd();// necessary!
191 hh1->Fit(m_f1, "");
192 double y1 = hh1->GetMaximum();
193 double y2 = hh1->GetMinimum();
194 m_line->SetY1(y1 + (y1 - y2) * 0.05);
195 m_line_lo->SetY1(y1 + (y1 - y2) * 0.05);
196 m_line_hi->SetY1(y1 + (y1 - y2) * 0.05);
197// m_line->SetY2(y2-(y1-y2)*0.05);
198// m_line_lo->SetY2(y2-(y1-y2)*0.05);
199// m_line_hi->SetY2(y2-(y1-y2)*0.05);
200 double x = m_f1->GetParameter(1);
201 m_line->SetX1(x);
202 m_line->SetX2(x);
203 if (!flag) {
204 // dont add another line...
205 m_line->Draw();
206 m_line_lo->Draw();
207 m_line_hi->Draw();
208 }
209 m_c1->Modified();
210 m_c1->Update();
211 } else {
212 B2DEBUG(20, "Histo " << m_histoname << " not found");
213 }
214
215 if (m_parameters > 0) {
216 for (auto i = 0; i < m_parameters; i++) {
217 double data;
218 data = m_f1->GetParameter(i);
219 setEpicsPV(mypv[i], data);
220 }
221 }
222}
223
225{
226 B2DEBUG(20, "DQMHistAnalysisEpicsExample : endRun called");
227}
228
229
231{
232 B2DEBUG(20, "DQMHistAnalysisEpicsExample: terminate called");
233}
234
TLine * m_line_hi
The line for the higher bound.
void terminate() override final
This method is called at the end of the event processing.
Int_t m_parameters
The fit function parameters for EPICS.
void event() override final
This method is called for each event.
TLine * m_line_lo
The line for the lower bound.
void endRun() override final
This method is called if the current run ends.
std::string m_histoname
The name of the histogram.
void beginRun() override final
Called when entering a new run.
TLine * m_line
The line for the fitting result.
std::vector< std::string > mypv
list of pv names
std::string m_function
The definition of the fit function.
The base class for the histogram analysis module.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
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 setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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:559
#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.