Belle II Software  release-08-01-10
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>
22 using namespace Belle2;
23 using namespace boost::property_tree;
24 
25 //-----------------------------------------------------------------
26 // Register the Module
27 //-----------------------------------------------------------------
28 REG_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 analyis 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);
142  clearHistList();
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(), h);
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);
193  m_eventMetaDataPtr->setRun(m_runno);
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  B2INFO(a.first << " " << a.second->m_type << " " << a.second->m_parameter
211  << " " << a.second->m_amountDeltas << " " << a.second->m_deltaHists.size());
212  }
213 
214  for (auto n : m_myNames) {
215  // we use fixed dir here as example
216  std::string name = "test/" + n;
217  auto c = new TCanvas();
218  c->Divide(3, 3);
219  c->cd(1);
220  auto h = findHist(name);
221  if (h) h->Draw("hist");
222 
223  // for testing, we use low level access to histograms here
224  // for analysis, better use getDelta(name,index) base class function
225  c->cd(2);
226  auto it = getDeltaList().find(name);
227  if (it != getDeltaList().end()) {
228  h = it->second->m_lastHist;
229  if (h) h->Draw("hist");
230 
231  for (unsigned int i = 0; i < it->second->m_amountDeltas; i++) {
232  c->cd(i + 4);
233  h = it->second->getDelta(i);
234  if (h) {
235  h->Draw("hist");
236  c->cd(3);
237  if (i == 0) {
238  h->Draw();
239  } else {
240  h->SetLineColor(i + 2);
241  h->Draw("same");
242  }
243  }
244  if (i + 4 == 9) break;
245  }
246  }
247 
248  c->cd(0);
249 
250  TString fn;
251  fn.Form("%s_Delta_%d.png", n.data(), m_count);
252  c->Print(fn);
253  delete c; // remove temporary canvas, otherwise it may end up in output
254  }
255 }
256 
258 {
259  B2INFO("DQMHistAnalysisInputTest: endRun called. Run: " << m_runno);
260 
261  B2INFO("DQMHistAnalysisInputTest: endRun: Histos");
262  for (auto a : getHistList()) {
263  B2INFO(a.first);
264  }
265  // The following will produce errors, as the histograms may not exist in endRun
266  PlotDelta();
267  B2INFO("DQMHistAnalysisInputTest: endRun: done");
268 }
269 
270 
272 {
273  B2INFO("DQMHistAnalysisInputTest: terminate called. Run: " << m_runno);
274 }
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).
static const DeltaList & getDeltaList()
Get the list of the delta histograms.
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.
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
REG_MODULE(arichBtest)
Register the Module.
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
Abstract base class for different kinds of events.