EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HepMC3Event.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HepMC3Event.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
13 
17 
19  std::shared_ptr<HepMC3::GenEvent> event, const double momentumUnit) {
20  // Check, if the momentum unit fits Acts::UnitConstants::MeV or _GeV
21  HepMC3::Units::MomentumUnit mom;
22  if (momentumUnit == Acts::UnitConstants::MeV)
23  mom = HepMC3::Units::MomentumUnit::MEV;
24  else if (momentumUnit == Acts::UnitConstants::GeV)
25  mom = HepMC3::Units::MomentumUnit::GEV;
26  else {
27  // Report invalid momentum unit and set GeV
28  std::cout << "Invalid unit of momentum: " << momentumUnit << std::endl;
29  std::cout << "Momentum unit [GeV] will be used instead" << std::endl;
30  mom = HepMC3::Units::MomentumUnit::GEV;
31  }
32  // Set units
33  event->set_units(mom, event->length_unit());
34 }
35 
37  std::shared_ptr<HepMC3::GenEvent> event, const double lengthUnit) {
38  // Check, if the length unit fits Acts::UnitConstants::mm or _cm
39  HepMC3::Units::LengthUnit len;
40  if (lengthUnit == Acts::UnitConstants::mm)
41  len = HepMC3::Units::LengthUnit::MM;
42  else if (lengthUnit == Acts::UnitConstants::cm)
43  len = HepMC3::Units::LengthUnit::CM;
44  else {
45  // Report invalid length unit and set mm
46  std::cout << "Invalid unit of length: " << lengthUnit << std::endl;
47  std::cout << "Length unit [mm] will be used instead" << std::endl;
48  len = HepMC3::Units::LengthUnit::MM;
49  }
50 
51  // Set units
52  event->set_units(event->momentum_unit(), len);
53 }
54 
56  std::shared_ptr<HepMC3::GenEvent> event, const Acts::Vector3D& deltaPos,
57  const double deltaTime) {
58  // Create HepMC3::FourVector from position and time for shift
59  const HepMC3::FourVector vec(deltaPos(0), deltaPos(1), deltaPos(2),
60  deltaTime);
61  event->shift_position_by(vec);
62 }
63 
65  std::shared_ptr<HepMC3::GenEvent> event, const Acts::Vector3D& pos,
66  const double time) {
67  // Create HepMC3::FourVector from position and time for the new position
68  const HepMC3::FourVector vec(pos(0), pos(1), pos(2), time);
69  event->shift_position_to(vec);
70 }
71 
73  std::shared_ptr<HepMC3::GenEvent> event, const Acts::Vector3D& pos) {
74  // Create HepMC3::FourVector from position and time for the new position
75  const HepMC3::FourVector vec(pos(0), pos(1), pos(2), event->event_pos().t());
76  event->shift_position_to(vec);
77 }
78 
80  std::shared_ptr<HepMC3::GenEvent> event, const double time) {
81  // Create HepMC3::FourVector from position and time for the new position
82  const HepMC3::FourVector vec(event->event_pos().x(), event->event_pos().y(),
83  event->event_pos().z(), time);
84  event->shift_position_to(vec);
85 }
86 
90 
92  std::shared_ptr<SimParticle> actsParticle) {
93  // Extract momentum and energy from Acts particle for HepMC3::FourVector
94  const auto mom4 = actsParticle->momentum4();
95  const HepMC3::FourVector vec(mom4[0], mom4[1], mom4[2], mom4[3]);
96  // Create HepMC3::GenParticle
97  HepMC3::GenParticle genParticle(vec, actsParticle->pdg());
98  genParticle.set_generated_mass(actsParticle->mass());
99 
100  return std::shared_ptr<HepMC3::GenParticle>(&genParticle);
101 }
102 
104  std::shared_ptr<HepMC3::GenEvent> event,
105  std::shared_ptr<SimParticle> particle) {
106  // Add new particle
107  event->add_particle(actsParticleToGen(particle));
108 }
109 
111  const std::shared_ptr<SimVertex>& actsVertex) {
112  const HepMC3::FourVector vec(
113  actsVertex->position4[0], actsVertex->position4[1],
114  actsVertex->position4[2], actsVertex->position4[3]);
115 
116  // Create vertex
117  HepMC3::GenVertex genVertex(vec);
118 
119  // Store incoming particles
120  for (auto& particle : actsVertex->incoming) {
121  HepMC3::GenParticlePtr genParticle =
122  actsParticleToGen(std::make_shared<SimParticle>(particle));
123  genVertex.add_particle_in(genParticle);
124  }
125  // Store outgoing particles
126  for (auto& particle : actsVertex->outgoing) {
127  HepMC3::GenParticlePtr genParticle =
128  actsParticleToGen(std::make_shared<SimParticle>(particle));
129  genVertex.add_particle_out(genParticle);
130  }
131  return std::shared_ptr<HepMC3::GenVertex>(&genVertex);
132 }
133 
135  std::shared_ptr<HepMC3::GenEvent> event,
136  const std::shared_ptr<SimVertex> vertex) {
137  // Add new vertex
138  event->add_vertex(createGenVertex(vertex));
139 }
140 
144 
146  std::shared_ptr<HepMC3::GenEvent> event,
147  const std::shared_ptr<SimParticle>& particle) {
148  const std::vector<HepMC3::GenParticlePtr> genParticles = event->particles();
149  const auto id = particle->particleId();
150  // Search HepMC3::GenParticle with the same id as the Acts particle
151  for (auto& genParticle : genParticles) {
152  if (genParticle->id() == id) {
153  // Remove particle if found
154  event->remove_particle(genParticle);
155  break;
156  }
157  }
158 }
159 
161  const std::shared_ptr<SimVertex>& actsVertex,
162  const HepMC3::GenVertexPtr& genVertex) {
163  // Compare position, time, number of incoming and outgoing particles between
164  // both vertices. Return false if one criterium does not match, else true.
165  HepMC3::FourVector genVec = genVertex->position();
166  if (actsVertex->position4[0] != genVec.x())
167  return false;
168  if (actsVertex->position4[1] != genVec.y())
169  return false;
170  if (actsVertex->position4[2] != genVec.z())
171  return false;
172  if (actsVertex->position4[3] != genVec.t())
173  return false;
174  if (actsVertex->incoming.size() != genVertex->particles_in().size())
175  return false;
176  if (actsVertex->outgoing.size() != genVertex->particles_out().size())
177  return false;
178  return true;
179 }
180 
182  std::shared_ptr<HepMC3::GenEvent> event,
183  const std::shared_ptr<SimVertex>& vertex) {
184  const std::vector<HepMC3::GenVertexPtr> genVertices = event->vertices();
185  // Walk over every recorded vertex
186  for (auto& genVertex : genVertices)
187  if (compareVertices(vertex, genVertex)) {
188  // Remove vertex if it matches actsVertex
189  event->remove_vertex(genVertex);
190  break;
191  }
192 }
193 
197 
199  const std::shared_ptr<HepMC3::GenEvent> event) {
200  // HepMC allows only MEV and GEV. This allows an easy identification.
201  return (event->momentum_unit() == HepMC3::Units::MomentumUnit::MEV
204 }
205 
207  const std::shared_ptr<HepMC3::GenEvent> event) {
208  // HepMC allows only MM and CM. This allows an easy identification.
209  return (event->length_unit() == HepMC3::Units::LengthUnit::MM
212 }
213 
215  const std::shared_ptr<HepMC3::GenEvent> event) {
216  // Extract the position from HepMC3::FourVector
217  Acts::Vector3D vec;
218  vec(0) = event->event_pos().x();
219  vec(1) = event->event_pos().y();
220  vec(2) = event->event_pos().z();
221  return vec;
222 }
223 
225  const std::shared_ptr<HepMC3::GenEvent> event) {
226  // Extract the time from HepMC3::FourVector
227  return event->event_pos().t();
228 }
229 
230 std::vector<std::unique_ptr<ActsExamples::SimParticle>>
232  const std::shared_ptr<HepMC3::GenEvent> event) {
233  std::vector<std::unique_ptr<SimParticle>> actsParticles;
234  const std::vector<HepMC3::GenParticlePtr> genParticles = event->particles();
235 
236  HepMC3Particle simPart;
237 
238  // Translate all particles
239  for (auto& genParticle : genParticles)
240  actsParticles.push_back(
241  simPart.particle(std::make_shared<HepMC3::GenParticle>(*genParticle)));
242 
243  return actsParticles;
244 }
245 
246 std::vector<std::unique_ptr<ActsExamples::SimVertex>>
248  const std::shared_ptr<HepMC3::GenEvent> event) {
249  std::vector<std::unique_ptr<SimVertex>> actsVertices;
250  const std::vector<HepMC3::GenVertexPtr> genVertices = event->vertices();
251 
252  HepMC3Vertex simVert;
253 
254  // Translate all vertices
255  for (auto& genVertex : genVertices) {
256  actsVertices.push_back(
257  simVert.processVertex(std::make_shared<HepMC3::GenVertex>(*genVertex)));
258  }
259  return actsVertices;
260 }
261 
262 std::vector<std::unique_ptr<ActsExamples::SimParticle>>
264  const std::shared_ptr<HepMC3::GenEvent> event) {
265  std::vector<std::unique_ptr<SimParticle>> actsBeams;
266  const std::vector<HepMC3::GenParticlePtr> genBeams = event->beams();
267 
268  HepMC3Particle simPart;
269 
270  // Translate beam particles and store the result
271  for (auto& genBeam : genBeams)
272  actsBeams.push_back(
273  simPart.particle(std::make_shared<HepMC3::GenParticle>(*genBeam)));
274  return actsBeams;
275 }
276 
277 std::vector<std::unique_ptr<ActsExamples::SimParticle>>
279  const std::shared_ptr<HepMC3::GenEvent> event) {
280  std::vector<HepMC3::GenParticlePtr> particles = event->particles();
281  std::vector<std::unique_ptr<SimParticle>> fState;
282 
283  HepMC3Particle simPart;
284 
285  // Walk over every vertex
286  for (auto& particle : particles) {
287  // Collect particles without end vertex
288  if (!particle->end_vertex())
289  fState.push_back(
290  simPart.particle(std::make_shared<HepMC3::GenParticle>(*particle)));
291  }
292  return fState;
293 }