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/GeantStepPointView.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <G4StepPoint.hh>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/math/Quantity.hh"
0014 #include "geocel/g4/Convert.hh"
0015 #include "celeritas/UnitTypes.hh"
0016 
0017 #include "GeantParticleView.hh"
0018 #include "GeantUnits.hh"
0019 
0020 namespace celeritas
0021 {
0022 //---------------------------------------------------------------------------//
0023 /*!
0024  * Access and modify step point data from Geant4 with Celeritas units.
0025  *
0026  * This provides a uniform interface to G4StepPoint data using Celeritas types
0027  * and units. Geant4 data are all in double precision.
0028  */
0029 class GeantStepPointView
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using Energy = units::ClhepEnergy;
0035     using Length = lengthunits::ClhepLength;
0036     using Time = units::ClhepTime;
0037     using Speed = Quantity<units::CLight, double>;
0038     using real_type = double;
0039     //!@}
0040 
0041   public:
0042     // Construct from G4StepPoint
0043     explicit GeantStepPointView(G4StepPoint& step_point) : sp_(step_point) {}
0044 
0045     //!@{
0046     //! \name Accessors
0047 
0048     // Position in CLHEP length units (mm)
0049     inline Array<Length, 3> pos() const;
0050 
0051     // Momentum direction (unit vector)
0052     inline Array<double, 3> dir() const;
0053 
0054     // Kinetic energy [MeV]
0055     inline Energy energy() const;
0056 
0057     // Global time in CLHEP time units (ns)
0058     inline Time time() const;
0059 
0060     //! Statistical weight
0061     real_type weight() const { return sp_.GetWeight(); }
0062 
0063     //! Speed in units of the speed of light
0064     inline Speed speed() const;
0065 
0066     //!@}
0067     //!@{
0068     //! \name Mutators
0069 
0070     // Set position in CLHEP length units (mm)
0071     inline void pos(Array<Length, 3> const& position);
0072 
0073     // Set momentum direction (unit vector)
0074     inline void dir(Array<double, 3> const& direction);
0075 
0076     // Set kinetic energy [MeV]
0077     inline void energy(Energy kinetic_energy);
0078 
0079     // Set global time in CLHEP time units (ns)
0080     inline void time(Time global_time);
0081 
0082     // Set statistical weight
0083     void weight(real_type w) { sp_.SetWeight(w); }
0084 
0085     // Update attributes from logical volume
0086     void update_from_volume(G4LogicalVolume const& lv);
0087 
0088     // Update attributes from touchable's logical volume
0089     void update_from_volume();
0090 
0091     // Update mass and charge from particle definition
0092     void update_from_particle(GeantParticleView const& particle);
0093 
0094     // Clear unsupported attributes to invalid sentinel values
0095     void clear_unsupported();
0096 
0097     //!@}
0098 
0099     //! Access underlying G4 object
0100     G4StepPoint& step_point() { return sp_; }
0101 
0102   private:
0103     G4StepPoint& sp_;
0104 };
0105 
0106 //---------------------------------------------------------------------------//
0107 // INLINE DEFINITIONS
0108 //---------------------------------------------------------------------------//
0109 /*!
0110  * Get position in native Celeritas length units.
0111  */
0112 Array<GeantStepPointView::Length, 3> GeantStepPointView::pos() const
0113 {
0114     return make_quantity_array<Length>(to_array(sp_.GetPosition()));
0115 }
0116 
0117 //---------------------------------------------------------------------------//
0118 /*!
0119  * Get momentum direction.
0120  */
0121 Array<double, 3> GeantStepPointView::dir() const
0122 {
0123     return to_array(sp_.GetMomentumDirection());
0124 }
0125 
0126 //---------------------------------------------------------------------------//
0127 /*!
0128  * Get kinetic energy in MeV.
0129  */
0130 GeantStepPointView::Energy GeantStepPointView::energy() const
0131 {
0132     return Energy{sp_.GetKineticEnergy()};
0133 }
0134 
0135 //---------------------------------------------------------------------------//
0136 /*!
0137  * Get global time in native Celeritas time units.
0138  */
0139 GeantStepPointView::Time GeantStepPointView::time() const
0140 {
0141     return Time{sp_.GetGlobalTime()};
0142 }
0143 
0144 //---------------------------------------------------------------------------//
0145 /*!
0146  * Get speed as a fraction of the speed of light.
0147  *
0148  * Often denoted as a particle's beta speed.
0149  */
0150 GeantStepPointView::Speed GeantStepPointView::speed() const
0151 {
0152     return Speed{sp_.GetBeta()};
0153 }
0154 
0155 //---------------------------------------------------------------------------//
0156 /*!
0157  * Set position in native Celeritas length units.
0158  */
0159 void GeantStepPointView::pos(Array<Length, 3> const& position)
0160 {
0161     sp_.SetPosition(to_g4vector(value_as<Length>(position)));
0162 }
0163 
0164 //---------------------------------------------------------------------------//
0165 /*!
0166  * Set momentum direction.
0167  */
0168 void GeantStepPointView::dir(Array<double, 3> const& direction)
0169 {
0170     sp_.SetMomentumDirection(to_g4vector(direction));
0171 }
0172 
0173 //---------------------------------------------------------------------------//
0174 /*!
0175  * Set kinetic energy in MeV.
0176  */
0177 void GeantStepPointView::energy(Energy kinetic_energy)
0178 {
0179     CELER_EXPECT(kinetic_energy >= zero_quantity());
0180     sp_.SetKineticEnergy(kinetic_energy.value());
0181     // TODO: update speed based on mass, KE
0182 }
0183 
0184 //---------------------------------------------------------------------------//
0185 /*!
0186  * Set global time in native Celeritas time units.
0187  */
0188 void GeantStepPointView::time(Time global_time)
0189 {
0190     sp_.SetGlobalTime(global_time.value());
0191 }
0192 
0193 //---------------------------------------------------------------------------//
0194 }  // namespace celeritas