Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:29:26

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file accel/HepMC3PrimaryGenerator.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <deque>
0011 #include <memory>
0012 #include <mutex>
0013 #include <G4Event.hh>
0014 #include <G4VPrimaryGenerator.hh>
0015 
0016 #include "corecel/Config.hh"
0017 
0018 #include "corecel/Assert.hh"
0019 #include "corecel/Macros.hh"
0020 
0021 class G4VSolid;
0022 
0023 namespace HepMC3
0024 {
0025 class Reader;
0026 class GenEvent;
0027 }  // namespace HepMC3
0028 
0029 namespace celeritas
0030 {
0031 //---------------------------------------------------------------------------//
0032 /*!
0033  * HepMC3 reader class for sharing across threads.
0034  *
0035  * This singleton is shared among threads so that events can be correctly split
0036  * up between them, being constructed the first time `instance()` is invoked.
0037  * As this is a derived `G4VPrimaryGenerator` class, the HepMC3PrimaryGenerator
0038  * must be used by a concrete implementation of the
0039  * `G4VUserPrimaryGeneratorAction` class:
0040  * \code
0041    void PrimaryGeneratorAction::GeneratePrimaries(G4Event* event)
0042    {
0043        HepMC3PrimaryGenerator::Instance()->GeneratePrimaryVertex(event);
0044    }
0045  * \endcode
0046  */
0047 class HepMC3PrimaryGenerator final : public G4VPrimaryGenerator
0048 {
0049   public:
0050     // Construct with HepMC3 filename
0051     explicit HepMC3PrimaryGenerator(std::string const& filename);
0052 
0053     CELER_DELETE_COPY_MOVE(HepMC3PrimaryGenerator);
0054 
0055     //! Add primaries to Geant4 event
0056     void GeneratePrimaryVertex(G4Event* g4_event) final;
0057 
0058     //! Get total number of events
0059     int NumEvents() { return static_cast<int>(num_events_); }
0060 
0061   private:
0062     using SPReader = std::shared_ptr<HepMC3::Reader>;
0063     using SPHepEvt = std::shared_ptr<HepMC3::GenEvent>;
0064     using size_type = std::size_t;
0065 
0066     size_type num_events_{0};  // Total number of events
0067     G4VSolid* world_solid_{nullptr};  // World volume solid
0068 
0069     SPReader reader_;  // HepMC3 input reader
0070     std::mutex read_mutex_;
0071     std::deque<SPHepEvt> event_buffer_;
0072     size_type start_event_{0};
0073 
0074     // Read
0075     SPHepEvt read_event(size_type event_id);
0076 };
0077 
0078 //---------------------------------------------------------------------------//
0079 #if !CELERITAS_USE_HEPMC3
0080 inline HepMC3PrimaryGenerator::HepMC3PrimaryGenerator(std::string const&)
0081 {
0082     CELER_NOT_CONFIGURED("HepMC3");
0083     CELER_DISCARD(world_solid_);
0084     CELER_DISCARD(reader_);
0085     CELER_DISCARD(read_mutex_);
0086 }
0087 
0088 inline void HepMC3PrimaryGenerator::GeneratePrimaryVertex(G4Event*) {}
0089 #endif
0090 
0091 //---------------------------------------------------------------------------//
0092 }  // namespace celeritas