Belle II Software development
DQMHistAnalysisInputTest.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 : DQMHistAnalysisInputTest.cc
10// Description : Input module for offline testing of histogram analysis code.
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisInputTest.h>
15
16#include <boost/property_tree/ptree.hpp>
17#include <boost/property_tree/json_parser.hpp>
18
19#include <TROOT.h>
20
21//#include <iostream>
22using namespace Belle2;
23using namespace boost::property_tree;
24
25//-----------------------------------------------------------------
26// Register the Module
27//-----------------------------------------------------------------
28REG_MODULE(DQMHistAnalysisInputTest);
29
30//-----------------------------------------------------------------
31// Implementation
32//-----------------------------------------------------------------
33
36{
37 setDescription("Testing input file functionality for DQMHistAnalysisModules");
38 addParam("Experiment", m_expno, "Experiment Nr", 26u);
39 addParam("Run", m_runno, "Run Number List", 1u);
40 addParam("RunType", m_runtype, "Run Type", std::string("physics"));
41 addParam("Events", m_events, "Number of events for each run", 100u);
42 addParam("NonFillMod", m_nonfillmod, "Non-Fill event Modulo, 0 disable", 7u);
43 addParam("ConfigFiles", m_configs, "List of config files");
44 B2DEBUG(1, "DQMHistAnalysisInputTest: Constructor done.");
45}
46
48{
49 m_eventMetaDataPtr.registerInDataStore();
50 B2DEBUG(20, "DQMHistAnalysisInputTest: initialized.");
51
52 // do config file parsing
53 for (auto cf : m_configs) {
54 ptree tree;
55 json_parser::read_json(cf, tree);
56 // just name it, the list is called "HistogramList"
57 ptree pt = tree.get_child("HistogramList");
58 for (auto pair : pt) {
59 // the array entries do not have a name ...
60 std::string name;
61 std::string dir;
62 unsigned int dtype = 0;
63 unsigned int parameter = 0;
64 unsigned int amount = 0;
65 unsigned int fill = 0;
66 unsigned int underflow = 0;
67 std::string fitfunc;
68 std::vector<double> fitparm;
69 std::vector<double> histparm;
70 for (auto pair2 : pair.second) {
71 if (pair2.first == "Name") {
72 name = pair2.second.get_value<std::string>();
73 } else if (pair2.first == "Dir") {
74 dir = pair2.second.get_value<std::string>();
75 } else if (pair2.first == "Delta") {
76 for (auto pair3 : pair2.second) {
77 if (pair3.first == "Type") {
78 dtype = pair3.second.get_value<unsigned int>();
79 } else if (pair3.first == "Parameter") {
80 parameter = pair3.second.get_value<unsigned int>();
81 } else if (pair3.first == "Amount") {
82 amount = pair3.second.get_value<unsigned int>();
83 } else {
84 std::cout << pair3.first << " is unknown parameter -> parse error" << std::endl;
85 }
86 }// end Delta loop
87 } else if (pair2.first == "Simulation") {
88 for (auto pair3 : pair2.second) {
89 if (pair3.first == "Function") {
90 fitfunc = pair3.second.get_value<std::string>();
91 } else if (pair3.first == "Fill") {
92 fill = pair3.second.get_value<unsigned int>();
93 } else if (pair3.first == "Underflow") {
94 underflow = pair3.second.get_value<unsigned int>();
95 } else if (pair3.first == "FPar") {
96 for (auto pair4 : pair3.second) {
97 fitparm.push_back(pair4.second.get_value<double>());
98 }
99 } else if (pair3.first == "HPar") {
100 for (auto pair4 : pair3.second) {
101 histparm.push_back(pair4.second.get_value<double>());
102 }
103 }
104 }// end Simulation loop
105 } else if (pair2.first == "Analysis") {
106 // ignore
107 } else if (pair2.first == "EPICS") {
108 // ignore
109 } else {
110 std::cout << pair2.first << " is unknown parameter -> parse error" << std::endl;
111 }
112 }
113
114 if (dir == "" or name == "") {
115 std::cout << "Both Dir and Name must be given. -> parse error" << std::endl;
116 } else {
117 // as we are already on analysis side, we dont
118 // need to put it in the directory
119 // (even so it may be a cleaner solution)
120 m_testHisto.push_back((TH1*) new TH1F(name.data(), "test Histo", histparm.at(0), histparm.at(1), histparm.at(2)));
121 m_myNames.push_back(name);
122 auto func = new TF1(TString(name) + "_func", fitfunc.data(), fitparm.at(0), fitparm.at(1));
123 /* func->SetParameters(100, 100, 30); // scale, mean, sigma
124 func->SetParLimits(0, 100, 100);
125 func->SetParLimits(1, 100, 900); // mean
126 func->SetParLimits(2, 10, 100); // sigma*/
127 m_func.push_back(func);
128 m_fpar.push_back(fitparm);
129 m_fill.push_back(fill);
130 m_underflow.push_back(underflow);
131
132 if (dtype > 0) addDeltaPar(dir, name, HistDelta::EDeltaType(dtype), parameter, amount); // only if we want delta for this one
133 }
134 }
135 }
136
137}
138
140{
141 B2INFO("DQMHistAnalysisInputTest: beginRun called. Run: " << m_runno);
143
144 m_count = 0;
145 for (auto h : m_testHisto) h->Reset();
146
147}
148
150{
151 // attention, the first call to event is BEFORE the first call to begin run!
152
153 B2INFO("DQMHistAnalysisInputTest: event called.");
154
156
157 if (m_count > m_events) {
158 m_eventMetaDataPtr.create();
159 m_eventMetaDataPtr->setEndOfData();
160 B2INFO("DQMHistAnalysisInputTest: max event number reached, set EndOfData");
161 return;
162 }
163
164 if (m_count == 0 || m_nonfillmod == 0 || (m_count % m_nonfillmod) != 0) {
165 int i = 0;// index
166 for (auto h : m_testHisto) {
167 for (int p = 0; p < m_fpar[i].at(2); p++) {
168 m_func[i]->SetParameter(p, m_fpar[i].at(3 + 2 * p) + m_count * (m_fpar[i].at(4 + 2 * p) - m_fpar[i].at(3 + 2 * p)) / m_events);
169 }
170 h->FillRandom(TString(m_myNames[i]) + "_func", m_fill[i]);
171 if (m_underflow[i]) {
172 h->SetBinContent(0, h->GetBinContent(0) + m_underflow[i]);
173 }
174 i++;
175 }
176 }
177
178 for (auto h : m_testHisto) {
179 addHist("test", h->GetName(), (TH1*)h->Clone());
180 }
181
182 /*{
183 auto c=new TCanvas();
184 c->cd();
185 m_testHisto->Draw("hist");
186 TString fn;
187 fn.Form("testHist_%d.png",m_count);
188 c->Print(fn);
189 }*/
190
191 m_eventMetaDataPtr.create();
192 m_eventMetaDataPtr->setExperiment(m_expno);
194 m_eventMetaDataPtr->setEvent(m_count);
195 m_eventMetaDataPtr->setTime(m_count * 1e9);
196 //setExpNr(m_expno); // redundant access from MetaData
197 //setRunNr(m_runno); // redundant access from MetaData
199 setEventProcessed(m_count * 10000); // just fix it for now.
200 B2INFO("DQMHistAnalysisInputTest: event finished. count: " << m_count);
201
202 PlotDelta();
203 m_count++;
204}
205
207{
208 B2INFO("Delta");
209 for (auto& a : getDeltaList()) {
210 // must be reference iterator
211 B2INFO(a.first << " " << a.second.m_type << " " << a.second.m_parameter
212 << " " << a.second.m_amountDeltas << " " << a.second.m_deltaHists.size());
213 }
214
215 for (auto n : m_myNames) {
216 // we use fixed dir here as example
217 std::string name = "test/" + n;
218 auto c = new TCanvas();
219 c->Divide(3, 3);
220 c->cd(1);
221 auto h = findHist(name);
222 if (h) h->Draw("hist");
223
224 // for testing, we use low level access to histograms here
225 // for analysis, better use getDelta(name,index) base class function
226 c->cd(2);
227 auto it = getDeltaList().find(name);
228 if (it != getDeltaList().end()) {
229 h = it->second.m_lastHist.get();
230 if (h) h->Draw("hist");
231
232 for (unsigned int i = 0; i < it->second.m_amountDeltas; i++) {
233 c->cd(i + 4);
234 auto hist = it->second.getDelta(i);
235 if (hist) {
236 hist->Draw("hist");
237 c->cd(3);
238 if (i == 0) {
239 hist->Draw();
240 } else {
241 hist->SetLineColor(i + 2);
242 hist->Draw("same");
243 }
244 }
245 if (i + 4 == 9) break;
246 }
247 }
248
249 c->cd(0);
250
251 TString fn;
252 fn.Form("%s_Delta_%d.png", n.data(), m_count);
253 c->Print(fn);
254 delete c; // remove temporary canvas, otherwise it may end up in output
255 }
256}
257
259{
260 B2INFO("DQMHistAnalysisInputTest: endRun called. Run: " << m_runno);
261
262 B2INFO("DQMHistAnalysisInputTest: endRun: Histos");
263 for (auto& a : getHistList()) {
264 B2INFO(a.first);
265 }
266 // The following will produce errors, as the histograms may not exist in endRun
267 PlotDelta();
268 B2INFO("DQMHistAnalysisInputTest: endRun: done");
269}
270
271
273{
274 B2INFO("DQMHistAnalysisInputTest: terminate called. Run: " << m_runno);
275 clearlist(); // necessary in the Input Module! Otherwise ROOT may clean before we do
276}
void PlotDelta(void)
Plot/print function for debugging.
unsigned int m_nonfillmod
Fill skip modulo.
void initialize() override final
Initialize the module.
std::vector< std::string > m_configs
List of config files.
std::vector< unsigned int > m_fill
according list of fills per "event"
unsigned int m_events
total number of events for run.
void terminate() override final
Termination action.
std::vector< TF1 * > m_func
according list of function for filling histo
void event() override final
Event processor.
std::vector< std::string > m_myNames
keeping a list of histo Names
std::vector< TH1 * > m_testHisto
list of test histogram
std::vector< std::vector< double > > m_fpar
according parameter list for function definition
void endRun() override final
End-of-run action.
std::vector< unsigned int > m_underflow
according list of underflow fills per "event"
void beginRun() override final
Called when entering a new run.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
Global EventMetaData for run number and event number.
The base class for the histogram analysis module.
void addDeltaPar(const std::string &dirname, const std::string &histname, HistDelta::EDeltaType t, int p, unsigned int a=1)
Add Delta histogram parameters.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
void clearlist(void)
Clear all static global lists.
static void clearHistList(void)
Clears the list of histograms.
void setRunType(std::string &t)
Set the Run Type.
void setEventProcessed(int e)
Set the number of processed events.
static bool addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
static HistList & getHistList()
Get the list of the histograms.
static void initHistListBeforeEvent(void)
Reset the list of histograms.
static const DeltaList & getDeltaList()
Get the list of the delta histograms.
EDeltaType
enum definition for delta algo Disabled: nothing Entries: use nr histogram entries Underflow: use ent...
Definition: HistDelta.h:32
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: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.