Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:20

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2022-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 celeritas/ext/GeantImporter.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <memory>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "celeritas/io/ImportData.hh"
0015 #include "celeritas/io/ImporterInterface.hh"
0016 
0017 #include "GeantSetup.hh"
0018 
0019 // Geant4 forward declaration
0020 class G4VPhysicalVolume;  // 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 
0052 //---------------------------------------------------------------------------//
0053 /*!
0054  * Load problem data directly from Geant4.
0055  *
0056  * This can be used to circumvent ROOT as a serialization tool, whether to
0057  * simplify the toolchain or to integrate better with user frameworks. As much
0058  * data as possible is imported (subject to the data selection); downstream
0059  * Celeritas classes will validate the imported data as needed.
0060  *
0061  * \code
0062     GeantImporter import(GeantSetup("blah.gdml"));
0063     ImportData data = import();
0064    \endcode
0065  * or to import from an existing, initialized Geant4 state:
0066  * \code
0067  *  GeantImport import(world_volume);
0068     ImportData data = import();
0069  *  \endcode
0070  */
0071 class GeantImporter final : public ImporterInterface
0072 {
0073   public:
0074     //!@{
0075     //! \name Type aliases
0076     using DataSelection = GeantImportDataSelection;
0077     //!@}
0078 
0079   public:
0080     // Get the top-level geometry element from the run manager+navigator
0081     static G4VPhysicalVolume const* get_world_volume();
0082 
0083     // Construct from an existing Geant4 geometry, assuming physics is loaded
0084     explicit GeantImporter(G4VPhysicalVolume const* world);
0085 
0086     // Construct by capturing a GeantSetup object
0087     explicit GeantImporter(GeantSetup&& setup);
0088 
0089     // Fill data from Geant4
0090     ImportData operator()(DataSelection const& selection);
0091 
0092     //! Fill all available data from Geant4
0093     ImportData operator()() { return (*this)(DataSelection{}); }
0094 
0095   private:
0096     // Optional setup if celeritas handles initialization
0097     GeantSetup setup_;
0098     // World physical volume
0099     G4VPhysicalVolume const* world_{nullptr};
0100 
0101     //// HELPER FUNCTIONS ////
0102 
0103     std::vector<ImportVolume> import_volumes(bool unique_volumes) const;
0104 };
0105 
0106 //---------------------------------------------------------------------------//
0107 // INLINE DEFINITIONS
0108 //---------------------------------------------------------------------------//
0109 inline bool operator==(GeantImporter::DataSelection const& lhs,
0110                        GeantImporter::DataSelection const& rhs)
0111 {
0112     return lhs.particles == rhs.particles && lhs.processes == rhs.processes
0113            && lhs.reader_data == rhs.reader_data;
0114 }
0115 
0116 inline bool operator!=(GeantImporter::DataSelection const& lhs,
0117                        GeantImporter::DataSelection const& rhs)
0118 {
0119     return !(lhs == rhs);
0120 }
0121 
0122 #if !CELERITAS_USE_GEANT4
0123 inline G4VPhysicalVolume const* GeantImporter::get_world_volume()
0124 {
0125     CELER_NOT_CONFIGURED("Geant4");
0126 }
0127 
0128 inline GeantImporter::GeantImporter(G4VPhysicalVolume const*)
0129 {
0130     CELER_DISCARD(world_);
0131     CELER_NOT_CONFIGURED("Geant4");
0132 }
0133 
0134 inline GeantImporter::GeantImporter(GeantSetup&&)
0135 {
0136     CELER_NOT_CONFIGURED("Geant4");
0137 }
0138 
0139 inline ImportData GeantImporter::operator()(DataSelection const&)
0140 {
0141     CELER_ASSERT_UNREACHABLE();
0142 }
0143 #endif
0144 
0145 //---------------------------------------------------------------------------//
0146 }  // namespace celeritas