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 inConeCollection
 
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 138 of file deltar.py.

References deltaR2().

Referenced by PrimaryVertexAnalyzer4PUSlimmed.fillResolutionAndPullHistograms(), HLTMuonPlotter.findMatches(), CSCMake2DRecHit.findWireBx(), GlobalCosmicMuonTrajectoryBuilder.match(), matchObjectCollection(), and MuonIdTruthInfo.truthMatchMuon().

139 def bestMatch( object, matchCollection):
140  '''Return the best match to object in matchCollection, which is the closest object in delta R'''
141  deltaR2Min = float('+inf')
142  bm = None
143  for match in matchCollection:
144  dR2 = deltaR2( object.eta(), object.phi(),
145  match.eta(), match.phi() )
146  if dR2 < deltaR2Min:
147  deltaR2Min = dR2
148  bm = match
149  return bm, deltaR2Min
150 
def bestMatch
Definition: deltar.py:138
def deltaR2
Definition: deltar.py:7
def deltar.cleanObjectCollection (   objects,
  masks,
  deltaRMin 
)
Masks objects using a deltaR cut.

Definition at line 118 of file deltar.py.

References deltaR2().

Referenced by JetAnalyzer.JetAnalyzer.process().

119 def cleanObjectCollection( objects, masks, deltaRMin ):
120  '''Masks objects using a deltaR cut.'''
121  if len(objects)==0 or len(masks)==0:
122  return objects, []
123  deltaR2Min = deltaRMin*deltaRMin
124  cleanObjects = []
125  dirtyObjects = []
126  for object in objects:
127  ok = True
128  for mask in masks:
129  dR2 = deltaR2( object.eta(), object.phi(),
130  mask.eta(), mask.phi() )
131  if dR2 < deltaR2Min:
132  ok = False
133  if ok:
134  cleanObjects.append( object )
135  else:
136  dirtyObjects.append( object )
137  return cleanObjects, dirtyObjects
def cleanObjectCollection
Definition: deltar.py:118
def deltaR2
Definition: deltar.py:7
def deltar.cleanObjectCollection2 (   objects,
  masks,
  deltaRMin 
)
Masks objects using a deltaR cut, another algorithm (same results).

Definition at line 91 of file deltar.py.

References deltaR2().

91 
92 def cleanObjectCollection2( objects, masks, deltaRMin ):
93  '''Masks objects using a deltaR cut, another algorithm (same results).'''
94  if len(objects)==0:
95  return objects
96  deltaR2Min = deltaRMin*deltaRMin
97  cleanObjects = copy.copy( objects )
98  for mask in masks:
99  tooClose = []
100  for idx, object in enumerate(cleanObjects):
101  dR2 = deltaR2( object.eta(), object.phi(),
102  mask.eta(), mask.phi() )
103  if dR2 < deltaR2Min:
104  tooClose.append( idx )
105  nRemoved = 0
106  for idx in tooClose:
107  # yes, everytime an object is removed, the list of objects is updated
108  # so need to update the index accordingly.
109  # example: to remove : ele 1 and 2
110  # first, ele 1 is removed
111  # -> ele 2 is now at index 1
112  # one should again remove the element at index 1
113  idx -= nRemoved
114  del cleanObjects[idx]
115  nRemoved += 1
116  return cleanObjects
117 
def cleanObjectCollection2
Definition: deltar.py:91
def deltaR2
Definition: deltar.py:7
def deltar.deltaPhi (   p1,
  p2 
)
Computes delta phi, handling periodic limit conditions.

Definition at line 20 of file deltar.py.

Referenced by deltaR2().

20 
21 def deltaPhi( p1, p2):
22  '''Computes delta phi, handling periodic limit conditions.'''
23  res = p1 - p2
24  while res > math.pi:
25  res -= 2*math.pi
26  while res < -math.pi:
27  res += 2*math.pi
28  return res
29 
def deltaPhi
Definition: deltar.py:20
def deltar.deltaR (   args)

Definition at line 16 of file deltar.py.

References deltaR2().

16 
17 def deltaR( *args ):
18  return math.sqrt( deltaR2(*args) )
19 
def deltaR2
Definition: deltar.py:7
def deltaR
Definition: deltar.py:16
def deltar.deltaR2 (   e1,
  p1,
  e2 = None,
  p2 = None 
)
Take either 4 arguments (eta,phi, eta,phi) or two objects that have 'eta', 'phi' methods)

Definition at line 7 of file deltar.py.

References deltaPhi().

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

7 
8 def deltaR2( e1, p1, e2=None, p2=None):
9  """Take either 4 arguments (eta,phi, eta,phi) or two objects that have 'eta', 'phi' methods)"""
10  if (e2 == None and p2 == None):
11  return deltaR2(e1.eta(),e1.phi(), p1.eta(), p1.phi())
12  de = e1 - e2
13  dp = deltaPhi(p1, p2)
14  return de*de + dp*dp
15 
def deltaPhi
Definition: deltar.py:20
def deltaR2
Definition: deltar.py:7
def deltar.inConeCollection (   pivot,
  particles,
  deltaRMax,
  deltaRMin = 1e-5 
)
Returns the list of particles that are less than deltaRMax away from pivot.

Definition at line 30 of file deltar.py.

References deltaR2().

