Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:37

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/ext/GeantSd.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <vector>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "corecel/cont/EnumArray.hh"
0015 #include "geocel/Types.hh"
0016 #include "celeritas/geo/GeoFwd.hh"
0017 #include "celeritas/user/StepInterface.hh"
0018 
0019 class G4LogicalVolume;
0020 class G4ParticleDefinition;
0021 
0022 namespace celeritas
0023 {
0024 //---------------------------------------------------------------------------//
0025 namespace inp
0026 {
0027 struct GeantSd;
0028 }
0029 namespace detail
0030 {
0031 class HitProcessor;
0032 }
0033 
0034 class ParticleParams;
0035 
0036 //---------------------------------------------------------------------------//
0037 /*!
0038  * Hit Geant4 sensitive detectors with Celeritas steps.
0039  *
0040  * Construction:
0041  * - Created during SharedParams::Initialize alongside the step collector
0042  * - Is shared across threads
0043  * - Finds all logical volumes that have SDs attached
0044  * - Maps those volumes to Celeritas geometry
0045  *
0046  * Because of low-level use of Geant4 allocators through the associated Geant4
0047  * objects, the hit processors \em must be allocated and deallocated on the
0048  * same thread in which they're used, so \c make_local_processor is deferred
0049  * until after construction and called in the \c LocalTransporter constructor.
0050  */
0051 class GeantSd final : public StepInterface
0052 {
0053   public:
0054     //!@{
0055     //! \name Type aliases
0056     using StepStateHostRef = HostRef<StepStateData>;
0057     using StepStateDeviceRef = DeviceRef<StepStateData>;
0058     using SPConstVecLV
0059         = std::shared_ptr<std::vector<G4LogicalVolume const*> const>;
0060     using HitProcessor = detail::HitProcessor;
0061     using SPProcessor = std::shared_ptr<HitProcessor>;
0062     using SPConstGeo = std::shared_ptr<GeoParams const>;
0063     using VecVolId = std::vector<VolumeId>;
0064     using VecParticle = std::vector<G4ParticleDefinition const*>;
0065     using StepPointBool = EnumArray<StepPoint, bool>;
0066     using Input = inp::GeantSd;
0067     //!@}
0068 
0069   public:
0070     // Construct with Celeritas objects for mapping
0071     GeantSd(SPConstGeo geo,
0072             ParticleParams const& par,
0073             Input const& setup,
0074             StreamId::size_type num_streams);
0075 
0076     CELER_DEFAULT_MOVE_DELETE_COPY(GeantSd);
0077 
0078     // Default destructor
0079     ~GeantSd();
0080 
0081     // Create local hit processor
0082     SPProcessor make_local_processor(StreamId sid);
0083 
0084     // Selection of data required for this interface
0085     Filters filters() const final;
0086 
0087     // Selection of data required for this interface
0088     StepSelection selection() const final { return selection_; }
0089 
0090     // Process CPU-generated hits
0091     void process_steps(HostStepState) final;
0092 
0093     // Process device-generated hits
0094     void process_steps(DeviceStepState) final;
0095 
0096     //// ACCESSORS ////
0097 
0098     //! Access the logical volumes that have SDs attached
0099     SPConstVecLV const& geant_vols() const { return geant_vols_; }
0100 
0101     //! Access the Celeritas volume IDs corresponding to the detectors
0102     VecVolId const& celer_vols() const { return celer_vols_; }
0103 
0104     //! Access mapped particles if recreating G4Tracks later
0105     VecParticle const& geant_particles() const { return particles_; }
0106 
0107     //! Whether detailed volume information is reconstructed
0108     StepPointBool const& locate_touchable() const { return locate_touchable_; }
0109 
0110   private:
0111     using VecLV = std::vector<G4LogicalVolume const*>;
0112 
0113     bool nonzero_energy_deposition_{};
0114     VecVolId celer_vols_;
0115 
0116     // Hit processor setup
0117     SPConstGeo geo_;
0118     SPConstVecLV geant_vols_;
0119     VecParticle particles_;
0120     StepSelection selection_;
0121     StepPointBool locate_touchable_{};
0122 
0123     std::vector<std::weak_ptr<HitProcessor>> processor_weakptrs_;
0124     std::vector<HitProcessor*> processors_;
0125 
0126     // Construct vecgeom/geant volumes
0127     void setup_volumes(GeoParams const& geo, Input const& setup);
0128     // Construct celeritas/geant particles
0129     void setup_particles(ParticleParams const& par);
0130 
0131     // Ensure thread-local hit processor exists and return it
0132     HitProcessor& get_local_hit_processor(StreamId);
0133 };
0134 
0135 #if !CELERITAS_USE_GEANT4
0136 
0137 inline GeantSd::GeantSd(SPConstGeo,
0138                         ParticleParams const&,
0139                         Input const&,
0140                         StreamId::size_type)
0141 {
0142     CELER_NOT_CONFIGURED("Geant4");
0143 }
0144 
0145 inline GeantSd::~GeantSd() = default;
0146 
0147 inline GeantSd::SPProcessor GeantSd::make_local_processor(StreamId)
0148 {
0149     CELER_ASSERT_UNREACHABLE();
0150 }
0151 
0152 inline GeantSd::Filters GeantSd::filters() const
0153 {
0154     CELER_ASSERT_UNREACHABLE();
0155 }
0156 
0157 inline void GeantSd::process_steps(HostStepState)
0158 {
0159     CELER_ASSERT_UNREACHABLE();
0160 }
0161 
0162 inline void GeantSd::process_steps(DeviceStepState)
0163 {
0164     CELER_ASSERT_UNREACHABLE();
0165 }
0166 
0167 #endif
0168 //---------------------------------------------------------------------------//
0169 }  // namespace celeritas