Beispiel #1
0
        def correctDiLepton(diLepton):
            '''Corrects a di-lepton.

            This function is defined within the process function to have
            access to the variables available there, namely event.run.
            The goal of this function is to be able to call it with map,
            to get very compact code.
            '''
            p4_1 = rochcor.corrected_p4(diLepton.leg1(), event.run)
            p4_2 = rochcor.corrected_p4(diLepton.leg2(), event.run)
            # l1 = copy.copy( diLepton.leg1() )
            # l2 = copy.copy( diLepton.leg2() )
            l1 = diLepton.leg1()
            l2 = diLepton.leg2()
            l1.setP4(p4_1)
            l2.setP4(p4_2)
            diLeptonCor = DiMuon(l1, l2, diLepton)
            return diLeptonCor
        def correctDiLepton( diLepton ):
            '''Corrects a di-lepton.

            This function is defined within the process function to have
            access to the variables available there, namely event.run.
            The goal of this function is to be able to call it with map,
            to get very compact code.
            '''
            p4_1 = rochcor.corrected_p4( diLepton.leg1(), event.run ) 
            p4_2 = rochcor.corrected_p4( diLepton.leg2(), event.run )
            # l1 = copy.copy( diLepton.leg1() )
            # l2 = copy.copy( diLepton.leg2() )
            l1 = diLepton.leg1()
            l2 = diLepton.leg2()
            l1.setP4(p4_1)
            l2.setP4(p4_2)    
            diLeptonCor = DiMuon( l1, l2, diLepton)  
            return diLeptonCor
    def makeAllMuons(self, event):
        """
               make a list of all muons, and apply basic corrections to them
        """
        # Start from all muons
        allmuons = map(Muon, self.handles['muons'].product())

        # Muon scale and resolution corrections (if enabled)
        if self.cfg_ana.doMuScleFitCorrections:
            for mu in allmuons:
                self.muscleCorr.correct(mu, event.run)
        elif self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run)
                mu.setP4(corp4)

        # Clean up dulicate muons (note: has no effect unless the muon id is removed)
        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean(
                self.handles['muons'].product())
            newmu = []
            for i, mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        # Attach the vertex to them, for dxy/dz calculation
        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]

        # Compute relIso in 0.3 and 0.4 cones
        for mu in allmuons:
            mu.absIso03 = (
                mu.sourcePtr().pfIsolationR03().sumChargedHadronPt + max(
                    mu.sourcePtr().pfIsolationR03().sumNeutralHadronEt +
                    mu.sourcePtr().pfIsolationR03().sumPhotonEt -
                    mu.sourcePtr().pfIsolationR03().sumPUPt / 2, 0.0))
            mu.absIso04 = (
                mu.sourcePtr().pfIsolationR04().sumChargedHadronPt + max(
                    mu.sourcePtr().pfIsolationR04().sumNeutralHadronEt +
                    mu.sourcePtr().pfIsolationR04().sumPhotonEt -
                    mu.sourcePtr().pfIsolationR04().sumPUPt / 2, 0.0))
            mu.relIso03 = mu.absIso03 / mu.pt()
            mu.relIso04 = mu.absIso04 / mu.pt()

        return allmuons
