ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/python/pf2pat.py
(Generate patch)

Comparing UserCode/Betchart/TopRefTuple/python/pf2pat.py (file contents):
Revision 1.1 by bbetchar, Fri Nov 2 21:40:49 2012 UTC vs.
Revision 1.2 by bbetchar, Wed Nov 7 18:54:00 2012 UTC

# Line 10 | Line 10 | class TopRefPF2PAT(object) :
10      '''Implement the Top Reference configuration of PF2PAT.
11  
12      https://twiki.cern.ch/twiki/bin/viewauth/CMS/TWikiTopRefEventSel
13 +    Modify the PF selections to match the PAT selections, for consistent TopProjection cross-cleaning.
14      '''
15  
16      def __init__(self, process, options) :
17          self.stdout = sys.stdout
18          sys.stdout = open(os.devnull, 'w')
19 <        print >>self.stdout, "usePF2PAT() output suppressed."
19 >        print >>self.stdout, "usePF2PAT() output suppressed. (%s)"%__file__
20  
21          self.before = set(dir(process))
22          process.load( "PhysicsTools.PatAlgos.patSequences_cff" )
# Line 32 | Line 33 | class TopRefPF2PAT(object) :
33          if options.isData: coreTools.runOnData( process, names = [ 'PFAll' ], postfix = options.postfix )
34          coreTools.removeSpecificPATObjects( process, names = [ 'Photons', 'Taus' ], postfix = options.postfix )
35  
36 +        self.isoValues = {'el':0.15,'mu':0.20}
37 +        self.eleID = 'mvaTrigV0'
38 +        self.minEleID = 0.
39 +        self.dBFactor = -0.5
40 +        self.cuts = {'el': ['pt>20.',
41 +                            'abs(eta)<2.5',
42 +                            'gsfTrackRef.isNonnull',
43 +                            'gsfTrackRef.trackerExpectedHitsInner.numberOfLostHits<2',
44 +                            '(chargedHadronIso+max(0.,neutralHadronIso)+photonIso+%f*puChargedHadronIso)/et < %f'%(self.dBFactor, self.isoValues['el']),
45 +                            'electronID("%s") > %f'%(self.eleID,self.minEleID),
46 +                            ],
47 +                     'mu' :[#'(isGlobalMuon || isTrackerMuon)',                                              
48 +                            'pt>10.',                                                                      
49 +                            'abs(eta)<2.5',                                                                
50 +                            '(chargedHadronIso+neutralHadronIso+photonIso%+f*puChargedHadronIso)/pt < %f'%(self.dBFactor, self.isoValues['mu']),
51 +                            'isPFMuon',                                                                      
52 +                            ],
53 +                     'jet' : ['abs(eta) < 2.5', # Careful! these jet cuts affect the typeI met corrections
54 +                              '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          self.process = process
65          self.options = options
66          self.fix = options.postfix
# Line 45 | Line 74 | class TopRefPF2PAT(object) :
74          self.configPatMuons()
75          self.configPatElectrons()
76          self.configPatJets()
77 +        self.configLeptonFilter()
78          self.removeCruft()
79          sys.stdout = self.stdout
80          
81      def attr(self, item) : return getattr(self.process, item)
82 <    def show(self, item) : print >>self.stdout, item, '=', self.attr(item).dumpPython()
82 >    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  
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 <
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)
89 >        self.attr('pfNoElectron').verbose = True
90          return
91  
92      def configPfLepton(self, lep, iso3) :
93          full = {'el':'Electrons','mu':'Muons'}[lep] + self.fix
94          iso = self.attr('pfIsolated'+full)
95 <        iso.isolationCut = 0.2
95 >        iso.isolationCut = self.isoValues[lep]
96          iso.doDeltaBetaCorrection = True
97 <        iso.deltaBetaFactor = -0.5
97 >        iso.deltaBetaFactor = self.dBFactor
98 >        sel = self.attr('pfSelected'+full)
99 >        sel.cut = ' && '.join(self.cuts[lep][:-2])
100 >        #sel.filter = cms.bool(True)
101 >        #iso.filter = cms.bool(True)
102 >        print >>self.stdout, ""
103 >        if lep == 'el' :
104 >            idName = 'pfIdentifiedElectrons'+self.fix
105 >            id = cms.EDFilter("ElectronIDPFCandidateSelector",
106 >                              src = cms.InputTag('pfElectronsFromVertex'+self.fix),
107 >                              recoGsfElectrons = cms.InputTag("gsfElectrons"),
108 >                              electronIdMap = cms.InputTag(self.eleID),
109 >                              electronIdCut = cms.double(self.minEleID))
110 >            setattr(self.process, idName, id )
111 >            self.patSeq.replace( sel, id*sel)
112 >            sel.src = idName
113 >            self.show(idName)
114 >        
115          if iso3:
116              pf = {'el':'PFId','mu':''}[lep] + self.fix
117              for item in ['pf','pfIsolated'] :
# Line 89 | Line 125 | class TopRefPF2PAT(object) :
125              val.pfPUChargedHadrons = tags( lep+'PFIsoValuePU03' + pf )
126              val.pfPhotons          = tags( lep+'PFIsoValueGamma03' + pf )
127              val.pfChargedHadrons   = tags( lep+'PFIsoValueCharged03' + pf )
128 +
129 +        self.show('pfSelected'+full)
130 +        self.show('pfIsolated'+full)
131          return
132  
133      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
134          for mod,attr,val in [('patMuons','usePV',False),     # use beam spot rather than PV, which is necessary for 'dB' cut
135                               ('patMuons','embedTrack',True), # embedded track needed for muon ID cuts
136 <                             ('selectedPatMuons','cut', ' && '.join(muonCuts)),
136 >                             ('selectedPatMuons','cut', ' && '.join(self.cuts['mu'])),
137                               ] : setattr( self.attr(mod+self.fix), attr, val )
138          self.show('selectedPatMuons'+self.fix)
139          return
140  
141      def configPatElectrons(self) :
142 <        electronIDSources = cms.PSet(**dict([(i,tags(i)) for i in ['mvaTrigV0','mvaNonTrigV0']]))
143 <        electronCuts = ['pt > 20.',                                                                              # transverse energy
144 <                        '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)),
142 >        electronIDSources = cms.PSet(**dict([(i,tags(i)) for i in [self.eleID]]))
143 >        for mod,attr,val in [('patElectrons','electronIDSources', electronIDSources),
144 >                             ('selectedPatElectrons','cut', ' && '.join(c for c in self.cuts['el'] if 'gsfTrackRef' not in c)),
145                               ] : setattr( self.attr( mod+self.fix), attr, val )
146          self.show('selectedPatElectrons'+self.fix)
147          return
148  
149      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
150          for mod,attr,val in [('patJets','discriminatorSources',[tags(tag+'BJetTagsAOD'+self.fix) for tag in self.btags]),
151                               ('patJets','tagInfoSources',[tags(tag+'TagInfosAOD'+self.fix) for tag in self.taginfos]),
152                               ('patJets','addTagInfos',True),
153 <                             ('selectedPatJets','cut', ' && '.join(jetCuts)),
153 >                             ('selectedPatJets','cut', ' && '.join(self.cuts['jet'])),
154                               ] : setattr( self.attr(mod+self.fix), attr, val )
155          self.show('selectedPatJets'+self.fix)
156          return
157  
158 +    def configLeptonFilter(self) :
159 +        if not self.options.requireLepton : return
160 +        leptonsName = 'pfLeptons'+self.fix
161 +        requireLeptonName = 'requireLepton'+self.fix
162 +        leptons = cms.EDProducer("CandViewMerger", src = tags(['pfIsolated%s%s'%(lep,self.fix) for lep in ['Electrons','Muons']]))
163 +        requireLepton = cms.EDFilter("CandViewCountFilter", src = tags(leptonsName), minNumber = cms.uint32(1) )
164 +        setattr(self.process, leptonsName, leptons)
165 +        setattr(self.process, requireLeptonName, requireLepton)
166 +        
167 +        jets = self.attr('pfJets'+self.fix)
168 +        self.patSeq.replace(jets, leptons*requireLepton*jets)
169 +        self.show(leptonsName)
170 +        self.show(requireLeptonName)
171 +        return
172  
173      def removeCruft(self) :
174          nothanks = [mod for mod in set(str(self.patSeq).split('+'))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines