CMS 3D CMS Logo

DeltaR.py
Go to the documentation of this file.
1 #ROOTTOOLS
2 import math
3 import copy
4 
5 def deltaR2( e1, p1, e2, p2):
6  de = e1 - e2
7  dp = deltaPhi(p1, p2)
8  return de*de + dp*dp
9 
10 
11 def deltaR( *args ):
12  return math.sqrt( deltaR2(*args) )
13 
14 
15 def deltaPhi( p1, p2):
16  '''Computes delta phi, handling periodic limit conditions.'''
17  res = p1 - p2
18  while res > math.pi:
19  res -= 2*math.pi
20  while res < -math.pi:
21  res += 2*math.pi
22  return res
23 
24 def matchObjectCollection3 ( objects, matchCollection, deltaRMax = 0.3, filter = lambda x,y : True ):
25  '''Univoque association of an element from matchCollection to an element of objects.
26  Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
27  By default, the matching is true only if delta R is smaller than 0.3.
28  '''
29  #
30  pairs = {}
31  if len(objects)==0:
32  return pairs
33  if len(matchCollection)==0:
34  return dict( zip(objects, [None]*len(objects)) )
35  # build all possible combinations
36 
37  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)) ])
38  #
39  # to flag already matched objects
40  # FIXME this variable remains appended to the object, I do not like it
41 
42  for object in objects:
43  object.matched = False
44  for match in matchCollection:
45  match.matched = False
46  #
47 
48  deltaR2Max = deltaRMax * deltaRMax
49  for dR2, (object, match) in allPairs:
50  if dR2 > deltaR2Max:
51  break
52  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
53  object.matched = True
54  match.matched = True
55  pairs[object] = match
56  #
57 
58  for object in objects:
59  if object.matched == False:
60  pairs[object] = None
61  #
62 
63  return pairs
64  # by now, the matched attribute remains in the objects, for future usage
65  # one could remove it with delattr (object, attrname)
66 
67 
68 def cleanObjectCollection2( objects, masks, deltaRMin ):
69  '''Masks objects using a deltaR cut, another algorithm (same results).'''
70  if len(objects)==0:
71  return objects
72  deltaR2Min = deltaRMin*deltaRMin
73  cleanObjects = copy.copy( objects )
74  for mask in masks:
75  tooClose = []
76  for idx, object in enumerate(cleanObjects):
77  dR2 = deltaR2( object.eta(), object.phi(),
78  mask.eta(), mask.phi() )
79  if dR2 < deltaR2Min:
80  tooClose.append( idx )
81  nRemoved = 0
82  for idx in tooClose:
83  # yes, everytime an object is removed, the list of objects is updated
84  # so need to update the index accordingly.
85  # example: to remove : ele 1 and 2
86  # first, ele 1 is removed
87  # -> ele 2 is now at index 1
88  # one should again remove the element at index 1
89  idx -= nRemoved
90  del cleanObjects[idx]
91  nRemoved += 1
92  return cleanObjects
93 
94 
95 def cleanObjectCollection( objects, masks, deltaRMin ):
96  '''Masks objects using a deltaR cut.'''
97  if len(objects)==0 or len(masks)==0:
98  return objects, []
99  deltaR2Min = deltaRMin*deltaRMin
100  cleanObjects = []
101  dirtyObjects = []
102  for object in objects:
103  ok = True
104  for mask in masks:
105  dR2 = deltaR2( object.eta(), object.phi(),
106  mask.eta(), mask.phi() )
107  if dR2 < deltaR2Min:
108  ok = False
109  if ok:
110  cleanObjects.append( object )
111  else:
112  dirtyObjects.append( object )
113  return cleanObjects, dirtyObjects
114 
115 
116 
117 def bestMatch( object, matchCollection):
118  '''Return the best match to object in matchCollection, which is the closest object in delta R'''
119  deltaR2Min = float('+inf')
120  bm = None
121  for match in matchCollection:
122  dR2 = deltaR2( object.eta(), object.phi(),
123  match.eta(), match.phi() )
124  if dR2 < deltaR2Min:
125  deltaR2Min = dR2
126  bm = match
127  return bm, deltaR2Min
128 
129 
130 def matchObjectCollection( objects, matchCollection, deltaR2Max):
131  pairs = {}
132  if len(objects)==0:
133  return pairs
134  if len(matchCollection)==0:
135  return dict( zip(objects, [None]*len(objects)) )
136  for object in objects:
137  bm, dr2 = bestMatch( object, matchCollection )
138  if dr2<deltaR2Max:
139  pairs[object] = bm
140  else:
141  pairs[object] = None
142  return pairs
143 
144 
145 def matchObjectCollection2 ( objects, matchCollection, deltaRMax = 0.3 ):
146  '''Univoque association of an element from matchCollection to an element of objects.
147  Reco and Gen objects get the "matched" attribute, true if they are part of a matched tuple.
148  By default, the matching is true only if delta R is smaller than 0.3.
149  '''
150 
151  pairs = {}
152  if len(objects)==0:
153  return pairs
154  if len(matchCollection)==0:
155  return dict( zip(objects, [None]*len(objects)) )
156  # build all possible combinations
157  allPairs = sorted([(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection])
158 
159  # to flag already matched objects
160  # FIXME this variable remains appended to the object, I do not like it
161  for object in objects:
162  object.matched = False
163  for match in matchCollection:
164  match.matched = False
165 
166  deltaR2Max = deltaRMax * deltaRMax
167  for dR2, (object, match) in allPairs:
168  if dR2 > deltaR2Max:
169  break
170  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
171  object.matched = True
172  match.matched = True
173  pairs[object] = match
174 
175  for object in objects:
176  if object.matched == False:
177  pairs[object] = None
178 
179  return pairs
180  # by now, the matched attribute remains in the objects, for future usage
181  # one could remove it with delattr (object, attrname)
182 
183 
184 
185 if __name__ == '__main__':
186 
187  import sys
188  args = sys.argv[1:]
189  fargs = map( float, args )
190 
191  print 'dR2 = ', deltaR2( *fargs )
192  print 'dR = ', deltaR( *fargs )
193 
194 
195 
def cleanObjectCollection2(objects, masks, deltaRMin)
Definition: DeltaR.py:68
def deltaPhi(p1, p2)
Definition: DeltaR.py:15
def matchObjectCollection3
Definition: DeltaR.py:24
def cleanObjectCollection(objects, masks, deltaRMin)
Definition: DeltaR.py:95
def deltaR2(e1, p1, e2, p2)
Definition: DeltaR.py:5
def matchObjectCollection2(objects, matchCollection, deltaRMax=0.3)
Definition: DeltaR.py:145
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
def matchObjectCollection(objects, matchCollection, deltaR2Max)
Definition: DeltaR.py:130
def deltaR(args)
Definition: DeltaR.py:11
def bestMatch(object, matchCollection)
Definition: DeltaR.py:117
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run