CMS 3D CMS Logo

deltaR.h
Go to the documentation of this file.
1 #ifndef DataFormats_Math_deltaR_h
2 #define DataFormats_Math_deltaR_h
3 /* functions to compute deltaR
4  *
5  * Ported from original code in RecoJets
6  * by Fedor Ratnikov, FNAL
7  */
9 #include <cmath>
10 
11 namespace reco {
12 
13  // assumption is that eta and phi are cached AND phi is computed using std::atan2
14  // type is the type of T1::eta();
15  template <typename T1, typename T2>
16  constexpr auto deltaR2(const T1& t1, const T2& t2) -> decltype(t1.eta()) {
17  typedef decltype(t1.eta()) Float;
18  Float p1 = t1.phi();
19  Float p2 = t2.phi();
20  Float e1 = t1.eta();
21  Float e2 = t2.eta();
22  auto dp = std::abs(p1 - p2);
23  if (dp > Float(M_PI))
24  dp -= Float(2 * M_PI);
25  return (e1 - e2) * (e1 - e2) + dp * dp;
26  }
27 
28  // do not use it: always cut in deltaR2!
29  template <typename T1, typename T2>
30  constexpr auto deltaR(const T1& t1, const T2& t2) -> decltype(t1.eta()) {
31  return std::sqrt(deltaR2(t1, t2));
32  }
33 
34  //prefer the above...
35  template <class T1, class T2, class T3, class T4>
36  constexpr T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
37  T1 deta = eta1 - eta2;
38  T1 dphi = std::abs(phi1 - phi2);
39  if (dphi > T1(M_PI))
40  dphi -= T1(2 * M_PI);
41  return deta * deta + dphi * dphi;
42  }
43 
44  // to be avoided
45  template <class T1, class T2, class T3, class T4>
46  constexpr T1 deltaR(T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
47  return std::sqrt(deltaR2(eta1, phi1, eta2, phi2));
48  }
49 
50 } // namespace reco
51 
52 // woderful! VI
53 using reco::deltaR;
54 using reco::deltaR2;
55 
56 // obsolete use lambdas (and cut in deltaR2!)
57 template <typename T1, typename T2 = T1>
58 struct DeltaR {
59  constexpr double operator()(const T1& t1, const T2& t2) const { return reco::deltaR(t1, t2); }
60 };
61 #endif
Definition: DeltaR.py:1
T sqrt(T t)
Definition: SSEVec.h:19
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define M_PI
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
fixed size matrix
constexpr double operator()(const T1 &t1, const T2 &t2) const
Definition: deltaR.h:59