Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:33

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2023-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 geocel/g4/GeantGeoParams.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <string>
0011 
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     // Clean up on destruction
0049     ~GeantGeoParams();
0050 
0051     //! Access the world volume
0052     G4VPhysicalVolume const* world() const { return host_ref_.world; }
0053 
0054     //! Whether safety distance calculations are accurate and precise
0055     bool supports_safety() const final { return true; }
0056 
0057     //! Outer bounding box of geometry
0058     BBox const& bbox() const final { return bbox_; }
0059 
0060     //// VOLUMES ////
0061 
0062     //! Number of volumes
0063     VolumeId::size_type num_volumes() const final
0064     {
0065         return vol_labels_.size();
0066     }
0067 
0068     // Get the label for a placed volume ID
0069     Label const& id_to_label(VolumeId vol_id) const final;
0070 
0071     //! \cond
0072     using GeoParamsInterface::find_volume;
0073     //! \endcond
0074 
0075     // Get the volume ID corresponding to a unique label name
0076     VolumeId find_volume(std::string const& name) const final;
0077 
0078     // Get the volume ID corresponding to a unique label
0079     VolumeId find_volume(Label const& label) const final;
0080 
0081     // Get the volume ID corresponding to a Geant4 logical volume
0082     VolumeId find_volume(G4LogicalVolume const* volume) const final;
0083 
0084     // Get zero or more volume IDs corresponding to a name
0085     SpanConstVolumeId find_volumes(std::string const& name) const final;
0086 
0087     // Get the Geant4 logical volume corresponding to a volume ID
0088     G4LogicalVolume const* id_to_lv(VolumeId vol_id) const;
0089 
0090     //// DATA ACCESS ////
0091 
0092     //! Access geometry data on host
0093     HostRef const& host_ref() const final { return host_ref_; }
0094 
0095     //! No GPU support code
0096     DeviceRef const& device_ref() const final
0097     {
0098         CELER_NOT_IMPLEMENTED("Geant4 on GPU");
0099     }
0100 
0101   private:
0102     //// DATA ////
0103 
0104     bool loaded_gdml_{false};
0105     bool closed_geometry_{false};
0106     std::unique_ptr<ScopedGeantLogger> scoped_logger_;
0107     std::unique_ptr<ScopedGeantExceptionHandler> scoped_exceptions_;
0108 
0109     // Host metadata/access
0110     LabelIdMultiMap<VolumeId> vol_labels_;
0111     BBox bbox_;
0112 
0113     // Host/device storage and reference
0114     HostRef host_ref_;
0115 
0116     //// HELPER FUNCTIONS ////
0117 
0118     // Complete geometry construction
0119     void build_tracking();
0120 
0121     // Construct labels and other host-only metadata
0122     void build_metadata();
0123 };
0124 
0125 //---------------------------------------------------------------------------//
0126 }  // namespace celeritas