ATLAS Offline Software
P4Helpers.h
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // P4Helpers.h
8 // Header file for P4 helper functions
9 // Author: S.Binet<binet@cern.ch>
11 #ifndef FOURMOMUTILS_P4HELPERS_H
12 #define FOURMOMUTILS_P4HELPERS_H
13 
23 #include <cmath>
24 // CxxUtils includes
25 #include "CxxUtils/fpcompare.h" // for fpcompare::less
26 #include <algorithm> // for std::sort
27 #include <limits> // for std::numeric_limits
28 
29 namespace P4Helpers
30 
31 //Not object dependent methods
32 {
34 inline double deltaPhi(double phiA, double phiB) {
35  return -remainder(-phiA + phiB, 2 * M_PI);
36 }
37 
39 template <class Iterator_t, class Predicate_t>
40 inline void sort(Iterator_t itr, Iterator_t itrEnd, Predicate_t p) {
41  // Koenig's look-up at our rescue: handle correctly DataVector's
42  // special sort method. => We inject the std::sort into our namespace
43  using std::sort;
44  return sort(itr, itrEnd, p);
45 }
46 
48 template <class Container_t, class Predicate_t>
49 inline void sort(Container_t& container, Predicate_t p) {
50  return P4Helpers::sort(container.begin(), container.end(), p);
51 }
52 } // namespace P4Helpers
53 
54 #ifndef XAOD_ANALYSIS
55 // EventKernel includes
56 #include "EventKernel/I4Momentum.h"
57 
58 namespace P4Helpers
59 {
60  /*
61  * Old I4Momentum P4 Helpers see xAODP4Helpers for the xAOD::IParticle implementation
62  */
63 
65  inline
66  double deltaEta( const I4Momentum& p1, const I4Momentum& p2 )
67  {
68  double dEta(999);
69  if ( p1.kind() == I4Momentum::P4PXPYPZE && p2.kind() == I4Momentum::P4PXPYPZE )
70  {
71  const double mom1 = p1.p(); const double pz1 = p1.pz();
72  const double mom2 = p2.p(); const double pz2 = p2.pz();
73  if ( ( mom1 + pz1 ) * ( mom2 - pz2 ) > 0 )
74  dEta = -0.5 * std::log( ( mom1 - pz1 ) / ( mom1 + pz1 )
75  * ( mom2 + pz2 ) / ( mom2 - pz2 ) );
76  }
77  else if ( p1.kind() == I4Momentum::P4PXPYPZE )
78  {
79  const double mom1 = p1.p(); const double pz1 = p1.pz();
80  if ( mom1 + pz1 > 0 )
81  dEta = -0.5 * std::log( ( mom1 - pz1 ) / ( mom1 + pz1 ) ) - p2.eta();
82  }
83  else if ( p2.kind() == I4Momentum::P4PXPYPZE )
84  {
85  const double mom2 = p2.p(); const double pz2 = p2.pz();
86  if ( mom2 - pz2 > 0 )
87  dEta = p1.eta() - 0.5 * std::log( ( mom2 + pz2 ) / ( mom2 - pz2 ) );
88  }
89  else
90  {
91  dEta = p1.eta() - p2.eta();
92  }
93  return dEta;
94  }
95 
97  inline
98  double deltaEtaSq( const I4Momentum& p1, const I4Momentum& p2 )
99  {
100  double dEtaSq(999);
101  if ( p1.kind() == I4Momentum::P4PXPYPZE && p2.kind() == I4Momentum::P4PXPYPZE )
102  {
103  const double mom1 = p1.p(); const double pz1 = p1.pz();
104  const double mom2 = p2.p(); const double pz2 = p2.pz();
105  if ( ( mom1 + pz1 ) * ( mom2 - pz2 ) > 0 )
106  {
107  dEtaSq = std::log( ( mom1 - pz1 ) / ( mom1 + pz1 )
108  * ( mom2 + pz2 ) / ( mom2 - pz2 ) );
109  dEtaSq = 0.25 * dEtaSq * dEtaSq;
110  }
111  }
112  else if ( p1.kind() == I4Momentum::P4PXPYPZE )
113  {
114  const double mom1 = p1.p(); const double pz1 = p1.pz();
115  if ( mom1 + pz1 > 0 )
116  {
117  dEtaSq = - 0.5 * std::log( ( mom1 - pz1 ) / ( mom1 + pz1 ) ) - p2.eta();
118  dEtaSq = dEtaSq * dEtaSq;
119  }
120  }
121  else if ( p2.kind() == I4Momentum::P4PXPYPZE )
122  {
123  const double mom2 = p2.p(); const double pz2 = p2.pz();
124  if ( mom2 - pz2 > 0 )
125  {
126  dEtaSq = p1.eta() - 0.5 * std::log( ( mom2 + pz2 ) / ( mom2 - pz2 ) );
127  dEtaSq = dEtaSq * dEtaSq;
128  }
129  }
130  else
131  {
132  dEtaSq = p1.eta() - p2.eta();
133  dEtaSq = dEtaSq * dEtaSq;
134  }
135  return dEtaSq;
136  }
137 
139  inline
140  double deltaEta( const I4Momentum * const p1, const I4Momentum * const p2 )
141  {
142  return deltaEta(*p1, *p2);
143  }
144 
146  inline
147  double deltaPhi( const I4Momentum& p4, const double phi )
148  {
149  return P4Helpers::deltaPhi( p4.phi(), phi );
150  }
151 
153  inline
154  double deltaPhi( const I4Momentum & pA, const I4Momentum & pB )
155  {
156  double dPhi(999);
157  if ( pA.kind() == I4Momentum::P4PXPYPZE && pB.kind() == I4Momentum::P4PXPYPZE )
158  {
159  double xp = pA.px() * pB.px() ;
160  double xyp1 = 0;
161  if ( xp != 0 )
162  {
163  xyp1 = 1.0 + pA.py() * pB.py() / xp ;
164  dPhi = 0;
165  if ( xyp1 != 0 )
166  dPhi = atan( ( pA.py() / pA.px() - pB.py() / pB.px() ) / xyp1 );
167  if ( xyp1 < 0 )
168  {
169  if ( pA.py() / pA.px() > 0 )
170  dPhi += M_PI;
171  else
172  dPhi -= M_PI;
173  }
174  }
175  }
176  else
177  {
178  dPhi = remainder( -pA.phi() + pB.phi(), 2*M_PI );
179  }
180  return dPhi;
181  }
182 
184  inline
185  double deltaPhiSq( const I4Momentum & pA, const I4Momentum & pB )
186  {
187  double dPhiSq(999);
188  if ( pA.kind() == I4Momentum::P4PXPYPZE && pB.kind() == I4Momentum::P4PXPYPZE )
189  {
190  double cosPhi = ( pA.px() * pB.px() + pA.py() * pB.py() ) / pA.pt() / pB.pt();
191  double phi = acos(cosPhi);
192  dPhiSq = phi*phi;
193  }
194  else
195  {
196  dPhiSq = remainder( -pA.phi() + pB.phi(), 2*M_PI );
197  dPhiSq = dPhiSq * dPhiSq;
198  }
199  return dPhiSq;
200  }
201 
203  inline
204  double deltaPhi( const I4Momentum * const pA, const I4Momentum * const pB )
205  { return deltaPhi( pA->phi(), pB->phi() ); }
206 
208  inline
209  double deltaR( const I4Momentum& p4, double eta, double phi )
210  {
211  using std::sqrt;
212  const double dEta = p4.eta() - eta;
213  const double dPhi = P4Helpers::deltaPhi( p4, phi );
214  return sqrt( dEta*dEta + dPhi*dPhi );
215  }
216 
218  inline
219  double deltaR( const I4Momentum& pA, const I4Momentum& pB )
220  {
221  using std::sqrt;
222  const double dEtaSq = P4Helpers::deltaEtaSq( pA, pB );
223  const double dPhiSq = P4Helpers::deltaPhiSq( pA, pB );
224  return sqrt( dEtaSq + dPhiSq );
225  }
226 
228  inline
229  double deltaR( const I4Momentum * const pA, const I4Momentum * const pB )
230  { return P4Helpers::deltaR( *pA, *pB ); }
231 
235  inline
236  bool isInDeltaR( const I4Momentum& p1, const I4Momentum& p2,
237  double dR )
238  {
239  using std::abs;
240  using std::sqrt;
241  const double dPhi = abs( P4Helpers::deltaPhi(p1,p2) ); // in [-pi,pi)
242  if ( dPhi > dR ) return false; // <==
243  const double dEta = abs( P4Helpers::deltaEta(p1,p2) );
244  if ( dEta > dR ) return false; // <==
245  const double deltaR2 = dEta*dEta + dPhi*dPhi;
246  if ( deltaR2 > dR*dR ) return false; // <==
247  return true;
248  }
249 
251  inline
252  double invMass( const I4Momentum & pA, const I4Momentum & pB )
253  { return (pA.hlv()+pB.hlv()).m(); }
254 
256  inline
257  double invMass( const I4Momentum * const pA, const I4Momentum * const pB )
258  { return invMass( *pA, *pB ); }
259 
261  inline
262  double invMass( const I4Momentum & pA, const I4Momentum & pB,
263  const I4Momentum & pC, const I4Momentum & pD )
264  { return (pA.hlv()+pB.hlv()+pC.hlv()+pD.hlv()).m(); }
265 
267  inline
268  double invMass( const I4Momentum * const pA, const I4Momentum * const pB,
269  const I4Momentum * const pC, const I4Momentum * const pD )
270  { return invMass( *pA, *pB, *pC, *pD ); }
271 
272 
273 } //> namespace P4Helpers
274 
275 
276 #endif
277 
278 #endif // FOURMOMUTILS_P4HELPERS_H
I4Momentum::py
virtual double py() const =0
y component of momentum
I4Momentum
Definition: I4Momentum.h:31
P4Helpers::deltaEtaSq
double deltaEtaSq(const I4Momentum &p1, const I4Momentum &p2)
Computes efficiently .
Definition: P4Helpers.h:98
P4Helpers::invMass
double invMass(const I4Momentum &pA, const I4Momentum &pB)
invariant mass from two I4momentum references
Definition: P4Helpers.h:252
TRTCalib_cfilter.p1
p1
Definition: TRTCalib_cfilter.py:130
M_PI
#define M_PI
Definition: ActiveFraction.h:11
P4Helpers
P4Helpers provides static helper functions for kinematic calculation on objects deriving from I4Momen...
Definition: P4Helpers.h:32
xAOD::P4Helpers::deltaR2
double deltaR2(double rapidity1, double phi1, double rapidity2, double phi2)
from bare rapidity,phi
Definition: xAODP4Helpers.h:111
P4Helpers::isInDeltaR
bool isInDeltaR(const I4Momentum &p1, const I4Momentum &p2, double dR)
Check if 2 I4Momentum are in a cone.
Definition: P4Helpers.h:236
I4Momentum::pt
virtual double pt() const =0
transverse momentum
I4Momentum::hlv
virtual CLHEP::HepLorentzVector hlv() const =0
CLHEP HepLorentzVector.
I4Momentum.h
drawFromPickle.atan
atan
Definition: drawFromPickle.py:36
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
P4Helpers::deltaPhi
double deltaPhi(double phiA, double phiB)
delta Phi in range [-pi,pi[
Definition: P4Helpers.h:34
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
P4Helpers::deltaEta
double deltaEta(const I4Momentum &p1, const I4Momentum &p2)
Computes efficiently .
Definition: P4Helpers.h:66
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:538
fpcompare.h
Workaround x86 precision issues for FP inequality comparisons.
I4Momentum::eta
virtual double eta() const =0
pseudo rapidity
P4Helpers::deltaR
double deltaR(const I4Momentum &p4, double eta, double phi)
from 1 I4Momentum
Definition: P4Helpers.h:209
I4Momentum::phi
virtual double phi() const =0
phi in [-pi,pi[
I4Momentum::kind
virtual Kind kind() const =0
tells what kind of P4XYZT this is
P4Helpers::deltaPhiSq
double deltaPhiSq(const I4Momentum &pA, const I4Momentum &pB)
delta Phi squared in range ([-pi,pi[)^2 from two I4momentum references
Definition: P4Helpers.h:185
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
P4Helpers::sort
void sort(Iterator_t itr, Iterator_t itrEnd, Predicate_t p)
sort a container according to the given predicate
Definition: P4Helpers.h:40
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
P4Helpers::sort
void sort(Container_t &container, Predicate_t p)
sort a container according to the given predicate
Definition: P4Helpers.h:49
TauGNNUtils::Variables::Track::dEta
bool dEta(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:527
I4Momentum::px
virtual double px() const =0
x component of momentum
I4Momentum::P4PXPYPZE
@ P4PXPYPZE
Definition: I4Momentum.h:33