Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HepMC3Vertex.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HepMC3Vertex.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 
12 
13 namespace {
14 
18 std::vector<ActsExamples::SimParticle> genParticlesToActs(
19  const std::vector<HepMC3::GenParticlePtr>& genParticles) {
20  std::vector<ActsExamples::SimParticle> actsParticles;
21  // Translate all particles
22  for (auto& genParticle : genParticles) {
23  actsParticles.push_back(ActsExamples::HepMC3Particle::particle(
24  std::make_shared<HepMC3::GenParticle>(*genParticle)));
25  }
26  return actsParticles;
27 }
28 
33 HepMC3::GenParticlePtr actsParticleToGen(
34  const std::shared_ptr<ActsExamples::SimParticle>& actsParticle) {
35  // Extract momentum and energy from Acts particle for HepMC3::FourVector
36  const auto mom = actsParticle->fourMomentum();
37  const HepMC3::FourVector vec(mom[0], mom[1], mom[2], mom[3]);
38  // Create HepMC3::GenParticle
39  auto genParticle =
40  std::make_shared<HepMC3::GenParticle>(vec, actsParticle->pdg());
41  genParticle->set_generated_mass(actsParticle->mass());
42 
43  return genParticle;
44 }
45 
52 HepMC3::GenParticlePtr matchParticles(
53  const std::vector<HepMC3::GenParticlePtr>& genParticles,
54  const std::shared_ptr<ActsExamples::SimParticle>& actsParticle) {
55  const auto id = actsParticle->particleId();
56  // Search HepMC3::GenParticle with the same id as the Acts particle
57  for (auto& genParticle : genParticles) {
58  if (genParticle->id() == id) {
59  // Return particle if found
60  return genParticle;
61  }
62  }
63  return nullptr;
64 }
65 
66 } // namespace
67 
68 std::unique_ptr<ActsExamples::SimVertex>
70  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
71  SimVertex vtx({vertex->position().x(), vertex->position().y(),
72  vertex->position().z(), vertex->position().t()});
73  vtx.incoming = genParticlesToActs(vertex->particles_in());
74  vtx.outgoing = genParticlesToActs(vertex->particles_out());
75  // Create Acts vertex
76  return std::make_unique<SimVertex>(std::move(vtx));
77 }
78 
80  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
81  return vertex->in_event();
82 }
83 
85  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
86  return vertex->id();
87 }
88 
89 std::vector<ActsExamples::SimParticle> ActsExamples::HepMC3Vertex::particlesIn(
90  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
91  return genParticlesToActs(vertex->particles_in());
92 }
93 
94 std::vector<ActsExamples::SimParticle> ActsExamples::HepMC3Vertex::particlesOut(
95  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
96  return genParticlesToActs(vertex->particles_out());
97 }
98 
100  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
102  vec(0) = vertex->position().x();
103  vec(1) = vertex->position().y();
104  vec(2) = vertex->position().z();
105  return vec;
106 }
107 
109  const std::shared_ptr<HepMC3::GenVertex>& vertex) {
110  return vertex->position().t();
111 }
112 
114  const std::shared_ptr<HepMC3::GenVertex>& vertex,
115  const std::shared_ptr<SimParticle>& particle) {
116  vertex->add_particle_in(actsParticleToGen(particle));
117 }
118 
120  const std::shared_ptr<HepMC3::GenVertex>& vertex,
121  const std::shared_ptr<SimParticle>& particle) {
122  vertex->add_particle_out(actsParticleToGen(particle));
123 }
124 
126  const std::shared_ptr<HepMC3::GenVertex>& vertex,
127  const std::shared_ptr<SimParticle>& particle) {
128  // Remove particle if it exists
129  if (HepMC3::GenParticlePtr genParticle =
130  matchParticles(vertex->particles_in(), particle)) {
131  vertex->remove_particle_in(genParticle);
132  }
133 }
134 
136  const std::shared_ptr<HepMC3::GenVertex>& vertex,
137  const std::shared_ptr<SimParticle>& particle) {
138  // Remove particle if it exists
139  if (HepMC3::GenParticlePtr genParticle =
140  matchParticles(vertex->particles_out(), particle)) {
141  vertex->remove_particle_out(genParticle);
142  }
143 }
144 
146  const std::shared_ptr<HepMC3::GenVertex>& vertex,
147  const Acts::Vector3& pos) {
148  HepMC3::FourVector fVec(pos(0), pos(1), pos(2), vertex->position().t());
149  vertex->set_position(fVec);
150 }
151 
153  const std::shared_ptr<HepMC3::GenVertex>& vertex, double time) {
154  HepMC3::FourVector fVec(vertex->position().x(), vertex->position().y(),
155  vertex->position().z(), time);
156  vertex->set_position(fVec);
157 }