Belle II Software  release-05-02-19
ECLLocalRunCalibratorModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * ECLLocalRunCalibratorModule *
6  * *
7  * This module computes the mean values and standard deviations *
8  * for the fitted time and amplitude. *
9  * *
10  * Author: The Belle II Collaboration *
11  * Contributors: Sergei Gribanov (S.S.Gribanov@inp.nsk.su) (BINP), *
12  * Vitaly Vorobyev (vvorob@inp.nsk.su) (BINP) *
13  * *
14  * This software is provided "as is" without any warranty. *
15  **************************************************************************/
16 
17 // FRAMEWORK
18 #include <framework/dataobjects/EventMetaData.h>
19 #include <framework/datastore/StoreObjPtr.h>
20 #include <framework/datastore/StoreArray.h>
21 
22 // ECL
23 #include <ecl/modules/eclLocalRunCalibration/ECLLocalRunCalibratorModule.h>
24 #include <ecl/dataobjects/ECLTrig.h>
25 
26 // ROOT
27 #include "TFile.h"
28 
29 using namespace Belle2;
30 //-----------------------------------------------------------------
31 // Register the Module
32 //-----------------------------------------------------------------
33 REG_MODULE(ECLLocalRunCalibrator)
34 //-----------------------------------------------------------------
35 // Implementation
36 //-----------------------------------------------------------------
37 // Number of cell ids.
39 // Time payload name.
40 const std::string
42 // Amplitude payload name.
43 const std::string
45 // Constructor.
47  m_time(nullptr), m_ampl(nullptr)
48 {
49  // Module description.
50  setDescription("ECL Local Run Calibration.");
51  // Initial lower limit for time.
52  addParam("minTime", m_minTime, "Initial lower limit for time.", -500.f);
53  // Initial upper limit for time.
54  addParam("maxTime", m_maxTime, "Initial upper limit for time.", 500.f);
55  // Initial lower limit for amplitude.
56  addParam("minAmpl", m_minAmpl, "Initial lower limit for amplitude.", 1.e+04f);
57  // Initial upper limit for amplitude.
58  addParam("maxAmpl", m_maxAmpl, "Initial upper limit for amplitude.", 3.e+04f);
59  // Number of standard deviations
60  // used to update lower and
61  // upper value limits.
62  addParam("nOfStdDevs", m_devs,
63  "Number of standard deviations used to form initial limits.", 5);
64  // Boolean flag used to choose local or central DB.
65  addParam("isLocal", m_isLocal, "Boolean flag used to"
66  "select local or central DB.", true);
67  // Default path to a local database.
68  std::string defaultDBName = "localdb/database.txt";
69  // Central DB name or path to a local DB.
70  addParam("dbName", m_dbName, "Central DB name or path "
71  "to a local DB.", defaultDBName);
72  // Used to change validity intervals in the previous run.
73  addParam("changePrev", m_changePrev,
74  "If changePrev is set, the upper run in the interval of "
75  "validity for the previous written payload will be changed.",
76  false);
77  // Low run for the current validity interval.
78  addParam("lowRun", m_lowRun,
79  "Low run for validity interval. "
80  "If the low run value is negative, "
81  "current run number "
82  "is used as a low run.", -1);
83  // High run for the current validity interval.
84  addParam("highRun", m_highRun,
85  "High run for validity interval. "
86  "If the high run value is equal to -1, "
87  "the high run of validity interval is not limited.",
88  -1);
89  // Mark current run as reference.
90  addParam("addRef", m_addref,
91  "Mark run as reference if it is"
92  "needed to do that at start",
93  false);
94  // Enables histogram filling mode.
95  addParam("fillHisto", m_isFillHisto,
96  "Fill time and amplitude histograms"
97  "for a certain cell id.", false);
98  // Cell id.
99  addParam("cellid", m_cellid,
100  "Cell id number, used to "
101  "fill amplitude or time histogram.");
102  // Default file name used to save histograms,
103  // obtained in the histogram filling mode.
104  const std::string defaultHistoFileName =
105  "time_ampl_histo.root";
106  // File name used to save histograms,
107  // obtained in the histogram filling mode.
108  addParam("histoFileName", m_histoFileName,
109  "File name used to save amplitude and time"
110  "histograms for a certain cell id.",
111  defaultHistoFileName);
112  // Enables full tree writing mode.
113  addParam("fulltree", m_fulltree,
114  "Fill full tree.", false);
115 }
116 // Destructor.
118 {
119  if (m_time) {
120  delete m_time;
121  }
122  if (m_ampl) {
123  delete m_ampl;
124  }
125 }
126 // Begin run
128 {
129  if (m_isFillHisto) {
130  // Histogram filling mode.
131  // Creating histograms, which will
132  // be contain time and amplitude distributions
133  // for a certain cell id.
134  m_histo_time = new TH1F("histo_time", "", 10000, -1, -1);
135  m_histo_ampl = new TH1F("histo_ampl", "", 10000, -1, -1);
136  } else {
137  // Local run calibration mode.
138  // Creating objects, which will be contain
139  // time and amplitude accumulators for all
140  // cell ids.
143  &m_devs);
146  &m_devs);
147  if (m_fulltree) {
148  m_tree = new TTree("localrun", "");
149  m_tree->Branch("cellid", &m_tree_cellid,
150  "m_tree_cellid/I");
151  m_tree->Branch("time", &m_tree_time,
152  "time/F");
153  m_tree->Branch("ampl", &m_tree_ampl,
154  "ampl/F");
155  m_tree_event = 0;
156  m_tree->Branch("event", &m_tree_event,
157  "event/I");
158  }
159  }
160 }
161 // Event.
163 {
164  // Getting amplitude and time input values.
165  StoreArray<ECLDigit> eclDigits("ECLDigits");
166  if (!eclDigits.isValid()) {
167  B2FATAL("eclDigits not valid");
168  }
169  // Loop over the input array.
170  for (const auto& digit : eclDigits) {
171  // Getting cell id and setting the
172  // corresponding index.
173  auto cellid = digit.getCellId();
174  auto index = cellid - 1;
175  // Check that the cell id is valid.
176  if (index < 0 || index >= signed(c_ncellids)) {
177  B2FATAL("ECLLocalRunCalibratorModule::event(): cell id = "
178  << cellid << " out of range!");
179  }
180  // Getting raw time and decoding it.
181  auto time = digit.getTimeFit();
182  auto shiftedTime = time + getTimeShift(digit);
183  // Getting amplitude.
184  auto ampl = digit.getAmp();
185  if (m_isFillHisto) {
186  // Histogram filling mode.
187  // Filling time and amplitude
188  // histograms for a certain
189  // cell id.
190  if (cellid == m_cellid) {
191  m_histo_time->Fill(shiftedTime);
192  m_histo_ampl->Fill(ampl);
193  }
194  } else {
195  // Local run calibration mode.
196  // Accumulating time mean
197  // values and standard deviations.
198  m_time->add(index, shiftedTime);
199  // Accumulating amplitude mean
200  // values and standard deviations.
201  m_ampl->add(index, ampl);
202  if (m_fulltree) {
203  m_tree_cellid = cellid;
204  m_tree_time = shiftedTime;
205  m_tree_ampl = ampl;
206  m_tree->Fill();
207  }
208  // Enabling negative value alarm,
209  // if there are negative amplitudes.
210  if (ampl < 0) {
212  }
213  }
214  }
215 
216  if (m_fulltree) {
217  m_tree_event++;
218  }
219 }
220 // Write histograms to file
221 // in the case of the histogram
222 // filling mode.
224 {
225  TFile fl(m_histoFileName.c_str(), "recreate");
226  fl.cd();
227  m_histo_time->Write();
228  m_histo_ampl->Write();
229  fl.Close();
230  delete m_histo_time;
231  delete m_histo_ampl;
232 }
233 // Write calibration results
234 // into a database.
236 {
237  // Getting experiment and run
238  // numbers.
240  int exp = evtPtr->getExperiment();
241  int run = evtPtr->getRun();
242  int lowrun;
243  // Setting low run
244  // of validity interval.
245  if (m_lowRun < 0) {
246  lowrun = run;
247  } else {
248  lowrun = m_lowRun;
249  }
250  // Creating validity interval.
251  IntervalOfValidity iov(exp, lowrun, exp, m_highRun);
252  // Saving time payload.
254  c_timePayloadName, iov,
255  run, m_changePrev, m_addref);
256  // Saving amplitude payload.
258  c_amplPayloadName, iov,
259  run, m_changePrev, m_addref);
260 }
261 // End run.
263 {
264  if (m_isFillHisto) {
265  // Histogram filling mode.
266  // Writing histograms to file.
268  } else {
269  // Local run calibration
270  // mode.
271  // Getting accumulated
272  // mean values and
273  // standard deviations.
274  m_time->calc();
275  m_ampl->calc();
276  // Saving calibration results
277  // into a database.
279  if (m_fulltree) {
280  auto fl = TFile::Open("/ghi/fs01/belle2/bdata/group/detector/ECL/local-run-db/data-full/ecl_local_run_fulltree.root",
281  "recreate");
282  fl->cd();
283  m_tree->Write();
284  fl->Close();
285  delete m_tree;
286  }
287  }
288 }
289 // Read collector's time to
290 // tune the time starting point.
292  const ECLDigit& digit) const
293 {
294  auto trig =
295  static_cast<uint32_t>(
296  digit.getRelationsTo<ECLTrig>()[0]->getTimeTrig());
297  switch (decodeTrigTime(trig) % 3) {
298  case 1 : return 8;
299  case 2 : return -8;
300  }
301  return 0;
302 }
303 // Decode time obtained
304 // from collector.
305 inline uint32_t
307 {
308  // Bits magic by
309  // Vladimir Zhulanov.
310  return time - 2 * (time / 8);
311 }
312 
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::ECLLocalRunCalibratorModule::getTimeShift
int16_t getTimeShift(const ECLDigit &digit) const
Calculate time shift.
Definition: ECLLocalRunCalibratorModule.cc:291
Belle2::ECLLocalRunCalibratorModule::decodeTrigTime
uint32_t decodeTrigTime(uint32_t time) const
Decode time.
Definition: ECLLocalRunCalibratorModule.cc:306
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::ECLLocalRunCalibratorModule::m_histo_ampl
TH1F * m_histo_ampl
m_histo_ampl is the name of the histogram, which contains amplitude distribution for a certain cell i...
Definition: ECLLocalRunCalibratorModule.h:190
Belle2::ECLLocalRunCalibratorModule::beginRun
void beginRun() override
Begin run.
Definition: ECLLocalRunCalibratorModule.cc:127
Belle2::ECLLocalRunCalibratorModule::m_cellid
int m_cellid
cell id number.
Definition: ECLLocalRunCalibratorModule.h:113
Belle2::ECLLocalRunCalibratorModule::~ECLLocalRunCalibratorModule
~ECLLocalRunCalibratorModule()
Destructor.
Definition: ECLLocalRunCalibratorModule.cc:117
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::ECLLocalRunCalibratorModule::m_tree_time
float m_tree_time
Time varible used to fill tree.
Definition: ECLLocalRunCalibratorModule.h:239
Belle2::ECLLocalRunCalibratorModule::m_fulltree
bool m_fulltree
Write full tree of times and amplitudes per each cellid (before calculating mean values) to file "ecl...
Definition: ECLLocalRunCalibratorModule.h:223
Belle2::ECLLocalRunCalibratorModule::c_timePayloadName
static const std::string c_timePayloadName
Name of the time payload.
Definition: ECLLocalRunCalibratorModule.h:194
Belle2::ECLLocalRunCalibratorModule::m_lowRun
int m_lowRun
Low run of the validity interval.
Definition: ECLLocalRunCalibratorModule.h:202
Belle2::ECLLocalRunCalibratorModule::m_maxAmpl
float m_maxAmpl
Maximum allowed amplitude value.
Definition: ECLLocalRunCalibratorModule.h:136
Belle2::ECLLocalRunCalibratorModule::m_addref
bool m_addref
If m_addref is true, then the current calibration run will be marked as reference run immediately aft...
Definition: ECLLocalRunCalibratorModule.h:172
Belle2::ECLLocalRunCalibratorModule::m_dbName
std::string m_dbName
Tag of central database or path to a local database.
Definition: ECLLocalRunCalibratorModule.h:147
Belle2::RelationsInterface::getRelationsTo
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Definition: RelationsObject.h:199
Belle2::ECLLocalRunCalibratorModule::writeCalibResultsToDB
void writeCalibResultsToDB()
Write calibration results into a database.
Definition: ECLLocalRunCalibratorModule.cc:235
Belle2::ECLLocalRunCalibUnit::writeToDB
void writeToDB(bool isLocal, const std::string &dbName, const std::string &payloadName, const IntervalOfValidity &iov, const int &run, const bool &changePrev, const bool &addref)
Write calibration results into a database.
Definition: ECLLocalRunCalibUnit.cc:106
Belle2::ECLLocalRunCalibUnit
ECLLocalRunCalibUnit is the class designed for the control of mean value and the standard deviation a...
Definition: ECLLocalRunCalibUnit.h:47
Belle2::ECLLocalRunCalibratorModule::m_histoFileName
std::string m_histoFileName
The path of the .root file with the histograms obtained in the histogram filling mode.
Definition: ECLLocalRunCalibratorModule.h:120
Belle2::ECLLocalRunCalibratorModule::m_maxTime
float m_maxTime
Maximum allowed time value.
Definition: ECLLocalRunCalibratorModule.h:128
Belle2::ECLLocalRunCalibUnit::add
void add(const int &cellid, const float &value)
Add value to accumulate mean value, standard deviation and number of accepted events.
Definition: ECLLocalRunCalibUnit.cc:56
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ECLLocalRunCalibratorModule::c_ncellids
static const int c_ncellids
Number of cell ids.
Definition: ECLLocalRunCalibratorModule.h:96
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::ECLLocalRunCalibratorModule::m_tree_ampl
float m_tree_ampl
Amplitude varible used to fill tree.
Definition: ECLLocalRunCalibratorModule.h:244
Belle2::ECLLocalRunCalibratorModule::m_isFillHisto
bool m_isFillHisto
If m_isFillHisto is false, than the mode of local run calibration is enabled.
Definition: ECLLocalRunCalibratorModule.h:107
Belle2::ECLLocalRunCalibratorModule::m_isLocal
bool m_isLocal
Enables local database usage.
Definition: ECLLocalRunCalibratorModule.h:152
Belle2::ECLLocalRunCalibratorModule::m_changePrev
bool m_changePrev
If m_changePrev is true, the validity intervals of the previous payloads stored into the database wil...
Definition: ECLLocalRunCalibratorModule.h:164
Belle2::ECLLocalRunCalibratorModule::m_tree_cellid
int m_tree_cellid
Cellid varible used to fill tree.
Definition: ECLLocalRunCalibratorModule.h:234
Belle2::ECLLocalRunCalibratorModule::endRun
void endRun() override
End run.
Definition: ECLLocalRunCalibratorModule.cc:262
Belle2::ECLLocalRunCalibratorModule::ECLLocalRunCalibratorModule
ECLLocalRunCalibratorModule()
Constructor.
Definition: ECLLocalRunCalibratorModule.cc:46
Belle2::StoreArray::isValid
bool isValid() const
Check wether the array was registered.
Definition: StoreArray.h:298
Belle2::ECLDigit
Class to store ECL digitized hits (output of ECLDigi) relation to ECLHit filled in ecl/modules/eclDig...
Definition: ECLDigit.h:34
Belle2::ECLLocalRunCalibratorModule::m_highRun
int m_highRun
High run of the validity interval.
Definition: ECLLocalRunCalibratorModule.h:206
Belle2::ECLLocalRunCalibUnit::calc
void calc()
Calculate accumulated values.
Definition: ECLLocalRunCalibUnit.cc:61
Belle2::ECLLocalRunCalibratorModule::m_tree
TTree * m_tree
Full tree of times and amplitudes per each cellid before calculating mean values.
Definition: ECLLocalRunCalibratorModule.h:229
Belle2::Module::addParam
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:562
Belle2::ECLLocalRunCalibratorModule::m_ampl
ECLLocalRunCalibUnit * m_ampl
m_ampl contains amplitude mean value and standard deviation accumulators for all cell ids.
Definition: ECLLocalRunCalibratorModule.h:216
Belle2::ECLLocalRunCalibratorModule::m_devs
int m_devs
Number of standard deviations used to update value limits.
Definition: ECLLocalRunCalibratorModule.h:142
Belle2::ECLLocalRunCalibratorModule::m_minAmpl
float m_minAmpl
Minimum allowed amplitude value.
Definition: ECLLocalRunCalibratorModule.h:132
Belle2::ECLLocalRunCalibratorModule::m_histo_time
TH1F * m_histo_time
m_histo_time is the name of the histogram, which contains time distribution for a certain cell id.
Definition: ECLLocalRunCalibratorModule.h:181
Belle2::ECLLocalRunCalibratorModule::m_minTime
float m_minTime
Minimum allowed time value.
Definition: ECLLocalRunCalibratorModule.h:124
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::ECLLocalRunCalibUnit::enableNegAmpl
void enableNegAmpl()
This function will be called only in the case, if negative amplitudes are observed in the current run...
Definition: ECLLocalRunCalibUnit.cc:42
Belle2::ECLTrig
Class to store ECLTrig, still need to be study relation to ECLHit filled in ecl/modules/eclDigitizer/...
Definition: ECLTrig.h:38
Belle2::ECLLocalRunCalibratorModule::event
void event() override
Event.
Definition: ECLLocalRunCalibratorModule.cc:162
Belle2::ECLLocalRunCalibratorModule::c_amplPayloadName
static const std::string c_amplPayloadName
Name of the amplitude payload.
Definition: ECLLocalRunCalibratorModule.h:198
Belle2::ECLLocalRunCalibratorModule::m_time
ECLLocalRunCalibUnit * m_time
m_time contains time mean value and standard deviation accumulators for all cell ids.
Definition: ECLLocalRunCalibratorModule.h:211
Belle2::ECLLocalRunCalibratorModule::writeHistoToFile
void writeHistoToFile()
Write histograms to file in the case, if the histogram filling mode is enabled.
Definition: ECLLocalRunCalibratorModule.cc:223
Belle2::ECLLocalRunCalibratorModule::m_tree_event
int m_tree_event
Event varible used to fill tree.
Definition: ECLLocalRunCalibratorModule.h:249