ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitPhysics/Utils/interface/MatchingTools.h
Revision: 1.3
Committed: Thu Jun 18 16:51:04 2009 UTC (15 years, 10 months ago) by loizides
Content type: text/plain
Branch: MAIN
Changes since 1.2: +4 -4 lines
Log Message:
Default is to exclude oneself from the collection.

File Contents

# Content
1 //--------------------------------------------------------------------------------------------------
2 // $Id: MatchingTools.h,v 1.2 2009/05/18 06:31:23 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 *ClosestRPt(const V1 *v1, const Collection<V2> &col, Double_t max,
28 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
34 //------------------------------------------------------------------------------------------------
35 template<class V1, class V2>
36 const V2 *MatchingTools::Closest(const V1 *v1, const Collection<V2> &col, Double_t maxR,
37 Bool_t self)
38 {
39 // Return closest geometrical neighbor in eta-phi within maximum delta R of maxR.
40 // If self is kFALSE make sure that identical pointers are excluded.
41
42 if (!v1)
43 return 0;
44
45 const V2 *res = 0;
46 const UInt_t ents = col.GetEntries();
47 if (ents>0) {
48 Double_t dR = 1e30;
49 for (UInt_t i = 0; i<ents; ++i) {
50 const V2 *v2 = col.At(i);
51 if (!v2)
52 continue;
53 if (!self && (void*)v1==(void*)v2)
54 continue;
55 Double_t diff = MathUtils::DeltaR(*v1,*v2);
56 if ((diff<maxR) && (diff<dR)) {
57 res = v2;
58 dR = diff;
59 }
60 }
61 }
62 return res;
63 }
64
65 //------------------------------------------------------------------------------------------------
66 template<class V1, class V2>
67 const V2 *MatchingTools::ClosestRPt(const V1 *v1, const Collection<V2> &col, Double_t max,
68 Bool_t self)
69 {
70 // Return closest geometrical neighbor in eta-phi-pt within maximum delta of max.
71 // If self is kFALSE make sure that identical pointers are excluded.
72
73 if (!v1)
74 return 0;
75
76 const V2 *res = 0;
77 const UInt_t ents = col.GetEntries();
78 if (ents>0) {
79 Double_t d = 1e30;
80 for (UInt_t i = 0; i<ents; ++i) {
81 const V2 *v2 = col.At(i);
82 if (!v2)
83 continue;
84 if (!self && (void*)v1==(void*)v2)
85 continue;
86 Double_t diff = MathUtils::DeltaR2(*v1,*v2);
87 Double_t ptd = 1-v1->Pt()/v2->Pt();
88 diff += ptd*ptd;
89 diff = TMath::Sqrt(diff);
90 if ((diff<max) && (diff<d)) {
91 res = v2;
92 d = diff;
93 }
94 }
95 }
96 return res;
97 }
98
99 //------------------------------------------------------------------------------------------------
100 template<class V1, class V2>
101 TObjArray *MatchingTools::Closests(const V1 *v1, const Collection<V2> &col, Double_t maxR,
102 Bool_t self)
103 {
104 // Return closest geometrical neighbors in eta-phi within maximum delta R of maxR.
105 // If self is kFALSE make sure that identical pointers are excluded.
106
107 if (!v1)
108 return 0;
109
110 TObjArray *res = new TObjArray;
111 res->SetOwner(kFALSE);
112 const UInt_t ents = col.GetEntries();
113 for (UInt_t i = 0; i<ents; ++i) {
114 const V2 *v2 = col.At(i);
115 if (!v2)
116 continue;
117 if (!self && (void*)v1==(void*)v2)
118 continue;
119 Double_t diff = MathUtils::DeltaR(*v1,*v2);
120 if (diff<maxR)
121 res->Add(const_cast<V2*>(v2));
122 }
123 return res;
124 }
125 }
126 #endif