ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/python/pf2pat.py
Revision: 1.1
Committed: Fri Nov 2 21:40:49 2012 UTC (12 years, 6 months ago) by bbetchar
Content type: text/x-python
Branch: MAIN
Log Message:
break out files

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     '''
14    
15     def __init__(self, process, options) :
16     self.stdout = sys.stdout
17     sys.stdout = open(os.devnull, 'w')
18     print >>self.stdout, "usePF2PAT() output suppressed."
19    
20     self.before = set(dir(process))
21     process.load( "PhysicsTools.PatAlgos.patSequences_cff" )
22     pfTools.usePF2PAT( process,
23     runPF2PAT = True,
24     postfix = options.postfix,
25     runOnMC = not options.isData,
26     pvCollection = tags( 'goodOfflinePrimaryVertices' ),
27     typeIMetCorrections = True,
28     jetAlgo = 'AK5',
29     jetCorrections = ('AK5PFchs', ['L1FastJet','L2Relative','L3Absolute','L2L3Residual'][:None if options.isData else -1] )
30     )
31     getattr( process, 'pfPileUpIso'+options.postfix).checkClosestZVertex = True
32     if options.isData: coreTools.runOnData( process, names = [ 'PFAll' ], postfix = options.postfix )
33     coreTools.removeSpecificPATObjects( process, names = [ 'Photons', 'Taus' ], postfix = options.postfix )
34    
35     self.process = process
36     self.options = options
37     self.fix = options.postfix
38     self.patSeq = getattr(process, 'patPF2PATSequence' + self.fix)
39     self.btags = ['combinedSecondaryVertex','jetProbability']
40     self.taginfos = ["impactParameter","secondaryVertex"]
41    
42     self.configTopProjections()
43     self.configPfLepton('el', iso3 = True)
44     self.configPfLepton('mu', iso3 = False)
45     self.configPatMuons()
46     self.configPatElectrons()
47     self.configPatJets()
48     self.removeCruft()
49     sys.stdout = self.stdout
50    
51     def attr(self, item) : return getattr(self.process, item)
52     def show(self, item) : print >>self.stdout, item, '=', self.attr(item).dumpPython()
53    
54     def configTopProjections(self) :
55     '''Enable TopProjections except for Taus (do not remove tau cands from jet collection).'''
56    
57     for key,val in {'PileUp':1,'Muon':1,'Electron':1,'Jet':1,'Tau':0}.items() :
58     self.attr('pfNo'+key+self.fix).enable = bool(val)
59    
60     for lep in ['Muon','Electron'] :
61     sub = (lep,self.fix)
62     pfNo = self.attr('pfNo%s%s'%sub)
63     pfNo.topCollection = 'selectedPat%ss%s'%sub
64     mods = [ self.attr( item) for item in [ lep.lower() + 'Match'+self.fix,
65     'pat%ss%s'%sub,
66     'selectedPat%ss%s'%sub]][int(self.options.isData):]
67     for mod in mods : self.patSeq.remove(mod)
68     self.patSeq.replace( pfNo, reduce(operator.mul, mods) * pfNo )
69     pfNo.verbose = True
70     self.show('pfNo%s%s'%sub)
71     return
72    
73     def configPfLepton(self, lep, iso3) :
74     full = {'el':'Electrons','mu':'Muons'}[lep] + self.fix
75     iso = self.attr('pfIsolated'+full)
76     iso.isolationCut = 0.2
77     iso.doDeltaBetaCorrection = True
78     iso.deltaBetaFactor = -0.5
79     if iso3:
80     pf = {'el':'PFId','mu':''}[lep] + self.fix
81     for item in ['pf','pfIsolated'] :
82     col = self.attr( item+full )
83     col.deltaBetaIsolationValueMap = tags( lep+'PFIsoValuePU03'+pf )
84     col.isolationValueMapsCharged = tags( [lep+'PFIsoValueCharged03'+pf] )
85     col.isolationValueMapsNeutral = tags( [ lep+'PFIsoValueNeutral03'+pf, lep+'PFIsoValueGamma03'+pf] )
86     val = self.attr( 'pat'+full ).isolationValues
87     val.pfNeutralHadrons = tags( lep+'PFIsoValueNeutral03' + pf )
88     val.pfChargedAll = tags( lep+'PFIsoValueChargedAll03' + pf )
89     val.pfPUChargedHadrons = tags( lep+'PFIsoValuePU03' + pf )
90     val.pfPhotons = tags( lep+'PFIsoValueGamma03' + pf )
91     val.pfChargedHadrons = tags( lep+'PFIsoValueCharged03' + pf )
92     return
93    
94     def configPatMuons(self) :
95     muonCuts = ['isPFMuon', # general reconstruction property
96     '(isGlobalMuon || isTrackerMuon)', # general reconstruction property
97     'pt > 10.', # transverse momentum
98     'abs(eta) < 2.5', # pseudo-rapisity range
99     '(chargedHadronIso+neutralHadronIso+photonIso-0.5*puChargedHadronIso)/pt < 0.20']# relative isolation w/ Delta beta corrections (factor 0.5)
100    
101     for mod,attr,val in [('patMuons','usePV',False), # use beam spot rather than PV, which is necessary for 'dB' cut
102     ('patMuons','embedTrack',True), # embedded track needed for muon ID cuts
103     ('selectedPatMuons','cut', ' && '.join(muonCuts)),
104     ] : setattr( self.attr(mod+self.fix), attr, val )
105     self.show('selectedPatMuons'+self.fix)
106     return
107    
108     def configPatElectrons(self) :
109     electronIDSources = cms.PSet(**dict([(i,tags(i)) for i in ['mvaTrigV0','mvaNonTrigV0']]))
110     electronCuts = ['pt > 20.', # transverse energy
111     'abs(eta) < 2.5', # pseudo-rapidity range
112     'electronID("mvaTrigV0") > 0.', # MVA electrons ID
113     '(chargedHadronIso+max(0.,neutralHadronIso)+photonIso-0.5*puChargedHadronIso)/et < 0.15']# relative isolation with Delta beta corrections
114    
115     for mod,attr,val in [('patElectrons','electronIDSources',electronIDSources),
116     ('selectedPatElectrons','cut', ' && '.join(electronCuts)),
117     ] : setattr( self.attr( mod+self.fix), attr, val )
118     self.show('selectedPatElectrons'+self.fix)
119     return
120    
121     def configPatJets(self) :
122     # Careful! these jet cuts affect the typeI met corrections
123     jetCuts = ['abs(eta) < 2.5',
124     'pt > 15.',
125     # PF jet ID:
126     'numberOfDaughters > 1',
127     'neutralHadronEnergyFraction < 0.99',
128     'neutralEmEnergyFraction < 0.99',
129     '(chargedEmEnergyFraction < 0.99 || abs(eta) >= 2.4)',
130     '(chargedHadronEnergyFraction > 0. || abs(eta) >= 2.4)',
131     '(chargedMultiplicity > 0 || abs(eta) >= 2.4)']
132    
133     for mod,attr,val in [('patJets','discriminatorSources',[tags(tag+'BJetTagsAOD'+self.fix) for tag in self.btags]),
134     ('patJets','tagInfoSources',[tags(tag+'TagInfosAOD'+self.fix) for tag in self.taginfos]),
135     ('patJets','addTagInfos',True),
136     ('selectedPatJets','cut', ' && '.join(jetCuts)),
137     ] : setattr( self.attr(mod+self.fix), attr, val )
138     self.show('selectedPatJets'+self.fix)
139     return
140    
141    
142     def removeCruft(self) :
143     nothanks = [mod for mod in set(str(self.patSeq).split('+'))
144     if ( any( keyword in mod for keyword in ['count','Legacy','pfJetsPiZeros','ak7','soft','iterativeCone',
145     'tauIsoDeposit','tauGenJet','hpsPFTau','tauMatch','pfTau','hpsSelection',
146     'photonMatch','phPFIso','pfIsolatedPhotons','pfCandsNotInJet','pfCandMETcorr',
147     'Negative','ToVertex','particleFlowDisplacedVertex',
148     'ype2Corr','ype0','patType1p2CorrectedPFMet','atCandidateSummaryTR','atPFParticles'] ) or
149     ( 'JetTagsAOD' in mod and not any( (btag+'B') in mod for btag in self.btags ) ) or
150     ( 'TagInfosAOD' in mod and not any( (info+'TagInfosAOD') in mod for info in self.taginfos ) ) or
151     ( 'elPFIsoValue' in mod and ('04' in mod or 'NoPFId' in mod ) ) or
152     ( 'muPFIsoValue' in mod and ('03' in mod))) ]
153     for mod in nothanks : self.patSeq.remove( self.attr(mod) )
154    
155     nothanks = ['ic5','kt4','kt6','JPT','ak7','ak5Calo']
156     now = set(dir(self.process))
157     for item in now - self.before - set(str(self.patSeq).split('+')) :
158     if any(part in item for part in nothanks) :
159     delattr(self.process,item)
160     return