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