Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-27 09:25:10

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/GeantTrackReconstruction.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <vector>
0011 
0012 #include "corecel/Macros.hh"
0013 #include "celeritas/Types.hh"
0014 
0015 class G4ParticleDefinition;
0016 class G4Step;
0017 class G4Track;
0018 class G4VProcess;
0019 class G4VUserTrackInformation;
0020 
0021 namespace celeritas
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Manage track information for reconstruction.
0026  *
0027  * This class handles the bookkeeping of Geant4 track information needed
0028  * to reconstruct tracks during hit processing. It maintains mappings between
0029  * Celeritas PrimaryID and Geant4 track data.
0030  *
0031  * \par Usage
0032  * - \c init_event
0033  * - \c acquire (multiple times)
0034  * - \c view (may be interleaved with acquire)
0035  * - \c clear (once all active tracks are used up)
0036  * - then it can be initialized with a new event, or new primaries can be
0037  *   added to the current event.
0038  */
0039 class GeantTrackReconstruction
0040 {
0041   public:
0042     //!@{
0043     //! \name Type aliases
0044     using VecParticle = std::vector<G4ParticleDefinition const*>;
0045     using SPStep = std::shared_ptr<G4Step>;
0046     using EventIdGetter = int (*)();
0047     //!@}
0048 
0049   public:
0050     // Create a G4Step object with cleared data
0051     static SPStep make_g4step();
0052 
0053     // Construct with particle definitions for track reconstruction
0054     GeantTrackReconstruction(VecParticle const&, SPStep);
0055 
0056     ~GeantTrackReconstruction();
0057     CELER_DEFAULT_MOVE_DELETE_COPY(GeantTrackReconstruction);
0058 
0059     // Clear G4Track reconstruction data
0060     void clear();
0061 
0062     // Register mapping from Celeritas PrimaryID to Geant4 track ID
0063     [[nodiscard]] PrimaryId acquire(G4Track&);
0064 
0065     // Reset primary ID at each event start
0066     void init_event();
0067 
0068     // Restore track information for given primary and particle IDs
0069     [[nodiscard]] G4Track& view(ParticleId, PrimaryId) const;
0070 
0071     // View a track with the given particle ID
0072     [[nodiscard]] G4Track& view(ParticleId) const;
0073 
0074     // Event ID function pointer for unit testing (only used in
0075     // CELERITAS_DEBUG)
0076     static EventIdGetter get_current_event_id;
0077 
0078   private:
0079     //! Data needed to reconstruct a G4Track from Celeritas transport
0080     class AcquiredData
0081     {
0082       public:
0083         //! Save the G4Track reconstruction data
0084         explicit AcquiredData(G4Track&);
0085         //! Whether the data is valid
0086         explicit operator bool() const { return track_id_ >= 0; }
0087         //! Restore the G4Track from the reconstruction data
0088         void restore(G4Track&) const;
0089 
0090       private:
0091         //! Original Geant4 track ID
0092         int track_id_{-1};
0093         //! Original Geant4 parent ID
0094         int parent_id_{0};
0095         //! User track information
0096         std::unique_ptr<G4VUserTrackInformation> user_info_;
0097         //! Process that created the track
0098         G4VProcess const* creator_process_{nullptr};
0099     };
0100 
0101     //! G4Track reconstruction data indexed by Celeritas PrimaryID
0102     std::vector<AcquiredData> g4_track_data_;
0103     //! Tracks for each particle type
0104     std::vector<std::unique_ptr<G4Track>> tracks_;
0105     //! Shared step object
0106     SPStep step_;
0107     //! Starting primary id
0108     PrimaryId start_{0};
0109     //! Last G4 event ID for error checking
0110     int g4_event_id_{-1};
0111 };
0112 
0113 //---------------------------------------------------------------------------//
0114 }  // namespace celeritas