Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:37

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     //!@}
0081 
0082   public:
0083     // Get the top-level geometry element from the run manager+navigator
0084     static G4VPhysicalVolume const* get_world_volume();
0085 
0086     // Construct from an existing Geant4 geometry, assuming physics is loaded
0087     explicit GeantImporter(G4VPhysicalVolume const* world);
0088 
0089     // Construct by capturing a GeantSetup object
0090     explicit GeantImporter(GeantSetup&& setup);
0091 
0092     // Fill data from Geant4
0093     ImportData operator()(DataSelection const& selection);
0094 
0095     //! Fill all available data from Geant4
0096     ImportData operator()() final { return (*this)(DataSelection{}); }
0097 
0098   private:
0099     // Optional setup if celeritas handles initialization
0100     GeantSetup setup_;
0101     // World physical volume
0102     G4VPhysicalVolume const* world_{nullptr};
0103 };
0104 
0105 //---------------------------------------------------------------------------//
0106 
0107 std::vector<ImportVolume> import_volumes(G4VPhysicalVolume const& world);
0108 
0109 ImportParticle import_particle(G4ParticleDefinition const& p);
0110 
0111 //---------------------------------------------------------------------------//
0112 // INLINE DEFINITIONS
0113 //---------------------------------------------------------------------------//
0114 inline constexpr bool operator==(GeantImporter::DataSelection const& lhs,
0115                                  GeantImporter::DataSelection const& rhs)
0116 {
0117     // clang-format off
0118     return    lhs.particles == rhs.particles
0119            && lhs.materials == rhs.materials
0120            && lhs.processes == rhs.processes
0121            && lhs.unique_volumes == rhs.unique_volumes
0122            && lhs.reader_data == rhs.reader_data;
0123     // clang-format on
0124 }
0125 
0126 inline bool operator!=(GeantImporter::DataSelection const& lhs,
0127                        GeantImporter::DataSelection const& rhs)
0128 {
0129     return !(lhs == rhs);
0130 }
0131 
0132 #if !CELERITAS_USE_GEANT4
0133 inline G4VPhysicalVolume const* GeantImporter::get_world_volume()
0134 {
0135     CELER_NOT_CONFIGURED("Geant4");
0136 }
0137 
0138 inline GeantImporter::GeantImporter(G4VPhysicalVolume const*)
0139 {
0140     CELER_DISCARD(world_);
0141     CELER_NOT_CONFIGURED("Geant4");
0142 }
0143 
0144 inline GeantImporter::GeantImporter(GeantSetup&&)
0145 {
0146     CELER_NOT_CONFIGURED("Geant4");
0147 }
0148 
0149 inline ImportData GeantImporter::operator()(DataSelection const&)
0150 {
0151     CELER_ASSERT_UNREACHABLE();
0152 }
0153 #endif
0154 
0155 //---------------------------------------------------------------------------//
0156 }  // namespace celeritas