Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:11:02

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file celeritas/phys/PrimaryGenerator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <functional>
0010 #include <memory>
0011 #include <random>
0012 #include <vector>
0013 
0014 #include "celeritas/Types.hh"
0015 #include "celeritas/io/EventIOInterface.hh"
0016 
0017 #include "PDGNumber.hh"
0018 #include "PrimaryGeneratorOptions.hh"
0019 
0020 namespace celeritas
0021 {
0022 //---------------------------------------------------------------------------//
0023 class ParticleParams;
0024 struct Primary;
0025 namespace inp
0026 {
0027 struct PrimaryGenerator;
0028 }
0029 
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * Generate a vector of primaries.
0033  *
0034  * This simple helper class can be used to generate primary particles of one or
0035  * more particle types with the energy, position, and direction sampled from
0036  * distributions. If more than one PDG number is specified, an equal number of
0037  * each particle type will be produced. Each \c operator() call will return a
0038  * single event until \c num_events events have been generated.
0039  *
0040  * \todo Refactor generators so that event ID is an input and vector of
0041  * primaries (which won't have an event ID) is output.
0042  */
0043 class PrimaryGenerator : public EventReaderInterface
0044 {
0045   public:
0046     //!@{
0047     //! \name Type aliases
0048     using PrimaryGeneratorEngine = std::mt19937;
0049     using EnergySampler = std::function<real_type(PrimaryGeneratorEngine&)>;
0050     using PositionSampler = std::function<Real3(PrimaryGeneratorEngine&)>;
0051     using DirectionSampler = std::function<Real3(PrimaryGeneratorEngine&)>;
0052     using SPConstParticles = std::shared_ptr<ParticleParams const>;
0053     using result_type = std::vector<Primary>;
0054     using Input = inp::PrimaryGenerator;
0055     //!@}
0056 
0057   public:
0058     // Construct from user input (deprecated)
0059     static PrimaryGenerator
0060     from_options(SPConstParticles, PrimaryGeneratorOptions const&);
0061 
0062     // Construct from shared particle data and new input
0063     PrimaryGenerator(Input const&, ParticleParams const& particles);
0064 
0065     // Construct from particle IDs and new input
0066     PrimaryGenerator(Input const&, std::vector<ParticleId> particle_ids);
0067 
0068     //! Prevent copying and moving
0069     CELER_DELETE_COPY_MOVE(PrimaryGenerator);
0070     ~PrimaryGenerator() override = default;
0071 
0072     // Generate primary particles from a single event
0073     result_type operator()() final;
0074 
0075     //! Get total number of events
0076     size_type num_events() const override { return num_events_; }
0077 
0078     // Reseed RNG for interaction with celer-g4
0079     void seed(UniqueEventId);
0080 
0081   private:
0082     size_type num_events_{};
0083     size_type primaries_per_event_{};
0084     unsigned int seed_{};
0085     EnergySampler sample_energy_;
0086     PositionSampler sample_pos_;
0087     DirectionSampler sample_dir_;
0088     std::vector<ParticleId> particle_id_;
0089     size_type event_count_{0};
0090     PrimaryGeneratorEngine rng_;
0091 };
0092 //---------------------------------------------------------------------------//
0093 }  // namespace celeritas