Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 09:55:27

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