Back to home page

EIC code displayed by LXR

 
 

    


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

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/GeantGdmlLoader.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <map>
0010 #include <string>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "corecel/Assert.hh"
0015 
0016 class G4LogicalVolume;
0017 class G4VPhysicalVolume;
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Load a GDML file into memory.
0024  *
0025  * The pointer treatment gives three options:
0026  * - \c ignore leaves names as they are imported by Geant4's GDML reader, which
0027  *   strips them from material/region names but leaves solid/logical/physical
0028  *   pointers in place.
0029  * - \c truncate lets the Geant4 GDML remove the pointers, which cuts
0030  *   everything after \c 0x including suffixes like \c _refl added during
0031  *   volume construction.
0032  * - \c remove uses a regular expression to remove pointers from volume names.
0033  *
0034  * The \c detectors option reads \c auxiliary tags in the \c structure that
0035  * have \c auxtype=SensDet and returns a multimap of strings to volume
0036  * pointers.
0037  */
0038 class GeantGdmlLoader
0039 {
0040   public:
0041     //!@{
0042     //! \name Type aliases
0043     using MapDetectors = std::multimap<std::string, G4LogicalVolume*>;
0044     //!@}
0045 
0046     //! How to handle pointers in volume names
0047     enum class PointerTreatment
0048     {
0049         ignore,  //!< Pointers will remain in the volume name
0050         truncate,  //!< All text after '0x' is removed
0051         remove,  //!< Only pointers are carefully removed
0052     };
0053 
0054     struct Options
0055     {
0056         //! Strip pointer extensions from solids/volumes
0057         PointerTreatment pointers{PointerTreatment::remove};
0058         //! Load sensitive detector map
0059         bool detectors{false};
0060     };
0061 
0062     struct Result
0063     {
0064         //! Self-owning pointer to the loaded top-level volume
0065         G4VPhysicalVolume* world{nullptr};
0066         //! If requested, load a sensitive detector map
0067         MapDetectors detectors;
0068     };
0069 
0070   public:
0071     //! Construct with options
0072     explicit GeantGdmlLoader(Options const& opts) : opts_{opts} {}
0073 
0074     //! Construct with defaults
0075     GeantGdmlLoader() : GeantGdmlLoader{Options{}} {}
0076 
0077     // Load a GDML file
0078     Result operator()(std::string const& filename) const;
0079 
0080   private:
0081     Options opts_;
0082 };
0083 
0084 //---------------------------------------------------------------------------//
0085 // Load a Geant4 geometry, excising pointers
0086 G4VPhysicalVolume* load_gdml(std::string const& filename);
0087 
0088 // Save a Geant4 geometry to a file
0089 void save_gdml(G4VPhysicalVolume const* world, std::string const& out_filename);
0090 
0091 //---------------------------------------------------------------------------//
0092 // INLINE DEFINITIONS
0093 //---------------------------------------------------------------------------//
0094 /*!
0095  * Load a Geant4 geometry, excising pointers.
0096  *
0097  * This provides a good default for using GDML in Celeritas.
0098  *
0099  * \return Geant4-owned world volume
0100  */
0101 inline G4VPhysicalVolume* load_gdml(std::string const& filename)
0102 {
0103     return GeantGdmlLoader()(filename).world;
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 #if !CELERITAS_USE_GEANT4
0108 
0109 inline auto GeantGdmlLoader::operator()(std::string const&) const -> Result
0110 {
0111     CELER_DISCARD(opts_);
0112     CELER_NOT_CONFIGURED("Geant4");
0113 }
0114 
0115 inline void save_gdml(G4VPhysicalVolume const*, std::string const&)
0116 {
0117     CELER_NOT_CONFIGURED("Geant4");
0118 }
0119 
0120 #endif
0121 
0122 //---------------------------------------------------------------------------//
0123 }  // namespace celeritas