test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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:5
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:5
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:5
def cleanObjectCollection2
Definition: DeltaR.py:69
def DeltaR.deltaPhi (   p1,
  p2 
)
Computes delta phi, handling periodic limit conditions.

Definition at line 15 of file DeltaR.py.

Referenced by deltaR2().

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

Definition at line 11 of file DeltaR.py.

References deltaR2().

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

Definition at line 5 of file DeltaR.py.

References deltaPhi().

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

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

Definition at line 131 of file DeltaR.py.

References bestMatch(), cmsPerfStripChart.dict, 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 cmsPerfStripChart.dict, and 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 = [(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection]
160  allPairs.sort ()
161 
162  # to flag already matched objects
163  # FIXME this variable remains appended to the object, I do not like it
164  for object in objects:
165  object.matched = False
166  for match in matchCollection:
167  match.matched = False
168 
169  deltaR2Max = deltaRMax * deltaRMax
170  for dR2, (object, match) in allPairs:
171  if dR2 > deltaR2Max:
172  break
173  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
174  object.matched = True
175  match.matched = True
176  pairs[object] = match
177 
178  for object in objects:
179  if object.matched == False:
180  pairs[object] = None
181 
182  return pairs
183  # by now, the matched attribute remains in the objects, for future usage
184  # one could remove it with delattr (object, attrname)
185 
186 
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 24 of file DeltaR.py.

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

24 
25 def matchObjectCollection3 ( objects, matchCollection, deltaRMax = 0.3, filter = lambda x,y : True ):
26  '''Univoque association of an element from matchCollection to an element of objects.
27  Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
28  By default, the matching is true only if delta R is smaller than 0.3.
29  '''
30  #
31  pairs = {}
32  if len(objects)==0:
33  return pairs
34  if len(matchCollection)==0:
35  return dict( zip(objects, [None]*len(objects)) )
36  # build all possible combinations
37 
38  allPairs = [(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection if filter(object,match) ]
39  allPairs.sort ()
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:24
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)