Belle II Software  release-06-00-14
TrgEclFAMFit.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 <framework/datastore/StoreArray.h>
10 
11 #include <framework/logging/Logger.h>
12 #include "trg/ecl/TrgEclFAMFit.h"
13 
14 #include "trg/ecl/dataobjects/TRGECLHit.h"
15 #include "trg/ecl/dataobjects/TRGECLDigi0.h"
16 #include "trg/ecl/dataobjects/TRGECLFAMAna.h"
17 
18 #include <stdlib.h>
19 #include <iostream>
20 #include <math.h>
21 
22 using namespace std;
23 using namespace Belle2;
24 //using namespace TRG;
25 //
26 //
27 //
28 TrgEclFAMFit::TrgEclFAMFit(): _BeamBkgTag(0), _AnaTag(0), EventId(0) //, bin(0)
29 {
30 
31  CoeffSigPDF0.clear();
32  CoeffSigPDF1.clear();
33  CoeffNoise31.clear();
34  CoeffNoise32.clear();
35  CoeffNoise33.clear();
36 
37  _TCMap = new TrgEclMapping();
38  _DataBase = new TrgEclDataBase();
39 
40  Threshold.clear();
41 
42  TCFitEnergy.clear();
43  TCFitTiming.clear();
44  TCRawEnergy.clear();
45  TCRawTiming.clear();
46  BeamBkgTag.clear();
47  TCLatency.clear();
48 
49  Threshold.resize(576, 100.0);
50  TCFitEnergy.resize(576);
51  TCFitTiming.resize(576);
52  TCRawEnergy.resize(576);
53  TCRawTiming.resize(576);
54  BeamBkgTag.resize(576);
55 }
56 //
57 //
58 //
60 {
61 
62  delete _TCMap;
63  delete _DataBase;
64 }
65 //
66 //
67 //
68 
69 void
70 TrgEclFAMFit::setup(int eventId)
71 {
72  //
73  // prepare coefficient for fitting
74  //
75 
76  _DataBase -> getCoeffSigPDF(CoeffSigPDF0 , CoeffSigPDF1);
77  _DataBase -> getCoeffNoise(0, CoeffNoise31 , CoeffNoise32, CoeffNoise33);
78 
79  EventId = eventId;
80  //
81  return;
82 }
83 
84 void
85 TrgEclFAMFit::FAMFit01(std::vector<std::vector<double>> digiEnergy, std::vector<std::vector<double>> digiTiming)
86 {
87  //============================
88  // In this function,
89  // o Energy unit must be [MeV/c2]
90  // o Time unit must be [us]
91  // but
92  // o return energy unit must be [GeV/c2]
93  // o return time unit must be [ns]
94  //============================
95  double* TCFitSample = new double [12]; // MeV/c2
96  double* preped = new double [4];
97 
98  int nbin_pedestal = 4;
99  int fam_sampling_interval = 125;
100  int NSampling = 64;// 8 [us] / 125 [ns]
101 
102  int pedFlag = 0;
103  double CoeffAAA = 0;
104  double CoeffBBB = 0;
105  double CoeffPPP = 0;
106  int dTBin = 0;
107  int ShiftdTBin = 0;
108  int Nsmalldt = 10;
109  int SmallOffset = 1;
110  double IntervaldT = 125 * 0.001 / Nsmalldt;
111  // double EThreshold = _Threshold; //[MeV]
112  int FitSleepCounter = 100; // counter to suspend fit
113  int FitSleepThreshold = 2; // # of clk to suspend fit
114  /* cppcheck-suppress variableScope */
115  double FitE;
116  /* cppcheck-suppress variableScope */
117  double FitT;
118 
119  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
120 
121  FitE = 0;
122  FitT = 0;
123  for (int iShift = 20; iShift < (NSampling - 12); iShift++) { // In order to avoid BKG shaping effect, iShift start from 20.
124 
125  FitSleepCounter++;
126  if (FitSleepCounter <= FitSleepThreshold) {continue;}
127  for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
128  int iReplace = iFitSample + iShift;
129  TCFitSample[iFitSample] = digiEnergy[iTCIdm][iReplace] * 1000.0;
130  if (0) {
131  if (pedFlag == 1 && iFitSample < 4) {
132  TCFitSample[iFitSample] = preped[iFitSample];
133  pedFlag = 0;
134  }
135  }
136  }
137  //
138  //
139  //
140 
141  dTBin = (int)(ShiftdTBin + Nsmalldt);
142  if (dTBin < 1) {dTBin = 1;}
143  if (dTBin > 20) {dTBin = 20;}
144 
145  CoeffAAA = 0;
146  CoeffBBB = 0;
147  CoeffPPP = 0;
148  for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
149  CoeffAAA += CoeffNoise31[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
150  CoeffBBB += CoeffNoise32[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
151  CoeffPPP += CoeffNoise33[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
152  }
153  double deltaT = CoeffBBB / CoeffAAA; // deltaT [us]
154 
155  ShiftdTBin = int(deltaT / IntervaldT + dTBin);
156 
157  FitE = CoeffAAA;
158 
159  //-------
160  // Require "expected time" is around middle of table = Nsmalldt.
161  //-------
162  double condition_t = -(deltaT + dTBin * IntervaldT - fam_sampling_interval * 0.001);
163 
164  if (fabs(condition_t) < 0.8 * (fam_sampling_interval * 0.001) && FitE > Threshold[iTCIdm]) {
165  FitT = condition_t + (SmallOffset + iShift + nbin_pedestal - 5) * (fam_sampling_interval * 0.001);
166 
167 
168  pedFlag = 1;
169  double rand_sampling_correction = digiTiming[iTCIdm][iShift] + (nbin_pedestal - iShift + 32) * fam_sampling_interval;
170 
171  TCFitEnergy[iTCIdm].push_back(FitE / 1000.0); // [GeV/c2]
172  TCFitTiming[iTCIdm].push_back(FitT * 1000 - 4000 + (_DataBase->GetTCFLatency(iTCIdm + 1)) + rand_sampling_correction);
173  FitSleepCounter = 0;
174  ShiftdTBin = 0;
175 
176  }
177  }
178  }
179  if (_BeamBkgTag == 1 || _AnaTag == 1) {
180  SetBeamBkgTag();
181  }
182  //
183  //
184  //
185  delete [] TCFitSample;
186  delete [] preped;
187  return;
188 }
189 //
190 //
191 //
192 void
193 TrgEclFAMFit::FAMFit02(std::vector<std::vector<double>> TCDigiE, std::vector<std::vector<double>> TCDigiT)
194 {
195 
196  int NSampling = 64;
197 
198  //==================
199  // Peak search
200  //==================
201  //@ T_a and T_b is time at sampling points in which 0.6*E exists.
202  int ta_id[20] = {1000}; //@ id of T_a
203  double ttt_a[20] = {0}; //@ time of T_a
204  double ttt_b[20] = {0}; //@ time of T_b
205  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
206  int noutput = 0;
207 
208  // double threshold = _Threshold * 0.001; //GeV
209  int maxId[500] = {0};
210  for (int iii = 0 ; iii < 500 ; iii++) {
211 
212  maxId[iii] = 0;
213  }
214  int count_up = 0;
215  int count_down = 0;
216  int flag_up = 3;
217  int flag_down = 3;
218 
219  double max = 0;
220  for (int iSampling = 1; iSampling < NSampling; iSampling++) {
221  //-------------------------------------------------------------------------
222  //Peak finding Method 1
223  //------------------------------------------------------------------------
224 
225  if (TCDigiE[iTCIdm][iSampling] >= max) {
226 
227  max = TCDigiE[iTCIdm][iSampling];
228  maxId[noutput] = iSampling;
229  count_up ++;
230  count_down = 0;
231  } else {
232  count_down++;
233  if (count_down >= flag_down) {
234  if (count_up >= flag_up) {
235  if (Threshold[iTCIdm] * 0.001 < max) {
236  max = 0;
237  count_up = 0;
238  count_down = 0;
239 
240  double NoiseLevel = 0;
241  double NoiseCount = 0;
242  for (int iNoise = 0; iNoise < 5; iNoise++) {
243  int iNoiseReplace = (maxId[noutput] - 10) + iNoise;
244  if (iNoiseReplace >= 0) {
245  NoiseLevel += TCDigiE[iTCIdm][iNoiseReplace];
246  NoiseCount++;
247  }
248  }
249  if (NoiseCount != 0) { NoiseLevel /= NoiseCount; }
250  //** Peak point is the Energy */
251  TCFitEnergy[iTCIdm].push_back(TCDigiE[iTCIdm][maxId[noutput]] - NoiseLevel);
252  if (!(maxId[noutput] - 1)) {
253  for (int jSampling = 1; jSampling < maxId[noutput] + 3; jSampling++) {
254  TCDigiE[iTCIdm][jSampling] -= NoiseLevel;
255  }
256  } else {
257  for (int jSampling = maxId[noutput] - 1; jSampling < maxId[noutput] + 3; jSampling++) {
258  TCDigiE[iTCIdm][jSampling] -= NoiseLevel;
259  }
260  }
261  //@ Search T_a ID
262  for (int iSearch = 0; iSearch < 5; iSearch++) {
263 
264  if (TCDigiE[iTCIdm][maxId[noutput] - iSearch] > 0.6 * TCFitEnergy[iTCIdm][noutput] &&
265  TCDigiE[iTCIdm][maxId[noutput] - iSearch - 1] < 0.6 * TCFitEnergy[iTCIdm][noutput]) {
266  ta_id[noutput] = maxId[noutput] - iSearch - 1;
267  }
268  }
269 
270  //@ Estimate timing of t0
271  if (ta_id[noutput] == 1000) {
272  printf("TrgEclFAMFit::digi02> Cannot find TC Timing (TCId=%5i, E=%8.5f)!!!\n", iTCIdm - 1, TCFitEnergy[iTCIdm][0]);
273  B2ERROR("TrgEclFAMFit::digi02> Cannot find TC Timing");
274  } else {
275  ttt_a[noutput] = TCDigiT[iTCIdm][ta_id[noutput]];
276  ttt_b[noutput] = TCDigiT[iTCIdm][ta_id[noutput] + 1];
277  TCFitTiming[iTCIdm].push_back((ttt_a[noutput] +
278  (0.6 * TCFitEnergy[iTCIdm][noutput] - TCDigiE[iTCIdm][ta_id[noutput]]) * (ttt_b[noutput] -
279  ttt_a[noutput])
280  / (TCDigiE[iTCIdm][ta_id[noutput] + 1] - TCDigiE[iTCIdm][ta_id[noutput]])) - (278.7 + 2) + (_DataBase->GetTCFLatency(iTCIdm + 1)));
281  //@ time between t0 and 0.6*peak_energy
282  //@ Alex's number = 274.4 (how he got this value ?)
283  //@ by my check = 278.7 [ns]
284  //@ here "+2" is a shift due to imperfectness of no-fit method.
285  }
286 
287  }
288  }
289  }
290  }
291  }
292  }
293  if (_BeamBkgTag == 1 || _AnaTag == 1) {
294  SetBeamBkgTag();
295  }
296 
297  //
298  //
299  //
300  return;
301 
302 }
303 //
304 //
305 //
306 void
307 TrgEclFAMFit::FAMFit03(std::vector<std::vector<double>> TCDigiEnergy, std::vector<std::vector<double>> TCDigiTiming)
308 {
309  //===============
310  // (03)Signal digitization (w/ 12ns interval for method-0)
311  //===============
312  // double cut_energy_tot = 0.03; // [GeV]
313  // int nbin_pedestal = 100;
314  // float fam_sampling_interval = 12; // [ns]
315  int NSampling = 666;
316 
317  //==================
318  // (03)Peak search
319  //==================
320  float max_shape_time = 563.48; // [ns], time between peak of PDF and t0.
321  // double threshold = _Threshold * 0.001; //GeV
322  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
323  int noutput = 0;
324  int maxId[500] = {0};
325  int count_up = 0;
326  int count_down = 0;
327  int flag_up = 30;;
328  int flag_down = 40;
329  double max = 0;
330  for (int iSampling = 1; iSampling < NSampling; iSampling++) {
331 
332  if (TCDigiEnergy[iTCIdm][iSampling] >= max) {
333 
334  max = TCDigiEnergy[iTCIdm][iSampling];
335  maxId[noutput] = iSampling;
336  count_up ++;
337  count_down = 0;
338  } else {
339  count_down++;
340  if (count_down >= flag_down) {
341  if (count_up >= flag_up) {
342  if (Threshold[iTCIdm] * 0.001 < max) {
343  max = 0;
344  count_up = 0;
345  count_down = 0;
346  //@ Remove noise effect
347  float NoiseLevel = 0;
348  float NoiseCount = 0;
349  for (int iNoise = 0; iNoise < 42; iNoise++) {
350  int iNoiseReplace = (maxId[noutput] - 88) + iNoise;
351  if (iNoiseReplace >= 0) {
352  NoiseLevel += TCDigiEnergy[iTCIdm][iNoiseReplace];
353  NoiseCount++;
354  }
355  }
356  if (NoiseCount != 0) { NoiseLevel /= NoiseCount; }
357  TCFitEnergy[iTCIdm].push_back(TCDigiEnergy[iTCIdm][maxId[noutput]] - NoiseLevel);
358  TCFitTiming[iTCIdm].push_back(TCDigiTiming[iTCIdm][maxId[noutput]] - max_shape_time + (_DataBase->GetTCFLatency(iTCIdm)));
359  noutput++;
360  }
361  }
362  }
363  }
364  }
365  }
366 
367  if (_BeamBkgTag == 1 || _AnaTag == 1) {
368  SetBeamBkgTag();
369  }
370 
371 
372  return;
373 }
374 //
375 //
376 //
377 void
379 {
380  std::vector<int> TCId;
381  std::vector<double> RawTCTiming;
382  std::vector<double> RawTCEnergy;
383  std::vector<double> RawBeamBkgTag;
384  RawBeamBkgTag.clear();
385  TCId.clear();
386  RawTCTiming.clear();
387  RawTCEnergy.clear();
388  // BeamBkgTag.resize(576 ,std::vector<int> (size,100));
389  // TCRawEnergy.resize(576,std::vector<double>(size,0.));
390  // TCRawTiming.resize(576,std::vector<double>(size,0.));
391  BeamBkgTag.resize(576);
392  TCRawEnergy.resize(576);
393  TCRawTiming.resize(576);
394 
395  StoreArray<TRGECLDigi0> trgeclDigiArray;
396  for (int ii = 0; ii < trgeclDigiArray.getEntries(); ii++) {
397  TRGECLDigi0* aTRGECLDigi = trgeclDigiArray[ii];
398  int eventid = aTRGECLDigi->getEventId();
399  if (EventId != eventid) {continue;}
400  TCId.push_back(aTRGECLDigi->getTCId());
401  RawTCTiming.push_back(aTRGECLDigi -> getRawTiming());
402  RawTCEnergy.push_back(aTRGECLDigi -> getRawEnergy());
403  RawBeamBkgTag.push_back(aTRGECLDigi -> getBeamBkgTag());
404  }
405 
406  for (int iTCId = 0; iTCId < 576 ; iTCId ++) {
407  const int hitsize = TCFitEnergy[iTCId].size();
408  for (int iHit = 0; iHit < hitsize; iHit++) {
409  const int rawsize = TCId.size();
410  for (int iDigi = 0; iDigi < rawsize; iDigi++) {
411  if (TCId[iDigi] != (iTCId + 1)) {continue;}
412  if (abs(TCFitEnergy[iTCId][iHit] - RawTCEnergy[iDigi]) < 12) {
413  if (abs(TCFitTiming[iTCId][iHit] - RawTCTiming[iDigi]) < 30) {
414  BeamBkgTag[iTCId].push_back(RawBeamBkgTag[iDigi]);
415  TCRawEnergy[iTCId].push_back(RawTCEnergy[iDigi]);
416  TCRawTiming[iTCId].push_back(RawTCTiming[iDigi]);
417  }
418  }
419  }
420  }
421  }
422 
423 }
424 //
425 //
426 //
427 void
428 TrgEclFAMFit::save(int m_nEvent)
429 {
430  //---------------
431  // Root Output
432  //---------------
433  int m_hitNum = 0;
434 
435  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
436  const int hitsize = TCFitEnergy[iTCIdm].size();
437  for (int iHit = 0; iHit < hitsize; iHit++) {
438  StoreArray<TRGECLHit> TrgEclHitArray;
439  TrgEclHitArray.appendNew();
440  m_hitNum = TrgEclHitArray.getEntries() - 1;
441  TrgEclHitArray[m_hitNum]->setEventId(m_nEvent);
442  TrgEclHitArray[m_hitNum]->setTCId(iTCIdm + 1);
443  TrgEclHitArray[m_hitNum]->setEnergyDep(TCFitEnergy[iTCIdm][iHit]);
444  TrgEclHitArray[m_hitNum]->setTimeAve(TCFitTiming[iTCIdm][iHit]);
445  if (_BeamBkgTag == 1) {
446  TrgEclHitArray[m_hitNum]->setBeamBkgTag(BeamBkgTag[iTCIdm][iHit]);
447  }
448  }
449  }
450 
451  m_hitNum = 0;
452  if (_AnaTag == 1) {
453  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
454  if (TCFitEnergy[iTCIdm].size() != TCRawEnergy[iTCIdm].size()) {continue;}
455  const int hitsize = TCFitEnergy[iTCIdm].size();
456  for (int iHit = 0; iHit < hitsize; iHit++) {
457  StoreArray<TRGECLFAMAna> TrgEclAnaArray;
458  TrgEclAnaArray.appendNew();
459  m_hitNum = TrgEclAnaArray.getEntries() - 1;
460  TrgEclAnaArray[m_hitNum]->setEventId(m_nEvent);
461  TrgEclAnaArray[m_hitNum]->setTCId(iTCIdm + 1);
462  TrgEclAnaArray[m_hitNum]->setPhiId(_TCMap->getTCPhiIdFromTCId(iTCIdm + 1));
463  TrgEclAnaArray[m_hitNum]->setThetaId(_TCMap->getTCThetaIdFromTCId(iTCIdm + 1));
464  TrgEclAnaArray[m_hitNum]->setRawEnergy(TCRawEnergy[iTCIdm][iHit]);
465  TrgEclAnaArray[m_hitNum]->setRawTiming(TCRawTiming[iTCIdm][iHit]);
466 
467  // TrgEclAnaArray[m_hitNum]->setFitEnergy(TCFitEnergy[iTCIdm][iHit]);
468  int p_ene2adc = 525;
469  int ene_i0 = (int)(TCFitEnergy[iTCIdm][iHit] * 100000.0 / p_ene2adc);
470  double ene_d = (double) ene_i0 * p_ene2adc / 100000;
471  TrgEclAnaArray[m_hitNum]->setFitEnergy(ene_d);
472 
473  TrgEclAnaArray[m_hitNum]->setFitTiming(TCFitTiming[iTCIdm][iHit]);
474  TrgEclAnaArray[m_hitNum]->setBeamBkgTag(BeamBkgTag[iTCIdm][iHit]);
475  }
476  }
477  }
478  return;
479 }
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
Raw TC result nefor digitizing.
Definition: TRGECLDigi0.h:21
int getEventId() const
Get event id.
Definition: TRGECLDigi0.h:55
int getTCId() const
Get TC id.
Definition: TRGECLDigi0.h:57
class TrgEclDataBase;
double GetTCFLatency(int)
TC flight time latency
int _BeamBkgTag
Add beambkg.
Definition: TrgEclFAMFit.h:96
std::vector< std::vector< double > > CoeffSigPDF1
Coeffisients of signal PDF1.
Definition: TrgEclFAMFit.h:85
void save(int)
save fitting result into tables
std::vector< std::vector< int > > BeamBkgTag
fit timing
Definition: TrgEclFAMFit.h:70
int _AnaTag
Fill Analysis table.
Definition: TrgEclFAMFit.h:98
void setup(int)
setup fam module
Definition: TrgEclFAMFit.cc:70
std::vector< std::vector< double > > CoeffNoise32
Coeffisient of noise 2.
Definition: TrgEclFAMFit.h:89
std::vector< std::vector< double > > TCFitEnergy
fit energy
Definition: TrgEclFAMFit.h:66
std::vector< std::vector< double > > CoeffSigPDF0
Coeffisients of signal PDF0
Definition: TrgEclFAMFit.h:83
std::vector< std::vector< double > > TCFitTiming
fit timing
Definition: TrgEclFAMFit.h:68
TrgEclDataBase * _DataBase
Object of DataBase.
Definition: TrgEclFAMFit.h:80
std::vector< std::vector< double > > TCRawTiming
Raw timing.
Definition: TrgEclFAMFit.h:74
std::vector< std::vector< double > > CoeffNoise31
Coeffisients of noise 1.
Definition: TrgEclFAMFit.h:87
void FAMFit03(std::vector< std::vector< double >>, std::vector< std::vector< double >>)
function for backup2
void SetBeamBkgTag()
Set Beam Background Tag.
virtual ~TrgEclFAMFit()
Destructor.
Definition: TrgEclFAMFit.cc:59
std::vector< std::vector< double > > CoeffNoise33
Coeffisient of noise 3
Definition: TrgEclFAMFit.h:91
std::vector< double > TCLatency
TC Latency.
Definition: TrgEclFAMFit.h:93
void FAMFit01(std::vector< std::vector< double >>, std::vector< std::vector< double >>)
function for fitting
Definition: TrgEclFAMFit.cc:85
std::vector< std::vector< double > > TCRawEnergy
Raw energy.
Definition: TrgEclFAMFit.h:72
void FAMFit02(std::vector< std::vector< double >>, std::vector< std::vector< double >>)
function for backup 1
int EventId
Fill Analysis table.
Definition: TrgEclFAMFit.h:102
std::vector< int > Threshold
Threshold (MeV)
Definition: TrgEclFAMFit.h:100
TrgEclMapping * _TCMap
Object of TC Mapping.
Definition: TrgEclFAMFit.h:78
A class of TC Mapping.
Definition: TrgEclMapping.h:26
int getTCThetaIdFromTCId(int)
get [TC Theta ID] from [TC ID]
int getTCPhiIdFromTCId(int)
get [TC Phi ID] from [TC ID]
Abstract base class for different kinds of events.