forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
HepMC3Event.cpp
303 lines (267 loc) · 10.9 KB
/
HepMC3Event.cpp
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
// This file is part of the Acts project.
//
// Copyright (C) 2018 CERN for the benefit of the Acts project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "ActsExamples/Io/HepMC3/HepMC3Event.hpp"
#include "ActsExamples/Io/HepMC3/HepMC3Particle.hpp"
#include "ActsExamples/Io/HepMC3/HepMC3Vertex.hpp"
namespace {
/// @brief Converts an SimParticle into HepMC3::GenParticle
/// @note The conversion ignores HepMC status codes
/// @param actsParticle Acts particle that will be converted
/// @return converted particle
HepMC3::GenParticlePtr actsParticleToGen(
const std::shared_ptr<ActsExamples::SimParticle>& actsParticle) {
// Extract momentum and energy from Acts particle for HepMC3::FourVector
const auto mom4 = actsParticle->fourMomentum();
const HepMC3::FourVector vec(mom4[0], mom4[1], mom4[2], mom4[3]);
// Create HepMC3::GenParticle
auto genParticle =
std::make_shared<HepMC3::GenParticle>(vec, actsParticle->pdg());
genParticle->set_generated_mass(actsParticle->mass());
return genParticle;
}
/// @brief Converts an Acts vertex to a HepMC3::GenVertexPtr
/// @note The conversion ignores HepMC status codes
/// @param actsVertex Acts vertex that will be converted
/// @return Converted Acts vertex to HepMC3::GenVertexPtr
HepMC3::GenVertexPtr createGenVertex(
const std::shared_ptr<ActsExamples::SimVertex>& actsVertex) {
const HepMC3::FourVector vec(
actsVertex->position4[0], actsVertex->position4[1],
actsVertex->position4[2], actsVertex->position4[3]);
// Create vertex
auto genVertex = std::make_shared<HepMC3::GenVertex>(vec);
// Store incoming particles
for (auto& particle : actsVertex->incoming) {
HepMC3::GenParticlePtr genParticle = actsParticleToGen(
std::make_shared<ActsExamples::SimParticle>(particle));
genVertex->add_particle_in(genParticle);
}
// Store outgoing particles
for (auto& particle : actsVertex->outgoing) {
HepMC3::GenParticlePtr genParticle = actsParticleToGen(
std::make_shared<ActsExamples::SimParticle>(particle));
genVertex->add_particle_out(genParticle);
}
return genVertex;
}
/// @brief Compares an Acts vertex with a HepMC3::GenVertex
/// @note An Acts vertex does not store a barcode. Therefore the content of
/// both vertices is compared. The position, time and number of incoming and
/// outgoing particles will be compared. Since a second vertex could exist in
/// the record with identical information (although unlikely), this
/// comparison could lead to false positive results. On the other hand, a
/// numerical deviation of the parameters could lead to a false negative.
/// @param actsVertex Acts vertex
/// @param genVertex HepMC3::GenVertex
/// @return boolean result if both vertices are identical
bool compareVertices(const std::shared_ptr<ActsExamples::SimVertex>& actsVertex,
const HepMC3::GenVertexPtr& genVertex) {
// Compare position, time, number of incoming and outgoing particles between
// both vertices. Return false if one criterium does not match, else true.
HepMC3::FourVector genVec = genVertex->position();
if (actsVertex->position4[0] != genVec.x()) {
return false;
}
if (actsVertex->position4[1] != genVec.y()) {
return false;
}
if (actsVertex->position4[2] != genVec.z()) {
return false;
}
if (actsVertex->position4[3] != genVec.t()) {
return false;
}
if (actsVertex->incoming.size() != genVertex->particles_in().size()) {
return false;
}
if (actsVertex->outgoing.size() != genVertex->particles_out().size()) {
return false;
}
return true;
}
} // namespace
///
/// Setter
///
void ActsExamples::HepMC3Event::momentumUnit(HepMC3::GenEvent& event,
const double momentumUnit) {
// Check, if the momentum unit fits Acts::UnitConstants::MeV or _GeV
HepMC3::Units::MomentumUnit mom = HepMC3::Units::MomentumUnit::GEV;
if (momentumUnit == Acts::UnitConstants::MeV) {
mom = HepMC3::Units::MomentumUnit::MEV;
} else if (momentumUnit == Acts::UnitConstants::GeV) {
mom = HepMC3::Units::MomentumUnit::GEV;
} else {
// Report invalid momentum unit and set GeV
std::cout << "Invalid unit of momentum: " << momentumUnit << std::endl;
std::cout << "Momentum unit [GeV] will be used instead" << std::endl;
}
// Set units
event.set_units(mom, event.length_unit());
}
void ActsExamples::HepMC3Event::lengthUnit(HepMC3::GenEvent& event,
const double lengthUnit) {
// Check, if the length unit fits Acts::UnitConstants::mm or _cm
HepMC3::Units::LengthUnit len = HepMC3::Units::LengthUnit::MM;
if (lengthUnit == Acts::UnitConstants::mm) {
len = HepMC3::Units::LengthUnit::MM;
} else if (lengthUnit == Acts::UnitConstants::cm) {
len = HepMC3::Units::LengthUnit::CM;
} else {
// Report invalid length unit and set mm
std::cout << "Invalid unit of length: " << lengthUnit << std::endl;
std::cout << "Length unit [mm] will be used instead" << std::endl;
}
// Set units
event.set_units(event.momentum_unit(), len);
}
void ActsExamples::HepMC3Event::shiftPositionBy(HepMC3::GenEvent& event,
const Acts::Vector3& deltaPos,
const double deltaTime) {
// Create HepMC3::FourVector from position and time for shift
const HepMC3::FourVector vec(deltaPos(0), deltaPos(1), deltaPos(2),
deltaTime);
event.shift_position_by(vec);
}
void ActsExamples::HepMC3Event::shiftPositionTo(HepMC3::GenEvent& event,
const Acts::Vector3& pos,
const double time) {
// Create HepMC3::FourVector from position and time for the new position
const HepMC3::FourVector vec(pos(0), pos(1), pos(2), time);
event.shift_position_to(vec);
}
void ActsExamples::HepMC3Event::shiftPositionTo(HepMC3::GenEvent& event,
const Acts::Vector3& pos) {
// Create HepMC3::FourVector from position and time for the new position
const HepMC3::FourVector vec(pos(0), pos(1), pos(2), event.event_pos().t());
event.shift_position_to(vec);
}
void ActsExamples::HepMC3Event::shiftPositionTo(HepMC3::GenEvent& event,
const double time) {
// Create HepMC3::FourVector from position and time for the new position
const HepMC3::FourVector vec(event.event_pos().x(), event.event_pos().y(),
event.event_pos().z(), time);
event.shift_position_to(vec);
}
///
/// Adder
///
void ActsExamples::HepMC3Event::addParticle(
HepMC3::GenEvent& event, const std::shared_ptr<SimParticle>& particle) {
// Add new particle
event.add_particle(actsParticleToGen(particle));
}
void ActsExamples::HepMC3Event::addVertex(
HepMC3::GenEvent& event, const std::shared_ptr<SimVertex>& vertex) {
// Add new vertex
event.add_vertex(createGenVertex(vertex));
}
///
/// Remover
///
void ActsExamples::HepMC3Event::removeParticle(
HepMC3::GenEvent& event, const std::shared_ptr<SimParticle>& particle) {
const std::vector<HepMC3::GenParticlePtr> genParticles = event.particles();
const auto id = particle->particleId();
// Search HepMC3::GenParticle with the same id as the Acts particle
for (auto& genParticle : genParticles) {
if (genParticle->id() == id) {
// Remove particle if found
event.remove_particle(genParticle);
break;
}
}
}
void ActsExamples::HepMC3Event::removeVertex(
HepMC3::GenEvent& event, const std::shared_ptr<SimVertex>& vertex) {
const std::vector<HepMC3::GenVertexPtr> genVertices = event.vertices();
// Walk over every recorded vertex
for (auto& genVertex : genVertices) {
if (compareVertices(vertex, genVertex)) {
// Remove vertex if it matches actsVertex
event.remove_vertex(genVertex);
break;
}
}
}
///
/// Getter
///
double ActsExamples::HepMC3Event::momentumUnit(const HepMC3::GenEvent& event) {
// HepMC allows only MEV and GEV. This allows an easy identification.
return (event.momentum_unit() == HepMC3::Units::MomentumUnit::MEV
? Acts::UnitConstants::MeV
: Acts::UnitConstants::GeV);
}
double ActsExamples::HepMC3Event::lengthUnit(const HepMC3::GenEvent& event) {
// HepMC allows only MM and CM. This allows an easy identification.
return (event.length_unit() == HepMC3::Units::LengthUnit::MM
? Acts::UnitConstants::mm
: Acts::UnitConstants::cm);
}
Acts::Vector3 ActsExamples::HepMC3Event::eventPos(
const HepMC3::GenEvent& event) {
// Extract the position from HepMC3::FourVector
Acts::Vector3 vec;
vec(0) = event.event_pos().x();
vec(1) = event.event_pos().y();
vec(2) = event.event_pos().z();
return vec;
}
double ActsExamples::HepMC3Event::eventTime(const HepMC3::GenEvent& event) {
// Extract the time from HepMC3::FourVector
return event.event_pos().t();
}
std::vector<ActsExamples::SimParticle> ActsExamples::HepMC3Event::particles(
const HepMC3::GenEvent& event) {
std::vector<SimParticle> actsParticles;
const std::vector<HepMC3::ConstGenParticlePtr> genParticles =
event.particles();
// Translate all particles
for (auto& genParticle : genParticles) {
actsParticles.push_back(HepMC3Particle::particle(
std::make_shared<HepMC3::GenParticle>(*genParticle)));
}
return actsParticles;
}
std::vector<std::unique_ptr<ActsExamples::SimVertex>>
ActsExamples::HepMC3Event::vertices(const HepMC3::GenEvent& event) {
std::vector<std::unique_ptr<SimVertex>> actsVertices;
const std::vector<HepMC3::ConstGenVertexPtr> genVertices = event.vertices();
// Translate all vertices
for (auto& genVertex : genVertices) {
actsVertices.push_back(HepMC3Vertex::processVertex(
std::make_shared<HepMC3::GenVertex>(*genVertex)));
}
return actsVertices;
}
std::vector<ActsExamples::SimParticle> ActsExamples::HepMC3Event::beams(
const HepMC3::GenEvent& event) {
std::vector<SimParticle> actsBeams;
const std::vector<HepMC3::ConstGenParticlePtr> genBeams = event.beams();
// Translate beam particles and store the result
for (auto& genBeam : genBeams) {
actsBeams.push_back(HepMC3Particle::particle(
std::make_shared<HepMC3::GenParticle>(*genBeam)));
}
return actsBeams;
}
std::vector<ActsExamples::SimParticle> ActsExamples::HepMC3Event::finalState(
const HepMC3::GenEvent& event) {
std::vector<HepMC3::ConstGenParticlePtr> particles = event.particles();
std::vector<SimParticle> fState;
// Walk over every vertex
for (auto& particle : particles) {
// Collect particles without end vertex
if (!particle->end_vertex()) {
fState.push_back(HepMC3Particle::particle(
std::make_shared<HepMC3::GenParticle>(*particle)));
}
}
return fState;
}