-
Notifications
You must be signed in to change notification settings - Fork 652
Expand file tree
/
Copy pathDelphesO2TrackSmearer.cxx
More file actions
378 lines (347 loc) · 14.4 KB
/
DelphesO2TrackSmearer.cxx
File metadata and controls
378 lines (347 loc) · 14.4 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
// 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 DelphesO2TrackSmearer.cxx
/// \author Roberto Preghenella
/// \brief Porting to O2Physics of DelphesO2 code.
/// Minimal changes have been made to the original code for adaptation purposes, formatting and commented parts have been considered.
/// Relevant sources:
/// DelphesO2/src/lutCovm.hh https://github.com/AliceO2Group/DelphesO2/blob/master/src/lutCovm.hh
/// DelphesO2/src/TrackSmearer.cc https://github.com/AliceO2Group/DelphesO2/blob/master/src/TrackSmearer.cc
/// DelphesO2/src/TrackSmearer.hh https://github.com/AliceO2Group/DelphesO2/blob/master/src/TrackSmearer.hh
/// @email: preghenella@bo.infn.it
///
//////////////////////////////
// DelphesO2/src/lutCovm.cc //
//////////////////////////////
/// @author: Roberto Preghenella
/// @email: preghenella@bo.infn.it
// #include "TrackSmearer.hh"
// #include "TrackUtils.hh"
// #include "TRandom.h"
// #include <iostream>
// #include <fstream>
#include "ALICE3/Core/DelphesO2TrackSmearer.h"
#include <CommonConstants/PhysicsConstants.h>
#include <Framework/Logger.h>
#include <map>
#include <string>
namespace o2
{
namespace delphes
{
/*****************************************************************/
bool TrackSmearer::loadTable(int pdg, const char* filename, bool forceReload)
{
if (!filename || filename[0] == '\0') {
LOG(info) << " --- No LUT file provided for PDG " << pdg << ". Skipping load.";
return false;
}
const auto ipdg = getIndexPDG(pdg);
LOGF(info, "Will load %s lut file ..: '%s'", getParticleName(pdg), filename);
if (mLUTHeader[ipdg] && !forceReload) {
LOG(info) << " --- LUT table for PDG " << pdg << " has been already loaded with index " << ipdg << std::endl;
return false;
}
if (strncmp(filename, "ccdb:", 5) == 0) { // Check if filename starts with "ccdb:"
LOG(info) << " --- LUT file source identified as CCDB.";
std::string path = std::string(filename).substr(5); // Remove "ccdb:" prefix
const std::string outPath = "/tmp/LUTs/";
filename = Form("%s/%s/snapshot.root", outPath.c_str(), path.c_str());
std::ifstream checkFile(filename); // Check if file already exists
if (!checkFile.is_open()) { // File does not exist, retrieve from CCDB
LOG(info) << " --- CCDB source detected for PDG " << pdg << ": " << path;
if (!mCcdbManager) {
LOG(fatal) << " --- CCDB manager not set. Please set it before loading LUT from CCDB.";
}
std::map<std::string, std::string> metadata;
mCcdbManager->getCCDBAccessor().retrieveBlob(path, outPath, metadata, 1);
// Add CCDB handling logic here if needed
LOG(info) << " --- Now retrieving LUT file from CCDB to: " << filename;
} else { // File exists, proceed to load
LOG(info) << " --- LUT file already exists: " << filename << ". Skipping download.";
checkFile.close();
}
return loadTable(pdg, filename, forceReload);
}
mLUTHeader[ipdg] = new lutHeader_t;
std::ifstream lutFile(filename, std::ifstream::binary);
if (!lutFile.is_open()) {
LOG(info) << " --- cannot open covariance matrix file for PDG " << pdg << ": " << filename << std::endl;
delete mLUTHeader[ipdg];
mLUTHeader[ipdg] = nullptr;
return false;
}
lutFile.read(reinterpret_cast<char*>(mLUTHeader[ipdg]), sizeof(lutHeader_t));
if (lutFile.gcount() != sizeof(lutHeader_t)) {
LOG(info) << " --- troubles reading covariance matrix header for PDG " << pdg << ": " << filename << std::endl;
delete mLUTHeader[ipdg];
mLUTHeader[ipdg] = nullptr;
return false;
}
if (mLUTHeader[ipdg]->version != LUTCOVM_VERSION) {
LOG(info) << " --- LUT header version mismatch: expected/detected = " << LUTCOVM_VERSION << "/" << mLUTHeader[ipdg]->version << std::endl;
delete mLUTHeader[ipdg];
mLUTHeader[ipdg] = nullptr;
return false;
}
bool specialPdgCase = false;
switch (pdg) { // Handle special cases
case o2::constants::physics::kAlpha: // Special case: Allow Alpha particles to use He3 LUT
specialPdgCase = (mLUTHeader[ipdg]->pdg == o2::constants::physics::kHelium3);
if (specialPdgCase)
LOG(info)
<< " --- Alpha particles (PDG " << pdg << ") will use He3 LUT data (PDG " << mLUTHeader[ipdg]->pdg << ")" << std::endl;
break;
default:
break;
}
if (mLUTHeader[ipdg]->pdg != pdg && !specialPdgCase) {
LOG(info) << " --- LUT header PDG mismatch: expected/detected = " << pdg << "/" << mLUTHeader[ipdg]->pdg << std::endl;
delete mLUTHeader[ipdg];
mLUTHeader[ipdg] = nullptr;
return false;
}
const int nnch = mLUTHeader[ipdg]->nchmap.nbins;
const int nrad = mLUTHeader[ipdg]->radmap.nbins;
const int neta = mLUTHeader[ipdg]->etamap.nbins;
const int npt = mLUTHeader[ipdg]->ptmap.nbins;
mLUTEntry[ipdg] = new lutEntry_t****[nnch];
for (int inch = 0; inch < nnch; ++inch) {
mLUTEntry[ipdg][inch] = new lutEntry_t***[nrad];
for (int irad = 0; irad < nrad; ++irad) {
mLUTEntry[ipdg][inch][irad] = new lutEntry_t**[neta];
for (int ieta = 0; ieta < neta; ++ieta) {
mLUTEntry[ipdg][inch][irad][ieta] = new lutEntry_t*[npt];
for (int ipt = 0; ipt < npt; ++ipt) {
mLUTEntry[ipdg][inch][irad][ieta][ipt] = new lutEntry_t;
lutFile.read(reinterpret_cast<char*>(mLUTEntry[ipdg][inch][irad][ieta][ipt]), sizeof(lutEntry_t));
if (lutFile.gcount() != sizeof(lutEntry_t)) {
LOG(info) << " --- troubles reading covariance matrix entry for PDG " << pdg << ": " << filename << std::endl;
return false;
}
}
}
}
}
LOG(info) << " --- read covariance matrix table for PDG " << pdg << ": " << filename << std::endl;
mLUTHeader[ipdg]->print();
lutFile.close();
return true;
}
/*****************************************************************/
lutEntry_t* TrackSmearer::getLUTEntry(const int pdg, const float nch, const float radius, const float eta, const float pt, float& interpolatedEff)
{
const int ipdg = getIndexPDG(pdg);
if (!mLUTHeader[ipdg]) {
LOG(error) << " --- getLUTEntry: LUT header not loaded for pdg=" << pdg << ". Returning nullptr.";
return nullptr;
}
auto inch = mLUTHeader[ipdg]->nchmap.find(nch);
auto irad = mLUTHeader[ipdg]->radmap.find(radius);
auto ieta = mLUTHeader[ipdg]->etamap.find(eta);
auto ipt = mLUTHeader[ipdg]->ptmap.find(pt);
// Interpolate if requested
auto fraction = mLUTHeader[ipdg]->nchmap.fracPositionWithinBin(nch);
if (mInterpolateEfficiency) {
static constexpr float kFractionThreshold = 0.5f;
if (fraction > kFractionThreshold) {
switch (mWhatEfficiency) {
case 1:
if (inch < mLUTHeader[ipdg]->nchmap.nbins - 1) {
interpolatedEff = (1.5f - fraction) * mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff + (-0.5f + fraction) * mLUTEntry[ipdg][inch + 1][irad][ieta][ipt]->eff;
} else {
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff;
}
break;
case 2:
if (inch < mLUTHeader[ipdg]->nchmap.nbins - 1) {
interpolatedEff = (1.5f - fraction) * mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff2 + (-0.5f + fraction) * mLUTEntry[ipdg][inch + 1][irad][ieta][ipt]->eff2;
} else {
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff2;
}
break;
default:
LOG(fatal) << " --- getLUTEntry: unknown efficiency type " << mWhatEfficiency;
}
} else {
float comparisonValue = mLUTHeader[ipdg]->nchmap.log ? std::log10(nch) : nch;
switch (mWhatEfficiency) {
case 1:
if (inch > 0 && comparisonValue < mLUTHeader[ipdg]->nchmap.max) {
interpolatedEff = (0.5f + fraction) * mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff + (0.5f - fraction) * mLUTEntry[ipdg][inch - 1][irad][ieta][ipt]->eff;
} else {
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff;
}
break;
case 2:
if (inch > 0 && comparisonValue < mLUTHeader[ipdg]->nchmap.max) {
interpolatedEff = (0.5f + fraction) * mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff2 + (0.5f - fraction) * mLUTEntry[ipdg][inch - 1][irad][ieta][ipt]->eff2;
} else {
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff2;
}
break;
default:
LOG(fatal) << " --- getLUTEntry: unknown efficiency type " << mWhatEfficiency;
}
}
} else {
switch (mWhatEfficiency) {
case 1:
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff;
break;
case 2:
interpolatedEff = mLUTEntry[ipdg][inch][irad][ieta][ipt]->eff2;
break;
default:
LOG(fatal) << " --- getLUTEntry: unknown efficiency type " << mWhatEfficiency;
}
}
return mLUTEntry[ipdg][inch][irad][ieta][ipt];
} //;
/*****************************************************************/
bool TrackSmearer::smearTrack(O2Track& o2track, lutEntry_t* lutEntry, float interpolatedEff)
{
bool isReconstructed = true;
// generate efficiency
if (mUseEfficiency) {
auto eff = 0.;
switch (mWhatEfficiency) {
case 1:
eff = lutEntry->eff;
break;
case 2:
eff = lutEntry->eff2;
break;
}
if (mInterpolateEfficiency)
eff = interpolatedEff;
if (gRandom->Uniform() > eff)
isReconstructed = false;
}
// return false already now in case not reco'ed
if (!isReconstructed && mSkipUnreconstructed)
return false;
// transform params vector and smear
static constexpr int kParSize = 5;
double params[kParSize];
for (int i = 0; i < kParSize; ++i) {
double val = 0.;
for (int j = 0; j < kParSize; ++j)
val += lutEntry->eigvec[j][i] * o2track.getParam(j);
params[i] = gRandom->Gaus(val, std::sqrt(lutEntry->eigval[i]));
}
// transform back params vector
for (int i = 0; i < kParSize; ++i) {
double val = 0.;
for (int j = 0; j < kParSize; ++j)
val += lutEntry->eiginv[j][i] * params[j];
o2track.setParam(val, i);
}
// should make a sanity check that par[2] sin(phi) is in [-1, 1]
if (std::fabs(o2track.getParam(2)) > 1.) {
LOG(info) << " --- smearTrack failed sin(phi) sanity check: " << o2track.getParam(2) << std::endl;
}
// set covariance matrix
static constexpr int kCovMatSize = 15;
for (int i = 0; i < kCovMatSize; ++i)
o2track.setCov(lutEntry->covm[i], i);
return isReconstructed;
}
/*****************************************************************/
bool TrackSmearer::smearTrack(O2Track& o2track, int pdg, float nch)
{
auto pt = o2track.getPt();
switch (pdg) {
case o2::constants::physics::kHelium3:
case -o2::constants::physics::kHelium3:
pt *= 2.f;
break;
}
auto eta = o2track.getEta();
float interpolatedEff = 0.0f;
lutEntry_t* lutEntry = getLUTEntry(pdg, nch, 0., eta, pt, interpolatedEff);
if (!lutEntry || !lutEntry->valid)
return false;
return smearTrack(o2track, lutEntry, interpolatedEff);
}
/*****************************************************************/
// relative uncertainty on pt
double TrackSmearer::getPtRes(const int pdg, const float nch, const float eta, const float pt)
{
float dummy = 0.0f;
lutEntry_t* lutEntry = getLUTEntry(pdg, nch, 0., eta, pt, dummy);
auto val = std::sqrt(lutEntry->covm[14]) * lutEntry->pt;
return val;
}
/*****************************************************************/
// relative uncertainty on eta
double TrackSmearer::getEtaRes(const int pdg, const float nch, const float eta, const float pt)
{
float dummy = 0.0f;
lutEntry_t* lutEntry = getLUTEntry(pdg, nch, 0., eta, pt, dummy);
auto sigmatgl = std::sqrt(lutEntry->covm[9]); // sigmatgl2
auto etaRes = std::fabs(std::sin(2.0 * std::atan(std::exp(-eta)))) * sigmatgl; // propagate tgl to eta uncertainty
etaRes /= lutEntry->eta; // relative uncertainty
return etaRes;
}
/*****************************************************************/
// absolute uncertainty on pt
double TrackSmearer::getAbsPtRes(const int pdg, const float nch, const float eta, const float pt)
{
float dummy = 0.0f;
lutEntry_t* lutEntry = getLUTEntry(pdg, nch, 0., eta, pt, dummy);
auto val = std::sqrt(lutEntry->covm[14]) * lutEntry->pt * lutEntry->pt;
return val;
}
/*****************************************************************/
// absolute uncertainty on eta
double TrackSmearer::getAbsEtaRes(const int pdg, const float nch, const float eta, const float pt)
{
float dummy = 0.0f;
lutEntry_t* lutEntry = getLUTEntry(pdg, nch, 0., eta, pt, dummy);
auto sigmatgl = std::sqrt(lutEntry->covm[9]); // sigmatgl2
auto etaRes = std::fabs(std::sin(2.0 * std::atan(std::exp(-eta)))) * sigmatgl; // propagate tgl to eta uncertainty
return etaRes;
}
/*****************************************************************/
// efficiency
double TrackSmearer::getEfficiency(const int pdg, const float nch, const float eta, const float pt)
{
float efficiency = 0.0f;
getLUTEntry(pdg, nch, 0., eta, pt, efficiency);
return efficiency;
}
/*****************************************************************/
// Only in DelphesO2
// bool TrackSmearer::smearTrack(Track& track, bool atDCA)
// {
// O2Track o2track;
// TrackUtils::convertTrackToO2Track(track, o2track, atDCA);
// int pdg = track.PID;
// float nch = mdNdEta; // use locally stored dNch/deta for the time being
// if (!smearTrack(o2track, pdg, nch))
// return false;
// TrackUtils::convertO2TrackToTrack(o2track, track, atDCA);
// return true;
// #if 0
// lutEntry_t* lutEntry = getLUTEntry(track.PID, 0., 0., track.Eta, track.PT);
// if (!lutEntry)
// return;
// O2Track o2track;
// TrackUtils::convertTrackToO2Track(track, o2track, atDCA);
// smearTrack(o2track, lutEntry);
// TrackUtils::convertO2TrackToTrack(o2track, track, atDCA);
// #endif
// }
/*****************************************************************/
} // namespace delphes
} // namespace o2