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); if (dp>Float(M_PI)) dp-=Float(2*M_PI);
23  return (e1-e2)*(e1-e2) + dp*dp;
24  }
25 
26  // do not use it: always cut in deltaR2!
27  template<typename T1, typename T2>
28  constexpr auto deltaR(const T1 & t1, const T2 & t2) -> decltype(t1.eta()) {
29  return std::sqrt(deltaR2(t1,t2));
30  }
31 
32 
33  //prefer the above...
34  template <class T1, class T2, class T3, class T4>
35  constexpr
36  T1 deltaR2 (T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
37  T1 deta = eta1 - eta2;
38  T1 dphi = std::abs(phi1-phi2); if (dphi>T1(M_PI)) dphi-=T1(2*M_PI);
39  return deta*deta + dphi*dphi;
40  }
41 
42  // to be avoided
43  template <class T1, class T2, class T3, class T4>
44  constexpr
45  T1 deltaR (T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
46  return std::sqrt (deltaR2 (eta1, phi1, eta2, phi2));
47  }
48 
49 }
50 
51 // woderful! VI
52 using reco::deltaR2;
53 using reco::deltaR;
54 
55 // obsolete use lambdas (and cut in deltaR2!)
56 template<typename T1, typename T2 = T1>
57 struct DeltaR {
58  constexpr double operator()( const T1 & t1, const T2 & t2 ) const {
59  return reco::deltaR(t1, t2);
60  }
61 
62 };
63 #endif
Definition: DeltaR.py:1
T sqrt(T t)
Definition: SSEVec.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double p2[4]
Definition: TauolaWrapper.h:90
#define M_PI
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
constexpr double operator()(const T1 &t1, const T2 &t2) const
Definition: deltaR.h:58
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
fixed size matrix
double p1[4]
Definition: TauolaWrapper.h:89
#define constexpr