Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-24 09:47:12

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 #pragma once
0010 
0011 #include "ActsExamples/Utilities/GroupBy.hpp"
0012 #include "ActsFatras/EventData/Particle.hpp"
0013 #include "ActsFatras/EventData/SimulationOutcome.hpp"
0014 
0015 #include <boost/container/flat_set.hpp>
0016 
0017 namespace ActsExamples {
0018 
0019 using SimBarcode = ::ActsFatras::Barcode;
0020 using SimBarcodeContainer = ::boost::container::flat_set<SimBarcode>;
0021 
0022 using SimParticleState = ::ActsFatras::Particle;
0023 
0024 /// Encodes hadron heavy-flavour origin classification
0025 /// the enum values correspond to the quark flavour numbering scheme (from PDG)
0026 enum class HeavyFlavourOrigin : std::uint8_t {
0027   /// None: no heavy-flavour origin, depending on the criterion regulated
0028   ///       by the searchUpToHeavyFlavourQuark flag
0029   None = 0,
0030   /// Charm: decay product of a charmed hadron, or particle originate
0031   ///        in the fragmentation of a charm quark depending
0032   ///        on the searchUpToHeavyFlavourQuark flag
0033   Charm = 4,
0034   /// Bottom: decay product of a bottomed hadron (it can also be B->D->X),
0035   ///         or particle originate in the fragmentation of a bottom quark
0036   ///         depending on the searchUpToHeavyFlavourQuark flag
0037   Bottom = 5
0038 };
0039 
0040 /// Print heavy-flavour origin outcome to output stream
0041 /// @param os Output stream
0042 /// @param outcome Heavy-flavour origin outcome to print
0043 /// @return Output stream
0044 std::ostream& operator<<(std::ostream& os, HeavyFlavourOrigin outcome);
0045 class SimParticle final {
0046  public:
0047   /// Construct a default particle with invalid identity.
0048   SimParticle() = default;
0049 
0050   /// Construct a particle at rest with explicit mass and charge.
0051   ///
0052   /// @param particleId Particle identifier within an event
0053   /// @param pdg PDG id
0054   /// @param charge Particle charge in native units
0055   /// @param mass Particle mass in native units
0056   ///
0057   /// @warning It is the users responsibility that charge and mass match
0058   ///          the PDG particle number.
0059   SimParticle(SimBarcode particleId, Acts::PdgParticle pdg, double charge,
0060               double mass)
0061       : m_initial(particleId, pdg, charge, mass),
0062         m_final(particleId, pdg, charge, mass) {}
0063 
0064   /// Construct a particle at rest from a PDG particle number.
0065   ///
0066   /// @param particleId Particle identifier within an event
0067   /// @param pdg PDG particle number
0068   ///
0069   /// Charge and mass are retrieved from the particle data table.
0070   SimParticle(SimBarcode particleId, Acts::PdgParticle pdg)
0071       : m_initial(particleId, pdg), m_final(particleId, pdg) {}
0072 
0073   SimParticle(const SimParticleState& initial,
0074               const SimParticleState& finalState)
0075       : m_initial(initial), m_final(finalState) {
0076     if (m_initial.particleId() != m_final.particleId()) {
0077       throw std::invalid_argument("Particle id mismatch");
0078     }
0079   }
0080 
0081   const SimParticleState& initialState() const { return m_initial; }
0082   const SimParticleState& finalState() const { return m_final; }
0083 
0084   SimParticleState& initialState() { return m_initial; }
0085   SimParticleState& finalState() { return m_final; }
0086 
0087   /// Construct a new particle with a new identifier but same kinematics.
0088   ///
0089   /// @note This is intentionally not a regular setter. The particle id
0090   ///       is used to identify the whole particle. Setting it on an existing
0091   ///       particle is usually a mistake.
0092   SimParticle withParticleId(SimBarcode particleId) const {
0093     SimParticle copy(initialState().withParticleId(particleId),
0094                      finalState().withParticleId(particleId));
0095     copy.setParentParticleId(parentParticleId());
0096     return copy;
0097   }
0098 
0099   /// Set the process type that generated this particle.
0100   SimParticle& setProcess(ActsFatras::GenerationProcess proc) {
0101     initialState().setProcess(proc);
0102     finalState().setProcess(proc);
0103     return *this;
0104   }
0105   /// Set the pdg.
0106   SimParticle& setPdg(Acts::PdgParticle pdg) {
0107     initialState().setPdg(pdg);
0108     finalState().setPdg(pdg);
0109     return *this;
0110   }
0111   /// Set the charge.
0112   SimParticle& setCharge(double charge) {
0113     initialState().setCharge(charge);
0114     finalState().setCharge(charge);
0115     return *this;
0116   }
0117   /// Set the mass.
0118   SimParticle& setMass(double mass) {
0119     initialState().setMass(mass);
0120     finalState().setMass(mass);
0121     return *this;
0122   }
0123   /// Set the particle ID.
0124   SimParticle& setParticleId(SimBarcode barcode) {
0125     initialState().setParticleId(barcode);
0126     finalState().setParticleId(barcode);
0127     return *this;
0128   }
0129   /// Set the parent particle id on both initial and final state.
0130   SimParticle& setParentParticleId(SimBarcode parentId) {
0131     initialState().setParentParticleId(parentId);
0132     finalState().setParentParticleId(parentId);
0133     return *this;
0134   }
0135   /// Original particle index (to match HepMC).
0136   SimParticle& setOrigParticleIdx(std::uint32_t idx) {
0137     initialState().setOrigParticleIdx(idx);
0138     finalState().setOrigParticleIdx(idx);
0139     return *this;
0140   }
0141   /// Particle heavy-flavour origin (0->none, 4->charm, 5->beauty)
0142   SimParticle& setHeavyFlavourOrigin(HeavyFlavourOrigin origin) {
0143     m_hfOrigin = origin;
0144     return *this;
0145   }
0146 
0147   /// Particle identifier within an event.
0148   SimBarcode particleId() const { return initialState().particleId(); }
0149   /// Parent particle id, or default-constructed @c SimBarcode if unknown.
0150   SimBarcode parentParticleId() const {
0151     return initialState().parentParticleId();
0152   }
0153   /// Original particle index (to match HepMC)
0154   std::uint32_t origParticleIdx() const {
0155     return initialState().origParticleIdx();
0156   }
0157   /// Particle heavy-flavour origin (0->none, 4->charm, 5->beauty)
0158   HeavyFlavourOrigin heavyFlavourOrigin() const { return m_hfOrigin; }
0159   /// Which type of process generated this particle.
0160   ActsFatras::GenerationProcess process() const {
0161     return initialState().process();
0162   }
0163   /// PDG particle number that identifies the type.
0164   Acts::PdgParticle pdg() const { return initialState().pdg(); }
0165   /// Absolute PDG particle number that identifies the type.
0166   Acts::PdgParticle absolutePdg() const { return initialState().absolutePdg(); }
0167   /// Particle charge.
0168   double charge() const { return initialState().charge(); }
0169   /// Particle absolute charge.
0170   double absoluteCharge() const { return initialState().absoluteCharge(); }
0171   /// Particle mass.
0172   double mass() const { return initialState().mass(); }
0173 
0174   /// Check if this is a secondary particle.
0175   bool isSecondary() const { return initialState().isSecondary(); }
0176 
0177   /// Particle hypothesis.
0178   Acts::ParticleHypothesis hypothesis() const {
0179     return initialState().hypothesis();
0180   }
0181   /// Particl qOverP.
0182   double qOverP() const { return initialState().qOverP(); }
0183 
0184   /// Space-time position four-vector.
0185   const Acts::Vector4& fourPosition() const {
0186     return initialState().fourPosition();
0187   }
0188   /// Three-position, i.e. spatial coordinates without the time.
0189   auto position() const { return initialState().position(); }
0190   /// Time coordinate.
0191   double time() const { return initialState().time(); }
0192   /// Energy-momentum four-vector.
0193   Acts::Vector4 fourMomentum() const { return initialState().fourMomentum(); }
0194   /// Unit three-direction, i.e. the normalized momentum three-vector.
0195   const Acts::Vector3& direction() const { return initialState().direction(); }
0196   /// Polar angle.
0197   double theta() const { return initialState().theta(); }
0198   /// Azimuthal angle.
0199   double phi() const { return initialState().phi(); }
0200   /// Absolute momentum in the x-y plane.
0201   double transverseMomentum() const {
0202     return initialState().transverseMomentum();
0203   }
0204   /// Absolute momentum.
0205   double absoluteMomentum() const { return initialState().absoluteMomentum(); }
0206   /// Absolute momentum.
0207   Acts::Vector3 momentum() const { return initialState().momentum(); }
0208   /// Total energy, i.e. norm of the four-momentum.
0209   double energy() const { return initialState().energy(); }
0210 
0211   /// Energy loss over the particles lifetime or simulation time.
0212   double energyLoss() const {
0213     return initialState().energy() - finalState().energy();
0214   }
0215 
0216   /// Accumulated path within material measured in radiation lengths.
0217   double pathInX0() const { return finalState().pathInX0(); }
0218   /// Accumulated path within material measured in interaction lengths.
0219   double pathInL0() const { return finalState().pathInL0(); }
0220 
0221   /// Number of hits.
0222   std::uint32_t numberOfHits() const { return finalState().numberOfHits(); }
0223 
0224   /// Particle outcome.
0225   ActsFatras::SimulationOutcome outcome() const {
0226     return finalState().outcome();
0227   }
0228 
0229  private:
0230   SimParticleState m_initial;
0231   SimParticleState m_final;
0232   /// particle origin (0->none, 4->charm, 5->beauty)
0233   HeavyFlavourOrigin m_hfOrigin = HeavyFlavourOrigin::None;
0234 };
0235 
0236 std::ostream& operator<<(std::ostream& os, const SimParticle& particle);
0237 
0238 namespace detail {
0239 struct CompareParticleId {
0240   using is_transparent = void;
0241   bool operator()(const SimParticleState& lhs,
0242                   const SimParticleState& rhs) const {
0243     return lhs.particleId() < rhs.particleId();
0244   }
0245   bool operator()(const SimParticle& lhs, const SimParticle& rhs) const {
0246     return lhs.particleId() < rhs.particleId();
0247   }
0248   bool operator()(SimBarcode lhs, const SimParticleState& rhs) const {
0249     return lhs < rhs.particleId();
0250   }
0251   bool operator()(SimBarcode lhs, const SimParticle& rhs) const {
0252     return lhs < rhs.particleId();
0253   }
0254   bool operator()(const SimParticleState& lhs, SimBarcode rhs) const {
0255     return lhs.particleId() < rhs;
0256   }
0257   bool operator()(const SimParticle& lhs, SimBarcode rhs) const {
0258     return lhs.particleId() < rhs;
0259   }
0260 };
0261 struct PrimaryVertexIdGetter {
0262   SimBarcode operator()(const SimParticleState& particle) const {
0263     return SimBarcode().withVertexPrimary(
0264         particle.particleId().vertexPrimary());
0265   }
0266   SimBarcode operator()(const SimParticle& particle) const {
0267     return SimBarcode().withVertexPrimary(
0268         particle.particleId().vertexPrimary());
0269   }
0270 };
0271 struct SecondaryVertexIdGetter {
0272   SimBarcode operator()(const SimParticleState& particle) const {
0273     return SimBarcode()
0274         .withVertexPrimary(particle.particleId().vertexPrimary())
0275         .withVertexSecondary(particle.particleId().vertexSecondary());
0276   }
0277   SimBarcode operator()(const SimParticle& particle) const {
0278     return SimBarcode()
0279         .withVertexPrimary(particle.particleId().vertexPrimary())
0280         .withVertexSecondary(particle.particleId().vertexSecondary());
0281   }
0282 };
0283 struct VertexIdGetter {
0284   SimBarcode operator()(const SimParticleState& particle) const {
0285     return particle.particleId().vertexId();
0286   }
0287   SimBarcode operator()(const SimParticle& particle) const {
0288     return particle.particleId().vertexId();
0289   }
0290 };
0291 }  // namespace detail
0292 
0293 using SimParticleStateContainer =
0294     ::boost::container::flat_set<SimParticleState, detail::CompareParticleId>;
0295 
0296 /// Store particles ordered by particle identifier.
0297 using SimParticleContainer =
0298     ::boost::container::flat_set<SimParticle, detail::CompareParticleId>;
0299 
0300 /// Iterate over groups of particles belonging to the same primary vertex.
0301 inline GroupBy<SimParticleContainer::const_iterator,
0302                detail::PrimaryVertexIdGetter>
0303 groupByPrimaryVertex(const SimParticleContainer& container) {
0304   return makeGroupBy(container, detail::PrimaryVertexIdGetter());
0305 }
0306 
0307 /// Iterate over groups of particles belonging to the same secondary vertex.
0308 ///
0309 /// For each primary vertex, this yields one group of particles belonging
0310 /// directly to the secondary vertex and a group for each secondary vertex.
0311 inline GroupBy<SimParticleContainer::const_iterator,
0312                detail::SecondaryVertexIdGetter>
0313 groupBySecondaryVertex(const SimParticleContainer& container) {
0314   return makeGroupBy(container, detail::SecondaryVertexIdGetter());
0315 }
0316 
0317 /// Iterate over groups of particles belonging to the same vertex.
0318 ///
0319 /// For each vertex, this yields one group of particles belonging
0320 /// directly to the vertex and a group for each secondary vertex.
0321 inline GroupBy<SimParticleContainer::const_iterator, detail::VertexIdGetter>
0322 groupByVertexId(const SimParticleContainer& container) {
0323   return makeGroupBy(container, detail::VertexIdGetter());
0324 }
0325 
0326 }  // namespace ActsExamples