Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59:57

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 geocel/g4/GeantGeoParams.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <string>
0010 
0011 #include "corecel/Macros.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/LabelIdMultiMap.hh"
0014 #include "corecel/data/ParamsDataInterface.hh"
0015 #include "geocel/BoundingBox.hh"
0016 #include "geocel/GeoParamsInterface.hh"
0017 #include "geocel/Types.hh"
0018 
0019 #include "GeantGeoData.hh"
0020 
0021 class G4VPhysicalVolume;
0022 
0023 namespace celeritas
0024 {
0025 //---------------------------------------------------------------------------//
0026 class ScopedGeantLogger;
0027 class ScopedGeantExceptionHandler;
0028 
0029 //---------------------------------------------------------------------------//
0030 /*!
0031  * Shared Geant4 geometry model wrapper.
0032  *
0033  * This can be constructed directly by loading a GDML file, or in-memory using
0034  * an existing physical volume. One "gotcha" is that due to persistent static
0035  * variables in Geant4, the volume IDs will be offset if a geometry has been
0036  * loaded and closed previously.
0037  */
0038 class GeantGeoParams final : public GeoParamsInterface,
0039                              public ParamsDataInterface<GeantGeoParamsData>
0040 {
0041   public:
0042     // Construct from a GDML filename
0043     explicit GeantGeoParams(std::string const& gdml_filename);
0044 
0045     // Create a VecGeom model from a pre-existing Geant4 geometry
0046     explicit GeantGeoParams(G4VPhysicalVolume const* world);
0047 
0048     CELER_DEFAULT_MOVE_DELETE_COPY(GeantGeoParams);
0049 
0050     // Clean up on destruction
0051     ~GeantGeoParams() final;
0052 
0053     //! Access the world volume
0054     G4VPhysicalVolume const* world() const { return host_ref_.world; }
0055 
0056     //! Whether safety distance calculations are accurate and precise
0057     bool supports_safety() const final { return true; }
0058 
0059     //! Outer bounding box of geometry
0060     BBox const& bbox() const final { return bbox_; }
0061 
0062     // Maximum nested scene/volume depth
0063     LevelId::size_type max_depth() const final { return max_depth_; }
0064 
0065     //// VOLUMES ////
0066 
0067     // Get (logical) volume metadata
0068     inline VolumeMap const& volumes() const final;
0069 
0070     // Get (physical) volume instance metadata
0071     inline VolInstanceMap const& volume_instances() const final;
0072 
0073     // Get the volume ID corresponding to a Geant4 logical volume
0074     VolumeId find_volume(G4LogicalVolume const* volume) const final;
0075 
0076     // Get the Geant4 physical volume corresponding to a volume instance ID
0077     GeantPhysicalInstance id_to_geant(VolumeInstanceId vol_id) const final;
0078 
0079     // Get the Geant4 logical volume corresponding to a volume ID
0080     G4LogicalVolume const* id_to_geant(VolumeId vol_id) const;
0081 
0082     // DEPRECATED
0083     using GeoParamsInterface::find_volume;
0084 
0085     //! Offset of logical volume ID after reloading geometry
0086     VolumeId::size_type lv_offset() const { return lv_offset_; }
0087 
0088     //! Offset of physical volume ID after reloading geometry
0089     VolumeId::size_type pv_offset() const { return pv_offset_; }
0090 
0091     //// DATA ACCESS ////
0092 
0093     //! Access geometry data on host
0094     HostRef const& host_ref() const final { return host_ref_; }
0095 
0096     //! No GPU support code
0097     DeviceRef const& device_ref() const final
0098     {
0099         CELER_NOT_IMPLEMENTED("Geant4 on GPU");
0100     }
0101 
0102   private:
0103     //// DATA ////
0104 
0105     bool loaded_gdml_{false};
0106     bool closed_geometry_{false};
0107     std::unique_ptr<ScopedGeantLogger> scoped_logger_;
0108     std::unique_ptr<ScopedGeantExceptionHandler> scoped_exceptions_;
0109 
0110     // Host metadata/access
0111     VolumeMap volumes_;
0112     VolInstanceMap vol_instances_;
0113     BBox bbox_;
0114     LevelId::size_type max_depth_{0};
0115     VolumeId::size_type lv_offset_{0};
0116     VolumeInstanceId::size_type pv_offset_{0};
0117 
0118     // Host/device storage and reference
0119     HostRef host_ref_;
0120 
0121     //// HELPER FUNCTIONS ////
0122 
0123     // Complete geometry construction
0124     void build_tracking();
0125 
0126     // Construct labels and other host-only metadata
0127     void build_metadata();
0128 };
0129 
0130 //---------------------------------------------------------------------------//
0131 // INLINE DEFINITIONS
0132 //---------------------------------------------------------------------------//
0133 /*!
0134  * Get volume metadata.
0135  *
0136  * Volumes correspond directly to Geant4 logical volumes.
0137  */
0138 auto GeantGeoParams::volumes() const -> VolumeMap const&
0139 {
0140     return volumes_;
0141 }
0142 
0143 //---------------------------------------------------------------------------//
0144 /*!
0145  * Get volume instance metadata.
0146  *
0147  * Volume instances correspond directly to Geant4 physical volumes.
0148  */
0149 auto GeantGeoParams::volume_instances() const -> VolInstanceMap const&
0150 {
0151     return vol_instances_;
0152 }
0153 
0154 //---------------------------------------------------------------------------//
0155 }  // namespace celeritas