Belle II Software development
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
22using namespace std;
23using namespace Belle2;
24//using namespace TRG;
25//
26//
27//
29{
30
31 m_CoeffSigPDF0.clear();
32 m_CoeffSigPDF1.clear();
33 m_CoeffNoise31.clear();
34 m_CoeffNoise32.clear();
35 m_CoeffNoise33.clear();
36
37 m_TCMap = new TrgEclMapping();
39
40 m_TCEThreshold.clear();
41
42 m_TCFitEnergy.clear();
43 m_TCFitTiming.clear();
44 m_TCRawEnergy.clear();
45 m_TCRawTiming.clear();
46 m_BeamBkgInfo.clear();
47 m_TCLatency.clear();
48
49 m_TCEThreshold.resize(576, 100.0);
50 m_TCFitEnergy.resize(576);
51 m_TCFitTiming.resize(576);
52 m_TCRawEnergy.resize(576);
53 m_TCRawTiming.resize(576);
54 m_BeamBkgInfo.resize(576);
55}
56//
57//
58//
60{
61
62 delete m_TCMap;
63 delete m_DataBase;
64}
65//
66//
67//
68
69void
71{
72 //
73 // prepare coefficient for fitting
74 //
75
78
79 m_EventId = eventId;
80 //
81 return;
82}
83
84void
85TrgEclFAMFit::FAMFit01(std::vector<std::vector<double>> digiEnergy,
86 std::vector<std::vector<double>> digiTiming)
87{
88 //============================
89 // In this function,
90 // o Energy unit must be [MeV/c2]
91 // o Time unit must be [us]
92 // but
93 // o return energy unit must be [GeV/c2]
94 // o return time unit must be [ns]
95 //============================
96 double* TCFitSample = new double [12]; // MeV/c2
97 double* preped = new double [4];
98
99 int nbin_pedestal = 4;
100 int fam_sampling_interval = 125;
101 int NSampling = 64;// 8 [us] / 125 [ns]
102
103 int pedFlag = 0;
104 double CoeffAAA = 0;
105 double CoeffBBB = 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 = m_Threshold; //[MeV]
112 int FitSleepCounter = 100; // counter to suspend fit
113 int FitSleepThreshold = 12; // # of clk to suspend fit
114
115 for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
116
117 for (int iShift = 20; iShift < (NSampling - 12); iShift++) { // In order to avoid BKG shaping effect, iShift start from 20.
118
119 FitSleepCounter++;
120 if (FitSleepCounter <= FitSleepThreshold) {continue;}
121 for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
122 int iReplace = iFitSample + iShift;
123 TCFitSample[iFitSample] = digiEnergy[iTCIdm][iReplace] * 1000.0;
124 if (0) {
125 if (pedFlag == 1 && iFitSample < 4) {
126 // cppcheck-suppress uninitdata
127 TCFitSample[iFitSample] = preped[iFitSample];
128 pedFlag = 0;
129 }
130 }
131 }
132 //
133 //
134 //
135
136 dTBin = (int)(ShiftdTBin + Nsmalldt);
137 if (dTBin < 1) {dTBin = 1;}
138 if (dTBin > 20) {dTBin = 20;}
139
140 CoeffAAA = 0;
141 CoeffBBB = 0;
142 for (int iFitSample = 0; iFitSample < 12; iFitSample++) {
143 CoeffAAA += m_CoeffNoise31[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
144 CoeffBBB += m_CoeffNoise32[dTBin - 1][iFitSample] * TCFitSample[iFitSample];
145 }
146 double deltaT = CoeffBBB / CoeffAAA; // deltaT [us]
147
148 ShiftdTBin = int(deltaT / IntervaldT + dTBin);
149
150 double fitE = CoeffAAA;
151
152 //-------
153 // Require "expected time" is around middle of table = Nsmalldt.
154 //-------
155 double condition_t = -(deltaT + dTBin * IntervaldT - fam_sampling_interval * 0.001);
156
157 if (fabs(condition_t) < 0.8 * (fam_sampling_interval * 0.001) && fitE > m_TCEThreshold[iTCIdm]) {
158 double fitT =
159 condition_t +
160 (SmallOffset + iShift + nbin_pedestal - 5) * (fam_sampling_interval * 0.001);
161
162
163 pedFlag = 1;
164 double rand_sampling_correction =
165 digiTiming[iTCIdm][iShift] +
166 (nbin_pedestal - iShift + 32) * fam_sampling_interval;
167
168 m_TCFitEnergy[iTCIdm].push_back(fitE / 1000.0); // [GeV/c2]
169 m_TCFitTiming[iTCIdm].push_back(fitT * 1000 - 4000 +
170 (m_DataBase->getTCFLatency(iTCIdm + 1)) +
171 rand_sampling_correction);
172 FitSleepCounter = 0;
173 ShiftdTBin = 0;
174
175 }
176 }
177 }
178 if (m_BeamBkgTag == 1 || m_AnaTag == 1) {
180 }
181 //
182 //
183 //
184 delete [] TCFitSample;
185 delete [] preped;
186 return;
187}
188//
189//
190//
191void
192TrgEclFAMFit::FAMFit02(std::vector<std::vector<double>> TCDigiE,
193 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 = m_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 (m_TCEThreshold[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 m_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 * m_TCFitEnergy[iTCIdm][noutput] &&
265 TCDigiE[iTCIdm][maxId[noutput] - iSearch - 1] < 0.6 * m_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, m_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 m_TCFitTiming[iTCIdm].push_back((ttt_a[noutput] +
278 (0.6 * m_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) + (m_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 (m_BeamBkgTag == 1 || m_AnaTag == 1) {
295 }
296
297 //
298 //
299 //
300 return;
301
302}
303//
304//
305//
306void
307TrgEclFAMFit::FAMFit03(std::vector<std::vector<double>> TCDigiEnergy,
308 std::vector<std::vector<double>> TCDigiTiming)
309{
310 //===============
311 // (03)Signal digitization (w/ 12ns interval for method-0)
312 //===============
313 // double cut_energy_tot = 0.03; // [GeV]
314 // int nbin_pedestal = 100;
315 // float fam_sampling_interval = 12; // [ns]
316 int NSampling = 666;
317
318 //==================
319 // (03)Peak search
320 //==================
321 float max_shape_time = 563.48; // [ns], time between peak of PDF and t0.
322 // double threshold = m_Threshold * 0.001; //GeV
323 for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
324 int noutput = 0;
325 int maxId[500] = {0};
326 int count_up = 0;
327 int count_down = 0;
328 int flag_up = 30;;
329 int flag_down = 40;
330 double max = 0;
331 for (int iSampling = 1; iSampling < NSampling; iSampling++) {
332
333 if (TCDigiEnergy[iTCIdm][iSampling] >= max) {
334
335 max = TCDigiEnergy[iTCIdm][iSampling];
336 maxId[noutput] = iSampling;
337 count_up ++;
338 count_down = 0;
339 } else {
340 count_down++;
341 if (count_down >= flag_down) {
342 if (count_up >= flag_up) {
343 if (m_TCEThreshold[iTCIdm] * 0.001 < max) {
344 max = 0;
345 count_up = 0;
346 count_down = 0;
347 //@ Remove noise effect
348 float NoiseLevel = 0;
349 float NoiseCount = 0;
350 for (int iNoise = 0; iNoise < 42; iNoise++) {
351 int iNoiseReplace = (maxId[noutput] - 88) + iNoise;
352 if (iNoiseReplace >= 0) {
353 NoiseLevel += TCDigiEnergy[iTCIdm][iNoiseReplace];
354 NoiseCount++;
355 }
356 }
357 if (NoiseCount != 0) { NoiseLevel /= NoiseCount; }
358 m_TCFitEnergy[iTCIdm].push_back(TCDigiEnergy[iTCIdm][maxId[noutput]] - NoiseLevel);
359 m_TCFitTiming[iTCIdm].push_back(TCDigiTiming[iTCIdm][maxId[noutput]] - max_shape_time + (m_DataBase->getTCFLatency(iTCIdm)));
360 noutput++;
361 }
362 }
363 }
364 }
365 }
366 }
367
368 if (m_BeamBkgTag == 1 || m_AnaTag == 1) {
370 }
371
372
373 return;
374}
375//
376//
377//
378void
380{
381 std::vector<int> TCId;
382 std::vector<double> RawTCTiming;
383 std::vector<double> RawTCEnergy;
384 std::vector<double> RawBeamBkgTag;
385 RawBeamBkgTag.clear();
386 TCId.clear();
387 RawTCTiming.clear();
388 RawTCEnergy.clear();
389 // BeamBkgTag.resize(576 ,std::vector<int> (size,100));
390 // m_TCRawEnergy.resize(576,std::vector<double>(size,0.));
391 // m_TCRawTiming.resize(576,std::vector<double>(size,0.));
392 m_BeamBkgInfo.resize(576);
393 m_TCRawEnergy.resize(576);
394 m_TCRawTiming.resize(576);
395
396 StoreArray<TRGECLDigi0> trgeclDigiArray;
397 for (int ii = 0; ii < trgeclDigiArray.getEntries(); ii++) {
398 TRGECLDigi0* aTRGECLDigi = trgeclDigiArray[ii];
399 int eventid = aTRGECLDigi->getEventId();
400 if (m_EventId != eventid) {continue;}
401 TCId.push_back(aTRGECLDigi->getTCId());
402 RawTCTiming.push_back(aTRGECLDigi -> getRawTiming());
403 RawTCEnergy.push_back(aTRGECLDigi -> getRawEnergy());
404 RawBeamBkgTag.push_back(aTRGECLDigi -> getBeamBkgTag());
405 }
406
407 for (int iTCId = 0; iTCId < 576 ; iTCId ++) {
408 const int hitsize = m_TCFitEnergy[iTCId].size();
409 for (int iHit = 0; iHit < hitsize; iHit++) {
410 const int rawsize = TCId.size();
411 for (int iDigi = 0; iDigi < rawsize; iDigi++) {
412 if (TCId[iDigi] != (iTCId + 1)) {continue;}
413 if (abs(m_TCFitEnergy[iTCId][iHit] - RawTCEnergy[iDigi]) < 12) {
414 if (abs(m_TCFitTiming[iTCId][iHit] - RawTCTiming[iDigi]) < 30) {
415 m_BeamBkgInfo[iTCId].push_back(RawBeamBkgTag[iDigi]);
416 m_TCRawEnergy[iTCId].push_back(RawTCEnergy[iDigi]);
417 m_TCRawTiming[iTCId].push_back(RawTCTiming[iDigi]);
418 }
419 }
420 }
421 }
422 }
423
424}
425//
426//
427//
428void
430{
431 //---------------
432 // Root Output
433 //---------------
434 int hitNum = 0;
435
436 // adjust TC timing (ns) to T=0 ns
437 double tc_timing_correction = -15;
438
439 for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
440 const int hitsize = m_TCFitEnergy[iTCIdm].size();
441 for (int iHit = 0; iHit < hitsize; iHit++) {
442 StoreArray<TRGECLHit> TrgEclHitArray;
443 TrgEclHitArray.appendNew();
444 hitNum = TrgEclHitArray.getEntries() - 1;
445 TrgEclHitArray[hitNum]->setEventId(eventid);
446 TrgEclHitArray[hitNum]->setTCId(iTCIdm + 1);
447 TrgEclHitArray[hitNum]->setEnergyDep(m_TCFitEnergy[iTCIdm][iHit]);
448 TrgEclHitArray[hitNum]->setTimeAve(m_TCFitTiming[iTCIdm][iHit] + tc_timing_correction);
449 if (m_BeamBkgTag == 1) {
450 TrgEclHitArray[hitNum]->setBeamBkgTag(m_BeamBkgInfo[iTCIdm][iHit]);
451 }
452 }
453 }
454
455 if (m_AnaTag == 1) {
456 for (int iTCIdm = 0; iTCIdm < 576; iTCIdm++) {
457 if (m_TCFitEnergy[iTCIdm].size() != m_TCRawEnergy[iTCIdm].size()) {continue;}
458 const int hitsize = m_TCFitEnergy[iTCIdm].size();
459 for (int iHit = 0; iHit < hitsize; iHit++) {
460 StoreArray<TRGECLFAMAna> TrgEclAnaArray;
461 TrgEclAnaArray.appendNew();
462 hitNum = TrgEclAnaArray.getEntries() - 1;
463 TrgEclAnaArray[hitNum]->setEventId(eventid);
464 TrgEclAnaArray[hitNum]->setTCId(iTCIdm + 1);
465 TrgEclAnaArray[hitNum]->setPhiId(m_TCMap->getTCPhiIdFromTCId(iTCIdm + 1));
466 TrgEclAnaArray[hitNum]->setThetaId(m_TCMap->getTCThetaIdFromTCId(iTCIdm + 1));
467 TrgEclAnaArray[hitNum]->setRawEnergy(m_TCRawEnergy[iTCIdm][iHit]);
468 TrgEclAnaArray[hitNum]->setRawTiming(m_TCRawTiming[iTCIdm][iHit]);
469
470 // TrgEclAnaArray[hitNum]->setFitEnergy(m_TCFitEnergy[iTCIdm][iHit]);
471 int p_ene2adc = 525;
472 int ene_i0 = (int)(m_TCFitEnergy[iTCIdm][iHit] * 100000.0 / p_ene2adc);
473 double ene_d = (double) ene_i0 * p_ene2adc / 100000;
474 TrgEclAnaArray[hitNum]->setFitEnergy(ene_d);
475
476 TrgEclAnaArray[hitNum]->setFitTiming(m_TCFitTiming[iTCIdm][iHit] + tc_timing_correction);
477 TrgEclAnaArray[hitNum]->setBeamBkgTag(m_BeamBkgInfo[iTCIdm][iHit]);
478 }
479 }
480 }
481 return;
482}
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;
int m_AnaTag
Fill Analysis table.
std::vector< std::vector< double > > m_TCFitEnergy
fit energy
void save(int)
save fitting result into tables
void setup(int)
setup fam module
TrgEclFAMFit()
Constructor.
std::vector< int > m_TCEThreshold
Threshold (MeV)
int m_BeamBkgTag
Add beambkg.
std::vector< std::vector< double > > m_CoeffNoise32
Coeffisient of noise 2.
void setBeamBkgTag()
set Beam Background Tag
std::vector< std::vector< double > > m_CoeffSigPDF1
Coeffisients of signal PDF1.
std::vector< double > m_TCLatency
TC Latency.
std::vector< std::vector< int > > getBeamBkgTag()
Get Background Tag of TC Hit.
std::vector< std::vector< double > > m_CoeffNoise31
Coeffisients of noise 1.
virtual ~TrgEclFAMFit()
Destructor.
TrgEclDataBase * m_DataBase
Object of DataBase.
void FAMFit02(std::vector< std::vector< double > >, std::vector< std::vector< double > >)
function for backup 1
void FAMFit01(std::vector< std::vector< double > >, std::vector< std::vector< double > >)
function for fitting
std::vector< std::vector< double > > m_TCFitTiming
fit timing
std::vector< std::vector< double > > m_TCRawTiming
Raw timing.
int m_EventId
Fill Analysis table.
std::vector< std::vector< double > > m_CoeffNoise33
Coeffisient of noise 3.
void FAMFit03(std::vector< std::vector< double > >, std::vector< std::vector< double > >)
function for backup2
std::vector< std::vector< double > > m_TCRawEnergy
Raw energy.
std::vector< std::vector< int > > m_BeamBkgInfo
fit timing
std::vector< std::vector< double > > m_CoeffSigPDF0
Coeffisients of signal PDF0.
TrgEclMapping * m_TCMap
Object of TC Mapping.
A class of TC Mapping.
Abstract base class for different kinds of events.
STL namespace.