1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: MatchingTools.h,v 1.4 2009/06/28 08:03:09 loizides Exp $
|
3 |
//
|
4 |
// MatchingTools
|
5 |
//
|
6 |
// This class implements a couple of simple geometrical matching functions.
|
7 |
//
|
8 |
// Authors: C.Loizides
|
9 |
//--------------------------------------------------------------------------------------------------
|
10 |
|
11 |
#ifndef MITPHYSICS_UTILS_MATCHINGTOOLS_H
|
12 |
#define MITPHYSICS_UTILS_MATCHINGTOOLS_H
|
13 |
|
14 |
#include <TObjArray.h>
|
15 |
#include <TMath.h>
|
16 |
#include "MitCommon/MathTools/interface/MathUtils.h"
|
17 |
#include "MitAna/DataCont/interface/Collection.h"
|
18 |
|
19 |
namespace mithep
|
20 |
{
|
21 |
class MatchingTools {
|
22 |
public:
|
23 |
template<class V1, class V2>
|
24 |
static const V2 *Closest(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
25 |
Bool_t self=kFALSE);
|
26 |
template<class V1, class V2>
|
27 |
static const V2 *Closest(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
28 |
Double_t maxPtDiff, Bool_t self=kFALSE);
|
29 |
template<class V1, class V2>
|
30 |
static TObjArray *Closests(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
31 |
Bool_t self=kFALSE);
|
32 |
|
33 |
ClassDef(MatchingTools, 0) // Geometric matching tools in eta-phi
|
34 |
};
|
35 |
|
36 |
//------------------------------------------------------------------------------------------------
|
37 |
template<class V1, class V2>
|
38 |
const V2 *MatchingTools::Closest(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
39 |
Bool_t self)
|
40 |
{
|
41 |
// Return closest geometrical neighbor in eta-phi within maximum DeltaR of maxR.
|
42 |
// If self is kFALSE make sure that identical pointers are excluded.
|
43 |
|
44 |
if (!v1)
|
45 |
return 0;
|
46 |
|
47 |
const V2 *res = 0;
|
48 |
const UInt_t ents = col.GetEntries();
|
49 |
if (ents>0) {
|
50 |
Double_t dR = 1e30;
|
51 |
for (UInt_t i = 0; i<ents; ++i) {
|
52 |
const V2 *v2 = col.At(i);
|
53 |
if (!v2)
|
54 |
continue;
|
55 |
if (!self && (void*)v1==(void*)v2)
|
56 |
continue;
|
57 |
Double_t diff = MathUtils::DeltaR(*v1,*v2);
|
58 |
if ((diff<maxR) && (diff<dR)) {
|
59 |
res = v2;
|
60 |
dR = diff;
|
61 |
}
|
62 |
}
|
63 |
}
|
64 |
return res;
|
65 |
}
|
66 |
|
67 |
//------------------------------------------------------------------------------------------------
|
68 |
template<class V1, class V2>
|
69 |
const V2 *MatchingTools::Closest(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
70 |
Double_t maxPtDiff, Bool_t self)
|
71 |
{
|
72 |
// Return closest geometrical neighbor in eta-phi within maximum DeltaR of maxR.
|
73 |
// Exclude particles where the relative pt differs by more than maxPtDiff.
|
74 |
// If self is kFALSE make sure that identical pointers are excluded.
|
75 |
|
76 |
if (!v1)
|
77 |
return 0;
|
78 |
|
79 |
const V2 *res = 0;
|
80 |
const UInt_t ents = col.GetEntries();
|
81 |
if (ents>0) {
|
82 |
Double_t dR = 1e30;
|
83 |
for (UInt_t i = 0; i<ents; ++i) {
|
84 |
const V2 *v2 = col.At(i);
|
85 |
if (!v2)
|
86 |
continue;
|
87 |
if (!self && (void*)v1==(void*)v2)
|
88 |
continue;
|
89 |
Double_t ptd = TMath::Abs(1-v2->Pt()/v1->Pt());
|
90 |
if (ptd>maxPtDiff)
|
91 |
continue;
|
92 |
Double_t diff = MathUtils::DeltaR(*v1,*v2);
|
93 |
if ((diff<maxR) && (diff<dR)) {
|
94 |
res = v2;
|
95 |
dR = diff;
|
96 |
}
|
97 |
}
|
98 |
}
|
99 |
return res;
|
100 |
}
|
101 |
|
102 |
//------------------------------------------------------------------------------------------------
|
103 |
template<class V1, class V2>
|
104 |
TObjArray *MatchingTools::Closests(const V1 *v1, const Collection<V2> &col, Double_t maxR,
|
105 |
Bool_t self)
|
106 |
{
|
107 |
// Return closest geometrical neighbors in eta-phi within maximum DeltaR of maxR.
|
108 |
// If self is kFALSE make sure that identical pointers are excluded.
|
109 |
|
110 |
if (!v1)
|
111 |
return 0;
|
112 |
|
113 |
TObjArray *res = new TObjArray;
|
114 |
res->SetOwner(kFALSE);
|
115 |
const UInt_t ents = col.GetEntries();
|
116 |
for (UInt_t i = 0; i<ents; ++i) {
|
117 |
const V2 *v2 = col.At(i);
|
118 |
if (!v2)
|
119 |
continue;
|
120 |
if (!self && (void*)v1==(void*)v2)
|
121 |
continue;
|
122 |
Double_t diff = MathUtils::DeltaR(*v1,*v2);
|
123 |
if (diff<maxR)
|
124 |
res->Add(const_cast<V2*>(v2));
|
125 |
}
|
126 |
return res;
|
127 |
}
|
128 |
}
|
129 |
#endif
|