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
40{
41 B2DEBUG(20, "DQMHistAnalysisEpicsExample: initialized.");
42
43 TString a;
44 a = m_histoname;
45 a.ReplaceAll("/", "_");
46 m_c1 = new TCanvas("c_" + a);
47 m_f1 = new TF1("f_" + a, TString(m_function), -30, 300);
48 m_f1->SetParameter(0, 1000);
49 m_f1->SetParameter(1, 0);
50 m_f1->SetParameter(2, 10);
51 m_f1->SetLineColor(4);
52 m_f1->SetNpx(512);
53 m_f1->SetNumberFitPoints(512);
54
55 m_line = new TLine(0, 10, 0, 0);
56 m_line->SetVertical(true);
57 m_line->SetLineColor(8);
58 m_line->SetLineWidth(3);
59
60 m_line_lo = new TLine(0, 10, 0, 0);
61 m_line_lo->SetVertical(true);
62 m_line_lo->SetLineColor(2);
63 m_line_lo->SetLineWidth(3);
64
65 m_line_hi = new TLine(0, 10, 0, 0);
66 m_line_hi->SetVertical(true);
67 m_line_hi->SetLineColor(2);
68 m_line_hi->SetLineWidth(3);
69
70 m_line_lo->SetX1(5);// get from epics
71 m_line_lo->SetX2(5);
72
73 m_line_hi->SetX1(50);// get from epics
74 m_line_hi->SetX2(50);
75
76 // need the function to get parameter names
77 if (m_parameters > 0) {
78 if (m_parameters > 10) m_parameters = 10; // hard limit
79 for (auto i = 0; i < m_parameters; i++) {
80 std::string aa;
81 aa = m_f1->GetParName(i);
82 if (aa == "") aa = string("par") + string(TString::Itoa(i, 10).Data());
83 mypv.push_back(aa);
85 // Read LO and HI limits from EPICS if needed, like
86 // requestLimitsFromEpicsPVs("mean", m_meanLowerAlarm, m_meanLowerWarn, m_meanUpperWarn, m_meanUpperAlarm);
87 }
88 } else {
89 m_parameters = 0;
90 }
91}
92
93
95{
96 //m_serv->SetTimer(100, kFALSE);
97 B2DEBUG(20, "DQMHistAnalysisEpicsExample: beginRun called.");
98 m_c1->Clear();
99
100 TH1* hh1;
101 hh1 = findHist(m_histoname);
102
103 if (hh1 == NULL) {
104 B2DEBUG(20, "Histo " << m_histoname << " not in memfile");
105 TDirectory* d = gROOT;
106 TString myl = m_histoname;
107 TString tok;
108 Ssiz_t from = 0;
109 while (myl.Tokenize(tok, from, "/")) {
110 TString dummy;
111 Ssiz_t f;
112 f = from;
113 if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
114 auto e = d->GetDirectory(tok);
115 if (e) {
116 B2DEBUG(20, "Cd Dir " << tok);
117 d = e;
118 }
119 d->cd();
120 } else {
121 break;
122 }
123 }
124 TObject* obj = d->FindObject(tok);
125 if (obj != NULL) {
126 if (obj->IsA()->InheritsFrom("TH1")) {
127 B2DEBUG(20, "Histo " << m_histoname << " found in mem");
128 hh1 = (TH1*)obj;
129 }
130 } else {
131 B2DEBUG(20, "Histo " << m_histoname << " NOT found in mem");
132 }
133 }
134
135 if (hh1 != NULL) {
136 m_c1->cd();
137 hh1->Draw();
138 m_line->Draw();
139 m_line_lo->Draw();
140 m_line_hi->Draw();
141 } else {
142 B2DEBUG(20, "Histo " << m_histoname << " not found");
143 }
144}
145
147{
148 TH1* hh1;
149 bool flag = false;
150
151 hh1 = findHist(m_histoname);
152 if (hh1 == NULL) {
153 B2DEBUG(20, "Histo " << m_histoname << " not in memfile");
154 TDirectory* d = gROOT;
155 TString myl = m_histoname;
156 TString tok;
157 Ssiz_t from = 0;
158 while (myl.Tokenize(tok, from, "/")) {
159 TString dummy;
160 Ssiz_t f;
161 f = from;
162 if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
163 auto e = d->GetDirectory(tok);
164 if (e) {
165 B2DEBUG(20, "Cd Dir " << tok);
166 d = e;
167 }
168 d->cd();
169 } else {
170 break;
171 }
172 }
173 TObject* obj = d->FindObject(tok);
174 if (obj != NULL) {
175 if (obj->IsA()->InheritsFrom("TH1")) {
176 B2DEBUG(20, "Histo " << m_histoname << " found in mem");
177 hh1 = (TH1*)obj;
178 flag = true;
179 }
180 } else {
181 B2DEBUG(20, "Histo " << m_histoname << " NOT found in mem");
182 }
183 }
184 if (hh1 != NULL) {
185 m_c1->cd();// necessary!
186 hh1->Fit(m_f1, "");
187 double y1 = hh1->GetMaximum();
188 double y2 = hh1->GetMinimum();
189 m_line->SetY1(y1 + (y1 - y2) * 0.05);
190 m_line_lo->SetY1(y1 + (y1 - y2) * 0.05);
191 m_line_hi->SetY1(y1 + (y1 - y2) * 0.05);
192// m_line->SetY2(y2-(y1-y2)*0.05);
193// m_line_lo->SetY2(y2-(y1-y2)*0.05);
194// m_line_hi->SetY2(y2-(y1-y2)*0.05);
195 double x = m_f1->GetParameter(1);
196 m_line->SetX1(x);
197 m_line->SetX2(x);
198 if (!flag) {
199 // dont add another line...
200 m_line->Draw();
201 m_line_lo->Draw();
202 m_line_hi->Draw();
203 }
204 m_c1->Modified();
205 m_c1->Update();
206 } else {
207 B2DEBUG(20, "Histo " << m_histoname << " not found");
208 }
209
210 if (m_parameters > 0) {
211 for (auto i = 0; i < m_parameters; i++) {
212 double data;
213 data = m_f1->GetParameter(i);
214 setEpicsPV(mypv[i], data);
215 }
216 }
217}
218
220{
221 B2DEBUG(20, "DQMHistAnalysisEpicsExample : endRun called");
222}
223
224
226{
227 B2DEBUG(20, "DQMHistAnalysisEpicsExample: terminate called");
228}
229
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.
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
DQMHistAnalysisModule()
Constructor / Destructor.
void setEpicsPV(const std::string &keyname, double value)
Write value to a EPICS PV.
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.