Beispiel #4
0
    def makeLeptons(self, event):

        event.looseLeptons = []
        event.selectedLeptons = []
        event.inclusiveLeptons = []

        #muons
        allmuons = map(Muon, self.handles['muons'].product())

        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D = types.MethodType(lambda self: self._sip3d, mu,
                                                mu.__class__)

        if self.cfg_ana.doMuScleFitCorrections:
            for mu in allmuons:
                self.muscleCorr.correct(mu, event.run)
        elif self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run)
                mu.setP4(corp4)

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean(
                self.handles['muons'].product())
            newmu = []
            for i, mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            if (mu.isGlobal() or mu.isTracker() and mu.numberOfMatches() > 0
                ) and mu.pt() > 5 and abs(mu.eta()) < 2.4 and abs(
                    mu.dxy()) < 0.5 and abs(mu.dz()) < 1.:
                #pid = mu.sourcePtr().originalObject().track().id()
                #key = mu.sourcePtr().originalObject().track().key()
                #mu.dEdX = self.handles['dEdX'].product().get(pid,key)
                if mu.sourcePtr().userFloat("isPFMuon") > 0.5 and mu.sip3D(
                ) < self.cfg_ana.sip3dCut and mu.relIso(
                        dBetaFactor=0.5) < self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(mu)
                else:
                    event.looseLeptons.append(mu)
                if mu.sourcePtr().userFloat("isPFMuon") > 0.5 and mu.sip3D(
                ) < self.cfg_ana.sip3dCutVeryLoose:
                    event.inclusiveLeptons.append(mu)

        #electrons
        allelectrons = map(Electron, self.handles['electrons'].product())

        ## duplicate removal for fast sim (to be checked if still necessary in 5_3_12+)
        allelenodup = []
        for e in allelectrons:
            dup = False
            for e2 in allelenodup:
                if abs(e.pt() - e2.pt()) < 1e-6 and abs(e.eta() - e2.eta(
                )) < 1e-6 and abs(e.phi() - e2.phi()) < 1e-6 and e.charge(
                ) == e2.charge():
                    dup = True
                    break
            if not dup: allelenodup.append(e)
        allelectrons = allelenodup

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D = types.MethodType(lambda self: self._sip3d, ele,
                                                 ele.__class__)

        # fill EA for rho-corrected isolation
        for ele in allelectrons:
            ele.rho = float(self.handles['rhoEle'].product()[0])
            SCEta = abs(ele.sourcePtr().superCluster().eta())
            if (abs(SCEta) >= 0.0 and abs(SCEta) < 1.0):
                ele.EffectiveArea = 0.130
            if (abs(SCEta) >= 1.0 and abs(SCEta) < 1.479):
                ele.EffectiveArea = 0.137
            if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0):
                ele.EffectiveArea = 0.067
            if (abs(SCEta) >= 2.0 and abs(SCEta) < 2.2):
                ele.EffectiveArea = 0.089
            if (abs(SCEta) >= 2.2 and abs(SCEta) < 2.3):
                ele.EffectiveArea = 0.107
            if (abs(SCEta) >= 2.3 and abs(SCEta) < 2.4):
                ele.EffectiveArea = 0.110
            if (abs(SCEta) >= 2.4): ele.EffectiveArea = 0.138

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                self.electronEnergyCalibrator.correct(ele, event.run)

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (
                        mu.isGlobal()
                        or mu.sourcePtr().userFloat("isPFMuon") > 0.5):
                    muForEleCrossCleaning.append(mu)

        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## apply selection
            if ele.pt() > 7 and abs(ele.eta()) < 2.5 and abs(
                    ele.dxy()) < 0.5 and abs(
                        ele.dz()) < 1. and ele.numberOfHits() <= 1:
                ## fill tightId:
                if (ele.pt() > 20):
                    SCEta = abs(ele.sourcePtr().superCluster().eta())
                    if SCEta < 0.8:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.00)
                    elif SCEta < 1.479:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.10)
                    else:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.62)
                elif (ele.pt() > 10):
                    if SCEta < 0.8:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.94)
                    elif SCEta < 1.479:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.85)
                    else:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.92)
                else:
                    ele.tightIdResult = False
                if (self.relaxId or ele.mvaIDZZ() and
                        ele.sip3D() < self.cfg_ana.sip3dCut) and ele.relIso(
                            dBetaFactor=0.5) < self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(ele)
                else:
                    event.looseLeptons.append(ele)
                if (self.relaxId or ele.mvaIDZZ()
                    ) and ele.sip3D() < self.cfg_ana.sip3dCutVeryLoose:
                    event.inclusiveLeptons.append(ele)

        event.looseLeptons.sort(key=lambda l: l.pt(), reverse=True)
        event.selectedLeptons.sort(key=lambda l: l.pt(), reverse=True)
        event.inclusiveLeptons.sort(key=lambda l: l.pt(), reverse=True)

        for lepton in event.selectedLeptons:
            if hasattr(self, 'efficiency'):
                self.efficiency.attachToObject(lepton)
