CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions
DeltaR Namespace Reference

Functions

def bestMatch
 
def cleanObjectCollection
 
def cleanObjectCollection2
 
def deltaPhi
 
def deltaR
 
def deltaR2
 
def matchObjectCollection
 
def matchObjectCollection2
 
def matchObjectCollection3
 

Function Documentation

def DeltaR.bestMatch (   object,
  matchCollection 
)
Return the best match to object in matchCollection, which is the closest object in delta R

Definition at line 118 of file DeltaR.py.

References deltaR2().

Referenced by matchObjectCollection().

119 def bestMatch( object, matchCollection):
120  '''Return the best match to object in matchCollection, which is the closest object in delta R'''
121  deltaR2Min = float('+inf')
122  bm = None
123  for match in matchCollection:
124  dR2 = deltaR2( object.eta(), object.phi(),
125  match.eta(), match.phi() )
126  if dR2 < deltaR2Min:
127  deltaR2Min = dR2
128  bm = match
129  return bm, deltaR2Min
130 
def bestMatch
Definition: DeltaR.py:118
def deltaR2
Definition: DeltaR.py:6
def DeltaR.cleanObjectCollection (   objects,
  masks,
  deltaRMin 
)
Masks objects using a deltaR cut.

Definition at line 96 of file DeltaR.py.

References deltaR2().

96 
97 def cleanObjectCollection( objects, masks, deltaRMin ):
98  '''Masks objects using a deltaR cut.'''
99  if len(objects)==0 or len(masks)==0:
100  return objects, []
101  deltaR2Min = deltaRMin*deltaRMin
102  cleanObjects = []
103  dirtyObjects = []
104  for object in objects:
105  ok = True
106  for mask in masks:
107  dR2 = deltaR2( object.eta(), object.phi(),
108  mask.eta(), mask.phi() )
109  if dR2 < deltaR2Min:
110  ok = False
111  if ok:
112  cleanObjects.append( object )
113  else:
114  dirtyObjects.append( object )
115  return cleanObjects, dirtyObjects
116 
117 
def cleanObjectCollection
Definition: DeltaR.py:96
def deltaR2
Definition: DeltaR.py:6
def DeltaR.cleanObjectCollection2 (   objects,
  masks,
  deltaRMin 
)
Masks objects using a deltaR cut, another algorithm (same results).

Definition at line 69 of file DeltaR.py.

References deltaR2().

69 
70 def cleanObjectCollection2( objects, masks, deltaRMin ):
71  '''Masks objects using a deltaR cut, another algorithm (same results).'''
72  if len(objects)==0:
73  return objects
74  deltaR2Min = deltaRMin*deltaRMin
75  cleanObjects = copy.copy( objects )
76  for mask in masks:
77  tooClose = []
78  for idx, object in enumerate(cleanObjects):
79  dR2 = deltaR2( object.eta(), object.phi(),
80  mask.eta(), mask.phi() )
81  if dR2 < deltaR2Min:
82  tooClose.append( idx )
83  nRemoved = 0
84  for idx in tooClose:
85  # yes, everytime an object is removed, the list of objects is updated
86  # so need to update the index accordingly.
87  # example: to remove : ele 1 and 2
88  # first, ele 1 is removed
89  # -> ele 2 is now at index 1
90  # one should again remove the element at index 1
91  idx -= nRemoved
92  del cleanObjects[idx]
93  nRemoved += 1
94  return cleanObjects
95 
def deltaR2
Definition: DeltaR.py:6
def cleanObjectCollection2
Definition: DeltaR.py:69
def DeltaR.deltaPhi (   p1,
  p2 
)
Computes delta phi, handling periodic limit conditions.

Definition at line 16 of file DeltaR.py.

Referenced by deltaR2().

16 
17 def deltaPhi( p1, p2):
18  '''Computes delta phi, handling periodic limit conditions.'''
19  res = p1 - p2
20  while res > math.pi:
21  res -= 2*math.pi
22  while res < -math.pi:
23  res += 2*math.pi
24  return res
def deltaPhi
Definition: DeltaR.py:16
def DeltaR.deltaR (   args)

Definition at line 12 of file DeltaR.py.

References deltaR2().

12 
13 def deltaR( *args ):
14  return math.sqrt( deltaR2(*args) )
15 
def deltaR2
Definition: DeltaR.py:6
def deltaR
Definition: DeltaR.py:12
def DeltaR.deltaR2 (   e1,
  p1,
  e2,
  p2 
)

