ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/python/pf2pat.py
Revision: 1.3
Committed: Wed Nov 7 19:59:32 2012 UTC (12 years, 5 months ago) by bbetchar
Content type: text/x-python
Branch: MAIN
Changes since 1.2: +7 -10 lines
Log Message:
tweak

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     getattr( process, 'pfPileUpIso'+options.postfix).checkClosestZVertex = True
33     if options.isData: coreTools.runOnData( process, names = [ 'PFAll' ], postfix = options.postfix )
34     coreTools.removeSpecificPATObjects( process, names = [ 'Photons', 'Taus' ], postfix = options.postfix )
35    
36 bbetchar 1.2 self.isoValues = {'el':0.15,'mu':0.20}
37     self.eleID = 'mvaTrigV0'
38     self.minEleID = 0.
39     self.dBFactor = -0.5
40 bbetchar 1.3 self.cuts = {'el': ['abs(eta)<2.5',
41     'pt>20.',
42 bbetchar 1.2 'gsfTrackRef.isNonnull',
43     'gsfTrackRef.trackerExpectedHitsInner.numberOfLostHits<2',
44 bbetchar 1.3 '(chargedHadronIso+max(0.,neutralHadronIso)+photonIso%+f*puChargedHadronIso)/et < %f'%(self.dBFactor, self.isoValues['el']),
45 bbetchar 1.2 'electronID("%s") > %f'%(self.eleID,self.minEleID),
46     ],
47 bbetchar 1.3 'mu' :['abs(eta)<2.5',
48     'pt>10.',
49 bbetchar 1.2 '(chargedHadronIso+neutralHadronIso+photonIso%+f*puChargedHadronIso)/pt < %f'%(self.dBFactor, self.isoValues['mu']),
50 bbetchar 1.3 '(isPFMuon && (isGlobalMuon || isTrackerMuon) )',
51 bbetchar 1.2 ],
52 bbetchar 1.3 'jet' : ['abs(eta)<2.5', # Careful! these jet cuts affect the typeI met corrections
53 bbetchar 1.2 'pt > 15.',
54     # PF jet ID:
55     'numberOfDaughters > 1',
56     'neutralHadronEnergyFraction < 0.99',
57     'neutralEmEnergyFraction < 0.99',
58     '(chargedEmEnergyFraction < 0.99 || abs(eta) >= 2.4)',
59     '(chargedHadronEnergyFraction > 0. || abs(eta) >= 2.4)',
60     '(chargedMultiplicity > 0 || abs(eta) >= 2.4)']
61     }
62    
63 bbetchar 1.1 self.process = process
64     self.options = options
65     self.fix = options.postfix
66     self.patSeq = getattr(process, 'patPF2PATSequence' + self.fix)
67     self.btags = ['combinedSecondaryVertex','jetProbability']
68     self.taginfos = ["impactParameter","secondaryVertex"]
69    
70     self.configTopProjections()
71     self.configPfLepton('el', iso3 = True)
72     self.configPfLepton('mu', iso3 = False)
73     self.configPatMuons()
74     self.configPatElectrons()
75     self.configPatJets()
76 bbetchar 1.2 self.configLeptonFilter()
77 bbetchar 1.1 self.removeCruft()
78     sys.stdout = self.stdout
79    
80     def attr(self, item) : return getattr(self.process, item)
81 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.'
82 bbetchar 1.1
83     def configTopProjections(self) :
84     '''Enable TopProjections except for Taus (do not remove tau cands from jet collection).'''
85    
86     for key,val in {'PileUp':1,'Muon':1,'Electron':1,'Jet':1,'Tau':0}.items() :
87     self.attr('pfNo'+key+self.fix).enable = bool(val)
88 bbetchar 1.2 self.attr('pfNoElectron').verbose = True
89 bbetchar 1.1 return
90    
91     def configPfLepton(self, lep, iso3) :
92     full = {'el':'Electrons','mu':'Muons'}[lep] + self.fix
93     iso = self.attr('pfIsolated'+full)
94 bbetchar 1.2 iso.isolationCut = self.isoValues[lep]
95 bbetchar 1.1 iso.doDeltaBetaCorrection = True
96 bbetchar 1.2 iso.deltaBetaFactor = self.dBFactor
97     sel = self.attr('pfSelected'+full)
98     sel.cut = ' && '.join(self.cuts[lep][:-2])
99     print >>self.stdout, ""
100     if lep == 'el' :
101     idName = 'pfIdentifiedElectrons'+self.fix
102     id = cms.EDFilter("ElectronIDPFCandidateSelector",
103     src = cms.InputTag('pfElectronsFromVertex'+self.fix),
104     recoGsfElectrons = cms.InputTag("gsfElectrons"),
105     electronIdMap = cms.InputTag(self.eleID),
106     electronIdCut = cms.double(self.minEleID))
107     setattr(self.process, idName, id )
108     self.patSeq.replace( sel, id*sel)
109     sel.src = idName
110     self.show(idName)
111    
112 bbetchar 1.1 if iso3:
113     pf = {'el':'PFId','mu':''}[lep] + self.fix
114     for item in ['pf','pfIsolated'] :
115     col = self.attr( item+full )
116     col.deltaBetaIsolationValueMap = tags( lep+'PFIsoValuePU03'+pf )
117     col.isolationValueMapsCharged = tags( [lep+'PFIsoValueCharged03'+pf] )
118     col.isolationValueMapsNeutral = tags( [ lep+'PFIsoValueNeutral03'+pf, lep+'PFIsoValueGamma03'+pf] )
119     val = self.attr( 'pat'+full ).isolationValues
120     val.pfNeutralHadrons = tags( lep+'PFIsoValueNeutral03' + pf )
121     val.pfChargedAll = tags( lep+'PFIsoValueChargedAll03' + pf )
122     val.pfPUChargedHadrons = tags( lep+'PFIsoValuePU03' + pf )
123     val.pfPhotons = tags( lep+'PFIsoValueGamma03' + pf )
124     val.pfChargedHadrons = tags( lep+'PFIsoValueCharged03' + pf )
125 bbetchar 1.2
126     self.show('pfSelected'+full)
127     self.show('pfIsolated'+full)
128 bbetchar 1.1 return
129    
130     def configPatMuons(self) :
131     for mod,attr,val in [('patMuons','usePV',False), # use beam spot rather than PV, which is necessary for 'dB' cut
132     ('patMuons','embedTrack',True), # embedded track needed for muon ID cuts
133 bbetchar 1.2 ('selectedPatMuons','cut', ' && '.join(self.cuts['mu'])),
134 bbetchar 1.1 ] : setattr( self.attr(mod+self.fix), attr, val )
135     self.show('selectedPatMuons'+self.fix)
136     return
137    
138     def configPatElectrons(self) :
139 bbetchar 1.2 electronIDSources = cms.PSet(**dict([(i,tags(i)) for i in [self.eleID]]))
140     for mod,attr,val in [('patElectrons','electronIDSources', electronIDSources),
141     ('selectedPatElectrons','cut', ' && '.join(c for c in self.cuts['el'] if 'gsfTrackRef' not in c)),
142 bbetchar 1.1 ] : setattr( self.attr( mod+self.fix), attr, val )
143     self.show('selectedPatElectrons'+self.fix)
144     return
145    
146     def configPatJets(self) :
147     for mod,attr,val in [('patJets','discriminatorSources',[tags(tag+'BJetTagsAOD'+self.fix) for tag in self.btags]),
148     ('patJets','tagInfoSources',[tags(tag+'TagInfosAOD'+self.fix) for tag in self.taginfos]),
149     ('patJets','addTagInfos',True),
150 bbetchar 1.2 ('selectedPatJets','cut', ' && '.join(self.cuts['jet'])),
151 bbetchar 1.1 ] : setattr( self.attr(mod+self.fix), attr, val )
152     self.show('selectedPatJets'+self.fix)
153     return
154    
155 bbetchar 1.2 def configLeptonFilter(self) :
156     if not self.options.requireLepton : return
157     leptonsName = 'pfLeptons'+self.fix
158     requireLeptonName = 'requireLepton'+self.fix
159     leptons = cms.EDProducer("CandViewMerger", src = tags(['pfIsolated%s%s'%(lep,self.fix) for lep in ['Electrons','Muons']]))
160     requireLepton = cms.EDFilter("CandViewCountFilter", src = tags(leptonsName), minNumber = cms.uint32(1) )
161     setattr(self.process, leptonsName, leptons)
162     setattr(self.process, requireLeptonName, requireLepton)
163    
164     jets = self.attr('pfJets'+self.fix)
165     self.patSeq.replace(jets, leptons*requireLepton*jets)
166     self.show(leptonsName)
167     self.show(requireLeptonName)
168     return
169 bbetchar 1.1
170     def removeCruft(self) :
171     nothanks = [mod for mod in set(str(self.patSeq).split('+'))
172     if ( any( keyword in mod for keyword in ['count','Legacy','pfJetsPiZeros','ak7','soft','iterativeCone',
173     'tauIsoDeposit','tauGenJet','hpsPFTau','tauMatch','pfTau','hpsSelection',
174     'photonMatch','phPFIso','pfIsolatedPhotons','pfCandsNotInJet','pfCandMETcorr',
175     'Negative','ToVertex','particleFlowDisplacedVertex',
176     'ype2Corr','ype0','patType1p2CorrectedPFMet','atCandidateSummaryTR','atPFParticles'] ) or
177     ( 'JetTagsAOD' in mod and not any( (btag+'B') in mod for btag in self.btags ) ) or
178     ( 'TagInfosAOD' in mod and not any( (info+'TagInfosAOD') in mod for info in self.taginfos ) ) or
179     ( 'elPFIsoValue' in mod and ('04' in mod or 'NoPFId' in mod ) ) or
180     ( 'muPFIsoValue' in mod and ('03' in mod))) ]
181     for mod in nothanks : self.patSeq.remove( self.attr(mod) )
182    
183     nothanks = ['ic5','kt4','kt6','JPT','ak7','ak5Calo']
184     now = set(dir(self.process))
185     for item in now - self.before - set(str(self.patSeq).split('+')) :
186     if any(part in item for part in nothanks) :
187     delattr(self.process,item)
188     return