ATLAS Offline Software
phihelper.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
10 #ifndef CXXUTILS_PHIHELPER_H
11 #define CXXUTILS_PHIHELPER_H
12 
13 #include <cmath>
14 #include <type_traits>
15 
16 namespace CxxUtils {
17 
23  template <typename T>
24  inline T wrapToPi(T phi)
25  {
26  static_assert(std::is_floating_point<T>::value);
27 
28  constexpr auto PI = static_cast<T>(M_PI);
29  // For large values this is faster:
30  if (phi < -100 || phi > 100) {
31  return std::remainder(phi, 2 * PI);
32  }
33  while (phi > PI) phi -= 2 * PI;
34  while (phi < -PI) phi += 2 * PI;
35  return phi;
36  }
37 
41  template <typename T>
42  inline T deltaPhi(T phiA, T phiB)
43  {
44  static_assert(std::is_floating_point<T>::value);
45  return wrapToPi(phiA - phiB);
46  }
47 
59  template <typename T>
60  inline T phiMean(T phiA, T phiB)
61  {
62  static_assert(std::is_floating_point<T>::value);
63  const T diff = wrapToPi(phiA - phiB);
64  return wrapToPi(phiB + 0.5 * diff);
65  }
66 
76  template <typename T>
77  inline T phiBisect(T phiA, T phiB)
78  {
79  static_assert(std::is_floating_point<T>::value);
80  T phi = 0.5 * (phiA + phiB);
81  if (phiA > phiB) phi += M_PI;
82  return wrapToPi(phi);
83  }
84 
85 } // namespace CxxUtils
86 
87 #endif
CxxUtils::wrapToPi
T wrapToPi(T phi)
Wrap angle in radians to [-pi, pi].
Definition: phihelper.h:24
M_PI
#define M_PI
Definition: ActiveFraction.h:11
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
athena.value
value
Definition: athena.py:124
PI
const float PI
Definition: test_isolaitonTool.cxx:61
CxxUtils::phiMean
T phiMean(T phiA, T phiB)
Calculate average of two angles.
Definition: phihelper.h:60
CxxUtils
Definition: aligned_vector.h:29
CxxUtils::phiBisect
T phiBisect(T phiA, T phiB)
Bisect (average) the angle spanned by phiA and phiB.
Definition: phihelper.h:77
remainder
std::vector< std::string > remainder(const std::vector< std::string > &v1, const std::vector< std::string > &v2)
list of entries in a vector that are not in another
Definition: compareFlatTrees.cxx:44
CxxUtils::deltaPhi
T deltaPhi(T phiA, T phiB)
Return difference phiA - phiB in range [-pi, pi].
Definition: phihelper.h:42