Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-05 08:34:03

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 celeritas/ext/GeantImporter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 
0011 #include "corecel/Config.hh"
0012 
0013 #include "celeritas/io/ImportData.hh"
0014 #include "celeritas/io/ImporterInterface.hh"
0015 
0016 #include "GeantSetup.hh"
0017 
0018 // Geant4 forward declaration
0019 class G4VPhysicalVolume;  // IWYU pragma: keep
0020 class G4ParticleDefinition;  // IWYU pragma: keep
0021 
0022 namespace celeritas
0023 {
0024 //---------------------------------------------------------------------------//
0025 //! Only import a subset of available Geant4 data
0026 struct GeantImportDataSelection
0027 {
0028     //! Bit flags for selecting particles and process types
0029     using Flags = unsigned int;
0030     enum : unsigned int
0031     {
0032         none = 0x0,
0033         dummy = 0x1,  //!< Dummy particles+processes
0034         em_basic = 0x2,  //!< Electron, positron, gamma
0035         em_ex = 0x4,  //!< Extended EM particles
0036         optical = 0x8,  //!< Optical particles and processes
0037         em = em_basic | em_ex,  //!< Any EM
0038         hadron = 0x10,  //!< Hadronic particles and processes
0039     };
0040 
0041     Flags particles = em | optical;
0042     bool materials = true;
0043     Flags processes = em | optical;
0044 
0045     //! Change volume names to match exported GDML file
0046     bool unique_volumes = false;
0047 
0048     // TODO expand/set reader flags automatically based on loaded processes
0049     bool reader_data = true;
0050 
0051     //! Linear (default) or spline interpolation (for grids that support it)
0052     inp::Interpolation interpolation{};
0053 };
0054 
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Load problem data directly from Geant4.
0058  *
0059  * This can be used to circumvent ROOT as a serialization tool, whether to
0060  * simplify the toolchain or to integrate better with user frameworks. As much
0061  * data as possible is imported (subject to the data selection); downstream
0062  * Celeritas classes will validate the imported data as needed.
0063  *
0064  * \code
0065     GeantImporter import(GeantSetup("blah.gdml"));
0066     ImportData data = import();
0067    \endcode
0068  * or to import from an existing, initialized Geant4 state:
0069  * \code
0070  *  GeantImport import(world_volume);
0071     ImportData data = import();
0072  *  \endcode
0073  */
0074 class GeantImporter final : public ImporterInterface
0075 {
0076   public:
0077     //!@{
0078     //! \name Type aliases
0079     using DataSelection = GeantImportDataSelection;
0080     using SPGeantGeo = GeantSetup::SPGeantGeo;
0081     //!@}
0082 
0083   public:
0084     // Construct from global Geant4 geometry, assuming physics is loaded
0085     GeantImporter();
0086 
0087     // Construct by capturing a GeantSetup object
0088     explicit GeantImporter(GeantSetup&& setup);
0089 
0090     // Fill data from Geant4
0091     ImportData operator()(DataSelection const& selection);
0092 
0093     //! Fill all available data from Geant4
0094     ImportData operator()() final { return (*this)(DataSelection{}); }
0095 
0096     //! Get the constructed geometry if using setup (may be null)
0097     SPGeantGeo const& geo_params() const { return setup_.geo_params(); }
0098 
0099   private:
0100     // Optional setup if celeritas handles initialization
0101     GeantSetup setup_;
0102 };
0103 
0104 //---------------------------------------------------------------------------//
0105 
0106 ImportParticle import_particle(G4ParticleDefinition const& p);
0107 
0108 //---------------------------------------------------------------------------//
0109 // INLINE DEFINITIONS
0110 //---------------------------------------------------------------------------//
0111 inline constexpr bool operator==(GeantImporter::DataSelection const& lhs,
0112                                  GeantImporter::DataSelection const& rhs)
0113 {
0114     // clang-format off
0115     return    lhs.particles == rhs.particles
0116            && lhs.materials == rhs.materials
0117            && lhs.processes == rhs.processes
0118            && lhs.unique_volumes == rhs.unique_volumes
0119            && lhs.reader_data == rhs.reader_data;
0120     // clang-format on
0121 }
0122 
0123 inline bool operator!=(GeantImporter::DataSelection const& lhs,
0124                        GeantImporter::DataSelection const& rhs)
0125 {
0126     return !(lhs == rhs);
0127 }
0128 
0129 #if !CELERITAS_USE_GEANT4
0130 inline GeantImporter::GeantImporter()
0131 {
0132     CELER_NOT_CONFIGURED("Geant4");
0133 }
0134 
0135 inline GeantImporter::GeantImporter(GeantSetup&&)
0136 {
0137     CELER_NOT_CONFIGURED("Geant4");
0138 }
0139 
0140 inline ImportData GeantImporter::operator()(DataSelection const&)
0141 {
0142     CELER_ASSERT_UNREACHABLE();
0143 }
0144 #endif
0145 
0146 //---------------------------------------------------------------------------//
0147 }  // namespace celeritas