Belle II Software  release-08-01-10
TrgEclTimingCalibration.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 #include "trg/ecl/modules/trgeclTimingCal/TrgEclTimingCalibration.h"
10 
11 #include "TDecompLU.h"
12 #include "TFile.h"
13 #include "TTree.h"
14 
15 using namespace Belle2;
16 using namespace std;
17 
18 REG_MODULE(TRGECLTimingCal); // Register module
19 
21 {
22 
23  setDescription("example module for TRGECL timing calibration");
25 
26  addParam("TRGECLCalSim", fSimulation,
27  "0 : Raw data 1 : Simulation data", 0);
28 
29  addParam("TRGECLCalType", fCalType,
30  "0 : Beam data 1 : Cosmic ray data", 0);
31 
32  addParam("TRGECLCal3DBhabhaVeto", f3Dbhabha_veto,
33  "0 : Not using 3D bhabha veto bit 1 : Using 3D bhabha veto bit ", 1);
34 
35  addParam("TRGECLCalTCRef", TC_ref,
36  "Reference TCID. Default setting : 184", 184);
37 
38  addParam("TCEnergyCalibrationConstant", TCEnergyCalibrationConstant,
39  "A TC energy calibration factor (GeV/ADC), default 0.00525", 0.00525);
40 
41  addParam("TRGECLCalnTC", cut_ntc,
42  "Number of TC cut : Events will be used < ntc", 999);
43 
44  addParam("TRGECLCalHighEnergyCut", cut_high_energy,
45  "High energy cut", 9999.0);
46 
47  addParam("TRGECLCalLowEnergyCut", cut_low_energy,
48  "Low energy cut", 0.0);
49 
50  addParam("TRGECLCalFWD", fInclude_FWD,
51  "Flag including FWD end cap", 1);
52 
53  addParam("TRGECLCalBR", fInclude_BR,
54  "Flag including Barrel", 1);
55 
56  addParam("TRGECLCalBWD", fInclude_BWD,
57  "Flag including BWD end cap", 1);
58 
59  addParam("TRGECLCalofname", str_ofilename,
60  "Output file name", str_default_name);
61 
62  addParam("TRGECLCalIteration", fIteration,
63  "iteration", 0);
64 
65  addParam("TRGECLCalChisqCut", cut_chisq,
66  "iteration chisq cut", 1000.0);
67 
68  addParam("TRGECLCalOffsetFname", str_timeoffset_fname,
69  "Input offset file name", str_timeoffset_fname);
70 
71 }
72 
74 {
75 }
76 
78 {
80  StoreArray<TRGECLUnpackerStore> TCHitUnpacker;
83  //StoreArray<TRGECLCluster> TCCluster;
84 
85  if (fSimulation == 1) {
86  TCHit.isRequired();
87  if (f3Dbhabha_veto == 1) {
88  TRGECLTrg.isRequired();
89  }
90  //StoreArray<TRGECLHit>::registerPersistent();
91  } else if (fSimulation == 0) {
92  TCHitUnpacker.isRequired();
93  if (f3Dbhabha_veto == 1) {
94  TRGECLEvtStore.isRequired();
95  }
96  //StoreArray<TRGECLUnpackerStore>::registerPersistent();
97  }
98 
99  InitParams();
100 }
101 
103 {
104 }
105 
107 {
108 }
109 
111 {
112 
114  // -1 : No event for reference TC
115  // 0 : Singular matrix
116  // 1 : Solvable matrix
117  Calculate_Chisq();
118  Save_Result();
119 
120 }
121 
123 {
124  nevt_read++;
125 
126  if (f3Dbhabha_veto == 1) {
127  if (fSimulation == 0) {
128  StoreArray<TRGECLUnpackerEvtStore> TRGECLEvtStore;
129  int nEvtStore = TRGECLEvtStore.getEntries();
130  if (nEvtStore <= 0) return;
131  if (TRGECLEvtStore[0] -> getCheckSum() != 0) return;
132  if (TRGECLEvtStore[0] -> get3DBhabhaV() != 1) return;
133  }
134 
135  if (fSimulation == 1) {
137  int nEvtStore = TRGECLTrg.getEntries();
138  if (nEvtStore <= 0) return;
139  if (TRGECLTrg[0] -> get3DBhabha() != 1) return;
140  }
141  }
143 
144  if (ReadData() == 0) return;
145  if (FillMatrix() == 1)
146  nevt_selected++;
147 
148  return;
149 }
150 
151 // 0: Skip this event 1: Use this event
153 {
154 
155  int ntc;
156  if (fSimulation == 1) { // Simulation data : TRGECLHit table
157  StoreArray<TRGECLHit> TCHit;
158  ntc = TCHit.getEntries();
159  if (ntc < 2 || ntc > cut_ntc) return 0; // ntc cut
160  for (int itc = 0; itc < ntc; itc++) {
161  TCId.push_back(TCHit[itc]->getTCId());
162  TCEnergy.push_back(TCHit[itc]->getEnergyDep());
163  TCTiming.push_back((double) TCHit[itc]->getTimeAve());
164  }
165  } else { // Raw data : TRGECLUnpackerStore table
167  ntc = TCHit.getEntries();
168  if (ntc < 2 || ntc > cut_ntc) return 0; // ntc cut
169  for (int itc = 0; itc < ntc; itc++) {
170  if (TCHit[itc] -> getChecksum() != 0) return 0;
171  if (!(TCHit[itc] -> getHitWin() == 3 || TCHit[itc] -> getHitWin() == 4)) continue;
172  TCId.push_back(TCHit[itc]->getTCId());
173  TCEnergy.push_back((double) TCHit[itc]->getTCEnergy() * TCEnergyCalibrationConstant);
174  TCTiming.push_back((double) TCHit[itc]->getTCTime());
175  }
176  }
177  return 1;
178 }
179 
180 // 0: Skip this event 1: Use this event
182 {
183  int flag_event_used = 0;
184  int ntc = TCId.size();
185 
186  for (int itc1 = 0; itc1 < ntc; itc1++) {
187  if (TCEnergy[itc1] > cut_high_energy || TCEnergy[itc1] < cut_low_energy) continue;
188 
189  int m_TCID1 = TCId[itc1] - 1;
190  if (b_Exclude_TC[m_TCID1] == true) continue;
191 
192  for (int itc2 = itc1 + 1; itc2 < ntc; itc2++) {
193  int m_TCID2 = TCId[itc2] - 1;
194  if (b_Exclude_TC[m_TCID2] == true) continue;
195  // energy cut
196  if (TCEnergy[itc2] > cut_high_energy || TCEnergy[itc2] < cut_low_energy) continue;
197  //double sigma2 = 1.0/(TCEnergy[itc1]*TCEnergy[itc1]) + 1.0/(TCEnergy[itc2]*TCEnergy[itc2]); // sigma i,j or j,i
198 
199  double sigma2 = 5.0 * 5.0; // temporal resolution
200  double tmp_chisq = pow((TCTiming[itc1] - TimeOffset[m_TCID1]) - (TCTiming[itc2] - TimeOffset[m_TCID2]), 2) / sigma2;
201  if (fIteration == 1 && tmp_chisq > cut_chisq) continue;
202 
203  // Fill symmetric matrix
204  _Matrix[m_TCID1][m_TCID2] += (-1.0 / sigma2);
205  _Matrix[m_TCID2][m_TCID1] += (-1.0 / sigma2);
206  // Fill diagonal component
207  _Matrix[m_TCID1][m_TCID1] += (1.0 / sigma2);
208  _Matrix[m_TCID2][m_TCID2] += (1.0 / sigma2);
209 
210  // Fill Chisq matrix1
211  Chisq1[m_TCID1][m_TCID2] += (1.0 / sigma2);
212  Chisq1[m_TCID2][m_TCID1] += (1.0 / sigma2);
213 
214 
215  // TOF correction & Fill vector component
216  double Vector_component = 0;
217  if (fCalType == 0) { //no TOF correction for beam calibration
218  Vector_component = (TCTiming[itc1] - TCTiming[itc2]);
219  }
220 
221  else if (fCalType == 1) { //cosmic ray data
222  double TOF_TC2TC = (TCPosition[m_TCID1] - TCPosition[m_TCID2]).Mag() / 29.9792458;
223  if (TCPosition[m_TCID1].y() > TCPosition[m_TCID2].y()) {
224  Vector_component = (TCTiming[itc1] - (TCTiming[itc2] - TOF_TC2TC));
225  } else Vector_component = ((TCTiming[itc1] - TOF_TC2TC) - TCTiming[itc2]);
226  }
227  Vector_component /= sigma2;
228  _Vector[m_TCID1] += Vector_component;
229  _Vector[m_TCID2] += -Vector_component;
230  // Fill Chisq matrix2,3
231  Chisq2[m_TCID1][m_TCID2] += -2.0 * Vector_component;
232  Chisq2[m_TCID2][m_TCID1] += 2.0 * Vector_component;
233 
234  Chisq3[m_TCID1][m_TCID2] += Vector_component * Vector_component * sigma2;
235  Chisq3[m_TCID2][m_TCID1] += Vector_component * Vector_component * sigma2;
236 
237  Nevent_TC[m_TCID1]++;
238  Nevent_TC[m_TCID2]++;
239  flag_event_used = 1;
240  if (b_Inc_TC[m_TCID2] == false) {b_Inc_TC[m_TCID2] = true;}
241  }// for itc2
242  if (b_Inc_TC[m_TCID1] == false) {b_Inc_TC[m_TCID1] = true;}
243  }// for itc1
244  return flag_event_used;
245 }
246 
248 {
249  tcal_result.clear();
250  tcal_result_err.clear();
251  tcal_result.resize(576);
252  tcal_result_err.resize(576);
253  const int nTC_tot = 576;
254  TMatrixDSym mat_A(575);
255  mat_A.ResizeTo(575, 575, 0.0);
256  TVectorD vec_b(575);
257  vec_b.ResizeTo(575);
258 
259  // copy matrix excluding reference TC
260  for (int i = 0; i < 576; i++) {
261  if (i == TC_ref - 1) continue;
262  int indexi = i;
263  if (i > TC_ref - 1) indexi = i - 1;
264  vec_b[indexi] = _Vector[i];
265  for (int j = 0; j < 576; j++) {
266  if (j == TC_ref - 1) continue;
267  int indexj = j;
268  if (j > TC_ref - 1) indexj = j - 1;
269  mat_A[indexi][indexj] = _Matrix[i][j];
270  }
271  }
272 
273  // error calculation
274  //**** Not supported yet ***********//
275 
276  // (A^T * A)^-1_ii : error^2 of t_i
277  //TMatrixDSym mat_offset_err(mat_A);
278  //TMatrixDSym mat_offset_errT = mat_offset_err.Transpose();
279  //mat_offset_err.ResizeTo(576,576,0.0);
280  //mat_offset_errT.ResizeTo(576,576,0.0);
281 
282  //mat_offset_errT.TMult(mat_offset_err);
283  //mat_offset_errT.Invert();
284  //double *element_mat_offset_err = mat_offset_err.GetMatrixArray();
285  for (int i = 0; i < 576; i++) {
286  //tcal_result_err[i] = mat_offset_errT[i][i];
287  tcal_result_err[i] = 0.0;
288  }
289 
290  TDecompLU lu(mat_A);
291  bool b_solve; // Is this matrix solvable? 0 : no, singular 1 : yes, solvable
292 
293  // Solve Ax = b
294  TVectorD x = lu.Solve(vec_b, b_solve);
295  /* cppcheck-suppress variableScope */
296  int ix = 0;
297  if (b_solve) {
298  for (int i = 0; i < nTC_tot; i++) {
299  if (i == TC_ref - 1) {
300  tcal_result[i] = 0.0;
301  } else {
302  tcal_result[i] = x[ix];
303  ++ix;
304  }
305  }
306  return 1;
307  } else {
308  for (int i = 0; i < nTC_tot; i++) {
309  tcal_result[i] = -999.0;
310  tcal_result_err[i] = -999.0;
311  }
312  return 0;
313  }
314 }
315 
317 {
318  for (int iTCID = 0; iTCID < 576; iTCID++) {
319  TrgEclMapping* trgeclMap = new TrgEclMapping();
320  TCPosition.push_back(trgeclMap->getTCPosition(iTCID + 1));
321  delete trgeclMap;
322  }
323 }
324 
326 {
327  nevt_selected = 0;
328  nevt_read = 0;
329 
330  if (fCalType == 1) {
331  Set_TCposition();
332  }
333 
334  // initialize matrix and vector
335  for (int i = 0; i < 576; i++) {
336  b_Inc_TC.push_back(false);
337  }
338  TimeOffset.assign(576, 0.0);
339  _Matrix = TMatrixDSym(576);
340  _Vector = TVectorD(576);
341  _Matrix.ResizeTo(576, 576, 0.0);
342  _Vector.ResizeTo(576);
343  for (int i = 0; i < 576; i++) {
344  _Vector[i] = 0.0;
345  for (int j = 0; j < 576; j++) {
346  _Matrix[i][j] = 0.0;
347  }
348  }
349  // initialize chisq matrix
350  Chisq1.resize(576);
351  Chisq2.resize(576);
352  Chisq3.resize(576);
353  Nevent_TC.assign(576, 0);
354  for (int i = 0; i < 576; i++) {
355  Chisq1[i].assign(576, 0.0);
356  Chisq2[i].assign(576, 0.0);
357  Chisq3[i].assign(576, 0.0);
358  }
359 
360  // Exclude TC setup
361  b_Exclude_TC.resize(576);
362  for (int i = 0; i < 576; i++) {
363  b_Exclude_TC[i] = false;
364  }
365 
366  if (fInclude_FWD == 0) {
367  for (int i = 0; i < 80; i++) {
368  b_Exclude_TC[i] = true;
369  }
370  }
371  if (fInclude_BR == 0) {
372  for (int i = 80; i < 512; i++) {
373  b_Exclude_TC[i] = true;
374  }
375  }
376  if (fInclude_BWD == 0) {
377  for (int i = 512; i < 576; i++) {
378  b_Exclude_TC[i] = true;
379  }
380  }
381 
382  GetTimeOffset();
383 
384 }
385 
387 {
388  TString fname = str_ofilename;
389  TFile* tf = new TFile(fname, "RECREATE");
390  TTree* tr = new TTree("tree", "TC time calibration result");
391  vector<double> time_offset;
392  vector<double> time_offset_err;
393  vector<int> TCID;
394  vector<double> chisq;
395  vector<int> Nevent;
396  time_offset.resize(576);
397  time_offset_err.resize(576);
398  TCID.resize(576);
399  chisq.resize(576);
400  Nevent.resize(576);
401  double timeoffsetave = 0.0;
402  tr -> Branch("TCID", &TCID);
403  tr -> Branch("TimeOffset", &time_offset);
404  tr -> Branch("TimeOffAve", &timeoffsetave);
405  tr -> Branch("TimeOffsetErr", &time_offset_err);
406  tr -> Branch("Chisq", &chisq);
407  tr -> Branch("Nevent", &Nevent);
408  tr -> Branch("FlagSolved", &FlagMatrixSolved);
409  tr -> Branch("Matrix", &_Matrix);
410  tr -> Branch("Vector", &_Vector);
411  tr -> Branch("ChisqComponent1", &Chisq1);
412  tr -> Branch("ChisqComponent2", &Chisq2);
413  tr -> Branch("ChisqComponent3", &Chisq3);
414  tr -> Branch("NeventRead", &nevt_read);
415  tr -> Branch("NeventUsed", &nevt_selected);
416  for (int i = 0; i < 576; i++) {
417  TCID[i] = i + 1;
418  time_offset[i] = tcal_result[i];
419  timeoffsetave += tcal_result[i];
420  time_offset_err[i] = tcal_result_err[i];
421  chisq[i] = chisq_result[i];
422  Nevent[i] = Nevent_TC[i];
423  }
424  timeoffsetave /= 576.0;
425 
426  tr -> Fill();
427  tf -> cd();
428  tr -> Write();
429  tf -> Close();
430 
431 }
432 
434 {
435  TCId.clear();
436  TCEnergy.clear();
437  TCTiming.clear();
438 
439 }
440 
442 {
443  chisq_result.assign(576, 0.0);
444  if (FlagMatrixSolved == 1) {
445  for (int i = 0; i < 576; i++) {
446  if (b_Inc_TC[i] == false && i != TC_ref - 1) continue;
447  std::vector<double> offsetdiff;
448  offsetdiff.assign(576, 0.0);
449  for (int j = 0; j < 576; j++) {
450  if (i == j || (b_Inc_TC[j] == false && j != TC_ref - 1)) continue;
451  offsetdiff[j] = tcal_result[i] - tcal_result[j];
452  chisq_result[i] += Chisq1[i][j] * offsetdiff[j] * offsetdiff[j] + Chisq2[i][j] * offsetdiff[j] + Chisq3[i][j];
453  }
454  }
455  } else {
456  for (int i = 0; i < 576; i++) {
457  chisq_result[i] = -1.0;
458  }
459  }
460 }
461 
463 {
464  if (fIteration != 0) {
465  TString str_fname_tmp = str_timeoffset_fname;
466  TFile* tf = new TFile(str_fname_tmp, "READ");
467  auto tr = (TTree*) tf -> Get("tree");
468  vector<double>* TimeOffset_tmp = 0;
469  tr -> SetBranchAddress("TimeOffset", &TimeOffset_tmp);
470  TimeOffset = *TimeOffset_tmp;
471  tr -> GetEntry(0);
472  tf -> Close();
473  }
474 }
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
int nevt_selected
The number of selected events.
std::vector< double > tcal_result_err
Time offset error.
std::vector< double > TCEnergy
TC energy.
void InitParams()
Parameters initialization.
void Save_Result()
Save time offset and chisq as a root file.
std::vector< std::vector< double > > Chisq1
chisq component1
TVectorD _Vector
Vector component.
std::vector< double > tcal_result
Time offset calibration result.
int fIteration
Flag iteration 0 : first calbiration 1 : iteration.
int fSimulation
Flag simulation 0 : real data 1 : simulation data.
virtual void initialize() override
initialize function
std::string str_timeoffset_fname
Input time offset file name (iteration mode)
int nevt_read
The number of read events.
virtual void event() override
Event function.
virtual void endRun() override
End Run function.
std::vector< bool > b_Exclude_TC
Flag TC excluded.
void Calculate_Chisq()
Calculate chisq based on time offset obtained from "Solve_Matrix" function.
void GetTimeOffset()
Get previous time offsets (iteration mode)
std::vector< TVector3 > TCPosition
TC position from TRGECLMap (cosmic calibration)
double TCEnergyCalibrationConstant
ADC to GeV energy conversion factor.
double cut_chisq
TC chisq cut (iteration mode)
void Set_TCposition()
Set TC position from TRGECLMap.
virtual void terminate() override
Terminate function.
double cut_high_energy
TC energy cut upper limit.
double cut_low_energy
TC energy cut lower limit.
std::vector< double > TCTiming
TC time.
std::string str_default_name
Default output root file name.
std::vector< int > Nevent_TC
The number of uesed TC hit (TC by TC)
virtual void beginRun() override
Begin Run function.
int fInclude_FWD
Flag include forward endcap 0 : exclude FWD 1 : include FWD.
std::vector< double > TimeOffset
Time offset Input time offset (iteration mode)
int fInclude_BWD
Flag include backward endcap 0 : exclude BWD 1 : include BWD.
int fInclude_BR
Flag include barrel 0 : exclude BR 1 : include BR.
int f3Dbhabha_veto
Flag 3D bhabha veto 0 : not use 1 : use.
int FillMatrix()
Fill matrix and vector components.
virtual ~TRGECLTimingCalModule()
Destructor.
void TRGECLTimingCalClear()
Clear vector every event.
std::vector< bool > b_Inc_TC
Flag TC included.
int TC_ref
Reference TC whose time offset set to be 0.
std::vector< std::vector< double > > Chisq3
chisq component3
std::vector< double > chisq_result
Chisq values based on time offset obtained from "Solve_Matrix" function.
int FlagMatrixSolved
Flag matrix solved (0 : not solved 1 : solved)
std::vector< std::vector< double > > Chisq2
chisq component2
std::vector< int > TCId
Trigger Cell TCID.
std::string str_ofilename
Output root file name.
TMatrixDSym _Matrix
Matrix Marix component.
int fCalType
Calibration type 0 : beam 1 : cosmic.
Example Detector.
Definition: TRGECLTrg.h:20
A class of TC Mapping.
Definition: TrgEclMapping.h:26
TVector3 getTCPosition(int)
TC position (cm)
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.