Definition at line 6 of file DeltaR.py.

References deltaPhi().

Referenced by bestMatch(), cleanObjectCollection(), cleanObjectCollection2(), and deltaR().

6 
7 def deltaR2( e1, p1, e2, p2):
8  de = e1 - e2
9  dp = deltaPhi(p1, p2)
10  return de*de + dp*dp
11 
def deltaR2
Definition: DeltaR.py:6
def deltaPhi
Definition: DeltaR.py:16
def DeltaR.matchObjectCollection (   objects,
  matchCollection,
  deltaR2Max 
)

Definition at line 131 of file DeltaR.py.

References bestMatch(), and ComparisonHelper.zip().

132 def matchObjectCollection( objects, matchCollection, deltaR2Max):
133  pairs = {}
134  if len(objects)==0:
135  return pairs
136  if len(matchCollection)==0:
137  return dict( zip(objects, [None]*len(objects)) )
138  for object in objects:
139  bm, dr2 = bestMatch( object, matchCollection )
140  if dr2<deltaR2Max:
141  pairs[object] = bm
142  else:
143  pairs[object] = None
144  return pairs
145 
def bestMatch
Definition: DeltaR.py:118
def matchObjectCollection
Definition: DeltaR.py:131
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def DeltaR.matchObjectCollection2 (   objects,
  matchCollection,
  deltaRMax = 0.3 
)
Univoque association of an element from matchCollection to an element of objects.
Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
By default, the matching is true only if delta R is smaller than 0.3.

Definition at line 146 of file DeltaR.py.

References ComparisonHelper.zip().

147 def matchObjectCollection2 ( objects, matchCollection, deltaRMax = 0.3 ):
148  '''Univoque association of an element from matchCollection to an element of objects.
149  Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
150  By default, the matching is true only if delta R is smaller than 0.3.
151  '''
152 
153  pairs = {}
154  if len(objects)==0:
155  return pairs
156  if len(matchCollection)==0:
157  return dict( zip(objects, [None]*len(objects)) )
158  # build all possible combinations
159  allPairs = sorted([(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection])
160 
161  # to flag already matched objects
162  # FIXME this variable remains appended to the object, I do not like it
163  for object in objects:
164  object.matched = False
165  for match in matchCollection:
166  match.matched = False
167 
168  deltaR2Max = deltaRMax * deltaRMax
169  for dR2, (object, match) in allPairs:
170  if dR2 > deltaR2Max:
171  break
172  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
173  object.matched = True
174  match.matched = True
175  pairs[object] = match
176 
177  for object in objects:
178  if object.matched == False:
179  pairs[object] = None
180 
181  return pairs
182  # by now, the matched attribute remains in the objects, for future usage
183  # one could remove it with delattr (object, attrname)
184 
185 
def matchObjectCollection2
Definition: DeltaR.py:146
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def DeltaR.matchObjectCollection3 (   objects,
  matchCollection,
  deltaRMax = 0.3,
  filter = lambda x,
  y 
)

Definition at line 25 of file DeltaR.py.

References alcazmumu_cfi.filter, and ComparisonHelper.zip().

25 
26 def matchObjectCollection3 ( objects, matchCollection, deltaRMax = 0.3, filter = lambda x,y : True ):
27  '''Univoque association of an element from matchCollection to an element of objects.
28  Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
29  By default, the matching is true only if delta R is smaller than 0.3.
30  '''
31  #
32  pairs = {}
33  if len(objects)==0:
34  return pairs
35  if len(matchCollection)==0:
36  return dict( zip(objects, [None]*len(objects)) )
37  # build all possible combinations
38 
39  allPairs = sorted([(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection if list(filter(object,match)) ])
40  #
41  # to flag already matched objects
42  # FIXME this variable remains appended to the object, I do not like it
43 
44  for object in objects:
45  object.matched = False
46  for match in matchCollection:
47  match.matched = False
48  #
49 
50  deltaR2Max = deltaRMax * deltaRMax
51  for dR2, (object, match) in allPairs:
52  if dR2 > deltaR2Max:
53  break
54  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
55  object.matched = True
56  match.matched = True
57  pairs[object] = match
58  #
59 
60  for object in objects:
61  if object.matched == False:
62  pairs[object] = None
63  #
64 
65  return pairs
66  # by now, the matched attribute remains in the objects, for future usage
67  # one could remove it with delattr (object, attrname)
68 
def matchObjectCollection3
Definition: DeltaR.py:25
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)