Beispiel #5
0
    def makeLeptons(self, event):
        
        event.looseLeptons = []
        event.selectedLeptons = []

        #muons
        allmuons = map( Muon, self.handles['muons'].product() )

        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D  = types.MethodType(lambda self : self._sip3d, mu, mu.__class__)

        if self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run) 
                mu.setP4( corp4 )

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean( self.handles['muons'].product() )
            newmu = []
            for i,mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            if (mu.isGlobal() or mu.isTracker() and mu.numberOfMatches()>0) and mu.pt()>5 and abs(mu.eta())<2.4 and abs(mu.dxy())<0.5 and abs(mu.dz())<1.:
                if mu.sourcePtr().userFloat("isPFMuon")>0.5 and (self.relaxId or mu.sip3D()<10) and mu.relIso(dBetaFactor=0.5)<self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(mu)
                else:
                    event.looseLeptons.append(mu)

        #electrons        
        allelectrons = map( Electron, self.handles['electrons'].product() )

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D  = types.MethodType(lambda self : self._sip3d, ele, ele.__class__)

        for ele in allelectrons:
          ele.rho = float(self.handles['rhoEle'].product()[0])
          SCEta = abs(ele.sourcePtr().superCluster().eta())
          if (abs(SCEta) >= 0.0 and abs(SCEta) < 1.0 ) : ele.EffectiveArea = 0.130;
          if (abs(SCEta) >= 1.0 and abs(SCEta) < 1.479 ) : ele.EffectiveArea = 0.137;
          if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0 ) : ele.EffectiveArea = 0.067;
          if (abs(SCEta) >= 2.0 and abs(SCEta) < 2.2 ) : ele.EffectiveArea = 0.089;
          if (abs(SCEta) >= 2.2 and abs(SCEta) < 2.3 ) : ele.EffectiveArea = 0.107;
          if (abs(SCEta) >= 2.3 and abs(SCEta) < 2.4 ) : ele.EffectiveArea = 0.110;
          if (abs(SCEta) >= 2.4) : ele.EffectiveArea = 0.138;

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                calibratedPatEle = ele.sourcePtr().get()
                self.electronEnergyCalibrator.correctLite(calibratedPatEle, calibratedPatEle.r9(), event.run)
                ele.setP4(calibratedPatEle.p4(calibratedPatEle.P4_COMBINATION))

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (mu.isGlobal() or mu.sourcePtr().userFloat("isPFMuon")>0.5):
                    muForEleCrossCleaning.append(mu)


        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## apply selection
            if ele.pt()>7 and abs(ele.eta())<2.5 and abs(ele.dxy())<0.5 and abs(ele.dz())<1. and ele.numberOfHits()<=1:
                 if (self.relaxId or ele.mvaIDZZ() and ele.sip3D()<10) and ele.relIso(dBetaFactor=0.5)<self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(ele)
                 else:
                    event.looseLeptons.append(ele)
                    
        event.looseLeptons.sort(key = lambda l : l.pt(), reverse = True)
        event.selectedLeptons.sort(key = lambda l : l.pt(), reverse = True)

        for lepton in event.selectedLeptons:
            if hasattr(self,'efficiency'):
                self.efficiency.attachToObject(lepton)
    def makeLeptons(self, event):
        
        event.looseLeptons = []
        event.selectedLeptons = []
        event.inclusiveLeptons = []


        

        #muons
        allmuons = map( Muon, self.handles['muons'].product() )
        
        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D  = types.MethodType(lambda self : self._sip3d, mu, mu.__class__)

        if self.cfg_ana.doMuScleFitCorrections:
            for mu in allmuons:
                self.muscleCorr.correct(mu, event.run)
        elif self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run) 
                mu.setP4( corp4 )

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean( self.handles['muons'].product() )
            newmu = []
            for i,mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            mu.relIso03= (mu.sourcePtr().pfIsolationR03().sumChargedHadronPt + max( mu.sourcePtr().pfIsolationR03().sumNeutralHadronEt +  mu.sourcePtr().pfIsolationR03().sumPhotonEt -  mu.sourcePtr().pfIsolationR03().sumPUPt/2,0.0))/mu.pt()
            
            mu.looseFakeId = ((mu.isGlobal() or mu.isTracker() and mu.numberOfMatches()>0) and mu.sourcePtr().userFloat("isPFMuon")>0.5 and mu.pt()>10 and abs(mu.eta())<2.4 and mu.tightId()>0.5 and abs(mu.dxy())<0.2 and abs(mu.dz())<0.2 and mu.relIso03<1.0)
            mu.tightFakeId = ((mu.isGlobal() or mu.isTracker() and mu.numberOfMatches()>0) and mu.sourcePtr().userFloat("isPFMuon")>0.5 and mu.pt()>10 and abs(mu.eta())<2.4 and mu.tightId()>0.5 and abs(mu.dxy())<0.01 and abs(mu.dz())<0.2 and mu.relIso03<0.1)

            if mu.pt()>5 and abs(mu.eta())<2.4:
                if mu.looseFakeId:
                    event.inclusiveLeptons.append(mu)
                    event.selectedLeptons.append(mu)
                    

        #electrons        
        allelectrons = map( Electron, self.handles['electrons'].product() )

        ## duplicate removal for fast sim (to be checked if still necessary in 5_3_12+)
        allelenodup = []
        for e in allelectrons:
            dup = False
            for e2 in allelenodup:
                if abs(e.pt()-e2.pt()) < 1e-6 and abs(e.eta()-e2.eta()) < 1e-6 and abs(e.phi()-e2.phi()) < 1e-6 and e.charge() == e2.charge():
                    dup = True
                    break
            if not dup: allelenodup.append(e)
        allelectrons = allelenodup

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D  = types.MethodType(lambda self : self._sip3d, ele, ele.__class__)

        # fill EA for rho-corrected isolation
        for ele in allelectrons:
          ele.rho = float(self.handles['rhoEle'].product()[0])
          SCEta = abs(ele.sourcePtr().superCluster().eta())
          if (abs(SCEta) >= 0.0   and abs(SCEta) < 1.0   ) : ele.EffectiveArea = 0.13 # 0.130;
          if (abs(SCEta) >= 1.0   and abs(SCEta) < 1.479 ) : ele.EffectiveArea = 0.14 # 0.137;
          if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0   ) : ele.EffectiveArea = 0.07 # 0.067;
          if (abs(SCEta) >= 2.0   and abs(SCEta) < 2.2   ) : ele.EffectiveArea = 0.09 # 0.089;
          if (abs(SCEta) >= 2.2   and abs(SCEta) < 2.3   ) : ele.EffectiveArea = 0.11 # 0.107;
          if (abs(SCEta) >= 2.3   and abs(SCEta) < 2.4   ) : ele.EffectiveArea = 0.11 # 0.110;
          if (abs(SCEta) >= 2.4)                           : ele.EffectiveArea = 0.14 # 0.138;

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                self.electronEnergyCalibrator.correct(ele, event.run)

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (mu.isGlobal() or mu.sourcePtr().userFloat("isPFMuon")>0.5):
                    muForEleCrossCleaning.append(mu)


        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## fill POG MVA tight Id
            ele.tightIdResult = False
            if ele.pt()>7 and abs(ele.eta())<2.5 and abs(ele.dxy())<0.5 and abs(ele.dz())<1. and ele.numberOfHits()<=1:
                 ## fill tightId:
                 if (ele.pt() > 20):
                    SCEta = abs(ele.sourcePtr().superCluster().eta())
                    if   SCEta < 0.8:   ele.tightIdResult = (ele.mvaTrigV0() > 0.00)
                    elif SCEta < 1.479: ele.tightIdResult = (ele.mvaTrigV0() > 0.10)
                    else:               ele.tightIdResult = (ele.mvaTrigV0() > 0.62)
                 elif (ele.pt() > 10):
                    if   SCEta < 0.8:   ele.tightIdResult = (ele.mvaTrigV0() > 0.94)
                    elif SCEta < 1.479: ele.tightIdResult = (ele.mvaTrigV0() > 0.85)
                    else:               ele.tightIdResult = (ele.mvaTrigV0() > 0.92)
            # Compute isolation
            ele.relIso03 = (ele.chargedHadronIso(0.3) + max(ele.neutralHadronIso(0.3)+ele.photonIso(0.3)-ele.rho*ele.EffectiveArea,0))/ele.pt()
            # Compute electron id
            dEtaIn = abs(ele.sourcePtr().deltaEtaSuperClusterTrackAtVtx());
            dPhiIn = abs(ele.sourcePtr().deltaPhiSuperClusterTrackAtVtx());
            sigmaIEtaIEta = abs(ele.sourcePtr().sigmaIetaIeta());
            hoe = abs(ele.sourcePtr().hadronicOverEm());
            ooemoop = abs(1.0/ele.sourcePtr().ecalEnergy() - ele.sourcePtr().eSuperClusterOverP()/ele.sourcePtr().ecalEnergy());
            vtxFitConversion = ele.passConversionVeto();
            mHits = ele.numberOfHits();
            # Set tight and loose flags
            if abs(ele.sourcePtr().superCluster().eta()) < 1.479:
                ele.looseFakeId = dEtaIn < 0.004 and dPhiIn < 0.06 and sigmaIEtaIEta < 0.01 and hoe < 0.12 and ooemoop < 0.05
            else:
                ele.looseFakeId = dEtaIn < 0.007 and dPhiIn < 0.03 and sigmaIEtaIEta < 0.03 and hoe < 0.10 and ooemoop < 0.05
            ele.looseFakeId = ele.looseFakeId and vtxFitConversion and mHits <= 1 and abs(ele.dz()) < 0.1 and ele.relIso03 < 0.6
            ele.looseFakeId = ele.looseFakeId and mHits <= 1 and ele.sourcePtr().isGsfCtfScPixChargeConsistent()
            ele.tightFakeId = ele.looseFakeId and abs(ele.dxy()) < 0.02 and ele.relIso03 < 0.15
            #print "ele pt = %.3f  eta = %.3f sceta = %.3f detaIn = %.4f dphiIn = %.4f  sieie = %.4f  h/e = %.4f  1/e-1/p = %.4f  dxy = %.4f  dz = %.4f  losthits = %d conveto = %d relIso = %.4f     loose id = %d   tight id = %d" % ( ele.pt(),ele.eta(),ele.sourcePtr().superCluster().eta(),dEtaIn,dPhiIn,sigmaIEtaIEta,hoe,ooemoop,abs(ele.dxy()),abs(ele.dz()),mHits,vtxFitConversion,ele.relIso03, ele.looseFakeId, ele.tightFakeId)
            # add to the list if needed
            if ele.pt()>7 and abs(ele.eta())<2.5:
                if ele.looseFakeId:
                    event.inclusiveLeptons.append(ele)
                    event.selectedLeptons.append(ele)
                 
                    
        event.looseLeptons.sort(key = lambda l : l.pt(), reverse = True)
        event.selectedLeptons.sort(key = lambda l : l.pt(), reverse = True)
        event.inclusiveLeptons.sort(key = lambda l : l.pt(), reverse = True)

        for lepton in event.selectedLeptons:
            if hasattr(self,'efficiency'):
                self.efficiency.attachToObject(lepton)
