Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:01

0001 #ifndef DataFormats_Math_deltaR_h
0002 #define DataFormats_Math_deltaR_h
0003 /* functions to compute deltaR
0004  *
0005  * Ported from original code in RecoJets 
0006  * by Fedor Ratnikov, FNAL
0007  */
0008 #include "DataFormats/Math/interface/deltaPhi.h"
0009 #include <cmath>
0010 
0011 namespace reco {
0012 
0013   // assumption is that eta and phi are cached AND phi is computed using std::atan2
0014   // type is the type of T1::eta();
0015   template <typename T1, typename T2>
0016   constexpr auto deltaR2(const T1& t1, const T2& t2) -> decltype(t1.eta()) {
0017     typedef decltype(t1.eta()) Float;
0018     Float p1 = t1.phi();
0019     Float p2 = t2.phi();
0020     Float e1 = t1.eta();
0021     Float e2 = t2.eta();
0022     auto dp = std::abs(p1 - p2);
0023     if (dp > Float(M_PI))
0024       dp -= Float(2 * M_PI);
0025     return (e1 - e2) * (e1 - e2) + dp * dp;
0026   }
0027 
0028   // do not use it: always cut in deltaR2!
0029   template <typename T1, typename T2>
0030   constexpr auto deltaR(const T1& t1, const T2& t2) -> decltype(t1.eta()) {
0031     return std::sqrt(deltaR2(t1, t2));
0032   }
0033 
0034   //prefer the above...
0035   template <class T1, class T2, class T3, class T4>
0036   constexpr T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
0037     T1 deta = eta1 - eta2;
0038     T1 dphi = std::abs(phi1 - phi2);
0039     if (dphi > T1(M_PI))
0040       dphi -= T1(2 * M_PI);
0041     return deta * deta + dphi * dphi;
0042   }
0043 
0044   // to be avoided
0045   template <class T1, class T2, class T3, class T4>
0046   constexpr T1 deltaR(T1 eta1, T2 phi1, T3 eta2, T4 phi2) {
0047     return std::sqrt(deltaR2(eta1, phi1, eta2, phi2));
0048   }
0049 
0050 }  // namespace reco
0051 
0052 // woderful!  VI
0053 using reco::deltaR;
0054 using reco::deltaR2;
0055 
0056 // obsolete use lambdas (and cut in deltaR2!)
0057 template <typename T1, typename T2 = T1>
0058 struct DeltaR {
0059   constexpr double operator()(const T1& t1, const T2& t2) const { return reco::deltaR(t1, t2); }
0060 };
0061 #endif