Belle II Software  release-08-01-10
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  int dTBin = 0;
106  int ShiftdTBin = 0;
107  int Nsmalldt = 10;
108  int SmallOffset = 1;
109  double IntervaldT = 125 * 0.001 / Nsmalldt;
110  // double EThreshold = _Threshold; //[MeV]
111  int FitSleepCounter = 100; // counter to suspend fit
112  int FitSleepThreshold = 2; // # of clk to suspend fit
113 
114  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
115 
116  for (int iShift = 20; iShift < (NSampling - 12); iShift++) { // In order to avoid BKG shaping effect, iShift start from 20.
117 
118  FitSleepCounter++;
119  if (FitSleepCounter <= FitSleepThreshold) {continue;}
120  for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
121  int iReplace = iFitSample + iShift;
122  TCFitSample[iFitSample] = digiEnergy[iTCIdm][iReplace] * 1000.0;
123  if (0) {
124  if (pedFlag == 1 && iFitSample < 4) {
125  // cppcheck-suppress uninitdata
126  TCFitSample[iFitSample] = preped[iFitSample];
127  pedFlag = 0;
128  }
129  }
130  }
131  //
132  //
133  //
134 
135  dTBin = (int)(ShiftdTBin + Nsmalldt);
136  if (dTBin < 1) {dTBin = 1;}
137  if (dTBin > 20) {dTBin = 20;}
138 
139  CoeffAAA = 0;
140  CoeffBBB = 0;
141  for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
142  CoeffAAA += CoeffNoise31[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
143  CoeffBBB += CoeffNoise32[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
144  }
145  double deltaT = CoeffBBB / CoeffAAA; // deltaT [us]
146 
147  ShiftdTBin = int(deltaT / IntervaldT + dTBin);
148 
149  double fitE = CoeffAAA;
150 
151  //-------
152  // Require "expected time" is around middle of table = Nsmalldt.
153  //-------
154  double condition_t = -(deltaT + dTBin * IntervaldT - fam_sampling_interval * 0.001);
155 
156  if (fabs(condition_t) < 0.8 * (fam_sampling_interval * 0.001) && fitE > Threshold[iTCIdm]) {
157  double fitT = condition_t + (SmallOffset + iShift + nbin_pedestal - 5) * (fam_sampling_interval * 0.001);
158 
159 
160  pedFlag = 1;
161  double rand_sampling_correction = digiTiming[iTCIdm][iShift] + (nbin_pedestal - iShift + 32) * fam_sampling_interval;
162 
163  TCFitEnergy[iTCIdm].push_back(fitE / 1000.0); // [GeV/c2]
164  TCFitTiming[iTCIdm].push_back(fitT * 1000 - 4000 + (_DataBase->GetTCFLatency(iTCIdm + 1)) + rand_sampling_correction);
165  FitSleepCounter = 0;
166  ShiftdTBin = 0;
167 
168  }
169  }
170  }
171  if (_BeamBkgTag == 1 || _AnaTag == 1) {
172  SetBeamBkgTag();
173  }
174  //
175  //
176  //
177  delete [] TCFitSample;
178  delete [] preped;
179  return;
180 }
181 //
182 //
183 //
184 void
185 TrgEclFAMFit::FAMFit02(std::vector<std::vector<double>> TCDigiE, std::vector<std::vector<double>> TCDigiT)
186 {
187 
188  int NSampling = 64;
189 
190  //==================
191  // Peak search
192  //==================
193  //@ T_a and T_b is time at sampling points in which 0.6*E exists.
194  int ta_id[20] = {1000}; //@ id of T_a
195  double ttt_a[20] = {0}; //@ time of T_a
196  double ttt_b[20] = {0}; //@ time of T_b
197  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
198  int noutput = 0;
199 
200  // double threshold = _Threshold * 0.001; //GeV
201  int maxId[500] = {0};
202  for (int iii = 0 ; iii < 500 ; iii++) {
203 
204  maxId[iii] = 0;
205  }
206  int count_up = 0;
207  int count_down = 0;
208  int flag_up = 3;
209  int flag_down = 3;
210 
211  double max = 0;
212  for (int iSampling = 1; iSampling < NSampling; iSampling++) {
213  //-------------------------------------------------------------------------
214  //Peak finding Method 1
215  //------------------------------------------------------------------------
216 
217  if (TCDigiE[iTCIdm][iSampling] >= max) {
218 
219  max = TCDigiE[iTCIdm][iSampling];
220  maxId[noutput] = iSampling;
221  count_up ++;
222  count_down = 0;
223  } else {
224  count_down++;
225  if (count_down >= flag_down) {
226  if (count_up >= flag_up) {
227  if (Threshold[iTCIdm] * 0.001 < max) {
228  max = 0;
229  count_up = 0;
230  count_down = 0;
231 
232  double NoiseLevel = 0;
233  double NoiseCount = 0;
234  for (int iNoise = 0; iNoise < 5; iNoise++) {
235  int iNoiseReplace = (maxId[noutput] - 10) + iNoise;
236  if (iNoiseReplace >= 0) {
237  NoiseLevel += TCDigiE[iTCIdm][iNoiseReplace];
238  NoiseCount++;
239  }
240  }
241  if (NoiseCount != 0) { NoiseLevel /= NoiseCount; }
242  //** Peak point is the Energy */
243  TCFitEnergy[iTCIdm].push_back(TCDigiE[iTCIdm][maxId[noutput]] - NoiseLevel);
244  if (!(maxId[noutput] - 1)) {
245  for (int jSampling = 1; jSampling < maxId[noutput] + 3; jSampling++) {
246  TCDigiE[iTCIdm][jSampling] -= NoiseLevel;
247  }
248  } else {
249  for (int jSampling = maxId[noutput] - 1; jSampling < maxId[noutput] + 3; jSampling++) {
250  TCDigiE[iTCIdm][jSampling] -= NoiseLevel;
251  }
252  }
253  //@ Search T_a ID
254  for (int iSearch = 0; iSearch < 5; iSearch++) {
255 
256  if (TCDigiE[iTCIdm][maxId[noutput] - iSearch] > 0.6 * TCFitEnergy[iTCIdm][noutput] &&
257  TCDigiE[iTCIdm][maxId[noutput] - iSearch - 1] < 0.6 * TCFitEnergy[iTCIdm][noutput]) {
258  ta_id[noutput] = maxId[noutput] - iSearch - 1;
259  }
260  }
261 
262  //@ Estimate timing of t0
263  if (ta_id[noutput] == 1000) {
264  printf("TrgEclFAMFit::digi02> Cannot find TC Timing (TCId=%5i, E=%8.5f)!!!\n", iTCIdm - 1, TCFitEnergy[iTCIdm][0]);
265  B2ERROR("TrgEclFAMFit::digi02> Cannot find TC Timing");
266  } else {
267  ttt_a[noutput] = TCDigiT[iTCIdm][ta_id[noutput]];
268  ttt_b[noutput] = TCDigiT[iTCIdm][ta_id[noutput] + 1];
269  TCFitTiming[iTCIdm].push_back((ttt_a[noutput] +
270  (0.6 * TCFitEnergy[iTCIdm][noutput] - TCDigiE[iTCIdm][ta_id[noutput]]) * (ttt_b[noutput] -
271  ttt_a[noutput])
272  / (TCDigiE[iTCIdm][ta_id[noutput] + 1] - TCDigiE[iTCIdm][ta_id[noutput]])) - (278.7 + 2) + (_DataBase->GetTCFLatency(iTCIdm + 1)));
273  //@ time between t0 and 0.6*peak_energy
274  //@ Alex's number = 274.4 (how he got this value ?)
275  //@ by my check = 278.7 [ns]
276  //@ here "+2" is a shift due to imperfectness of no-fit method.
277  }
278 
279  }
280  }
281  }
282  }
283  }
284  }
285  if (_BeamBkgTag == 1 || _AnaTag == 1) {
286  SetBeamBkgTag();
287  }
288 
289  //
290  //
291  //
292  return;
293 
294 }
295 //
296 //
297 //
298 void
299 TrgEclFAMFit::FAMFit03(std::vector<std::vector<double>> TCDigiEnergy, std::vector<std::vector<double>> TCDigiTiming)
300 {
301  //===============
302  // (03)Signal digitization (w/ 12ns interval for method-0)
303  //===============
304  // double cut_energy_tot = 0.03; // [GeV]
305  // int nbin_pedestal = 100;
306  // float fam_sampling_interval = 12; // [ns]
307  int NSampling = 666;
308 
309  //==================
310  // (03)Peak search
311  //==================
312  float max_shape_time = 563.48; // [ns], time between peak of PDF and t0.
313  // double threshold = _Threshold * 0.001; //GeV
314  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
315  int noutput = 0;
316  int maxId[500] = {0};
317  int count_up = 0;
318  int count_down = 0;
319  int flag_up = 30;;
320  int flag_down = 40;
321  double max = 0;
322  for (int iSampling = 1; iSampling < NSampling; iSampling++) {
323 
324  if (TCDigiEnergy[iTCIdm][iSampling] >= max) {
325 
326  max = TCDigiEnergy[iTCIdm][iSampling];
327  maxId[noutput] = iSampling;
328  count_up ++;
329  count_down = 0;
330  } else {
331  count_down++;
332  if (count_down >= flag_down) {
333  if (count_up >= flag_up) {
334  if (Threshold[iTCIdm] * 0.001 < max) {
335  max = 0;
336  count_up = 0;
337  count_down = 0;
338  //@ Remove noise effect
339  float NoiseLevel = 0;
340  float NoiseCount = 0;
341  for (int iNoise = 0; iNoise < 42; iNoise++) {
342  int iNoiseReplace = (maxId[noutput] - 88) + iNoise;
343  if (iNoiseReplace >= 0) {
344  NoiseLevel += TCDigiEnergy[iTCIdm][iNoiseReplace];
345  NoiseCount++;
346  }
347  }
348  if (NoiseCount != 0) { NoiseLevel /= NoiseCount; }
349  TCFitEnergy[iTCIdm].push_back(TCDigiEnergy[iTCIdm][maxId[noutput]] - NoiseLevel);
350  TCFitTiming[iTCIdm].push_back(TCDigiTiming[iTCIdm][maxId[noutput]] - max_shape_time + (_DataBase->GetTCFLatency(iTCIdm)));
351  noutput++;
352  }
353  }
354  }
355  }
356  }
357  }
358 
359  if (_BeamBkgTag == 1 || _AnaTag == 1) {
360  SetBeamBkgTag();
361  }
362 
363 
364  return;
365 }
366 //
367 //
368 //
369 void
371 {
372  std::vector<int> TCId;
373  std::vector<double> RawTCTiming;
374  std::vector<double> RawTCEnergy;
375  std::vector<double> RawBeamBkgTag;
376  RawBeamBkgTag.clear();
377  TCId.clear();
378  RawTCTiming.clear();
379  RawTCEnergy.clear();
380  // BeamBkgTag.resize(576 ,std::vector<int> (size,100));
381  // TCRawEnergy.resize(576,std::vector<double>(size,0.));
382  // TCRawTiming.resize(576,std::vector<double>(size,0.));
383  BeamBkgTag.resize(576);
384  TCRawEnergy.resize(576);
385  TCRawTiming.resize(576);
386 
387  StoreArray<TRGECLDigi0> trgeclDigiArray;
388  for (int ii = 0; ii < trgeclDigiArray.getEntries(); ii++) {
389  TRGECLDigi0* aTRGECLDigi = trgeclDigiArray[ii];
390  int eventid = aTRGECLDigi->getEventId();
391  if (EventId != eventid) {continue;}
392  TCId.push_back(aTRGECLDigi->getTCId());
393  RawTCTiming.push_back(aTRGECLDigi -> getRawTiming());
394  RawTCEnergy.push_back(aTRGECLDigi -> getRawEnergy());
395  RawBeamBkgTag.push_back(aTRGECLDigi -> getBeamBkgTag());
396  }
397 
398  for (int iTCId = 0; iTCId < 576 ; iTCId ++) {
399  const int hitsize = TCFitEnergy[iTCId].size();
400  for (int iHit = 0; iHit < hitsize; iHit++) {
401  const int rawsize = TCId.size();
402  for (int iDigi = 0; iDigi < rawsize; iDigi++) {
403  if (TCId[iDigi] != (iTCId + 1)) {continue;}
404  if (abs(TCFitEnergy[iTCId][iHit] - RawTCEnergy[iDigi]) < 12) {
405  if (abs(TCFitTiming[iTCId][iHit] - RawTCTiming[iDigi]) < 30) {
406  BeamBkgTag[iTCId].push_back(RawBeamBkgTag[iDigi]);
407  TCRawEnergy[iTCId].push_back(RawTCEnergy[iDigi]);
408  TCRawTiming[iTCId].push_back(RawTCTiming[iDigi]);
409  }
410  }
411  }
412  }
413  }
414 
415 }
416 //
417 //
418 //
419 void
420 TrgEclFAMFit::save(int m_nEvent)
421 {
422  //---------------
423  // Root Output
424  //---------------
425  int hitNum = 0;
426 
427  // adjust TC timing (ns) to T=0 ns
428  double tc_timing_correction = -15;
429 
430  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
431  const int hitsize = TCFitEnergy[iTCIdm].size();
432  for (int iHit = 0; iHit < hitsize; iHit++) {
433  StoreArray<TRGECLHit> TrgEclHitArray;
434  TrgEclHitArray.appendNew();
435  hitNum = TrgEclHitArray.getEntries() - 1;
436  TrgEclHitArray[hitNum]->setEventId(m_nEvent);
437  TrgEclHitArray[hitNum]->setTCId(iTCIdm + 1);
438  TrgEclHitArray[hitNum]->setEnergyDep(TCFitEnergy[iTCIdm][iHit]);
439  TrgEclHitArray[hitNum]->setTimeAve(TCFitTiming[iTCIdm][iHit] + tc_timing_correction);
440  if (_BeamBkgTag == 1) {
441  TrgEclHitArray[hitNum]->setBeamBkgTag(BeamBkgTag[iTCIdm][iHit]);
442  }
443  }
444  }
445 
446  if (_AnaTag == 1) {
447  for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
448  if (TCFitEnergy[iTCIdm].size() != TCRawEnergy[iTCIdm].size()) {continue;}
449  const int hitsize = TCFitEnergy[iTCIdm].size();
450  for (int iHit = 0; iHit < hitsize; iHit++) {
451  StoreArray<TRGECLFAMAna> TrgEclAnaArray;
452  TrgEclAnaArray.appendNew();
453  hitNum = TrgEclAnaArray.getEntries() - 1;
454  TrgEclAnaArray[hitNum]->setEventId(m_nEvent);
455  TrgEclAnaArray[hitNum]->setTCId(iTCIdm + 1);
456  TrgEclAnaArray[hitNum]->setPhiId(_TCMap->getTCPhiIdFromTCId(iTCIdm + 1));
457  TrgEclAnaArray[hitNum]->setThetaId(_TCMap->getTCThetaIdFromTCId(iTCIdm + 1));
458  TrgEclAnaArray[hitNum]->setRawEnergy(TCRawEnergy[iTCIdm][iHit]);
459  TrgEclAnaArray[hitNum]->setRawTiming(TCRawTiming[iTCIdm][iHit]);
460 
461  // TrgEclAnaArray[hitNum]->setFitEnergy(TCFitEnergy[iTCIdm][iHit]);
462  int p_ene2adc = 525;
463  int ene_i0 = (int)(TCFitEnergy[iTCIdm][iHit] * 100000.0 / p_ene2adc);
464  double ene_d = (double) ene_i0 * p_ene2adc / 100000;
465  TrgEclAnaArray[hitNum]->setFitEnergy(ene_d);
466 
467  TrgEclAnaArray[hitNum]->setFitTiming(TCFitTiming[iTCIdm][iHit] + tc_timing_correction);
468  TrgEclAnaArray[hitNum]->setBeamBkgTag(BeamBkgTag[iTCIdm][iHit]);
469  }
470  }
471  }
472  return;
473 }
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.