ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/python/pf2pat.py
Revision: 1.7
Committed: Wed Nov 14 17:17:00 2012 UTC (12 years, 5 months ago) by bbetchar
Content type: text/x-python
Branch: MAIN
Changes since 1.6: +1 -0 lines
Log Message:
pfPileUp.checkClosestZVertex=False

File Contents

# User Rev Content
1 bbetchar 1.1 import operator,sys,os
2     from FWCore.ParameterSet import Config as cms
3     from PhysicsTools.PatAlgos.tools import coreTools,pfTools
4    
5     def tags(stuff) :
6     return ( cms.InputTag(stuff) if type(stuff)!=list else
7     cms.VInputTag([tags(item) for item in stuff]) )
8    
9     class TopRefPF2PAT(object) :
10     '''Implement the Top Reference configuration of PF2PAT.
11    
12     https://twiki.cern.ch/twiki/bin/viewauth/CMS/TWikiTopRefEventSel
13 bbetchar 1.2 Modify the PF selections to match the PAT selections, for consistent TopProjection cross-cleaning.
14 bbetchar 1.1 '''
15    
16     def __init__(self, process, options) :
17     self.stdout = sys.stdout
18     sys.stdout = open(os.devnull, 'w')
19 bbetchar 1.2 print >>self.stdout, "usePF2PAT() output suppressed. (%s)"%__file__
20 bbetchar 1.1
21     self.before = set(dir(process))
22     process.load( "PhysicsTools.PatAlgos.patSequences_cff" )
23     pfTools.usePF2PAT( process,
24     runPF2PAT = True,
25     postfix = options.postfix,
26     runOnMC = not options.isData,
27     pvCollection = tags( 'goodOfflinePrimaryVertices' ),
28     typeIMetCorrections = True,
29     jetAlgo = 'AK5',
30     jetCorrections = ('AK5PFchs', ['L1FastJet','L2Relative','L3Absolute','L2L3Residual'][:None if options.isData else -1] )
31     )
32 bbetchar 1.7 getattr( process, 'pfPileUp'+options.postfix).checkClosestZVertex = False # https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookJetEnergyCorrections#JetEnCorPFnoPU2012
33 bbetchar 1.1 getattr( process, 'pfPileUpIso'+options.postfix).checkClosestZVertex = True
34     if options.isData: coreTools.runOnData( process, names = [ 'PFAll' ], postfix = options.postfix )
35     coreTools.removeSpecificPATObjects( process, names = [ 'Photons', 'Taus' ], postfix = options.postfix )
36    
37 bbetchar 1.2 self.isoValues = {'el':0.15,'mu':0.20}
38     self.eleID = 'mvaTrigV0'
39     self.minEleID = 0.
40     self.dBFactor = -0.5
41 bbetchar 1.3 self.cuts = {'el': ['abs(eta)<2.5',
42     'pt>20.',
43 bbetchar 1.2 'gsfTrackRef.isNonnull',
44     'gsfTrackRef.trackerExpectedHitsInner.numberOfLostHits<2',
45 bbetchar 1.4 '(chargedHadronIso+max(0.,neutralHadronIso+photonIso%+f*puChargedHadronIso))/pt < %f'%(self.dBFactor, self.isoValues['el']),
46 bbetchar 1.2 'electronID("%s") > %f'%(self.eleID,self.minEleID),
47     ],
48 bbetchar 1.3 'mu' :['abs(eta)<2.5',
49     'pt>10.',
50 bbetchar 1.2 '(chargedHadronIso+neutralHadronIso+photonIso%+f*puChargedHadronIso)/pt < %f'%(self.dBFactor, self.isoValues['mu']),
51 bbetchar 1.3 '(isPFMuon && (isGlobalMuon || isTrackerMuon) )',
52 bbetchar 1.2 ],
53 bbetchar 1.3 'jet' : ['abs(eta)<2.5', # Careful! these jet cuts affect the typeI met corrections
54 bbetchar 1.2 'pt > 15.',
55     # PF jet ID:
56     'numberOfDaughters > 1',
57     'neutralHadronEnergyFraction < 0.99',
58     'neutralEmEnergyFraction < 0.99',
59     '(chargedEmEnergyFraction < 0.99 || abs(eta) >= 2.4)',
60     '(chargedHadronEnergyFraction > 0. || abs(eta) >= 2.4)',
61     '(chargedMultiplicity > 0 || abs(eta) >= 2.4)']
62     }
63    
64 bbetchar 1.1 self.process = process
65     self.options = options
66     self.fix = options.postfix
67     self.patSeq = getattr(process, 'patPF2PATSequence' + self.fix)
68 bbetchar 1.6 self.btags = self.options.btags
69 bbetchar 1.1 self.taginfos = ["impactParameter","secondaryVertex"]
70    
71     self.configTopProjections()
72     self.configPfLepton('el', iso3 = True)
73     self.configPfLepton('mu', iso3 = False)
74     self.configPatMuons()
75     self.configPatElectrons()
76     self.configPatJets()
77 bbetchar 1.2 self.configLeptonFilter()
78 bbetchar 1.1 self.removeCruft()
79     sys.stdout = self.stdout
80    
81     def attr(self, item) : return getattr(self.process, item)
82 bbetchar 1.2 def show(self, item) : print >> (sys.stdout if self.options.quiet else self.stdout), item, '=', self.attr(item).dumpPython() if hasattr(self.process, item) else 'Not Found.'
83 bbetchar 1.1
84     def configTopProjections(self) :
85     '''Enable TopProjections except for Taus (do not remove tau cands from jet collection).'''
86    
87     for key,val in {'PileUp':1,'Muon':1,'Electron':1,'Jet':1,'Tau':0}.items() :
88     self.attr('pfNo'+key+self.fix).enable = bool(val)
89 bbetchar 1.2 self.attr('pfNoElectron').verbose = True
90 bbetchar 1.1 return
91    
92     def configPfLepton(self, lep, iso3) :
93     full = {'el':'Electrons','mu':'Muons'}[lep] + self.fix
94     iso = self.attr('pfIsolated'+full)
95 bbetchar 1.2 iso.isolationCut = self.isoValues[lep]
96 bbetchar 1.1 iso.doDeltaBetaCorrection = True
97 bbetchar 1.2 iso.deltaBetaFactor = self.dBFactor
98     sel = self.attr('pfSelected'+full)
99     sel.cut = ' && '.join(self.cuts[lep][:-2])
100     print >>self.stdout, ""
101     if lep == 'el' :
102     idName = 'pfIdentifiedElectrons'+self.fix
103     id = cms.EDFilter("ElectronIDPFCandidateSelector",
104     src = cms.InputTag('pfElectronsFromVertex'+self.fix),
105     recoGsfElectrons = cms.InputTag("gsfElectrons"),
106     electronIdMap = cms.InputTag(self.eleID),
107     electronIdCut = cms.double(self.minEleID))
108     setattr(self.process, idName, id )
109     self.patSeq.replace( sel, id*sel)
110     sel.src = idName
111     self.show(idName)
112    
113 bbetchar 1.1 if iso3:
114     pf = {'el':'PFId','mu':''}[lep] + self.fix
115     for item in ['pf','pfIsolated'] :
116     col = self.attr( item+full )
117     col.deltaBetaIsolationValueMap = tags( lep+'PFIsoValuePU03'+pf )
118     col.isolationValueMapsCharged = tags( [lep+'PFIsoValueCharged03'+pf] )
119     col.isolationValueMapsNeutral = tags( [ lep+'PFIsoValueNeutral03'+pf, lep+'PFIsoValueGamma03'+pf] )
120     val = self.attr( 'pat'+full ).isolationValues
121     val.pfNeutralHadrons = tags( lep+'PFIsoValueNeutral03' + pf )
122     val.pfChargedAll = tags( lep+'PFIsoValueChargedAll03' + pf )
123     val.pfPUChargedHadrons = tags( lep+'PFIsoValuePU03' + pf )
124     val.pfPhotons = tags( lep+'PFIsoValueGamma03' + pf )
125     val.pfChargedHadrons = tags( lep+'PFIsoValueCharged03' + pf )
126 bbetchar 1.2
127     self.show('pfSelected'+full)
128     self.show('pfIsolated'+full)
129 bbetchar 1.1 return
130    
131     def configPatMuons(self) :
132     for mod,attr,val in [('patMuons','usePV',False), # use beam spot rather than PV, which is necessary for 'dB' cut
133     ('patMuons','embedTrack',True), # embedded track needed for muon ID cuts
134 bbetchar 1.2 ('selectedPatMuons','cut', ' && '.join(self.cuts['mu'])),
135 bbetchar 1.1 ] : setattr( self.attr(mod+self.fix), attr, val )
136     self.show('selectedPatMuons'+self.fix)
137     return
138    
139     def configPatElectrons(self) :
140 bbetchar 1.2 electronIDSources = cms.PSet(**dict([(i,tags(i)) for i in [self.eleID]]))
141     for mod,attr,val in [('patElectrons','electronIDSources', electronIDSources),
142     ('selectedPatElectrons','cut', ' && '.join(c for c in self.cuts['el'] if 'gsfTrackRef' not in c)),
143 bbetchar 1.1 ] : setattr( self.attr( mod+self.fix), attr, val )
144     self.show('selectedPatElectrons'+self.fix)
145     return
146    
147     def configPatJets(self) :
148     for mod,attr,val in [('patJets','discriminatorSources',[tags(tag+'BJetTagsAOD'+self.fix) for tag in self.btags]),
149     ('patJets','tagInfoSources',[tags(tag+'TagInfosAOD'+self.fix) for tag in self.taginfos]),
150     ('patJets','addTagInfos',True),
151 bbetchar 1.2 ('selectedPatJets','cut', ' && '.join(self.cuts['jet'])),
152 bbetchar 1.1 ] : setattr( self.attr(mod+self.fix), attr, val )
153     self.show('selectedPatJets'+self.fix)
154     return
155    
156 bbetchar 1.2 def configLeptonFilter(self) :
157     if not self.options.requireLepton : return
158 bbetchar 1.5 muons20Name = 'pfIsolatedMuons20'+self.fix
159 bbetchar 1.2 leptonsName = 'pfLeptons'+self.fix
160     requireLeptonName = 'requireLepton'+self.fix
161 bbetchar 1.5
162     muons20 = cms.EDFilter("GenericPFCandidateSelector", src = tags('pfIsolatedMuons'+self.fix), cut = cms.string('pt>20 && abs(eta) < 2.4'))
163     leptons = cms.EDProducer("CandViewMerger", src = tags(['pfIsolated%s%s'%(lep,self.fix) for lep in ['Electrons','Muons20']]))
164 bbetchar 1.2 requireLepton = cms.EDFilter("CandViewCountFilter", src = tags(leptonsName), minNumber = cms.uint32(1) )
165 bbetchar 1.5
166     setattr(self.process, muons20Name, muons20)
167 bbetchar 1.2 setattr(self.process, leptonsName, leptons)
168     setattr(self.process, requireLeptonName, requireLepton)
169    
170     jets = self.attr('pfJets'+self.fix)
171 bbetchar 1.5 self.patSeq.replace(jets, muons20*leptons*requireLepton*jets)
172    
173     self.show(muons20Name)
174 bbetchar 1.2 self.show(leptonsName)
175     self.show(requireLeptonName)
176     return
177 bbetchar 1.1
178     def removeCruft(self) :
179     nothanks = [mod for mod in set(str(self.patSeq).split('+'))
180     if ( any( keyword in mod for keyword in ['count','Legacy','pfJetsPiZeros','ak7','soft','iterativeCone',
181     'tauIsoDeposit','tauGenJet','hpsPFTau','tauMatch','pfTau','hpsSelection',
182     'photonMatch','phPFIso','pfIsolatedPhotons','pfCandsNotInJet','pfCandMETcorr',
183     'Negative','ToVertex','particleFlowDisplacedVertex',
184     'ype2Corr','ype0','patType1p2CorrectedPFMet','atCandidateSummaryTR','atPFParticles'] ) or
185     ( 'JetTagsAOD' in mod and not any( (btag+'B') in mod for btag in self.btags ) ) or
186     ( 'TagInfosAOD' in mod and not any( (info+'TagInfosAOD') in mod for info in self.taginfos ) ) or
187     ( 'elPFIsoValue' in mod and ('04' in mod or 'NoPFId' in mod ) ) or
188     ( 'muPFIsoValue' in mod and ('03' in mod))) ]
189     for mod in nothanks : self.patSeq.remove( self.attr(mod) )
190    
191     nothanks = ['ic5','kt4','kt6','JPT','ak7','ak5Calo']
192     now = set(dir(self.process))
193     for item in now - self.before - set(str(self.patSeq).split('+')) :
194     if any(part in item for part in nothanks) :
195     delattr(self.process,item)
196     return