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