Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/accel/HepMC3PrimaryGenerator.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 accel/HepMC3PrimaryGenerator.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <deque>
0010 #include <memory>
0011 #include <mutex>
0012 #include <G4Event.hh>
0013 #include <G4VPrimaryGenerator.hh>
0014 
0015 #include "corecel/Config.hh"
0016 
0017 #include "corecel/Assert.hh"
0018 #include "corecel/Macros.hh"
0019 
0020 class G4VSolid;
0021 
0022 namespace HepMC3
0023 {
0024 class Reader;
0025 class GenEvent;
0026 }  // namespace HepMC3
0027 
0028 namespace celeritas
0029 {
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * HepMC3 reader class for sharing across threads.
0033  *
0034  * This class should be \em shared among threads so that events can be
0035  * correctly split up between them. It should be called from a user's primary
0036  * generator action:
0037  * \code
0038     void MyAction::GeneratePrimaries(G4Event* event)
0039     {
0040         CELER_TRY_HANDLE(generator_->GeneratePrimaryVertex(event),
0041                          ExceptionConverter("celer.event.generate"));
0042     }
0043  * \endcode
0044  *
0045  * \note This class assumes that all threads will be reading all events
0046  * sequentially and that events in the HepMC3 file are numbered sequentially
0047  * from zero.
0048  */
0049 class HepMC3PrimaryGenerator final : public G4VPrimaryGenerator
0050 {
0051   public:
0052     // Construct with HepMC3 filename
0053     explicit HepMC3PrimaryGenerator(std::string const& filename);
0054 
0055     CELER_DELETE_COPY_MOVE(HepMC3PrimaryGenerator);
0056     ~HepMC3PrimaryGenerator() final = default;
0057 
0058     // Add primaries to Geant4 event
0059     void GeneratePrimaryVertex(G4Event* g4_event) final;
0060 
0061     //! Get total number of events
0062     int NumEvents() { return static_cast<int>(num_events_); }
0063 
0064   private:
0065     using SPReader = std::shared_ptr<HepMC3::Reader>;
0066     using SPHepEvt = std::shared_ptr<HepMC3::GenEvent>;
0067     using size_type = std::size_t;
0068 
0069     size_type num_events_{0};  // Total number of events
0070     G4VSolid* world_solid_{nullptr};  // World volume solid
0071 
0072     SPReader reader_;  // HepMC3 input reader
0073     std::mutex read_mutex_;
0074     std::deque<SPHepEvt> event_buffer_;
0075     size_type start_event_{0};
0076     bool warned_mismatched_events_{false};
0077 
0078     // Read
0079     SPHepEvt read_event(size_type event_id);
0080 };
0081 
0082 //---------------------------------------------------------------------------//
0083 #if !CELERITAS_USE_HEPMC3
0084 inline HepMC3PrimaryGenerator::HepMC3PrimaryGenerator(std::string const&)
0085 {
0086     CELER_NOT_CONFIGURED("HepMC3");
0087     CELER_DISCARD(num_events_);
0088     CELER_DISCARD(world_solid_);
0089     CELER_DISCARD(reader_);
0090     CELER_DISCARD(read_mutex_);
0091     CELER_DISCARD(event_buffer_);
0092     CELER_DISCARD(start_event_);
0093     CELER_DISCARD(warned_mismatched_events_);
0094 }
0095 
0096 inline void HepMC3PrimaryGenerator::GeneratePrimaryVertex(G4Event*)
0097 {
0098     CELER_ASSERT_UNREACHABLE();
0099 }
0100 #endif
0101 
0102 //---------------------------------------------------------------------------//
0103 }  // namespace celeritas