Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Examples/Io/HepMC3/src/HepMC3Vertex.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Io/HepMC3/HepMC3Vertex.hpp"
0010 
0011 #include "ActsExamples/EventData/SimParticle.hpp"
0012 #include "ActsExamples/EventData/SimVertex.hpp"
0013 #include "ActsExamples/Io/HepMC3/HepMC3Particle.hpp"
0014 
0015 namespace ActsExamples {
0016 
0017 namespace {
0018 
0019 /// @brief Converts HepMC3::GenParticle objects into Acts
0020 /// @param genParticles list of HepMC3::GenParticle objects
0021 /// @return converted list
0022 SimBarcodeContainer genBarcodeToActs(
0023     const std::vector<HepMC3::GenParticlePtr>& genParticles) {
0024   SimBarcodeContainer actsBarcodes;
0025   // Translate all particles
0026   for (auto& genParticle : genParticles) {
0027     actsBarcodes.insert(HepMC3Particle::barcode(
0028         std::make_shared<HepMC3::GenParticle>(*genParticle)));
0029   }
0030   return actsBarcodes;
0031 }
0032 
0033 /// @brief Converts HepMC3::GenParticle objects into Acts
0034 /// @param genParticles list of HepMC3::GenParticle objects
0035 /// @return converted list
0036 std::vector<SimParticle> genParticlesToActs(
0037     const std::vector<HepMC3::GenParticlePtr>& genParticles) {
0038   std::vector<SimParticle> actsParticles;
0039   // Translate all particles
0040   for (auto& genParticle : genParticles) {
0041     actsParticles.push_back(HepMC3Particle::particle(
0042         std::make_shared<HepMC3::GenParticle>(*genParticle)));
0043   }
0044   return actsParticles;
0045 }
0046 
0047 /// @brief Converts an SimParticle into HepMC3::GenParticle
0048 /// @note The conversion ignores HepMC status codes
0049 /// @param actsParticle Acts particle that will be converted
0050 /// @return converted particle
0051 HepMC3::GenParticlePtr actsParticleToGen(
0052     const std::shared_ptr<SimParticle>& actsParticle) {
0053   // Extract momentum and energy from Acts particle for HepMC3::FourVector
0054   const auto mom = actsParticle->fourMomentum();
0055   const HepMC3::FourVector vec(mom[0], mom[1], mom[2], mom[3]);
0056   // Create HepMC3::GenParticle
0057   auto genParticle =
0058       std::make_shared<HepMC3::GenParticle>(vec, actsParticle->pdg());
0059   genParticle->set_generated_mass(actsParticle->mass());
0060 
0061   return genParticle;
0062 }
0063 
0064 /// @brief Finds a HepMC3::GenParticle from a list that matches an
0065 /// SimParticle object
0066 /// @param genParticles list of HepMC particles
0067 /// @param actsParticle Acts particle
0068 /// @return HepMC particle that matched with the Acts particle or nullptr if
0069 /// no match was found
0070 HepMC3::GenParticlePtr matchParticles(
0071     const std::vector<HepMC3::GenParticlePtr>& genParticles,
0072     const std::shared_ptr<SimParticle>& actsParticle) {
0073   const auto id = actsParticle->particleId();
0074   // Search HepMC3::GenParticle with the same id as the Acts particle
0075   for (auto& genParticle : genParticles) {
0076     if (genParticle->id() == id) {
0077       // Return particle if found
0078       return genParticle;
0079     }
0080   }
0081   return nullptr;
0082 }
0083 
0084 }  // namespace
0085 
0086 std::unique_ptr<SimVertex> HepMC3Vertex::processVertex(
0087     const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0088   SimVertex vtx(SimVertexBarcode().setVertexPrimary(vertex->id()),
0089                 {vertex->position().x(), vertex->position().y(),
0090                  vertex->position().z(), vertex->position().t()});
0091   vtx.incoming = genBarcodeToActs(vertex->particles_in());
0092   vtx.outgoing = genBarcodeToActs(vertex->particles_out());
0093   // Create Acts vertex
0094   return std::make_unique<SimVertex>(std::move(vtx));
0095 }
0096 
0097 bool HepMC3Vertex::inEvent(const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0098   return vertex->in_event();
0099 }
0100 
0101 int HepMC3Vertex::id(const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0102   return vertex->id();
0103 }
0104 
0105 std::vector<SimParticle> HepMC3Vertex::particlesIn(
0106     const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0107   return genParticlesToActs(vertex->particles_in());
0108 }
0109 
0110 std::vector<SimParticle> HepMC3Vertex::particlesOut(
0111     const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0112   return genParticlesToActs(vertex->particles_out());
0113 }
0114 
0115 Acts::Vector3 HepMC3Vertex::position(
0116     const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0117   Acts::Vector3 vec;
0118   vec(0) = vertex->position().x();
0119   vec(1) = vertex->position().y();
0120   vec(2) = vertex->position().z();
0121   return vec;
0122 }
0123 
0124 double HepMC3Vertex::time(const std::shared_ptr<HepMC3::GenVertex>& vertex) {
0125   return vertex->position().t();
0126 }
0127 
0128 void HepMC3Vertex::addParticleIn(
0129     const std::shared_ptr<HepMC3::GenVertex>& vertex,
0130     const std::shared_ptr<SimParticle>& particle) {
0131   vertex->add_particle_in(actsParticleToGen(particle));
0132 }
0133 
0134 void HepMC3Vertex::addParticleOut(
0135     const std::shared_ptr<HepMC3::GenVertex>& vertex,
0136     const std::shared_ptr<SimParticle>& particle) {
0137   vertex->add_particle_out(actsParticleToGen(particle));
0138 }
0139 
0140 void HepMC3Vertex::removeParticleIn(
0141     const std::shared_ptr<HepMC3::GenVertex>& vertex,
0142     const std::shared_ptr<SimParticle>& particle) {
0143   // Remove particle if it exists
0144   if (HepMC3::GenParticlePtr genParticle =
0145           matchParticles(vertex->particles_in(), particle)) {
0146     vertex->remove_particle_in(genParticle);
0147   }
0148 }
0149 
0150 void HepMC3Vertex::removeParticleOut(
0151     const std::shared_ptr<HepMC3::GenVertex>& vertex,
0152     const std::shared_ptr<SimParticle>& particle) {
0153   // Remove particle if it exists
0154   if (HepMC3::GenParticlePtr genParticle =
0155           matchParticles(vertex->particles_out(), particle)) {
0156     vertex->remove_particle_out(genParticle);
0157   }
0158 }
0159 
0160 void HepMC3Vertex::position(const std::shared_ptr<HepMC3::GenVertex>& vertex,
0161                             const Acts::Vector3& pos) {
0162   HepMC3::FourVector fVec(pos(0), pos(1), pos(2), vertex->position().t());
0163   vertex->set_position(fVec);
0164 }
0165 
0166 void HepMC3Vertex::time(const std::shared_ptr<HepMC3::GenVertex>& vertex,
0167                         double time) {
0168   HepMC3::FourVector fVec(vertex->position().x(), vertex->position().y(),
0169                           vertex->position().z(), time);
0170   vertex->set_position(fVec);
0171 }
0172 
0173 }  // namespace ActsExamples