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/HitProcessor.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <memory>
0011 #include <string>
0012 #include <vector>
0013 #include <G4TouchableHandle.hh>
0014 
0015 #include "celeritas/Types.hh"
0016 #include "celeritas/user/DetectorSteps.hh"
0017 #include "celeritas/user/StepData.hh"
0018 
0019 class G4LogicalVolume;
0020 class G4Step;
0021 class G4Navigator;
0022 class G4ParticleDefinition;
0023 class G4Track;
0024 class G4VSensitiveDetector;
0025 
0026 namespace celeritas
0027 {
0028 struct StepSelection;
0029 struct DetectorStepOutput;
0030 
0031 namespace detail
0032 {
0033 //---------------------------------------------------------------------------//
0034 /*!
0035  * Transfer Celeritas sensitive detector hits to Geant4.
0036  *
0037  * This serves a similar purpose to the \c G4FastSimHitMaker class for
0038  * generating hit objects.
0039  *
0040  * \warning This class \b must be thread-local because the sensitive
0041  * detectors it points to are thread-local objects. Furthermore, Geant4
0042  * thread-local object allocators for the navigation state and tracks mean this
0043  * class \b must be destroyed on the same thread on which it was created.
0044  *
0045  * Call operator:
0046  * - Loop over detector steps
0047  * - Update step attributes based on hit selection for the detector (TODO:
0048  *   selection is global for now)
0049  * - Call the local detector (based on detector ID from map) with the step
0050  */
0051 class HitProcessor
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 VecParticle = std::vector<G4ParticleDefinition const*>;
0061     //!@}
0062 
0063   public:
0064     // Construct from volumes that have SDs and step selection
0065     HitProcessor(SPConstVecLV detector_volumes,
0066                  VecParticle const& particles,
0067                  StepSelection const& selection,
0068                  bool locate_touchable,
0069                  StreamId stream);
0070 
0071     // Log on destruction
0072     ~HitProcessor();
0073 
0074     // Process CPU-generated hits
0075     void operator()(StepStateHostRef const&);
0076 
0077     // Process device-generated hits
0078     void operator()(StepStateDeviceRef const&);
0079 
0080     // Generate and call hits from a detector output (for testing)
0081     void operator()(DetectorStepOutput const& out) const;
0082 
0083     // Access detector volume corresponding to an ID
0084     inline G4LogicalVolume const* detector_volume(DetectorId) const;
0085 
0086     // Access thread-local SD corresponding to an ID
0087     inline G4VSensitiveDetector* detector(DetectorId) const;
0088 
0089   private:
0090     //! Detector volumes for navigation updating
0091     SPConstVecLV detector_volumes_;
0092     //! Map detector IDs to sensitive detectors
0093     std::vector<G4VSensitiveDetector*> detectors_;
0094     //! Temporary CPU hit information
0095     DetectorStepOutput steps_;
0096 
0097     //! Temporary step
0098     std::unique_ptr<G4Step> step_;
0099     //! Tracks for each particle type
0100     std::vector<std::unique_ptr<G4Track>> tracks_;
0101     //! Navigator for finding points
0102     std::unique_ptr<G4Navigator> navi_;
0103     //! Geant4 reference-counted pointer to a G4VTouchable
0104     G4TouchableHandle touch_handle_;
0105 
0106     //! Post-step selection for copying to track
0107     StepPointSelection post_step_selection_;
0108 
0109     //! Stream ID
0110     StreamId stream_;
0111 
0112     void update_track(ParticleId id) const;
0113 };
0114 
0115 //---------------------------------------------------------------------------//
0116 // INLINE DEFINITIONS
0117 //---------------------------------------------------------------------------//
0118 /*!
0119  * Access detector volume corresponding to an ID.
0120  */
0121 G4LogicalVolume const* HitProcessor::detector_volume(DetectorId did) const
0122 {
0123     CELER_EXPECT(did < detector_volumes_->size());
0124     return (*detector_volumes_)[did.unchecked_get()];
0125 }
0126 
0127 //---------------------------------------------------------------------------//
0128 /*!
0129  * Access thread-local sensitive detector corresponding to an ID.
0130  */
0131 G4VSensitiveDetector* HitProcessor::detector(DetectorId did) const
0132 {
0133     CELER_EXPECT(did < detectors_.size());
0134     return detectors_[did.unchecked_get()];
0135 }
0136 
0137 //---------------------------------------------------------------------------//
0138 }  // namespace detail
0139 }  // namespace celeritas