Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:11:32

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/g4/MagneticField.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <G4MagneticField.hh>
0011 
0012 #include "corecel/Assert.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/cont/Range.hh"
0015 #include "corecel/cont/Span.hh"
0016 #include "geocel/g4/Convert.hh"
0017 #include "celeritas/Quantities.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Wrap a Celeritas field as a Geant4 magnetic field.
0024  *
0025  * \tparam P params for creating field
0026  * \tparam F field calculator
0027  */
0028 template<class P, class F>
0029 class MagneticField : public G4MagneticField
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using SPConstParams = std::shared_ptr<P const>;
0035     //!@}
0036 
0037   public:
0038     // Construct with field params
0039     inline explicit MagneticField(SPConstParams params);
0040 
0041     // Calculate values of the magnetic field vector
0042     inline void GetFieldValue(G4double const xyzt[4],
0043                               G4double* field) const override;
0044 
0045   private:
0046     SPConstParams params_;
0047 };
0048 
0049 //---------------------------------------------------------------------------//
0050 // INLINE DEFINITIONS
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * Construct with the Celeritas shared params.
0054  */
0055 template<class P, class F>
0056 MagneticField<P, F>::MagneticField(SPConstParams params)
0057     : params_(std::move(params))
0058 {
0059     CELER_EXPECT(params_);
0060 }
0061 
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Calculate the magnetic field vector at the given position.
0065  */
0066 template<class P, class F>
0067 void MagneticField<P, F>::GetFieldValue(G4double const xyzt[4],
0068                                         G4double* field) const
0069 {
0070     F calc_field{params_->host_ref()};
0071 
0072     // Get the g4 position
0073     Span<G4double const, 3> pos(xyzt, 3);
0074 
0075     // Calculate the magnetic field value in the native Celeritas unit system
0076     Real3 field_native = calc_field(
0077         native_from_geant<units::ClhepLength, real_type>(to_array(pos)));
0078     for (auto i : range(3))
0079     {
0080         // Return values of the field vector in native geant4 units
0081         field[i] = native_to_geant<units::ClhepField>(field_native[i]);
0082     }
0083 }
0084 
0085 //---------------------------------------------------------------------------//
0086 }  // namespace celeritas