Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-29 07:33:10

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cmath>
0012 #include <numbers>
0013 
0014 namespace Acts::AngleHelpers {
0015 
0016 template <typename Scalar>
0017 struct EtaThetaConversionTraits {};
0018 
0019 /// Conversion limits for `float` eta/theta calculations.
0020 ///
0021 /// Defines safe min/max theta and eta values for `float` precision.
0022 template <>
0023 struct EtaThetaConversionTraits<float> {
0024   /// Minimum theta value for float precision
0025   static constexpr float minTheta = 1e-6f;
0026   /// Maximum theta value for float precision
0027   static constexpr float maxTheta = std::numbers::pi_v<float> - minTheta;
0028 
0029   /// Maximum eta value for float precision
0030   static constexpr float maxEta = 80.0f;
0031   /// Minimum eta value for float precision
0032   static constexpr float minEta = -maxEta;
0033 };
0034 
0035 /// Conversion limits for `double` eta/theta calculations.
0036 ///
0037 /// Defines safe min/max theta and eta values for `double` precision.
0038 template <>
0039 struct EtaThetaConversionTraits<double> {
0040   /// Minimum theta value for double precision
0041   static constexpr double minTheta = 1e-12;
0042   /// Maximum theta value for double precision
0043   static constexpr double maxTheta = std::numbers::pi - minTheta;
0044 
0045   /// Maximum eta value for double precision
0046   static constexpr double maxEta = 700.0;
0047   /// Minimum eta value for double precision
0048   static constexpr double minEta = -maxEta;
0049 };
0050 
0051 /// Calculate the pseudorapidity from the polar angle theta.
0052 ///
0053 /// This function aims to be FPE safe and returns infinity for theta values
0054 /// outside the floating point precision range.
0055 ///
0056 /// @param theta is the polar angle in radian towards the z-axis.
0057 ///
0058 /// @return the pseudorapidity towards the z-axis.
0059 template <typename Scalar>
0060 Scalar etaFromTheta(Scalar theta) {
0061   if (theta <= EtaThetaConversionTraits<Scalar>::minTheta) {
0062     return std::numeric_limits<Scalar>::infinity();
0063   }
0064   if (theta >= EtaThetaConversionTraits<Scalar>::maxTheta) {
0065     return -std::numeric_limits<Scalar>::infinity();
0066   }
0067 
0068   return -std::log(std::tan(theta / 2));
0069 }
0070 
0071 /// Calculate the polar angle theta from the pseudorapidity.
0072 ///
0073 /// This function aims to be FPE safe and returns 0/pi for eta values outside
0074 /// the floating point precision range.
0075 ///
0076 /// @param eta is the pseudorapidity towards the z-axis.
0077 ///
0078 /// @return the polar angle in radian towards the z-axis.
0079 template <typename Scalar>
0080 Scalar thetaFromEta(Scalar eta) {
0081   if (eta <= EtaThetaConversionTraits<Scalar>::minEta) {
0082     return std::numbers::pi_v<Scalar>;
0083   }
0084   if (eta >= EtaThetaConversionTraits<Scalar>::maxEta) {
0085     return 0;
0086   }
0087 
0088   return 2 * std::atan(std::exp(-eta));
0089 }
0090 
0091 }  // namespace Acts::AngleHelpers