Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 09:54:43

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/detail/HitManager.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <memory>
0011 #include <vector>
0012 
0013 #include "geocel/Types.hh"
0014 #include "celeritas/geo/GeoFwd.hh"
0015 #include "celeritas/user/StepInterface.hh"
0016 
0017 class G4LogicalVolume;
0018 class G4ParticleDefinition;
0019 
0020 namespace celeritas
0021 {
0022 struct SDSetupOptions;
0023 class ParticleParams;
0024 
0025 namespace detail
0026 {
0027 //---------------------------------------------------------------------------//
0028 
0029 class HitProcessor;
0030 //---------------------------------------------------------------------------//
0031 /*!
0032  * Manage the conversion of hits from Celeritas to Geant4.
0033  *
0034  * Construction:
0035  * - Created during SharedParams::Initialize alongside the step collector
0036  * - Is shared across threads
0037  * - Finds *all* logical volumes that have SDs attached (TODO: add list of
0038  *   exclusions for SDs that are implemented natively on GPU)
0039  * - Maps those volumes to VecGeom geometry
0040  * - Creates a HitProcessor for each Geant4 thread
0041  *
0042  * \warning Because of low-level problems with Geant4 allocators, the hit
0043  * processors must be allocated and deallocated on the same thread in which
0044  * they're used.
0045  */
0046 class HitManager final : public StepInterface
0047 {
0048   public:
0049     //!@{
0050     //! \name Type aliases
0051     using StepStateHostRef = HostRef<StepStateData>;
0052     using StepStateDeviceRef = DeviceRef<StepStateData>;
0053     using SPConstVecLV
0054         = std::shared_ptr<std::vector<G4LogicalVolume const*> const>;
0055     using SPProcessor = std::shared_ptr<HitProcessor>;
0056     using VecVolId = std::vector<VolumeId>;
0057     using VecParticle = std::vector<G4ParticleDefinition const*>;
0058     //!@}
0059 
0060   public:
0061     // Construct with Celeritas objects for mapping
0062     HitManager(GeoParams const& geo,
0063                ParticleParams const& par,
0064                SDSetupOptions const& setup,
0065                StreamId::size_type num_streams);
0066 
0067     // Create local hit processor
0068     SPProcessor make_local_processor(StreamId sid);
0069 
0070     // Default destructor
0071     ~HitManager();
0072 
0073     // Selection of data required for this interface
0074     Filters filters() const final;
0075 
0076     // Selection of data required for this interface
0077     StepSelection selection() const final { return selection_; }
0078 
0079     // Process CPU-generated hits
0080     void process_steps(HostStepState) final;
0081 
0082     // Process device-generated hits
0083     void process_steps(DeviceStepState) final;
0084 
0085     //// ACCESSORS ////
0086 
0087     //! Access the logical volumes that have SDs attached
0088     SPConstVecLV const& geant_vols() const { return geant_vols_; }
0089 
0090     //! Access the Celeritas volume IDs corresponding to the detectors
0091     VecVolId const& celer_vols() const { return vecgeom_vols_; }
0092 
0093     //! Access mapped particles if recreating G4Tracks later
0094     VecParticle const& geant_particles() const { return particles_; }
0095 
0096   private:
0097     using VecLV = std::vector<G4LogicalVolume const*>;
0098 
0099     bool nonzero_energy_deposition_{};
0100     VecVolId vecgeom_vols_;
0101 
0102     // Hit processor setup
0103     SPConstVecLV geant_vols_;
0104     VecParticle particles_;
0105     StepSelection selection_;
0106     bool locate_touchable_{};
0107 
0108     std::vector<std::weak_ptr<HitProcessor>> processor_weakptrs_;
0109     std::vector<HitProcessor*> processors_;
0110 
0111     // Construct vecgeom/geant volumes
0112     void setup_volumes(GeoParams const& geo, SDSetupOptions const& setup);
0113     // Construct celeritas/geant particles
0114     void setup_particles(ParticleParams const& par);
0115 
0116     // Ensure thread-local hit processor exists and return it
0117     HitProcessor& get_local_hit_processor(StreamId);
0118 };
0119 
0120 //---------------------------------------------------------------------------//
0121 }  // namespace detail
0122 }  // namespace celeritas