Beispiel #7
0
    def makeLeptons(self, event):

        event.looseLeptons = []
        event.selectedLeptons = []

        #muons
        allmuons = map(Muon, self.handles['muons'].product())

        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D = types.MethodType(lambda self: self._sip3d, mu,
                                                mu.__class__)

        if self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run)
                mu.setP4(corp4)

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean(
                self.handles['muons'].product())
            newmu = []
            for i, mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            if (mu.isGlobal() or mu.isTracker() and mu.numberOfMatches() > 0
                ) and mu.pt() > 5 and abs(mu.eta()) < 2.4 and abs(
                    mu.dxy()) < 0.5 and abs(mu.dz()) < 1.:
                if mu.sourcePtr().userFloat("isPFMuon") > 0.5 and (
                        self.relaxId or mu.sip3D() < 10) and mu.relIso(
                            dBetaFactor=0.5) < self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(mu)
                else:
                    event.looseLeptons.append(mu)

        #electrons
        allelectrons = map(Electron, self.handles['electrons'].product())

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D = types.MethodType(lambda self: self._sip3d, ele,
                                                 ele.__class__)

        for ele in allelectrons:
            ele.rho = float(self.handles['rhoEle'].product()[0])
            SCEta = abs(ele.sourcePtr().superCluster().eta())
            if (abs(SCEta) >= 0.0 and abs(SCEta) < 1.0):
                ele.EffectiveArea = 0.130
            if (abs(SCEta) >= 1.0 and abs(SCEta) < 1.479):
                ele.EffectiveArea = 0.137
            if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0):
                ele.EffectiveArea = 0.067
            if (abs(SCEta) >= 2.0 and abs(SCEta) < 2.2):
                ele.EffectiveArea = 0.089
            if (abs(SCEta) >= 2.2 and abs(SCEta) < 2.3):
                ele.EffectiveArea = 0.107
            if (abs(SCEta) >= 2.3 and abs(SCEta) < 2.4):
                ele.EffectiveArea = 0.110
            if (abs(SCEta) >= 2.4): ele.EffectiveArea = 0.138

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                calibratedPatEle = ele.sourcePtr().get()
                self.electronEnergyCalibrator.correctLite(
                    calibratedPatEle, calibratedPatEle.r9(), event.run)
                ele.setP4(calibratedPatEle.p4(calibratedPatEle.P4_COMBINATION))

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (
                        mu.isGlobal()
                        or mu.sourcePtr().userFloat("isPFMuon") > 0.5):
                    muForEleCrossCleaning.append(mu)

        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## apply selection
            if ele.pt() > 7 and abs(ele.eta()) < 2.5 and abs(
                    ele.dxy()) < 0.5 and abs(
                        ele.dz()) < 1. and ele.numberOfHits() <= 1:
                if (self.relaxId
                        or ele.mvaIDZZ() and ele.sip3D() < 10) and ele.relIso(
                            dBetaFactor=0.5) < self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(ele)
                else:
                    event.looseLeptons.append(ele)

        event.looseLeptons.sort(key=lambda l: l.pt(), reverse=True)
        event.selectedLeptons.sort(key=lambda l: l.pt(), reverse=True)

        for lepton in event.selectedLeptons:
            if hasattr(self, 'efficiency'):
                self.efficiency.attachToObject(lepton)
    def makeLeptons(self, event):

        event.looseLeptons = []
        event.selectedLeptons = []
        event.inclusiveLeptons = []

        #muons
        allmuons = map(Muon, self.handles['muons'].product())

        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D = types.MethodType(lambda self: self._sip3d, mu,
                                                mu.__class__)

        if self.cfg_ana.doMuScleFitCorrections:
            for mu in allmuons:
                self.muscleCorr.correct(mu, event.run)
        elif self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run)
                mu.setP4(corp4)

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean(
                self.handles['muons'].product())
            newmu = []
            for i, mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            mu.relIso03 = (
                mu.sourcePtr().pfIsolationR03().sumChargedHadronPt + max(
                    mu.sourcePtr().pfIsolationR03().sumNeutralHadronEt +
                    mu.sourcePtr().pfIsolationR03().sumPhotonEt -
                    mu.sourcePtr().pfIsolationR03().sumPUPt / 2,
                    0.0)) / mu.pt()

            mu.looseFakeId = ((mu.isGlobal()
                               or mu.isTracker() and mu.numberOfMatches() > 0)
                              and mu.sourcePtr().userFloat("isPFMuon") > 0.5
                              and mu.pt() > 10 and abs(mu.eta()) < 2.4
                              and mu.tightId() > 0.5 and abs(mu.dxy()) < 0.2
                              and abs(mu.dz()) < 0.2 and mu.relIso03 < 1.0)
            mu.tightFakeId = ((mu.isGlobal()
                               or mu.isTracker() and mu.numberOfMatches() > 0)
                              and mu.sourcePtr().userFloat("isPFMuon") > 0.5
                              and mu.pt() > 10 and abs(mu.eta()) < 2.4
                              and mu.tightId() > 0.5 and abs(mu.dxy()) < 0.01
                              and abs(mu.dz()) < 0.2 and mu.relIso03 < 0.1)

            if mu.pt() > 5 and abs(mu.eta()) < 2.4:
                if mu.looseFakeId:
                    event.inclusiveLeptons.append(mu)
                    event.selectedLeptons.append(mu)

        #electrons
        allelectrons = map(Electron, self.handles['electrons'].product())

        ## duplicate removal for fast sim (to be checked if still necessary in 5_3_12+)
        allelenodup = []
        for e in allelectrons:
            dup = False
            for e2 in allelenodup:
                if abs(e.pt() - e2.pt()) < 1e-6 and abs(e.eta() - e2.eta(
                )) < 1e-6 and abs(e.phi() - e2.phi()) < 1e-6 and e.charge(
                ) == e2.charge():
                    dup = True
                    break
            if not dup: allelenodup.append(e)
        allelectrons = allelenodup

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D = types.MethodType(lambda self: self._sip3d, ele,
                                                 ele.__class__)

        # fill EA for rho-corrected isolation
        for ele in allelectrons:
            ele.rho = float(self.handles['rhoEle'].product()[0])
            SCEta = abs(ele.sourcePtr().superCluster().eta())
            if (abs(SCEta) >= 0.0 and abs(SCEta) < 1.0):
                ele.EffectiveArea = 0.13  # 0.130;
            if (abs(SCEta) >= 1.0 and abs(SCEta) < 1.479):
                ele.EffectiveArea = 0.14  # 0.137;
            if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0):
                ele.EffectiveArea = 0.07  # 0.067;
            if (abs(SCEta) >= 2.0 and abs(SCEta) < 2.2):
                ele.EffectiveArea = 0.09  # 0.089;
            if (abs(SCEta) >= 2.2 and abs(SCEta) < 2.3):
                ele.EffectiveArea = 0.11  # 0.107;
            if (abs(SCEta) >= 2.3 and abs(SCEta) < 2.4):
                ele.EffectiveArea = 0.11  # 0.110;
            if (abs(SCEta) >= 2.4): ele.EffectiveArea = 0.14  # 0.138;

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                self.electronEnergyCalibrator.correct(ele, event.run)

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (
                        mu.isGlobal()
                        or mu.sourcePtr().userFloat("isPFMuon") > 0.5):
                    muForEleCrossCleaning.append(mu)

        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## fill POG MVA tight Id
            ele.tightIdResult = False
            if ele.pt() > 7 and abs(ele.eta()) < 2.5 and abs(
                    ele.dxy()) < 0.5 and abs(
                        ele.dz()) < 1. and ele.numberOfHits() <= 1:
                ## fill tightId:
                if (ele.pt() > 20):
                    SCEta = abs(ele.sourcePtr().superCluster().eta())
                    if SCEta < 0.8:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.00)
                    elif SCEta < 1.479:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.10)
                    else:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.62)
                elif (ele.pt() > 10):
                    if SCEta < 0.8:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.94)
                    elif SCEta < 1.479:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.85)
                    else:
                        ele.tightIdResult = (ele.mvaTrigV0() > 0.92)
            # Compute isolation
            ele.relIso03 = (ele.chargedHadronIso(0.3) + max(
                ele.neutralHadronIso(0.3) + ele.photonIso(0.3) -
                ele.rho * ele.EffectiveArea, 0)) / ele.pt()
            # Compute electron id
            dEtaIn = abs(ele.sourcePtr().deltaEtaSuperClusterTrackAtVtx())
            dPhiIn = abs(ele.sourcePtr().deltaPhiSuperClusterTrackAtVtx())
            sigmaIEtaIEta = abs(ele.sourcePtr().sigmaIetaIeta())
            hoe = abs(ele.sourcePtr().hadronicOverEm())
            ooemoop = abs(1.0 / ele.sourcePtr().ecalEnergy() -
                          ele.sourcePtr().eSuperClusterOverP() /
                          ele.sourcePtr().ecalEnergy())
            vtxFitConversion = ele.passConversionVeto()
            mHits = ele.numberOfHits()
            # Set tight and loose flags
            if abs(ele.sourcePtr().superCluster().eta()) < 1.479:
                ele.looseFakeId = dEtaIn < 0.004 and dPhiIn < 0.06 and sigmaIEtaIEta < 0.01 and hoe < 0.12 and ooemoop < 0.05
            else:
                ele.looseFakeId = dEtaIn < 0.007 and dPhiIn < 0.03 and sigmaIEtaIEta < 0.03 and hoe < 0.10 and ooemoop < 0.05
            ele.looseFakeId = ele.looseFakeId and vtxFitConversion and mHits <= 1 and abs(
                ele.dz()) < 0.1 and ele.relIso03 < 0.6
            ele.looseFakeId = ele.looseFakeId and mHits <= 1 and ele.sourcePtr(
            ).isGsfCtfScPixChargeConsistent()
            ele.tightFakeId = ele.looseFakeId and abs(
                ele.dxy()) < 0.02 and ele.relIso03 < 0.15
            #print "ele pt = %.3f  eta = %.3f sceta = %.3f detaIn = %.4f dphiIn = %.4f  sieie = %.4f  h/e = %.4f  1/e-1/p = %.4f  dxy = %.4f  dz = %.4f  losthits = %d conveto = %d relIso = %.4f     loose id = %d   tight id = %d" % ( ele.pt(),ele.eta(),ele.sourcePtr().superCluster().eta(),dEtaIn,dPhiIn,sigmaIEtaIEta,hoe,ooemoop,abs(ele.dxy()),abs(ele.dz()),mHits,vtxFitConversion,ele.relIso03, ele.looseFakeId, ele.tightFakeId)
            # add to the list if needed
            if ele.pt() > 7 and abs(ele.eta()) < 2.5:
                if ele.looseFakeId:
                    event.inclusiveLeptons.append(ele)
                    event.selectedLeptons.append(ele)

        event.looseLeptons.sort(key=lambda l: l.pt(), reverse=True)
        event.selectedLeptons.sort(key=lambda l: l.pt(), reverse=True)
        event.inclusiveLeptons.sort(key=lambda l: l.pt(), reverse=True)

        for lepton in event.selectedLeptons:
            if hasattr(self, 'efficiency'):
                self.efficiency.attachToObject(lepton)
    def makeLeptons(self, event):
        
        event.looseLeptons = []
        event.selectedLeptons = []
        event.inclusiveLeptons = []

        #muons
        allmuons = map( Muon, self.handles['muons'].product() )

        if self.cfg_ana.doRecomputeSIP3D:
            for mu in allmuons:
                if mu.sourcePtr().innerTrack().isNonnull():
                    ## compute the variable and set it
                    mu._sip3d = abs(signedSip3D(mu, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    mu.sip3D  = types.MethodType(lambda self : self._sip3d, mu, mu.__class__)

        if self.cfg_ana.doMuScleFitCorrections:
            for mu in allmuons:
                self.muscleCorr.correct(mu, event.run)
        elif self.cfg_ana.doRochesterCorrections:
            for mu in allmuons:
                corp4 = rochcor.corrected_p4(mu, event.run) 
                mu.setP4( corp4 )

        if self.cfg_ana.doSegmentBasedMuonCleaning:
            isgood = cmgMuonCleanerBySegments.clean( self.handles['muons'].product() )
            newmu = []
            for i,mu in enumerate(allmuons):
                if isgood[i]: newmu.append(mu)
            allmuons = newmu

        for mu in allmuons:
            mu.associatedVertex = event.goodVertices[0]
            if (mu.isGlobal() or mu.isTracker() and mu.numberOfMatches()>0) and mu.pt()>5 and abs(mu.eta())<2.4 and abs(mu.dxy())<0.5 and abs(mu.dz())<1.:
                #pid = mu.sourcePtr().originalObject().track().id()
                #key = mu.sourcePtr().originalObject().track().key()
                #mu.dEdX = self.handles['dEdX'].product().get(pid,key)
                if mu.sourcePtr().userFloat("isPFMuon")>0.5 and mu.sip3D() < self.cfg_ana.sip3dCut and mu.relIso(dBetaFactor=0.5)<self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(mu)
                else:
                    event.looseLeptons.append(mu)
                if mu.sourcePtr().userFloat("isPFMuon")>0.5 and mu.sip3D() < self.cfg_ana.sip3dCutVeryLoose:
                    event.inclusiveLeptons.append(mu)

        #electrons        
        allelectrons = map( Electron, self.handles['electrons'].product() )

        ## duplicate removal for fast sim (to be checked if still necessary in 5_3_12+)
        allelenodup = []
        for e in allelectrons:
            dup = False
            for e2 in allelenodup:
                if abs(e.pt()-e2.pt()) < 1e-6 and abs(e.eta()-e2.eta()) < 1e-6 and abs(e.phi()-e2.phi()) < 1e-6 and e.charge() == e2.charge():
                    dup = True
                    break
            if not dup: allelenodup.append(e)
        allelectrons = allelenodup

        if self.cfg_ana.doRecomputeSIP3D:
            for ele in allelectrons:
                if ele.sourcePtr().gsfTrack().isNonnull():
                    ## compute the variable and set it
                    ele._sip3d = abs(signedSip3D(ele, event.goodVertices[0]))
                    ## attach it to the object redefining the sip3D() method
                    ele.sip3D  = types.MethodType(lambda self : self._sip3d, ele, ele.__class__)

        # fill EA for rho-corrected isolation
        for ele in allelectrons:
          ele.rho = float(self.handles['rhoEle'].product()[0])
          SCEta = abs(ele.sourcePtr().superCluster().eta())
          if (abs(SCEta) >= 0.0 and abs(SCEta) < 1.0 ) : ele.EffectiveArea = 0.130;
          if (abs(SCEta) >= 1.0 and abs(SCEta) < 1.479 ) : ele.EffectiveArea = 0.137;
          if (abs(SCEta) >= 1.479 and abs(SCEta) < 2.0 ) : ele.EffectiveArea = 0.067;
          if (abs(SCEta) >= 2.0 and abs(SCEta) < 2.2 ) : ele.EffectiveArea = 0.089;
          if (abs(SCEta) >= 2.2 and abs(SCEta) < 2.3 ) : ele.EffectiveArea = 0.107;
          if (abs(SCEta) >= 2.3 and abs(SCEta) < 2.4 ) : ele.EffectiveArea = 0.110;
          if (abs(SCEta) >= 2.4) : ele.EffectiveArea = 0.138;

        if self.cfg_ana.doElectronScaleCorrections:
            for ele in allelectrons:
                self.electronEnergyCalibrator.correct(ele, event.run)

        muForEleCrossCleaning = []
        if self.cfg_ana.doEleMuCrossCleaning:
            for mu in event.selectedLeptons + event.looseLeptons:
                if abs(mu.pdgId()) == 13 and (mu.isGlobal() or mu.sourcePtr().userFloat("isPFMuon")>0.5):
                    muForEleCrossCleaning.append(mu)


        for ele in allelectrons:
            ele.associatedVertex = event.goodVertices[0]
            ## remove muons if muForEleCrossCleaning is not empty
            if bestMatch(ele, muForEleCrossCleaning)[1] < 0.02: continue
            ## apply selection
            if ele.pt()>7 and abs(ele.eta())<2.5 and abs(ele.dxy())<0.5 and abs(ele.dz())<1. and ele.numberOfHits()<=1:
                 ## fill tightId:
                 if (ele.pt() > 20):
                    SCEta = abs(ele.sourcePtr().superCluster().eta())
                    if   SCEta < 0.8:   ele.tightIdResult = (ele.mvaTrigV0() > 0.00)
                    elif SCEta < 1.479: ele.tightIdResult = (ele.mvaTrigV0() > 0.10)
                    else:               ele.tightIdResult = (ele.mvaTrigV0() > 0.62)
                 elif (ele.pt() > 10):
                    if   SCEta < 0.8:   ele.tightIdResult = (ele.mvaTrigV0() > 0.94)
                    elif SCEta < 1.479: ele.tightIdResult = (ele.mvaTrigV0() > 0.85)
                    else:               ele.tightIdResult = (ele.mvaTrigV0() > 0.92)
                 else:
                    ele.tightIdResult = False
                 if (self.relaxId or ele.mvaIDZZ() and ele.sip3D() < self.cfg_ana.sip3dCut) and ele.relIso(dBetaFactor=0.5)<self.cfg_ana.isolationCut:
                    event.selectedLeptons.append(ele)
                 else:
                    event.looseLeptons.append(ele)
                 if (self.relaxId or ele.mvaIDZZ()) and ele.sip3D() < self.cfg_ana.sip3dCutVeryLoose:
                    event.inclusiveLeptons.append(ele)
                 
                    
        event.looseLeptons.sort(key = lambda l : l.pt(), reverse = True)
        event.selectedLeptons.sort(key = lambda l : l.pt(), reverse = True)
        event.inclusiveLeptons.sort(key = lambda l : l.pt(), reverse = True)

        for lepton in event.selectedLeptons:
            if hasattr(self,'efficiency'):
                self.efficiency.attachToObject(lepton)