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
11using namespace Belle2;
12
13//-----------------------------------------------------------------
14// Register the Module
15//-----------------------------------------------------------------
16REG_MODULE(DQMHistAnalysisEpicsExample);
17
18//-----------------------------------------------------------------
19// Implementation
20//-----------------------------------------------------------------
21
24{
25 setDescription("DQM analysis example module for EPICS");
26 // This module CAN NOT be run in parallel!
27
28 //Parameter definition
29 addParam("histogramDirectoryName", m_histogramDirectoryName, "Name of Histogram dir", std::string("test"));
30 addParam("histogramName", m_histogramName, "Name of Histogram", std::string("testHist"));
31 addParam("Function", m_function, "Fit function definition", std::string("gaus"));
32 addParam("Parameters", m_parameters, "Number of fit function parameters for EPICS", 3);
33 addParam("PVPrefix", m_pvPrefix, "PV Prefix", std::string("DQM:TEST:"));
34 B2DEBUG(20, "DQMHistAnalysisEpicsExample: Constructor done.");
35}
36
38{
39 B2DEBUG(20, "DQMHistAnalysisEpicsExample: initialized.");
40
41 m_c1 = new TCanvas(TString(m_histogramDirectoryName + "_c_" + m_histogramName));
42 m_f1 = new TF1(TString(m_histogramDirectoryName + "_f_" + m_histogramName), TString(m_function), -30, 300);
43 m_f1->SetParameter(0, 1000);
44 m_f1->SetParameter(1, 0);
45 m_f1->SetParameter(2, 10);
46 m_f1->SetLineColor(4);
47 m_f1->SetNpx(512);
48 m_f1->SetNumberFitPoints(512);
49
50 m_line = new TLine(0, 10, 0, 0);
51 m_line->SetVertical(true);
52 m_line->SetLineColor(8);
53 m_line->SetLineWidth(3);
54
55 m_line_lo = new TLine(0, 10, 0, 0);
56 m_line_lo->SetVertical(true);
57 m_line_lo->SetLineColor(2);
58 m_line_lo->SetLineWidth(3);
59
60 m_line_hi = new TLine(0, 10, 0, 0);
61 m_line_hi->SetVertical(true);
62 m_line_hi->SetLineColor(2);
63 m_line_hi->SetLineWidth(3);
64
65 m_line_lo->SetX1(5);// get from epics
66 m_line_lo->SetX2(5);
67
68 m_line_hi->SetX1(50);// get from epics
69 m_line_hi->SetX2(50);
70
71 // need the function to get parameter names
72 if (m_parameters > 0) {
73 if (m_parameters > 10) m_parameters = 10; // hard limit
74 for (auto i = 0; i < m_parameters; i++) {
75 std::string aa;
76 aa = m_f1->GetParName(i);
77 if (aa == "") aa = std::string("par") + std::string(TString::Itoa(i, 10).Data());
78 mypv.push_back(aa);
80 // Read LO and HI limits from EPICS if needed, like
81 // requestLimitsFromEpicsPVs("mean", m_meanLowerAlarm, m_meanLowerWarn, m_meanUpperWarn, m_meanUpperAlarm);
82 }
83 } else {
84 m_parameters = 0;
85 }
86}
87
88
90{
91 //m_serv->SetTimer(100, kFALSE);
92 B2DEBUG(20, "DQMHistAnalysisEpicsExample: beginRun called.");
93 m_c1->Clear();
94
95 if (auto hh1 = findHist(m_histogramDirectoryName, m_histogramName); hh1 != nullptr) {
96 m_c1->cd();
97 hh1->Draw();
98 m_line->Draw();
99 m_line_lo->Draw();
100 m_line_hi->Draw();
101 } else {
102 B2DEBUG(20, "Histo " << m_histogramName << " not found");
103 }
104}
105
107{
108 if (auto hh1 = findHist(m_histogramDirectoryName, m_histogramName); hh1 != nullptr) {
109 m_c1->cd();// necessary!
110 hh1->Fit(m_f1, "");
111 double y1 = hh1->GetMaximum();
112 double y2 = hh1->GetMinimum();
113 m_line->SetY1(y1 + (y1 - y2) * 0.05);
114 m_line_lo->SetY1(y1 + (y1 - y2) * 0.05);
115 m_line_hi->SetY1(y1 + (y1 - y2) * 0.05);
116// m_line->SetY2(y2-(y1-y2)*0.05);
117// m_line_lo->SetY2(y2-(y1-y2)*0.05);
118// m_line_hi->SetY2(y2-(y1-y2)*0.05);
119 double x = m_f1->GetParameter(1);
120 m_line->SetX1(x);
121 m_line->SetX2(x);
122 m_line->Draw();
123 m_line_lo->Draw();
124 m_line_hi->Draw();
125 m_c1->Modified();
126 m_c1->Update();
127 UpdateCanvas(m_c1->GetName());
128 } else {
129 B2DEBUG(20, "Histo " << m_histogramDirectoryName << "/" << m_histogramName << " not found");
130 }
131
132 if (m_parameters > 0) {
133 for (auto i = 0; i < m_parameters; i++) {
134 double data;
135 data = m_f1->GetParameter(i);
136 setEpicsPV(mypv[i], data);
137 }
138 }
139}
140
142{
143 B2DEBUG(20, "DQMHistAnalysisEpicsExample : endRun called");
144}
145
146
148{
149 B2DEBUG(20, "DQMHistAnalysisEpicsExample: terminate called");
150}
151
std::string m_histogramName
The name of the histogram.
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 number of fit function parameters for EPICS.
void event() override final
This method is called for each event.
std::string m_histogramDirectoryName
The name of the directory the histogram is in.
TLine * m_line_lo
The line for the lower bound.
void endRun() override final
This method is called if the current run ends.
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 void UpdateCanvas(const std::string &name, bool updated=true)
Mark canvas as updated (or not)
DQMHistAnalysisModule()
Constructor / Destructor.
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.
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.