forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAODMcProducerHelpers.cxx
More file actions
436 lines (405 loc) · 16.2 KB
/
AODMcProducerHelpers.cxx
File metadata and controls
436 lines (405 loc) · 16.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2023-2099 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 AODMcProducerHelpers.h
/// @author Christian Holm Christensen <cholm@nbi.dk>
/// common helpers for AOD MC producers
#include "AODProducerWorkflow/AODMcProducerHelpers.h"
#include <SimulationDataFormat/MCUtils.h>
#include <Framework/AnalysisDataModel.h>
#include <MathUtils/Utils.h>
#include <algorithm>
#define verbosity debug
namespace o2::aodmchelpers
{
//==================================================================
bool hasKeys(o2::dataformats::MCEventHeader const& header,
const std::vector<std::string>& keys,
bool anyNotAll)
{
auto check = [&header](const std::string& key) { // Do not format
return header.hasInfo(key);
};
return (anyNotAll ? // Do not format
std::any_of(keys.cbegin(), keys.cend(), check)
: // Do not format
std::all_of(keys.cbegin(), keys.cend(), check));
}
//====================================================================
short updateMCCollisions(const CollisionCursor& cursor,
int bcId,
float time,
o2::dataformats::MCEventHeader const& header,
short generatorId,
int sourceId,
unsigned int mask)
{
using Key = o2::dataformats::MCInfoKeys;
using GenProp = o2::mcgenid::GeneratorProperty;
using namespace o2::math_utils;
int subGenId = getEventInfo(header, GenProp::SUBGENERATORID, -1);
int genId = getEventInfo(header, GenProp::GENERATORID,
int(generatorId));
float weight = getEventInfo(header, Key::weight, 1.f);
auto encodedGeneratorId = o2::mcgenid::getEncodedGenId(genId,
sourceId,
subGenId);
LOG(verbosity) << "Updating MC Collisions table w/bcId=" << bcId;
cursor(0,
bcId,
encodedGeneratorId,
truncateFloatFraction(header.GetX(), mask),
truncateFloatFraction(header.GetY(), mask),
truncateFloatFraction(header.GetZ(), mask),
truncateFloatFraction(time, mask),
truncateFloatFraction(weight, mask),
header.GetB(),
getEventInfo(header, Key::planeAngle, header.GetRotZ()));
return encodedGeneratorId;
}
//--------------------------------------------------------------------
bool updateHepMCXSection(const XSectionCursor& cursor,
int collisionID,
short generatorID,
o2::dataformats::MCEventHeader const& header,
HepMCUpdate when)
{
using Key = o2::dataformats::MCInfoKeys;
if (when == HepMCUpdate::never or
(when != HepMCUpdate::always and
not hasKeys(header, { // Do not
Key::acceptedEvents, // mess with
Key::attemptedEvents, // with
Key::xSection, // my
Key::xSectionError}, // formatting
when == HepMCUpdate::anyKey))) {
return false;
}
LOG(verbosity) << "Updating HepMC cross-section table w/collisionId "
<< collisionID;
cursor(0,
collisionID,
generatorID,
getEventInfo(header, Key::acceptedEvents, 0),
getEventInfo(header, Key::attemptedEvents, 0),
getEventInfo(header, Key::xSection, 0.f),
getEventInfo(header, Key::xSectionError, 0.f),
getEventInfo(header, Key::eventScale, 1.f),
getEventInfo(header, Key::mpi, -1),
getEventInfo(header, Key::processCode, -1));
return true;
}
//--------------------------------------------------------------------
bool updateHepMCPdfInfo(const PdfInfoCursor& cursor,
int collisionID,
short generatorID,
o2::dataformats::MCEventHeader const& header,
HepMCUpdate when)
{
using Key = o2::dataformats::MCInfoKeys;
if (when == HepMCUpdate::never or
(when != HepMCUpdate::always and // Do
not hasKeys(header, {Key::pdfParton1Id, // not
Key::pdfParton2Id, // mess
Key::pdfCode1, // with
Key::pdfCode2, // my
Key::pdfX1, // formatting
Key::pdfX2, // .
Key::pdfScale, // It
Key::pdfXF1, // is
Key::pdfXF2}, // better
when == HepMCUpdate::anyKey))) {
return false;
}
LOG(verbosity) << "Updating HepMC PDF table w/collisionId " << collisionID;
cursor(0,
collisionID,
generatorID,
getEventInfo(header, Key::pdfParton1Id, int(0)),
getEventInfo(header, Key::pdfParton2Id, int(0)),
getEventInfo(header, Key::pdfCode1, int(0)),
getEventInfo(header, Key::pdfCode2, int(0)),
getEventInfo(header, Key::pdfX1, 0.f),
getEventInfo(header, Key::pdfX2, 0.f),
getEventInfo(header, Key::pdfScale, 1.f),
getEventInfo(header, Key::pdfXF1, 0.f),
getEventInfo(header, Key::pdfXF2, 0.f));
return true;
}
//--------------------------------------------------------------------
bool updateHepMCHeavyIon(const HeavyIonCursor& cursor,
int collisionID,
short generatorID,
o2::dataformats::MCEventHeader const& header,
HepMCUpdate when)
{
using Key = dataformats::MCInfoKeys;
if (when == HepMCUpdate::never or
(when != HepMCUpdate::always and // clang
not hasKeys(header, {Key::nCollHard, // format
Key::nPartProjectile, // is
Key::nPartTarget, // so
Key::nColl, // annoying
Key::nCollNNWounded, // .
Key::nCollNWoundedN, // It
Key::nCollNWoundedNwounded, // messes
Key::nSpecProjectileNeutron, // up
Key::nSpecTargetNeutron, // the
Key::nSpecProjectileProton, // clarity
Key::nSpecTargetProton, // of
Key::planeAngle, // the
"eccentricity", // code
Key::sigmaInelNN, // to
Key::centrality}, // noavail
when == HepMCUpdate::anyKey))) {
return false;
}
int specNeutrons = (getEventInfo(header, Key::nSpecProjectileNeutron, -1) +
getEventInfo(header, Key::nSpecTargetNeutron, -1));
int specProtons = (getEventInfo(header, Key::nSpecProjectileProton, -1) +
getEventInfo(header, Key::nSpecTargetProton, -1));
LOG(verbosity) << "Updating HepMC heavy-ion table w/collisionID "
<< collisionID;
cursor(0,
collisionID,
generatorID,
getEventInfo(header, Key::nCollHard, -1),
getEventInfo(header, Key::nPartProjectile, -1),
getEventInfo(header, Key::nPartTarget, -1),
getEventInfo(header, Key::nColl, -1),
getEventInfo(header, Key::nCollNNWounded, -1),
getEventInfo(header, Key::nCollNWoundedN, -1),
getEventInfo(header, Key::nCollNWoundedNwounded, -1),
specNeutrons,
specProtons,
header.GetB(),
getEventInfo(header, Key::planeAngle, header.GetRotZ()),
getEventInfo(header, "eccentricity", 0),
getEventInfo(header, Key::sigmaInelNN, 0.),
getEventInfo(header, Key::centrality, -1));
return true;
}
//--------------------------------------------------------------------
void updateParticle(const ParticleCursor& cursor,
const TrackToIndex& toStore,
int collisionID,
o2::MCTrack const& track,
std::vector<MCTrack> const& tracks,
uint8_t flags,
uint32_t weightMask,
uint32_t momentumMask,
uint32_t positionMask)
{
using o2::mcutils::MCTrackNavigator;
using namespace o2::aod::mcparticle::enums;
using namespace o2::math_utils;
using namespace o2::mcgenstatus;
auto mapping = [&toStore](int trackNo) {
auto iter = toStore.find(trackNo);
if (iter == toStore.end()) {
return -1;
}
return iter->second;
};
auto statusCode = track.getStatusCode().fullEncoding;
auto hepmc = getHepMCStatusCode(track.getStatusCode());
if (not track.isPrimary()) {
flags |= ProducedByTransport;
statusCode = track.getProcess();
}
if (MCTrackNavigator::isPhysicalPrimary(track, tracks)) {
flags |= PhysicalPrimary;
}
int daughters[2] = {-1, -1};
std::vector<int> mothers;
int id;
if ((id = mapping(track.getMotherTrackId())) >= 0) {
mothers.push_back(id);
}
if ((id = mapping(track.getSecondMotherTrackId())) >= 0) {
mothers.push_back(id);
}
if ((id = mapping(track.getFirstDaughterTrackId())) >= 0) {
daughters[0] = id;
}
if ((id = mapping(track.getLastDaughterTrackId())) >= 0) {
daughters[1] = id;
} else {
daughters[1] = daughters[0];
}
if (daughters[0] < 0 and daughters[1] >= 0) {
LOG(error) << "Problematic daughters: " << daughters[0] << " and "
<< daughters[1];
daughters[0] = daughters[1];
}
if (daughters[0] > daughters[1]) {
std::swap(daughters[0], daughters[1]);
}
float weight = track.getWeight();
float pX = float(track.Px());
float pY = float(track.Py());
float pZ = float(track.Pz());
float energy = float(track.GetEnergy());
float vX = float(track.Vx());
float vY = float(track.Vy());
float vZ = float(track.Vz());
float time = float(track.T());
LOG(verbosity) << "McParticle collisionId=" << collisionID << ","
<< "status=" << statusCode << ","
<< "hepmc=" << hepmc << ","
<< "pdg=" << track.GetPdgCode() << ","
<< "nMothers=" << mothers.size() << ","
<< "daughters=" << daughters[0] << ","
<< daughters[1];
cursor(0,
collisionID,
track.GetPdgCode(),
statusCode,
flags,
mothers,
daughters,
truncateFloatFraction(weight, weightMask),
truncateFloatFraction(pX, momentumMask),
truncateFloatFraction(pY, momentumMask),
truncateFloatFraction(pZ, momentumMask),
truncateFloatFraction(energy, momentumMask),
truncateFloatFraction(vX, positionMask),
truncateFloatFraction(vY, positionMask),
truncateFloatFraction(vZ, positionMask),
truncateFloatFraction(time, positionMask));
}
//--------------------------------------------------------------------
uint32_t updateParticles(const ParticleCursor& cursor,
int collisionID,
std::vector<MCTrack> const& tracks,
TrackToIndex& preselect,
uint32_t offset,
bool filter,
bool background,
uint32_t weightMask,
uint32_t momentumMask,
uint32_t positionMask,
bool signalFilter)
{
using o2::mcutils::MCTrackNavigator;
using namespace o2::aod::mcparticle::enums;
using namespace o2::mcgenstatus;
// First loop over particles to find out which to store
// TrackToIndex toStore(preselect.begin(), preselect.end());
//
// Guess we need to modifiy the passed in mapping so that MC labels
// can be set correctly
TrackToIndex& toStore = preselect;
// Mapping lambda. This maps the track number to the index into
// the table exported.
auto mapping = [&toStore](int trackNo) {
auto iter = toStore.find(trackNo);
if (iter == toStore.end()) {
return -1;
}
return iter->second;
};
LOG(verbosity) << "Got a total of " << tracks.size();
for (int trackNo = tracks.size() - 1; trackNo >= 0; trackNo--) {
auto& track = tracks[trackNo];
auto hepmc = getHepMCStatusCode(track.getStatusCode());
if (filter) {
if (toStore.find(trackNo) == toStore.end() and
/* The above test is in-correct. The track may be stored in
the list, but with a negative value. In that case, the
above test will still check mothers and daughters, and
possible store them in the output. This is however how
it is done in current `dev` branch, and so to enable
comparison on closure, we do this test for now. The
correct way it commented out below. */
// mapping(trackNo) < 0 and
not track.isPrimary() and
not MCTrackNavigator::isPhysicalPrimary(track, tracks) and
not MCTrackNavigator::isKeepPhysics(track, tracks)) {
LOG(verbosity) << "Skipping track " << trackNo << " " << hepmc << " "
<< mapping(trackNo) << " "
<< track.isPrimary() << " "
<< MCTrackNavigator::isPhysicalPrimary(track, tracks)
<< " "
<< MCTrackNavigator::isKeepPhysics(track, tracks);
continue;
}
}
if (background && signalFilter) {
continue;
}
// Store this particle. We mark that putting a 1 in the
// `toStore` mapping. This will later on be updated with the
// actual index into the table
toStore[trackNo] = 1;
// If we're filtering tracks, then also mark mothers and
// daughters(?) to be stored.
if (filter) {
int id;
if ((id = track.getMotherTrackId()) >= 0) {
toStore[id] = 1;
}
if ((id = track.getSecondMotherTrackId()) >= 0) {
toStore[id] = 1;
}
if ((id = track.getFirstDaughterTrackId()) >= 0) {
toStore[id] = 1;
}
if ((id = track.getLastDaughterTrackId()) >= 0) {
toStore[id] = 1;
}
}
}
// Second loop to set indexes. This is needed to be done before
// we actually update the table, because a particle may point to a
// later particle.
LOG(verbosity) << "Will store " << toStore.size() << " particles";
size_t index = 0;
for (size_t trackNo = 0U; trackNo < tracks.size(); trackNo++) {
auto storeIt = mapping(trackNo);
if (storeIt < 0) {
continue;
}
toStore[trackNo] = offset + index;
index++;
}
// Make sure we have the right number
assert(index == toStore.size());
LOG(verbosity) << "Starting index " << offset << ", last index "
<< offset + index << " "
<< "Selected " << toStore.size() << " tracks out of "
<< tracks.size() << " "
<< "Collision # " << collisionID;
// Third loop to actually store the particles in the order given
for (size_t trackNo = 0U; trackNo < tracks.size(); trackNo++) {
auto storeIt = mapping(trackNo);
if (storeIt < 0) {
continue;
}
auto& track = tracks[trackNo];
auto hepmc = getHepMCStatusCode(track.getStatusCode());
uint8_t flags = (background ? FromBackgroundEvent : 0);
updateParticle(cursor,
toStore,
collisionID,
track,
tracks,
flags,
weightMask,
momentumMask,
positionMask);
}
LOG(verbosity) << "Return new offset " << offset + index;
return offset + index;
}
} // namespace o2::aodmchelpers
// Local Variables:
// mode: C++
// End: