Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:03:15

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 orange/g4org/Scaler.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <utility>
0010 #include <G4ThreeVector.hh>
0011 #include <G4TwoVector.hh>
0012 
0013 #include "corecel/Assert.hh"
0014 #include "corecel/cont/Array.hh"
0015 #include "geocel/detail/LengthUnits.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace g4org
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Convert a unit from Geant4 scale to another.
0024  *
0025  * The input is the length scale of the original input in the new units.
0026  */
0027 class Scaler
0028 {
0029   public:
0030     //! Default scale to CLHEP units (mm)
0031     Scaler() : scale_{celeritas::lengthunits::millimeter} {}
0032 
0033     //! Scale with an explicit factor, probably for testing
0034     explicit Scaler(double sc) : scale_{sc} { CELER_EXPECT(scale_ > 0); }
0035 
0036     //! Multiply a value by the scale
0037     double operator()(double val) const { return val * scale_; }
0038 
0039     //! Convert and scale a 2D point
0040     Array<double, 2> operator()(G4TwoVector const& vec) const
0041     {
0042         return this->to<Array<double, 2>>(vec.x(), vec.y());
0043     }
0044 
0045     //! Convert and scale a 3D point
0046     Array<double, 3> operator()(G4ThreeVector const& vec) const
0047     {
0048         return this->to<Array<double, 3>>(vec.x(), vec.y(), vec.z());
0049     }
0050 
0051     //! Create an array or other object by scaling each argument
0052     template<class S, class... Ts>
0053     S to(Ts&&... args) const
0054     {
0055         return S{(*this)(std::forward<Ts>(args))...};
0056     }
0057 
0058   private:
0059     double scale_;
0060 };
0061 
0062 //---------------------------------------------------------------------------//
0063 }  // namespace g4org
0064 }  // namespace celeritas