Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 08:05:54

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2018-2021 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/EventData/Charge.hpp"
0012 #include "Acts/EventData/GenericCurvilinearTrackParameters.hpp"
0013 #include "Acts/EventData/TrackParameters.hpp"
0014 #include "Acts/Geometry/GeometryContext.hpp"
0015 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0016 #include "Acts/Propagator/AbortList.hpp"
0017 #include "Acts/Propagator/ActionList.hpp"
0018 #include "Acts/Propagator/Propagator.hpp"
0019 #include "Acts/Propagator/StandardAborters.hpp"
0020 #include "Acts/Utilities/Logger.hpp"
0021 #include "Acts/Utilities/Result.hpp"
0022 #include "ActsFatras/EventData/Hit.hpp"
0023 #include "ActsFatras/EventData/Particle.hpp"
0024 #include "ActsFatras/Kernel/SimulationResult.hpp"
0025 #include "ActsFatras/Kernel/detail/SimulationActor.hpp"
0026 #include "ActsFatras/Kernel/detail/SimulationError.hpp"
0027 
0028 #include <algorithm>
0029 #include <cassert>
0030 #include <iterator>
0031 #include <memory>
0032 #include <vector>
0033 
0034 namespace ActsFatras {
0035 
0036 /// Single particle simulation with fixed propagator, interactions, and decay.
0037 ///
0038 /// @tparam generator_t random number generator
0039 /// @tparam interactions_t interaction list
0040 /// @tparam hit_surface_selector_t selector for hit surfaces
0041 /// @tparam decay_t decay module
0042 template <typename propagator_t, typename interactions_t,
0043           typename hit_surface_selector_t, typename decay_t>
0044 struct SingleParticleSimulation {
0045   /// How and within which geometry to propagate the particle.
0046   propagator_t propagator;
0047   /// Absolute maximum step size
0048   double maxStepSize = std::numeric_limits<double>::max();
0049   /// Absolute maximum path length
0050   double pathLimit = std::numeric_limits<double>::max();
0051   /// Decay module.
0052   decay_t decay;
0053   /// Interaction list containing the simulated interactions.
0054   interactions_t interactions;
0055   /// Selector for surfaces that should generate hits.
0056   hit_surface_selector_t selectHitSurface;
0057   /// Logger for debug output.
0058   std::unique_ptr<const Acts::Logger> logger;
0059 
0060   /// Alternatively construct the simulator with an external logger.
0061   SingleParticleSimulation(propagator_t &&propagator_,
0062                            std::unique_ptr<const Acts::Logger> _logger)
0063       : propagator(propagator_), logger(std::move(_logger)) {}
0064 
0065   /// Simulate a single particle without secondaries.
0066   ///
0067   /// @tparam generator_t is the type of the random number generator
0068   ///
0069   /// @param geoCtx is the geometry context to access surface geometries
0070   /// @param magCtx is the magnetic field context to access field values
0071   /// @param generator is the random number generator
0072   /// @param particle is the initial particle state
0073   /// @returns Simulated particle state, hits, and generated particles.
0074   template <typename generator_t>
0075   Acts::Result<SimulationResult> simulate(
0076       const Acts::GeometryContext &geoCtx,
0077       const Acts::MagneticFieldContext &magCtx, generator_t &generator,
0078       const Particle &particle) const {
0079     // propagator-related additional types
0080     using Actor = detail::SimulationActor<generator_t, decay_t, interactions_t,
0081                                           hit_surface_selector_t>;
0082     using Aborter = typename Actor::ParticleNotAlive;
0083     using Result = typename Actor::result_type;
0084     using Actions = Acts::ActionList<Actor>;
0085     using Abort = Acts::AbortList<Aborter, Acts::EndOfWorldReached>;
0086     using PropagatorOptions =
0087         typename propagator_t::template Options<Actions, Abort>;
0088 
0089     // Construct per-call options.
0090     PropagatorOptions options(geoCtx, magCtx);
0091     options.stepping.maxStepSize = maxStepSize;
0092     options.pathLimit = pathLimit;
0093     // setup the interactor as part of the propagator options
0094     auto &actor = options.actionList.template get<Actor>();
0095     actor.generator = &generator;
0096     actor.decay = decay;
0097     actor.interactions = interactions;
0098     actor.selectHitSurface = selectHitSurface;
0099     actor.initialParticle = particle;
0100 
0101     if (particle.hasReferenceSurface()) {
0102       auto result = propagator.propagate(
0103           particle.boundParameters(geoCtx).value(), options);
0104       if (!result.ok()) {
0105         return result.error();
0106       }
0107       auto &value = result.value().template get<Result>();
0108       return std::move(value);
0109     }
0110 
0111     auto result =
0112         propagator.propagate(particle.curvilinearParameters(), options);
0113     if (!result.ok()) {
0114       return result.error();
0115     }
0116     auto &value = result.value().template get<Result>();
0117     return std::move(value);
0118   }
0119 };
0120 
0121 /// A particle that failed to simulate.
0122 struct FailedParticle {
0123   /// Initial particle state of the failed particle.
0124   ///
0125   /// This must store the full particle state to be able to handle secondaries
0126   /// that are not in the input particle list. Otherwise they could not be
0127   /// referenced.
0128   Particle particle;
0129   /// The associated error code for this particular failure case.
0130   std::error_code error;
0131 };
0132 
0133 /// Multi-particle/event simulation.
0134 ///
0135 /// @tparam charged_selector_t Callable selector type for charged particles
0136 /// @tparam charged_simulator_t Single particle simulator for charged particles
0137 /// @tparam neutral_selector_t Callable selector type for neutral particles
0138 /// @tparam neutral_simulator_t Single particle simulator for neutral particles
0139 ///
0140 /// The selector types for charged and neutral particles **do not** need to
0141 /// check for the particle charge. This is done automatically by the simulator
0142 /// to ensure consistency.
0143 template <typename charged_selector_t, typename charged_simulator_t,
0144           typename neutral_selector_t, typename neutral_simulator_t>
0145 struct Simulation {
0146   charged_selector_t selectCharged;
0147   neutral_selector_t selectNeutral;
0148   charged_simulator_t charged;
0149   neutral_simulator_t neutral;
0150 
0151   /// Construct from the single charged/neutral particle simulators.
0152   Simulation(charged_simulator_t &&charged_, neutral_simulator_t &&neutral_)
0153       : charged(std::move(charged_)), neutral(std::move(neutral_)) {}
0154 
0155   /// Simulate multiple particles and generated secondaries.
0156   ///
0157   /// @param geoCtx is the geometry context to access surface geometries
0158   /// @param magCtx is the magnetic field context to access field values
0159   /// @param generator is the random number generator
0160   /// @param inputParticles contains all particles that should be simulated
0161   /// @param simulatedParticlesInitial contains initial particle states
0162   /// @param simulatedParticlesFinal contains final particle states
0163   /// @param hits contains all generated hits
0164   /// @retval Acts::Result::Error if there is a fundamental issue
0165   /// @retval Acts::Result::Success with all particles that failed to simulate
0166   ///
0167   /// @warning Particle-hit association is based on particle ids generated
0168   ///          during the simulation. This requires that all input particles
0169   ///          **must** have generation and sub-particle number set to zero.
0170   /// @note Parameter edge-cases can lead to errors in the underlying propagator
0171   ///       and thus to particles that fail to simulate. Here, full events are
0172   ///       simulated and the failure to simulate one particle should not be
0173   ///       considered a general failure of the simulator. Instead, a list of
0174   ///       particles that fail to simulate is provided to the user. It is the
0175   ///       users responsibility to handle them.
0176   /// @note Failed particles are removed from the regular output, i.e. they do
0177   ///       not appear in the simulated particles containers nor do they
0178   ///       generate hits.
0179   ///
0180   /// This takes all input particles and simulates those passing the selection
0181   /// using the appropriate simulator. All selected particle states including
0182   /// additional ones generated from interactions are stored in separate output
0183   /// containers; both the initial state at the production vertex and the final
0184   /// state after propagation are stored. Hits generated from selected input and
0185   /// generated particles are stored in the hit container.
0186   ///
0187   /// @tparam generator_t is the type of the random number generator
0188   /// @tparam input_particles_t is a Container for particles
0189   /// @tparam output_particles_t is a SequenceContainer for particles
0190   /// @tparam hits_t is a SequenceContainer for hits
0191   template <typename generator_t, typename input_particles_t,
0192             typename output_particles_t, typename hits_t>
0193   Acts::Result<std::vector<FailedParticle>> simulate(
0194       const Acts::GeometryContext &geoCtx,
0195       const Acts::MagneticFieldContext &magCtx, generator_t &generator,
0196       const input_particles_t &inputParticles,
0197       output_particles_t &simulatedParticlesInitial,
0198       output_particles_t &simulatedParticlesFinal, hits_t &hits) const {
0199     assert(
0200         (simulatedParticlesInitial.size() == simulatedParticlesFinal.size()) &&
0201         "Inconsistent initial sizes of the simulated particle containers");
0202 
0203     using SingleParticleSimulationResult = Acts::Result<SimulationResult>;
0204 
0205     std::vector<FailedParticle> failedParticles;
0206 
0207     for (const Particle &inputParticle : inputParticles) {
0208       // only consider simulatable particles
0209       if (!selectParticle(inputParticle)) {
0210         continue;
0211       }
0212       // required to allow correct particle id numbering for secondaries later
0213       if ((inputParticle.particleId().generation() != 0u) ||
0214           (inputParticle.particleId().subParticle() != 0u)) {
0215         return detail::SimulationError::eInvalidInputParticleId;
0216       }
0217 
0218       // Do a *depth-first* simulation of the particle and its secondaries,
0219       // i.e. we simulate all secondaries, tertiaries, ... before simulating
0220       // the next primary particle. Use the end of the output container as
0221       // a queue to store particles that should be simulated.
0222       //
0223       // WARNING the initial particle state output container will be modified
0224       //         during iteration. New secondaries are added to and failed
0225       //         particles might be removed. To avoid issues, access must always
0226       //         occur via indices.
0227       auto iinitial = simulatedParticlesInitial.size();
0228       simulatedParticlesInitial.push_back(inputParticle);
0229       for (; iinitial < simulatedParticlesInitial.size(); ++iinitial) {
0230         const auto &initialParticle = simulatedParticlesInitial[iinitial];
0231 
0232         // only simulatable particles are pushed to the container and here we
0233         // only need to switch between charged/neutral.
0234         SingleParticleSimulationResult result =
0235             SingleParticleSimulationResult::success({});
0236         if (initialParticle.charge() != Particle::Scalar{0}) {
0237           result = charged.simulate(geoCtx, magCtx, generator, initialParticle);
0238         } else {
0239           result = neutral.simulate(geoCtx, magCtx, generator, initialParticle);
0240         }
0241 
0242         if (!result.ok()) {
0243           // remove particle from output container since it was not simulated.
0244           simulatedParticlesInitial.erase(
0245               std::next(simulatedParticlesInitial.begin(), iinitial));
0246           // record the particle as failed
0247           failedParticles.push_back({initialParticle, result.error()});
0248           continue;
0249         }
0250 
0251         copyOutputs(result.value(), simulatedParticlesInitial,
0252                     simulatedParticlesFinal, hits);
0253         // since physics processes are independent, there can be particle id
0254         // collisions within the generated secondaries. they can be resolved by
0255         // renumbering within each sub-particle generation. this must happen
0256         // before the particle is simulated since the particle id is used to
0257         // associate generated hits back to the particle.
0258         renumberTailParticleIds(simulatedParticlesInitial, iinitial);
0259       }
0260     }
0261 
0262     // the overall function call succeeded, i.e. no fatal errors occurred.
0263     // yet, there might have been some particle for which the propagation
0264     // failed. thus, the successful result contains a list of failed particles.
0265     // sounds a bit weird, but that is the way it is.
0266     return failedParticles;
0267   }
0268 
0269  private:
0270   /// Select if the particle should be simulated at all.
0271   bool selectParticle(const Particle &particle) const {
0272     if (particle.charge() != Particle::Scalar{0}) {
0273       return selectCharged(particle);
0274     } else {
0275       return selectNeutral(particle);
0276     }
0277   }
0278 
0279   /// Copy results to output containers.
0280   ///
0281   /// @tparam particles_t is a SequenceContainer for particles
0282   /// @tparam hits_t is a SequenceContainer for hits
0283   template <typename particles_t, typename hits_t>
0284   void copyOutputs(const SimulationResult &result,
0285                    particles_t &particlesInitial, particles_t &particlesFinal,
0286                    hits_t &hits) const {
0287     // initial particle state was already pushed to the container before
0288     // store final particle state at the end of the simulation
0289     particlesFinal.push_back(result.particle);
0290     // move generated secondaries that should be simulated to the output
0291     std::copy_if(
0292         result.generatedParticles.begin(), result.generatedParticles.end(),
0293         std::back_inserter(particlesInitial),
0294         [this](const Particle &particle) { return selectParticle(particle); });
0295     std::copy(result.hits.begin(), result.hits.end(), std::back_inserter(hits));
0296   }
0297 
0298   /// Renumber particle ids in the tail of the container.
0299   ///
0300   /// Ensures particle ids are unique by modifying the sub-particle number
0301   /// within each generation.
0302   ///
0303   /// @param particles particle container in which particles are renumbered
0304   /// @param lastValid index of the last particle with a valid particle id
0305   ///
0306   /// @tparam particles_t is a SequenceContainer for particles
0307   ///
0308   /// @note This function assumes that all particles in the tail have the same
0309   ///       vertex numbers (primary/secondary) and particle number and are
0310   ///       ordered according to their generation number.
0311   ///
0312   template <typename particles_t>
0313   static void renumberTailParticleIds(particles_t &particles,
0314                                       std::size_t lastValid) {
0315     // iterate over adjacent pairs; potentially modify the second element.
0316     // assume e.g. a primary particle 2 with generation=subparticle=0 that
0317     // generates two secondaries during simulation. we have the following
0318     // ids (decoded as vertex|particle|generation|subparticle)
0319     //
0320     //     v|2|0|0, v|2|1|0, v|2|1|0
0321     //
0322     // where v represents the vertex numbers. this will be renumbered to
0323     //
0324     //     v|2|0|0, v|2|1|0, v|2|1|1
0325     //
0326     // if each secondary then generates two tertiaries we could have e.g.
0327     //
0328     //     v|2|0|0, v|2|1|0, v|2|1|1, v|2|2|0, v|2|2|1, v|2|2|0, v|2|2|1
0329     //
0330     // which would then be renumbered to
0331     //
0332     //     v|2|0|0, v|2|1|0, v|2|1|1, v|2|2|0, v|2|2|1, v|2|2|2, v|2|2|3
0333     //
0334     for (auto j = lastValid; (j + 1u) < particles.size(); ++j) {
0335       const auto prevId = particles[j].particleId();
0336       auto currId = particles[j + 1u].particleId();
0337       // NOTE primary/secondary vertex and particle are assumed to be equal
0338       // only renumber within one generation
0339       if (prevId.generation() != currId.generation()) {
0340         continue;
0341       }
0342       // ensure the sub-particle is strictly monotonic within a generation
0343       if (prevId.subParticle() < currId.subParticle()) {
0344         continue;
0345       }
0346       // sub-particle numbering must be non-zero
0347       currId.setSubParticle(prevId.subParticle() + 1u);
0348       particles[j + 1u] = particles[j + 1u].withParticleId(currId);
0349     }
0350   }
0351 };
0352 
0353 }  // namespace ActsFatras