CMS 3D CMS Logo

pfIsolation.py
Go to the documentation of this file.
1 # Colin, March 2012
2 
3 import FWCore.ParameterSet.Config as cms
4 from PhysicsTools.PatAlgos.tools.helpers import cloneProcessingSnippet
5 
6 def _getattrGenerator( process, postfix ):
7  '''A function generator to simplify the getattr syntax'''
8  def fun(name):
9  return getattr(process, name+postfix)
10  return fun
11 
12 
13 _PFBRECO_loaded = False
14 
15 def _loadPFBRECO(process):
16  '''The particle-flow based reconstruction sequence should be loaded once in the process.
17  Not optimal, should load it only if it is not detected (hasattr)'''
18  global _PFBRECO_loaded
19  if _PFBRECO_loaded is False:
20  _PFBRECO_loaded = True
21  process.load("CommonTools.ParticleFlow.PFBRECO_cff")
22 
23 
24 def setupPFIso(process, leptonCollection, particleName, newpostfix='PFIso', postfix='', runPF2PAT = False):
25  '''Generic function to setup particle-based isolation for a given lepton collection.
26  Returns the isolation sequence.
27  You are responsible for adding it to your path.
28 
29  leptonCollection could e.g. be "gsfElectrons" or "muons"
30  particleName must be either "Electron" or "Muon".
31  newpostfix can be specified to define several particle-flow isolation sequences
32  '''
33  lepshort = None
34  if particleName=='Electron':
35  lepshort='el'
36  elif particleName=='Muon':
37  lepshort='mu'
38  else:
39  raise ValueError('particleName should be equal to "Electron" or "Muon"')
40 
41  if runPF2PAT != True :
42  _loadPFBRECO(process)
43 
44  # ADD VETOES IN ENDCAPS!
45  fullpostfix = postfix+newpostfix
46  ga = _getattrGenerator( process, postfix )
47  ganew = _getattrGenerator( process, fullpostfix )
48 
49  leptonSeq = cms.Sequence(
50  ga('pf{lepton}IsolationSequence'.format(lepton=particleName))
51  )
52  setattr( process, 'std{lepton}Sequence{postfix}'.format(lepton=particleName,
53  postfix=postfix), leptonSeq)
54 
55  leptonSource = leptonCollection
56  cloneProcessingSnippet(process,
57  ga('std{lepton}Sequence'.format(lepton=particleName)),
58  newpostfix)
59 
60  ganew("{lepshort}PFIsoDepositCharged".format(lepshort=lepshort) ).src = leptonSource
61  ganew("{lepshort}PFIsoDepositChargedAll".format(lepshort=lepshort)).src = leptonSource
62  ganew("{lepshort}PFIsoDepositNeutral".format(lepshort=lepshort)).src = leptonSource
63  ganew("{lepshort}PFIsoDepositGamma".format(lepshort=lepshort)).src = leptonSource
64  ganew("{lepshort}PFIsoDepositPU".format(lepshort=lepshort)).src = leptonSource
65 
66  return ganew('std{lepton}Sequence'.format(lepton=particleName))
67 
68 def setupPFIsoPhoton(process, photonCollection, particleName, newpostfix='PFIso'):
69  '''Generic function to setup particle-based isolation for a given lepton collection.
70  Returns the isolation sequence.
71  You are responsible for adding it to your path.
72 
73  leptonCollection could e.g. be "gsfElectrons" or "muons"
74  particleName must be either "Electron" or "Muon".
75  newpostfix can be specified to define several particle-flow isolation sequences
76  '''
77  phoshort = None
78  if particleName=='Photon':
79  phoshort='ph'
80  else:
81  raise ValueError('particleName should be equal to "Photon"')
82 
83  _loadPFBRECO(process)
84 
85  postfix = ''
86  # ADD VETOES IN ENDCAPS!
87  fullpostfix = postfix+newpostfix
88  #fullpostfix = ''
89  ga = _getattrGenerator( process, postfix )
90  ganew = _getattrGenerator( process, fullpostfix )
91 
92  photonSeq = cms.Sequence(
93  ga('pf{photon}IsolationSequence'.format(photon=particleName))
94  )
95  setattr( process, 'std{photon}Sequence{postfix}'.format(photon=particleName,
96  postfix=postfix), photonSeq)
97 
98  photonSource = photonCollection
99  cloneProcessingSnippet(process,
100  ga('std{photon}Sequence'.format(photon=particleName)),
101  newpostfix)
102 
103  ganew("{phoshort}PFIsoDepositCharged".format(phoshort=phoshort) ).src = photonSource
104  ganew("{phoshort}PFIsoDepositChargedAll".format(phoshort=phoshort)).src = photonSource
105  ganew("{phoshort}PFIsoDepositNeutral".format(phoshort=phoshort)).src = photonSource
106  ganew("{phoshort}PFIsoDepositGamma".format(phoshort=phoshort)).src = photonSource
107  ganew("{phoshort}PFIsoDepositPU".format(phoshort=phoshort)).src = photonSource
108 
109  return ganew('std{photon}Sequence'.format(photon=particleName))
110 
111 
112 def setupPFMuonIso(process, muonCollection, postfix='PFIso' ):
113  '''Set up particle-based isolation for the muons in muonCollection.
114 
115  Calls setupPFIso.
116  '''
117  return setupPFIso( process, muonCollection, 'Muon', postfix)
118 
119 
120 
121 def setupPFElectronIso(process, electronCollection, newpostfix='PFIso', postfix='', runPF2PAT = False ):
122  '''Set up particle-based isolation for the electrons in electronCollection.
123 
124  Calls setupPFIso.
125  '''
126  # print 'WARNING!!! the vetoes are the ones defined for the PF e-s (no veto...).'
127  # print 'Vetoes should be applied in the endcaps when doing particle-based isolation on gsfElectrons.'
128  # print 'Need a volunteer to implement that.'
129  return setupPFIso( process, electronCollection, 'Electron', newpostfix, postfix, runPF2PAT)
130 
131 
132 def setupPFPhotonIso(process, photonCollection, postfix='PFIso' ):
133  '''Set up particle-based isolation for the electrons in electronCollection.
134 
135  Calls setupPFIsoPhoton.
136  '''
137  # print 'WARNING!!! the vetoes are the ones defined for the PF e-s (no veto...).'
138  # print 'Please make sure that your file with vetoes is up to date'
139  return setupPFIsoPhoton( process, photonCollection, 'Photon', postfix)
140 
def setupPFElectronIso(process, electronCollection, newpostfix='PFIso', postfix='', runPF2PAT=False)
Definition: pfIsolation.py:121
def setupPFIsoPhoton(process, photonCollection, particleName, newpostfix='PFIso')
Definition: pfIsolation.py:68
def cloneProcessingSnippet(process, sequence, postfix, removePostfix="", noClones=[], addToTask=False, verbose=False)
Definition: helpers.py:263
def _getattrGenerator(process, postfix)
Definition: pfIsolation.py:6
def setupPFMuonIso(process, muonCollection, postfix='PFIso')
Definition: pfIsolation.py:112
def setupPFIso(process, leptonCollection, particleName, newpostfix='PFIso', postfix='', runPF2PAT=False)
Definition: pfIsolation.py:24
def setupPFPhotonIso(process, photonCollection, postfix='PFIso')
Definition: pfIsolation.py:132
def _loadPFBRECO(process)
Definition: pfIsolation.py:15