CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Matcher.py
Go to the documentation of this file.
1 from heppy.framework.analyzer import Analyzer
2 from heppy.utils.deltar import matchObjectCollection, deltaR
3 
4 import collections
5 
6 class Matcher(Analyzer):
7  '''Particle matcher.
8 
9  Works with any kind of object with a p4 function.
10 
11  Simple example configuration:
12 
13  from heppy_fcc.analyzers.Matcher import Matcher
14  papas_jet_match = cfg.Analyzer(
15  Matcher,
16  instance_label = 'papas',
17  match_particles = 'gen_jets',
18  particles = 'papas_jets'
19  )
20 
21  particles: Name of the collection containing the particles to be matched.
22  match_particles: Name of the collection containing the particles where a match
23  is to be found.
24 
25  In this particular case, each jet in "papas_jets" will end up with a new
26  attribute called "match". This attribute can be either the closest gen jet in the
27  "gen_jets" collection in case a gen_jet is found within delta R = 0.3,
28  or None in case a match cannot be found in this cone.
29 
30  More complex example configuration:
31 
32  papas_particle_match_g2r = cfg.Analyzer(
33  Matcher,
34  instance_label = 'papas_g2r',
35  particles = 'gen_particles_stable',
36  match_particles = [
37  ('papas_rec_particles', None),
38  ('papas_rec_particles', 211),
39  ('papas_rec_particles', 130),
40  ('papas_rec_particles', 22)
41  ]
42  )
43 
44  In this case, each gen particle in gen_particles_stable will end up with the following
45  new attributes:
46  - "match" : closest reconstructed particle in "papas_rec_particles", if any.
47  - "match_211": closest reconstructed particle of pdgId 211 in "papas_rec_particles",
48  if any.
49  - etc.
50 
51  '''
52 
53 
54  def beginLoop(self, setup):
55  super(Matcher, self).beginLoop(setup)
57  if isinstance( self.cfg_ana.match_particles, str):
58  self.match_collections.append( (self.cfg_ana.match_particles, None) )
59  else:
60  self.match_collections = self.cfg_ana.match_particles
61 
62  def process(self, event):
63  particles = getattr(event, self.cfg_ana.particles)
64  # match_particles = getattr(event, self.cfg_ana.match_particles)
65  for collname, pdgid in self.match_collections:
66  match_ptcs = getattr(event, collname)
67  match_ptcs_filtered = match_ptcs
68  if pdgid is not None:
69  match_ptcs_filtered = [ptc for ptc in match_ptcs
70  if ptc.pdgid()==pdgid]
71  pairs = matchObjectCollection(particles, match_ptcs_filtered,
72  0.3**2)
73  for ptc in particles:
74  matchname = 'match'
75  if pdgid:
76  matchname = 'match_{pdgid}'.format(pdgid=pdgid)
77  match = pairs[ptc]
78  setattr(ptc, matchname, match)
79  if match:
80  drname = 'dr'
81  if pdgid:
82  drname = 'dr_{pdgid}'.format(pdgid=pdgid)
83  dr = deltaR(ptc.theta(), ptc.phi(),
84  match.theta(), match.phi())
85  setattr(ptc, drname, dr)
86  # print dr, ptc, match
def matchObjectCollection
Definition: deltar.py:151