CMS 3D CMS Logo

deltaPhi.h
Go to the documentation of this file.
1 #ifndef DataFormats_Math_deltaPhi_h
2 #define DataFormats_Math_deltaPhi_h
3 /* function to compute deltaPhi
4  *
5  * Ported from original code in RecoJets
6  * by Fedor Ratnikov, FNAL
7  * stabilize range reduction
8  */
9 #include <cmath>
10 
11 namespace reco {
12 
13  // reduce to [-pi,pi]
14  template<typename T>
16  constexpr T o2pi = 1./(2.*M_PI);
17  if (std::abs(x) <= T(M_PI)) return x;
18  T n = std::round(x*o2pi);
19  return x - n*T(2.*M_PI);
20  }
21 
22  constexpr double deltaPhi(double phi1, double phi2) {
23  return reduceRange(phi1 - phi2);
24  }
25 
26  constexpr double deltaPhi(float phi1, double phi2) {
27  return deltaPhi(static_cast<double>(phi1), phi2);
28  }
29 
30  constexpr double deltaPhi(double phi1, float phi2) {
31  return deltaPhi(phi1, static_cast<double>(phi2));
32  }
33 
34 
35  constexpr float deltaPhi(float phi1, float phi2) {
36  return reduceRange(phi1 - phi2);
37  }
38 
39 
40  template<typename T1, typename T2>
41  constexpr auto deltaPhi(T1 const & t1, T2 const & t2)->decltype(deltaPhi(t1.phi(), t2.phi())) {
42  return deltaPhi(t1.phi(), t2.phi());
43  }
44 
45  template <typename T>
46  constexpr T deltaPhi (T phi1, T phi2) {
47  return reduceRange(phi1 - phi2);
48  }
49 }
50 
51 // lovely! VI
52 using reco::deltaPhi;
53 
54 template<typename T1, typename T2 = T1>
55 struct DeltaPhi {
56  constexpr
57  auto operator()(const T1 & t1, const T2 & t2)->decltype(reco::deltaPhi(t1, t2)) const {
58  return reco::deltaPhi(t1, t2);
59  }
60 };
61 
62 #endif
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
constexpr T reduceRange(T x)
Definition: deltaPhi.h:15
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
constexpr auto operator()(const T1 &t1, const T2 &t2) -> decltype(reco::deltaPhi(t1, t2)) const
Definition: deltaPhi.h:57
#define M_PI
fixed size matrix
long double T
#define constexpr