ATLAS Offline Software
Loading...
Searching...
No Matches
AnalysisMisc.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ANALYSISUTILS_ANALYSISMISC_H
6#define ANALYSISUTILS_ANALYSISMISC_H
7
13
14#include <math.h>
15#include <vector>
16#include <algorithm>
17
18#include "CxxUtils/fpcompare.h"
21#include <type_traits>
22
23
24#ifndef XAOD_STANDALONE
25#include "CLHEP/Vector/LorentzVector.h"
27#endif
28
29namespace AnalysisUtils {
30
31#ifndef XAOD_STANDALONE
32
35 namespace Delta {
36
39 inline double phi (const INavigable4Momentum *p1, const INavigable4Momentum *p2) {
40 double phi1 = (p1->phi()>M_PI) ? p1->phi()-2*M_PI : p1->phi();
41 double phi2 = (p2->phi()>M_PI) ? p2->phi()-2*M_PI : p2->phi();
42 double dphi = fabs(phi1-phi2);
43 if(dphi>M_PI) dphi = 2*M_PI-dphi;
44 return dphi;
45 }
46
49 inline double R (const INavigable4Momentum *p1, const double v_eta, const double v_phi) {
50 double phi1 = (p1->phi()>M_PI) ? p1->phi()-2*M_PI : p1->phi();
51 double phi2 = (v_phi>M_PI) ? v_phi-2*M_PI : v_phi;
52 double dphi = fabs(phi1-phi2);
53 if(dphi>M_PI) dphi = 2*M_PI-dphi;
54 double deta = p1->eta() - v_eta;
55 return sqrt(dphi*dphi+deta*deta);
56 }
57
60 inline double R (const INavigable4Momentum *p1, const INavigable4Momentum *p2) {
61 return R (p1, p2->eta(), p2->phi());
62 }
63 } // end of Delta namespace
64
66
69 namespace Imass {
70
71 inline double two (const INavigable4Momentum *p1, const INavigable4Momentum *p2) {
72 return (p1->hlv()+p2->hlv()).m();
73 }
74
75 inline double four (const INavigable4Momentum *p1, const INavigable4Momentum *p2,
76 const INavigable4Momentum *p3, const INavigable4Momentum *p4) {
77 return (p1->hlv()+p2->hlv()+p3->hlv()+p4->hlv()).m();
78 }
79 } // end of Imass namespace
80
82
85 namespace Match {
86
92 template <class COLL>
93 inline bool R (const double eta, const double phi, COLL *coll, int &index, double &deltaR, const int pdg)
94 {
95 deltaR = 10000.; // big value
96 bool l_return = false;
97 int l_idx = 0;
98 typename COLL::const_iterator it = coll->begin();
99 typename COLL::const_iterator itE = coll->end();
100 for (; it != itE; ++it)
101 {
102 if (((*it)->pdgId()==pdg) || pdg==0 )
103 {
104 double rtu = Delta::R(*it,eta,phi);
105 if ( rtu < deltaR )
106 {
107 index = l_idx;
108 deltaR = rtu;
109 l_return = true;
110 }
111 }
112 ++l_idx;
113 }
114 return l_return;
115 }
116
122 template <class COLL>
123 inline bool R (const double eta, const double phi, const double e, COLL *coll,
124 int &index, double &deltaR, const int pdg, double &deltaE)
125 {
126 deltaR = 1.0e+20; // big value
127 deltaE = 1.0e+20;
128 bool l_return = false;
129 int l_idx = 0;
130 typename COLL::const_iterator it = coll->begin();
131 typename COLL::const_iterator itE = coll->end();
132 for (; it != itE; ++it)
133 {
134 if (((*it)->pdgId()==pdg) || pdg==0 )
135 {
136 double rtu = Delta::R(*it,eta,phi);
137 double dE = fabs( e-(*it)->e() );
138 if ( rtu < deltaR && dE < deltaE)
139 {
140 index = l_idx;
141 deltaR = rtu;
142 deltaE = dE;
143 l_return = true;
144 }
145 }
146 ++l_idx;
147 }
148 return l_return;
149 }
150
156 template <class COLL>
157 inline bool R (const INavigable4Momentum *t, COLL *coll, int &index, double &deltaR, const int pdg)
158 {
159 return R (t->eta(), t->phi(), coll, index, deltaR, pdg);
160 }
161
167 template <class COLL>
168 inline bool R (const INavigable4Momentum *t, COLL *coll, int &index, double &deltaR,
169 const int pdg, double &deltaE)
170 {
171 return R (t->eta(), t->phi(), t->e(), coll, index, deltaR, pdg, deltaE);
172 }
173
177 template <class COLL>
178 inline bool R (const double eta, const double phi, COLL *coll, int &index, double &deltaR)
179 {
180 deltaR = 10000.; // big value
181 bool l_return = false;
182 int l_idx = 0;
183 typename COLL::const_iterator it = coll->begin();
184 typename COLL::const_iterator itE = coll->end();
185 for (; it != itE; ++it)
186 {
187 double rtu = Delta::R(*it,eta,phi);
188 if ( rtu < deltaR )
189 {
190 index = l_idx;
191 deltaR = rtu;
192 l_return = true;
193 }
194 ++l_idx;
195 }
196 return l_return;
197 }
198
202 template <class COLL>
203 inline bool R (const double eta, const double phi, const double e, COLL *coll, int &index,
204 double &deltaR, double &deltaE)
205 {
206 deltaR = 1.0e+20; // big value
207 deltaE = 1.0e+20;
208 bool l_return = false;
209 int l_idx = 0;
210 typename COLL::const_iterator it = coll->begin();
211 typename COLL::const_iterator itE = coll->end();
212 for (; it != itE; ++it)
213 {
214 double rtu = Delta::R(*it,eta,phi);
215 double dE = fabs( e-(*it)->e() );
216 if ( rtu < deltaR && dE < deltaE)
217 {
218 index = l_idx;
219 deltaR = rtu;
220 deltaE = dE;
221 l_return = true;
222 }
223 ++l_idx;
224 }
225 return l_return;
226 }
227
231 template <class COLL>
232 inline bool R (const INavigable4Momentum *t, COLL *coll, int &index, double &deltaR)
233 {
234 return R (t->eta(), t->phi(), coll, index, deltaR);
235 }
236
240 template <class COLL>
241 inline bool R (const INavigable4Momentum *t, COLL *coll, int &index, double &deltaR, double &deltaE)
242 {
243 return R (t->eta(), t->phi(), t->e(), coll, index, deltaR, deltaE);
244 }
245
246 } // end of Math namespace
247
249
250#endif
251
254 namespace Sort {
255
256 namespace Private {
257
259
260 // function object for pT sorting
261 template <class T> inline bool compPt (T a, T b)
262 {
263 return greater (a->pt(), b->pt());
264 }
265
266 // function object for e sorting
267 template <class T> inline bool compE (T a, T b)
268 {
269 return greater (a->e(), b->e());
270 }
271
272 // function object for eta sorting
273 template <class T> inline bool compEta (T a, T b)
274 {
275 return greater (a->eta(), b->eta());
276 }
277
278 // function object for phi sorting
279 template <class T> inline bool compPhi (T a, T b)
280 {
281 return greater (a->phi(), b->phi());
282 }
283
284 template <bool b>
286 {
287 template <class COLL, class COMP>
288 static void dosort (COLL& coll, COMP comp)
289 { std::sort (coll.begin(), coll.end(), comp); }
290 };
291
292 template <>
293 struct dosort_imp<true>
294 {
295 template <class COLL, class COMP>
296 static void dosort (COLL& coll, COMP comp)
297 { coll.sort(comp); }
298 };
299
300 template <class COLL, class COMP>
301 inline void dosort (COLL& coll, COMP comp)
302 {
303 typedef typename std::remove_pointer<typename COLL::value_type>::type valtype;
304 typedef DataVector<valtype> dvtype;
305 const bool is_dv = std::is_convertible<COLL&, dvtype&>::value;
306 dosort_imp<is_dv>::dosort (coll, comp);
307 }
308
309 } // end of Private namespace
310
315 template <class COLL> inline void pT (COLL *coll)
316 {
317 // sort
319 }
320
325 template <class COLL> inline void e (COLL *coll)
326 {
327 // sort
329 }
330
335 template <class COLL> inline void eta (COLL *coll)
336 {
337 // sort
339 }
340
345 template <class COLL> inline void phi (COLL *coll)
346 {
347 // sort
349 }
350 } // end of Sort namespace
351
352#ifndef XAOD_STANDALONE
354
357 namespace Classify {
358
365 template <class COLL> inline void charge (COLL *coll,
366 std::vector<typename COLL::value_type> &pos,
367 std::vector<typename COLL::value_type> &neg)
368 {
369 pos.clear();
370 neg.clear();
371
372 // classify
373 typename COLL::const_iterator it = coll->begin();
374 typename COLL::const_iterator itE = coll->end();
375 for (; it != itE; ++it)
376 {
377 if ((*it)->pdgId() > 0)
378 pos.push_back(*it);
379 else if ((*it)->pdgId() < 0)
380 neg.push_back(*it);
381 }
382 }
383 } // end of Classify namespace
384
385#endif
386
387} // end of AnalysisUtils namespace
388
389#endif
#define M_PI
Scalar eta() const
pseudorapidity method
Scalar deltaR(const MatrixBase< Derived > &vec) const
Scalar phi() const
phi method
An STL vector of pointers that by default owns its pointed-to elements.
static Double_t a
Derived DataVector<T>.
Definition DataVector.h:795
virtual CLHEP::HepLorentzVector hlv() const =0
CLHEP HepLorentzVector.
Workaround x86 precision issues for FP inequality comparisons.
void charge(COLL *coll, std::vector< typename COLL::value_type > &pos, std::vector< typename COLL::value_type > &neg)
classify by charge
double R(const INavigable4Momentum *p1, const double v_eta, const double v_phi)
compute Invariant mass
double four(const INavigable4Momentum *p1, const INavigable4Momentum *p2, const INavigable4Momentum *p3, const INavigable4Momentum *p4)
double two(const INavigable4Momentum *p1, const INavigable4Momentum *p2)
find the closest element in a collection to an INavigable4Momentum
bool R(const double eta, const double phi, COLL *coll, int &index, double &deltaR, const int pdg)
find the closest element in R
void dosort(COLL &coll, COMP comp)
void pT(COLL *coll)
sort by pT
void e(COLL *coll)
sort by e
utility class to select combination of elements in a collection
bool greater(double a, double b)
Compare two FP numbers, working around x87 precision issues.
Definition fpcompare.h:140
Definition index.py:1
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
static void dosort(COLL &coll, COMP comp)
static void dosort(COLL &coll, COMP comp)