30 
31 def inConeCollection(pivot, particles, deltaRMax, deltaRMin=1e-5):
32  '''Returns the list of particles that are less than deltaRMax away from pivot.'''
33  dR2Max = deltaRMax ** 2
34  dR2Min = deltaRMin ** 2 if deltaRMin > 0 else -1
35  results = []
36  for ptc in particles:
37  dR2 = deltaR2(pivot.eta(), pivot.phi(), ptc.eta(), ptc.phi())
38  if dR2Min < dR2 and dR2 < dR2Max:
39  results.append(ptc)
40  return results
def inConeCollection
Definition: deltar.py:30
def deltaR2
Definition: deltar.py:7
def deltar.matchObjectCollection (   objects,
  matchCollection,
  deltaR2Max,
  filter = lambda x,
  y 
)

Definition at line 151 of file deltar.py.

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

Referenced by SimpleJetAnalyzer.SimpleJetAnalyzer.process(), core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.process(), Matcher.Matcher.process(), and JetAnalyzer.JetAnalyzer.process().

152 def matchObjectCollection( objects, matchCollection, deltaR2Max, filter = lambda x,y : True):
153  pairs = {}
154  if len(objects)==0:
155  return pairs
156  if len(matchCollection)==0:
157  return dict( list(zip(objects, [None]*len(objects))) )
158  for object in objects:
159  bm, dr2 = bestMatch( object, [mob for mob in matchCollection if filter(object,mob)] )
160  if dr2<deltaR2Max:
161  pairs[object] = bm
162  else:
163  pairs[object] = None
164  return pairs
165 
def bestMatch
Definition: deltar.py:138
def matchObjectCollection
Definition: deltar.py:151
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 is they are re part of a matched tulpe.
By default, the matching is true only if delta R is smaller than 0.3.

Definition at line 166 of file deltar.py.

References ComparisonHelper.zip().

Referenced by objects.JetAnalyzer.JetAnalyzer.matchJets().

167 def matchObjectCollection2 ( objects, matchCollection, deltaRMax = 0.3 ):
168  '''Univoque association of an element from matchCollection to an element of objects.
169  Reco and Gen objects get the "matched" attribute, true is they are re part of a matched tulpe.
170  By default, the matching is true only if delta R is smaller than 0.3.
171  '''
172 
173  pairs = {}
174  if len(objects)==0:
175  return pairs
176  if len(matchCollection)==0:
177  return dict( list(zip(objects, [None]*len(objects))) )
178  # build all possible combinations
179  allPairs = sorted([(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection])
180 
181  # to flag already matched objects
182  # FIXME this variable remains appended to the object, I do not like it
183  for object in objects:
184  object.matched = False
185  for match in matchCollection:
186  match.matched = False
187 
188  deltaR2Max = deltaRMax * deltaRMax
189  for dR2, (object, match) in allPairs:
190  if dR2 > deltaR2Max:
191  break
192  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
193  object.matched = True
194  match.matched = True
195  pairs[object] = match
196 
197  for object in objects:
198  if object.matched == False:
199  pairs[object] = None
200 
201  return pairs
202  # by now, the matched attribute remains in the objects, for future usage
203  # one could remove it with delattr (object, attrname)
204 
205 
206 
def matchObjectCollection2
Definition: deltar.py:166
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 41 of file deltar.py.

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

Referenced by objects.LeptonAnalyzer.LeptonAnalyzer.matchAnyLeptons(), objects.IsoTrackAnalyzer.matchIsoTrack(), objects.LeptonAnalyzer.LeptonAnalyzer.matchLeptons(), objects.PhotonAnalyzer.PhotonAnalyzer.matchPhotons(), objects.TauAnalyzer.TauAnalyzer.matchTaus(), objects.LeptonAnalyzer.LeptonAnalyzer.matchToPhotons(), and core.TriggerMatchAnalyzer.TriggerMatchAnalyzer.process().

41 
42 def matchObjectCollection3 ( objects, matchCollection, deltaRMax = 0.3, filter = lambda x,y : True ):
43  '''Univoque association of an element from matchCollection to an element of objects.
44  Reco and Gen objects get the "matched" attribute, true is they are re part of a matched tulpe.
45  By default, the matching is true only if delta R is smaller than 0.3.
46  '''
47  #
48 
49  pairs = {}
50  if len(objects)==0:
51  return pairs
52  if len(matchCollection)==0:
53  return dict( list(zip(objects, [None]*len(objects))) )
54  # build all possible combinations
55 
56  objectCoords = [ (o.eta(),o.phi(),o) for o in objects ]
57  matchdCoords = [ (o.eta(),o.phi(),o) for o in matchCollection ]
58  allPairs = sorted([(deltaR2 (oeta, ophi, meta, mphi), (object, match)) for (oeta,ophi,object) in objectCoords for (meta,mphi,match) in matchdCoords if abs(oeta-meta)<=deltaRMax and filter(object,match) ])
59  #allPairs = [(deltaR2 (object.eta(), object.phi(), match.eta(), match.phi()), (object, match)) for object in objects for match in matchCollection if filter(object,match) ]
60  #
61  # to flag already matched objects
62  # FIXME this variable remains appended to the object, I do not like it
63 
64  for object in objects:
65  object.matched = False
66  for match in matchCollection:
67  match.matched = False
68  #
69 
70  deltaR2Max = deltaRMax * deltaRMax
71  for dR2, (object, match) in allPairs:
72  if dR2 > deltaR2Max:
73  break
74  if dR2 < deltaR2Max and object.matched == False and match.matched == False:
75  object.matched = True
76  match.matched = True
77  pairs[object] = match
78  #
79 
80  for object in objects:
81  if object.matched == False:
82  pairs[object] = None
83  #
84 
85  return pairs
86  # by now, the matched attribute remains in the objects, for future usage
87  # one could remove it with delattr (object, attrname)
88 
89 
90 
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def matchObjectCollection3
Definition: deltar.py:41