Belle II Software development
DQMHistAnalysisDeltaTest.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 : DQMHistAnalysisDeltaTest.cc
10// Description : Test Module for Delta Histogram Access
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisDeltaTest.h>
15#include <TROOT.h>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23REG_MODULE(DQMHistAnalysisDeltaTest);
24
25//-----------------------------------------------------------------
26// Implementation
27//-----------------------------------------------------------------
28
31{
32 // This module CAN NOT be run in parallel!
33 setDescription("Testing module for using delta histogramming functionality.");
34
35 //Parameter definition
36 addParam("histogramDirectoryName", m_histogramDirectoryName, "Name of Histogram dir", std::string("test"));
37 addParam("histogramName", m_histogramName, "Name of Histogram", std::string("testHist"));
38 addParam("PVPrefix", m_pvPrefix, "PV Prefix", std::string("TEST:"));
39 B2DEBUG(1, "DQMHistAnalysisDeltaTest: Constructor done.");
40}
41
43{
44 B2DEBUG(1, "DQMHistAnalysisDeltaTest: Destructor done.");
45}
46
48{
49 B2DEBUG(1, "DQMHistAnalysisDeltaTest: initialized.");
50
52
53 gROOT->cd(); // this seems to be important, or strange things happen
54
55 m_cTest = new TCanvas((m_histogramDirectoryName + "/c_Test").data());
56
58
59 registerEpicsPV(m_pvPrefix + "TEST1", "TEST1");
60 registerEpicsPV(m_pvPrefix + "TEST2", "TEST2");
61}
62
64{
65 B2DEBUG(1, "DQMHistAnalysisDeltaTest: beginRun called.");
66}
67
69{
70 B2DEBUG(1, "DQMHistAnalysisDeltaTest: endRun called.");
71}
72
74{
75 double data_Test1 = 0.0;
76 double data_Test2 = 0.0;
77
78 m_cTest->Clear();
79 m_cTest->Divide(3, 2);
80
81 // more handy to have it as a full name, but find/get functions
82 // can work with one or two parameters
83 std::string fullname = m_histogramDirectoryName + "/" + m_histogramName;
84
85 // get basic histogram (run integrated up)
86 auto hh1 = findHist(m_histogramDirectoryName, m_histogramName, false);// even if no update
87 if (hh1) {
88 m_cTest->cd(1);
89 auto a = (TH1*)hh1->DrawClone("hist");
90 a->SetTitle("Hist always");
91 data_Test2 = hh1->GetMean();
92 }
93
94 auto hh2 = findHist(m_histogramDirectoryName, m_histogramName, true);// only if updated
95 if (hh2) {
96 m_cTest->cd(2);
97 auto a = (TH1*)hh2->DrawClone("hist");
98 a->SetTitle("Hist only if updated");
99 }
100
101 // get most recent delta
102 auto hd2 = getDelta(m_histogramDirectoryName, m_histogramName, 0, false);// even if no update
103 if (hd2) {
104 m_cTest->cd(4);
105 auto a = (TH1*)hd2->DrawClone("hist");
106 a->SetTitle("Delta always");
107 }
108
109 // get most recent delta
110 auto hd1 = getDelta(m_histogramDirectoryName, m_histogramName, 0, true);// only if updated
111 if (hd1) {
112 m_cTest->cd(5);
113 auto a = (TH1*)hd1->DrawClone("hist");
114 a->SetTitle("Delta only if updated");
115 data_Test1 = hd1->GetMean();
116 }
117
118 UpdateCanvas(m_cTest->GetName(), hd1 != nullptr);
119
120 if (!hd2) {
121 // Depending on you analysis, you want to see/plot
122 // the histogram even before the first condition for update is met
123 // thus, no delta histogram is available
124 if (hh1) {
125 m_cTest->cd(3);
126 auto a = (TH1*)hh1->DrawClone("hist");
127 a->SetLineColor(2);
128 a->SetTitle("initial sampling");
129 }
130 // but, as the statistics is low, we dont want to have it updated yet (e.g. in EPICS)
131 // data_Test1=hd1->GetMean(); //< thus, do not uncomment
132 }
133
134 // plot all delta histograms, thus make recent shifts more visible
135 // it would be nicer to plot oldest first, left as exercise for reader
136 m_cTest->cd(6);
137 for (int i = 0; i < 99; i++) {
138 auto h = getDelta(fullname, i, false);
139 if (h == nullptr) break;
140 if (i == 0) {
141 h->Draw("hist");
142 } else {
143 h->SetLineColor(kBlue + 4 - i * 3); // a bit of shading
144 h->Draw("same");
145 }
146 }
147
148 // I see no reason to access that histogram, but just as low-level example.
149 m_cTest->cd(7);
150 auto it = getDeltaList().find(m_histogramName);
151 if (it != getDeltaList().end()) {
152 auto h = it->second.m_lastHist.get();
153 if (h) {
154 auto a = (TH1*)h->DrawClone("hist");
155 a->SetTitle("last update histogram");
156 }
157 }
158
159 m_cTest->cd(0);
160 m_cTest->Update();
161
162 TString fn;
163 static int plot_count = 0;
164 fn.Form("ana_%s_Delta_%d.png", m_histogramName.data(), plot_count++);
165 m_cTest->Print(fn);
166
167 // actually, we would prefer to only update the epics variable
168 // if the main histogram or delta~ has changed. but there is until now no easy
169 // way to check that
170 if (true) {
171 // currently, monObj are only used at end of run, thus this is not necessary
172 // but, in the future we may want to use finer granularity.
173 m_monObj->setVariable("data_Test1", data_Test1);
174 m_monObj->setVariable("data_Test2", data_Test2);
175
176 setEpicsPV("TEST1", data_Test1);
177 setEpicsPV("TEST2", data_Test2);
178 }
179}
180
182{
183 B2DEBUG(1, "DQMHistAnalysisDeltaTest: terminate called");
184}
185
void terminate(void) override final
This method is called at the end of the event processing.
void initialize(void) override final
Initializer.
std::string m_histogramName
name of histogram
void endRun(void) override final
Called when run ends.
std::string m_pvPrefix
prefix for EPICS PVs
MonitoringObject * m_monObj
Monitoring Object.
std::string m_histogramDirectoryName
name of histogram directory
void beginRun(void) override final
Called when entering a new run.
void event(void) override final
This method is called for each event.
The base class for the histogram analysis module.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
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.
static const DeltaList & getDeltaList()
Get the list of the delta histograms.
void UpdateCanvas(std::string name, bool updated=true)
Mark canvas as updated (or not)
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.