-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathEventTimeMaker.cxx
More file actions
406 lines (336 loc) · 12.7 KB
/
EventTimeMaker.cxx
File metadata and controls
406 lines (336 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
///
/// \file EventTimeMaker.cxx
/// \author Francesca Ercolessi francesca.ercolessi@cern.ch
/// \author Francesco Noferini francesco.noferini@cern.ch
/// \author Nicolò Jacazio nicolo.jacazio@cern.ch
/// \brief Implementation of the TOF event time maker
///
#include "TRandom.h"
#include "TMath.h"
#include "TOFBase/EventTimeMaker.h"
namespace o2
{
namespace tof
{
constexpr int MAXNTRACKINSET = 10;
// usefull constants
constexpr unsigned long combinatorial[MAXNTRACKINSET + 1] = {1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049};
//---------------
void computeEvTime(const std::vector<eventTimeTrack>& tracks, const std::vector<int>& trkIndex, eventTimeContainer& evtime)
{
static constexpr int maxNumberOfSets = 200;
static constexpr float weightLimit = 1E-6; // Limit in the weights
const int ntracks = tracks.size();
LOG(debug) << "For the collision time using " << ntracks << " tracks";
if (ntracks < 2) { // at least 2 tracks required
LOG(debug) << "Skipping event because at least 2 tracks are required";
return;
}
int hypo[MAXNTRACKINSET];
int nmaxtracksinset = ntracks > 22 ? 6 : MAXNTRACKINSET; // max number of tracks in a set for event time computation
LOG(debug) << "nmaxtracksinset " << nmaxtracksinset;
int ntracksinset = std::min(ntracks, nmaxtracksinset);
LOG(debug) << "ntracksinset " << ntracksinset;
int nset = ((ntracks - 1) / ntracksinset) + 1;
LOG(debug) << "nset " << nset;
int ntrackUsable = ntracks;
LOG(debug) << "ntrackUsable " << ntrackUsable;
if (nset > maxNumberOfSets) {
nset = maxNumberOfSets;
LOG(debug) << "resetting nset " << nset;
ntrackUsable = nmaxtracksinset * nset;
LOG(debug) << "resetting ntrackUsable " << ntrackUsable;
}
// list of tracks in set
std::vector<int> trackInSet[maxNumberOfSets];
for (int i = 0; i < ntrackUsable; i++) {
int iset = i % nset;
trackInSet[iset].push_back(i);
}
int status;
// compute event time for each set
for (int iset = 0; iset < nset; iset++) {
LOG(debug) << "iset " << iset << " has size " << trackInSet[iset].size();
unsigned long bestComb = 0;
while (!(status = getStartTimeInSet(tracks, trackInSet[iset], bestComb))) {
;
}
if (status == 1) {
int ntracks = trackInSet[iset].size();
// set the best in set
for (int itrk = 0; itrk < ntracks; itrk++) {
hypo[itrk] = bestComb % 3;
bestComb /= 3;
int index = trkIndex[trackInSet[iset][itrk]];
const eventTimeTrack& ctrack = tracks[trackInSet[iset][itrk]];
LOG(debug) << "Using hypothesis: " << hypo[itrk] << " tofSignal: " << ctrack.mSignal << " exp. time: " << ctrack.expTimes[hypo[itrk]] << " exp. sigma: " << ctrack.expSigma[hypo[itrk]];
LOG(debug) << "0= " << ctrack.expTimes[0] << " +- " << ctrack.expSigma[0] << " 1= " << ctrack.expTimes[1] << " +- " << ctrack.expSigma[1] << " 2= " << ctrack.expTimes[2] << " +- " << ctrack.expSigma[2];
evtime.mWeights[index] = 1. / (ctrack.expSigma[hypo[itrk]] * ctrack.expSigma[hypo[itrk]]);
evtime.mTrackTimes[index] = ctrack.mSignal - ctrack.expTimes[hypo[itrk]];
}
}
LOG(debug) << "iset " << iset << " did not have good status";
} // end loop in set
// do average among all tracks
float finalTime = 0, allweights = 0;
int ntrackUsed = 0;
for (int i = 0; i < evtime.mWeights.size(); i++) {
if (evtime.mWeights[i] < weightLimit) {
continue;
}
ntrackUsed++;
allweights += evtime.mWeights[i];
finalTime += evtime.mTrackTimes[i] * evtime.mWeights[i];
}
if (allweights < weightLimit) {
LOG(debug) << "Skipping because allweights " << allweights << " are lower than " << weightLimit;
return;
}
evtime.mEventTime = finalTime / allweights;
evtime.mEventTimeError = sqrt(1. / allweights);
evtime.mEventTimeMultiplicity = ntrackUsed;
}
void computeEvTimeFast(const std::vector<eventTimeTrack>& tracks, const std::vector<int>& trkIndex, eventTimeContainer& evtime)
{
static constexpr int maxNumberOfSets = 200;
static constexpr float weightLimit = 1E-6; // Limit in the weights
const int ntracks = tracks.size();
LOG(debug) << "For the collision time using " << ntracks;
if (ntracks < 2) { // at least 2 tracks required
LOG(debug) << "Skipping event because at least 2 tracks are required";
return;
}
int hypo[MAXNTRACKINSET];
int nmaxtracksinset = ntracks > 22 ? 6 : MAXNTRACKINSET; // max number of tracks in a set for event time computation
int ntracksinset = std::min(ntracks, nmaxtracksinset);
int nset = ((ntracks - 1) / ntracksinset) + 1;
int ntrackUsable = ntracks;
if (nset > maxNumberOfSets) {
nset = maxNumberOfSets;
ntrackUsable = nmaxtracksinset * nset;
}
// list of tracks in set
std::vector<int> trackInSet[maxNumberOfSets];
for (int i = 0; i < ntrackUsable; i++) {
int iset = i % nset;
trackInSet[iset].push_back(i);
}
int status;
// compute event time for each set
for (int iset = 0; iset < nset; iset++) {
unsigned long bestComb = 0;
while (!(status = getStartTimeInSetFast(tracks, trackInSet[iset], bestComb))) {
;
}
if (status == 1) {
int ntracks = trackInSet[iset].size();
// set the best in set
for (int itrk = 0; itrk < ntracks; itrk++) {
hypo[itrk] = bestComb % 3;
bestComb /= 3;
int index = trkIndex[trackInSet[iset][itrk]];
const eventTimeTrack& ctrack = tracks[trackInSet[iset][itrk]];
LOG(debug) << "Using hypothesis: " << hypo[itrk] << " tofSignal: " << ctrack.mSignal << " exp. time: " << ctrack.expTimes[hypo[itrk]] << " exp. sigma: " << ctrack.expSigma[hypo[itrk]];
LOG(debug) << "0= " << ctrack.expTimes[0] << " +- " << ctrack.expSigma[0] << " 1= " << ctrack.expTimes[1] << " +- " << ctrack.expSigma[1] << " 2= " << ctrack.expTimes[2] << " +- " << ctrack.expSigma[2];
evtime.mWeights[index] = 1. / (ctrack.expSigma[hypo[itrk]] * ctrack.expSigma[hypo[itrk]]);
evtime.mTrackTimes[index] = ctrack.mSignal - ctrack.expTimes[hypo[itrk]];
}
}
} // end loop in set
// do average among all tracks
float finalTime = 0, allweights = 0;
int ntrackUsed = 0;
for (int i = 0; i < evtime.mWeights.size(); i++) {
if (evtime.mWeights[i] < weightLimit) {
continue;
}
ntrackUsed++;
allweights += evtime.mWeights[i];
finalTime += evtime.mTrackTimes[i] * evtime.mWeights[i];
}
if (allweights < weightLimit) {
LOG(debug) << "Skipping because allweights " << allweights << " are lower than " << weightLimit;
return;
}
evtime.mEventTime = finalTime / allweights;
evtime.mEventTimeError = sqrt(1. / allweights);
evtime.mEventTimeMultiplicity = ntrackUsed;
}
int getStartTimeInSet(const std::vector<eventTimeTrack>& tracks, std::vector<int>& trackInSet, unsigned long& bestComb)
{
float chi2, chi2best, averageBest = 0;
int hypo[MAXNTRACKINSET];
float starttime[MAXNTRACKINSET], weighttime[MAXNTRACKINSET];
chi2best = 10000;
int ntracks = trackInSet.size();
LOG(debug) << "Computing the start time in set with " << ntracks << " tracks";
if (ntracks < 3) {
LOG(debug) << "Not enough tracks!";
return 2; // no event time in the set
}
unsigned long ncomb = combinatorial[ntracks];
for (unsigned long comb = 0; comb < ncomb; comb++) {
unsigned long curr = comb;
int ngood = 0;
float average = 0;
float sumweights = 0;
// get track info in the set for current combination
for (int itrk = 0; itrk < ntracks; itrk++) {
hypo[itrk] = curr % 3;
curr /= 3;
const eventTimeTrack& ctrack = tracks[trackInSet[itrk]];
starttime[itrk] = ctrack.mSignal - ctrack.expTimes[hypo[itrk]];
weighttime[itrk] = 1. / (ctrack.expSigma[hypo[itrk]] * ctrack.expSigma[hypo[itrk]]);
average += starttime[itrk] * weighttime[itrk];
sumweights += weighttime[itrk];
ngood++;
}
average /= sumweights;
// compute chi2
chi2 = 0;
float deltat;
for (int itrk = 0; itrk < ntracks; itrk++) {
deltat = starttime[itrk] - average;
chi2 += deltat * deltat * weighttime[itrk];
}
chi2 /= (ngood - 1);
if (chi2 < chi2best) {
bestComb = comb;
chi2best = chi2;
averageBest = average;
}
} // end loop in combinations
int worse = -1;
float errworse = 4;
// check the best combination
unsigned long curr = bestComb;
for (int itrk = 0; itrk < ntracks; itrk++) {
hypo[itrk] = curr % 3;
curr /= 3;
const eventTimeTrack& ctrack = tracks[trackInSet[itrk]];
float err = ctrack.mSignal - ctrack.expTimes[hypo[itrk]] - averageBest;
err /= ctrack.expSigma[hypo[itrk]];
err = fabs(err);
if (err > errworse) {
errworse = err;
worse = itrk;
}
}
if (worse > -1) {
const eventTimeTrack& ctrack = tracks[trackInSet[worse]];
// remove the track and try again
trackInSet.erase(trackInSet.begin() + worse);
return 0;
}
return 1; // good event time in the set
}
int getStartTimeInSetFast(const std::vector<eventTimeTrack>& tracks, std::vector<int>& trackInSet, unsigned long& bestComb)
{
float chi2, chi2best, averageBest = 0;
int hypo[MAXNTRACKINSET];
float starttime[MAXNTRACKINSET], weighttime[MAXNTRACKINSET];
chi2best = 10000;
int ntracks = trackInSet.size();
if (ntracks < 3) {
return 2; // no event time in the set
}
unsigned long ncomb = combinatorial[ntracks];
unsigned long comb = 0; // use only pions
int ngood = 0;
float average = 0;
float sumweights = 0;
// get track info in the set for current combination
for (int itrk = 0; itrk < ntracks; itrk++) {
hypo[itrk] = 0;
const eventTimeTrack& ctrack = tracks[trackInSet[itrk]];
starttime[itrk] = ctrack.mSignal - ctrack.expTimes[hypo[itrk]];
weighttime[itrk] = 1. / (ctrack.expSigma[hypo[itrk]] * ctrack.expSigma[hypo[itrk]]);
average += starttime[itrk] * weighttime[itrk];
sumweights += weighttime[itrk];
ngood++;
}
average /= sumweights;
// compute chi2
chi2 = 0;
float deltat;
for (int itrk = 0; itrk < ntracks; itrk++) {
deltat = starttime[itrk] - average;
chi2 += deltat * deltat * weighttime[itrk];
}
chi2 /= (ngood - 1);
bestComb = comb;
chi2best = chi2;
averageBest = average;
int worse = -1;
float errworse = 4;
// check the best combination
for (int itrk = 0; itrk < ntracks; itrk++) {
const eventTimeTrack& ctrack = tracks[trackInSet[itrk]];
float err = ctrack.mSignal - ctrack.expTimes[hypo[itrk]] - averageBest;
err /= ctrack.expSigma[hypo[itrk]];
err = fabs(err);
if (err > errworse) {
errworse = err;
worse = itrk;
}
}
if (worse > -1) {
const eventTimeTrack& ctrack = tracks[trackInSet[worse]];
// remove the track and try again
trackInSet.erase(trackInSet.begin() + worse);
return 0;
}
return 1; // good event time in the set
}
void generateEvTimeTracks(std::vector<eventTimeTrackTest>& tracks, int ntracks, float evTime)
{
eventTimeTrackTest track;
constexpr float masses[3] = {0.13957000, 0.49367700, 0.93827200};
constexpr float kCSPEED = TMath::C() * 1.0e2f * 1.0e-12f; /// Speed of light in TOF units (cm/ps)
float energy = 0.f;
float betas[3] = {0.f};
float pMismatch = ntracks * 0.00005;
for (int i = 0; i < ntracks; i++) {
track.mTOFChi2 = 1.f;
track.mP = gRandom->Exp(1);
track.mPt = track.mP;
track.mLength = 400.;
track.mHypo = gRandom->Exp(1);
if (track.mHypo > 2) {
track.mHypo = 2;
}
for (int j = 0; j < 3; j++) {
energy = sqrt(masses[j] * masses[j] + track.mP * track.mP);
betas[j] = track.mP / energy;
track.expTimes[j] = track.mLength / (betas[j] * kCSPEED);
track.expSigma[j] = 100.f;
if (j == track.mHypo) {
track.mSignal = track.expTimes[j] + gRandom->Gaus(0.f, track.expSigma[j]);
if (gRandom->Rndm() < pMismatch) { // assign time from a different particle
float p = gRandom->Exp(1);
float l = 400;
int hypo = gRandom->Exp(1);
if (hypo > 2) {
hypo = 2;
}
energy = sqrt(masses[hypo] * masses[hypo] + p * p);
float beta = p / energy;
track.mSignal = l / (beta * kCSPEED) + gRandom->Gaus(0.f, track.expSigma[j]);
}
}
}
tracks.push_back(track);
}
}
} // namespace tof
} // namespace o2