def fillTriggerSF(self, event, t, i1, i2, i3): self.ret["triggerSF"][t] = 1. if event.isData: return self.ret["triggerSF"][t] = triggerScaleFactorFullSim(self.leps[i1].pdgId, self.leps[i2].pdgId, \ self.leps[i1].pt , self.leps[i2].pt , self.ht) if self.isFastSim: self.ret["triggerSF"][t] *= FastSimTriggerEfficiency( self.ht, self.leps[i1].pt, self.leps[i1].pdgId, \ self.leps[i2].pt, self.leps[i2].pdgId)
def __call__(self,event): if not hasattr(self,"checked_fastsim_data"): if self.isFastSim and event.isData: raise RuntimeError,'Running with isFastSim on data!' self.checked_fastsim_data = True leps = [l for l in Collection(event,"LepGood","nLepGood")] lepsl = [leps[il] for il in getattr(event,"iL"+self.inputlabel)][0:getattr(event,"nLepLoose"+self.inputlabel)] # lepsc = [leps[il] for il in getattr(event,"iC"+self.inputlabel)][0:getattr(event,"nLepCleaning"+self.inputlabel)] # lepsf = [leps[il] for il in getattr(event,"iF"+self.inputlabel)][0:getattr(event,"nLepFO"+self.inputlabel)] lepst = [leps[il] for il in getattr(event,"iT"+self.inputlabel)][0:getattr(event,"nLepTight"+self.inputlabel)] # lepslv = [leps[il] for il in getattr(event,"iLV"+self.inputlabel)][0:getattr(event,"nLepLooseVeto"+self.inputlabel)] # lepscv = [leps[il] for il in getattr(event,"iCV"+self.inputlabel)][0:getattr(event,"nLepCleaningVeto"+self.inputlabel)] lepsfv = [leps[il] for il in getattr(event,"iFV"+self.inputlabel)][0:getattr(event,"nLepFOVeto"+self.inputlabel)] lepstv = [leps[il] for il in getattr(event,"iTV"+self.inputlabel)][0:getattr(event,"nLepTightVeto"+self.inputlabel)] systsFR={0:"", 1:"_ewkUp", -1:"_ewkDown"} systsJEC={0:"", 1:"_jecUp", -1:"_jecDown"} met={} metphi={} met[0]=event.met_pt met[1]=getattr(event,"met_jecUp_pt",event.met_pt) met[-1]=getattr(event,"met_jecDown_pt",event.met_pt) metphi[0]= event.met_phi metphi[1]= getattr(event,"met_jecUp_phi",event.met_phi) metphi[-1]= getattr(event,"met_jecDown_phi",event.met_phi) ret = {}; ret['mZ1'] = bestZ1TL(lepsl, lepsl) ret['mZ1cut10TL'] = bestZ1TL(lepsl, lepst, cut=lambda l:l.conePt>(10 if abs(l.pdgId)==13 else 15)) ret['minMllAFAS'] = minMllTL(lepsl, lepsl) # ret['minMllAFOS'] = minMllTL(lepsl, lepsl, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOS'] = minMllTL(lepsl, lepsl, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret['minMllAFASTL'] = minMllTL(lepsl, lepst) # ret['minMllAFOSTL'] = minMllTL(lepsl, lepst, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOSTL'] = minMllTL(lepsl, lepst, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret['minMllAFASTT'] = minMllTL(lepst, lepst) # ret['minMllAFOSTT'] = minMllTL(lepst, lepst, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOSTT'] = minMllTL(lepst, lepst, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret["nPairs"]=0 ret["i1"] = [0]*20 ret["i2"] = [0]*20 ret["whoIsFake"]=[0]*20 ret["appWeight"] = [0]*20 ret["appWeight_ewkUp"] = [0]*20 ret["appWeight_ewkDown"] = [0]*20 ret["SR"] = [0]*20 ret["SR_jecUp"] = [0]*20 ret["SR_jecDown"] = [0]*20 ret["hasTT"]=False ret["hasTF"]=False ret["hasFF"]=False ret["triggerSF"] = [0]*20 for var in self.systsLEPSF: ret["leptonSF"+self.systsLEPSF[var]] = [0]*20 if self.whichApplication == self.appl_Fakes: if self.lepChoiceMethod==self.style_TTSync: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=True,choose_SS_else_OS=True) if choice: ret["hasTT"]=True choice=choice[:1] if self.lepChoiceMethod==self.style_TT_loopTF_2FF: choice = self.findPairs(lepstv,lepstv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTT"]=True choice=choice[:1] else: choice = self.findPairs(lepstv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTF"]=True _probs = {} else: choice = self.findPairs(lepsfv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasFF"]=True elif self.lepChoiceMethod==self.style_sort_FO: choice = self.findPairs(lepsfv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: choice = choice[:1] tt_sort_FO = [False]*2 if choice[0][0] in lepstv: tt_sort_FO[0]=True if choice[0][1] in lepstv: tt_sort_FO[1]=True if tt_sort_FO==[True,True]: ret["hasTT"]=True elif tt_sort_FO==[False,False]: ret["hasFF"]=True else: ret["hasTF"] = True elif self.whichApplication == self.appl_Flips: choice = self.findPairs(lepstv,lepstv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTT"]=True choice=choice[:1] else: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=False,choose_SS_else_OS=False) if choice: ret["hasTF"]=True choice=choice[:1] elif self.whichApplication == self.appl_WZ: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=True,choose_SS_else_OS=True) if choice: ret["hasTT"]=True else: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=True,choose_SS_else_OS=False) if choice: ret["hasTF"]=True choice=choice[:1] if choice: ret["nPairs"] = len(choice) if ret["nPairs"]>20: raise RuntimeError,'Too many lepton pairs' for npair in xrange(len(choice)): i1 = leps.index(choice[npair][0]) i2 = leps.index(choice[npair][1]) ret["i1"][npair], ret["i2"][npair] = (i1,i2) if leps[i1].conePt>=leps[i2].conePt else (i2,i1) # warning: they are not necessarily ordered by pt! for var in systsJEC: mtwmin = min(sqrt(2*leps[i1].conePt*met[var]*(1-cos(leps[i1].phi-metphi[var]))),sqrt(2*leps[i2].conePt*met[var]*(1-cos(leps[i2].phi-metphi[var])))) ret["SR"+systsJEC[var]][npair]=self.SR(leps[i1].conePt,leps[i2].conePt,getattr(event,"htJet40j"+self.inputlabel+systsJEC[var]),met[var],getattr(event,"nJet40"+self.inputlabel+systsJEC[var]),getattr(event,"nBJetMedium25"+self.inputlabel+systsJEC[var]),mtwmin) ht = getattr(event,"htJet40j"+self.inputlabel) # central value ret["triggerSF"][npair] = triggerScaleFactorFullSim(leps[i1].pdgId,leps[i2].pdgId,leps[i1].pt,leps[i2].pt,ht) if not event.isData else 1 if self.isFastSim: ret["triggerSF"][npair] = ret["triggerSF"][npair] * FastSimTriggerEfficiency(ht,leps[i1].pt,leps[i1].pdgId,leps[i2].pt,leps[i2].pdgId) for var in self.systsLEPSF: lepsf=[1]*2 for i,ind in enumerate([i1,i2]): lepsf[i] = leptonScaleFactor_UCSx(leps[ind].pdgId,leps[ind].pt,leps[ind].eta,ht) if not event.isData else 1 #if abs(var)==1: TO BE IMPLEMENTED, at the moment it's done with a flat uncertainty on the final datacard if self.isFastSim: addsf, addsferr = self.read_FastSim_lepSF(leps[ind].pdgId,leps[ind].pt,leps[ind].eta,event.nVert) if not event.isData else 1 if var==2: lepsf[i] *= (addsf*(1+addsferr)) if var==-2: lepsf[i] *= (addsf*(1-addsferr)) else: lepsf[i] *= addsf ret["leptonSF"+self.systsLEPSF[var]][npair] = lepsf[0]*lepsf[1] if self.apply: if self.whichApplication == self.appl_Fakes: for varFR in systsFR: if self.lepChoiceMethod==self.style_TT_loopTF_2FF: if ret["hasTT"]: ret["appWeight"+systsFR[varFR]][npair] = 0.0 elif ret["hasTF"]: prev = 1.0 if varFR not in _probs: _probs[varFR]=[] for x in _probs[varFR]: prev *= (1-x) prob = self.FRprob(leps[i2],ht,varFR) transf = self.FRtransfer_fromprob(prob) _probs[varFR].append(prob) ret["appWeight"+systsFR[varFR]][npair] = prev * transf ret["whoIsFake"][npair] = 2 if leps[i1].conePt>=leps[i2].conePt else 1 elif ret["hasFF"]: ret["appWeight"+systsFR[varFR]][npair] = -self.FRtransfer(leps[i1],ht,varFR)*self.FRtransfer(leps[i2],ht,varFR) if len(choice)<2 else 0.0 # throw away events with three FO non Tight elif self.lepChoiceMethod==self.style_sort_FO: if ret["hasTT"]: ret["appWeight"+systsFR[varFR]][npair] = 0.0 elif ret["hasTF"]: ret["appWeight"+systsFR[varFR]][npair] = self.FRtransfer(leps[i2 if tt_sort_FO[0] else i1],ht,varFR) ret["whoIsFake"][npair] = (2 if leps[i1].conePt>=leps[i2].conePt else 1) if tt_sort_FO[0] else (1 if leps[i1].conePt>=leps[i2].conePt else 2) elif ret["hasFF"]: ret["appWeight"+systsFR[varFR]][npair] = -self.FRtransfer(leps[i1],ht,varFR)*self.FRtransfer(leps[i2],ht,varFR) elif self.whichApplication == self.appl_Flips: ret["appWeight"][npair] = self.flipRate(leps[i1])+self.flipRate(leps[i2]) ### attach labels and return fullret = {} for k,v in ret.iteritems(): fullret[k+self.label] = v return fullret
def __call__(self,event): if not hasattr(self,"checked_fastsim_data"): if self.isFastSim and event.isData: raise RuntimeError,'Running with isFastSim on data!' self.checked_fastsim_data = True leps = [l for l in Collection(event,"LepGood","nLepGood")] lepsl = [leps[il] for il in getattr(event,"iL"+self.inputlabel)] # lepsc = [leps[il] for il in getattr(event,"iC"+self.inputlabel)] # lepsf = [leps[il] for il in getattr(event,"iF"+self.inputlabel)] lepst = [leps[il] for il in getattr(event,"iT"+self.inputlabel)] # lepslv = [leps[il] for il in getattr(event,"iLV"+self.inputlabel)] # lepscv = [leps[il] for il in getattr(event,"iCV"+self.inputlabel)] lepsfv = [leps[il] for il in getattr(event,"iFV"+self.inputlabel)] lepstv = [leps[il] for il in getattr(event,"iTV"+self.inputlabel)] bjets25 = [j for j in Collection(event,"JetSel"+self.inputlabel,"nJetSel"+self.inputlabel) if (j.pt>25 and j.btagCSV>0.89)] jets40 = [j for j in Collection(event,"JetSel"+self.inputlabel,"nJetSel"+self.inputlabel) if j.pt>40] systsFR={0:"", 1:"_ewkUp", -1:"_ewkDown"} systsJEC={0:"", 1:"_jecUp", -1:"_jecDown"} met={} metphi={} met[0]=event.met_pt met[1]=getattr(event,"met_jecUp_pt",event.met_pt) met[-1]=getattr(event,"met_jecDown_pt",event.met_pt) metphi[0]= event.met_phi metphi[1]= getattr(event,"met_jecUp_phi",event.met_phi) metphi[-1]= getattr(event,"met_jecDown_phi",event.met_phi) ret = {}; ### 2lss specific things - still useful? ret['mZ1'] = self.bestZ1TL(lepsl, lepsl) ret['mZ1cut10TL'] = self.bestZ1TL(lepsl, lepst, cut=lambda l:l.conePt>(10 if abs(l.pdgId)==13 else 15)) ret['minMllAFAS'] = self.minMllTL(lepsl, lepsl) # ret['minMllAFOS'] = self.minMllTL(lepsl, lepsl, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOS'] = self.minMllTL(lepsl, lepsl, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret['minMllAFASTL'] = self.minMllTL(lepsl, lepst) # ret['minMllAFOSTL'] = self.minMllTL(lepsl, lepst, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOSTL'] = self.minMllTL(lepsl, lepst, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret['minMllAFASTT'] = self.minMllTL(lepst, lepst) # ret['minMllAFOSTT'] = self.minMllTL(lepst, lepst, paircut = lambda l1,l2 : l1.charge != l2.charge) ret['minMllSFOSTT'] = self.minMllTL(lepst, lepst, paircut = lambda l1,l2 : l1.pdgId == -l2.pdgId) ret["nPairs"]=0 ret["i1"] = [0]*20 ret["i2"] = [0]*20 ret["whoIsFake"]=[0]*20 ret["appWeight"] = [0]*20 ret["appWeight_ewkUp"] = [0]*20 ret["appWeight_ewkDown"] = [0]*20 ret["SR"] = [0]*20 ret["SR_jecUp"] = [0]*20 ret["SR_jecDown"] = [0]*20 ret["hasTT"]=False ret["hasTF"]=False ret["hasFF"]=False ret["triggerSF"] = [0]*20 for var in self.systsLEPSF: ret["leptonSF"+self.systsLEPSF[var]] = [0]*20 ret["maxDeltaPhiLepJet"] = [0]*20 ret["maxDeltaPhiLepBJet"] = [0]*20 ret["maxDeltaPhiJetJet"] = [0]*20 ret["minDeltaRLepJet"] = [0]*20 ret["minDeltaRLepBJet"] = [0]*20 if self.whichApplication == self.appl_Fakes: if self.lepChoiceMethod==self.style_TT_loopTF_2FF: choice = self.findPairs(lepstv,lepstv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTT"]=True choice=choice[:1] else: choice = self.findPairs(lepstv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTF"]=True _probs = {} else: choice = self.findPairs(lepsfv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasFF"]=True elif self.lepChoiceMethod==self.style_sort_FO: choice = self.findPairs(lepsfv,lepsfv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: choice = choice[:1] tt_sort_FO = [False]*2 if choice[0][0] in lepstv: tt_sort_FO[0]=True if choice[0][1] in lepstv: tt_sort_FO[1]=True if tt_sort_FO==[True,True]: ret["hasTT"]=True elif tt_sort_FO==[False,False]: ret["hasFF"]=True else: ret["hasTF"] = True elif self.whichApplication == self.appl_Flips: choice = self.findPairs(lepstv,lepstv,byflav=True,bypassMV=False,choose_SS_else_OS=True) if choice: ret["hasTT"]=True choice=choice[:1] else: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=False,choose_SS_else_OS=False) if choice: ret["hasTF"]=True choice=choice[:1] elif self.whichApplication == self.appl_WZ: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=True,choose_SS_else_OS=True) if choice: ret["hasTT"]=True else: choice = self.findPairs(lepst,lepst,byflav=True,bypassMV=True,choose_SS_else_OS=False) if choice: ret["hasTF"]=True choice=choice[:1] if choice: ret["nPairs"] = len(choice) if ret["nPairs"]>20: raise RuntimeError,'Too many lepton pairs' for npair in xrange(len(choice)): i1 = leps.index(choice[npair][0]) i2 = leps.index(choice[npair][1]) ret["i1"][npair], ret["i2"][npair] = (i1,i2) if leps[i1].conePt>=leps[i2].conePt else (i2,i1) # warning: they are not necessarily ordered by pt! for var in systsJEC: mtwmin = min(sqrt(2*leps[i1].conePt*met[var]*(1-cos(leps[i1].phi-metphi[var]))),sqrt(2*leps[i2].conePt*met[var]*(1-cos(leps[i2].phi-metphi[var])))) ret["SR"+systsJEC[var]][npair]=self.SR(leps[i1].conePt,leps[i2].conePt,getattr(event,"htJet40j"+systsJEC[var]+self.inputlabel),met[var],getattr(event,"nJet40"+systsJEC[var]+self.inputlabel),getattr(event,"nBJetMedium25"+systsJEC[var]+self.inputlabel),mtwmin) ht = getattr(event,"htJet40j"+self.inputlabel) # central value ret["triggerSF"][npair] = triggerScaleFactorFullSim(leps[i1].pdgId,leps[i2].pdgId,leps[i1].pt,leps[i2].pt,ht) if not event.isData else 1 if self.isFastSim: ret["triggerSF"][npair] = ret["triggerSF"][npair] * FastSimTriggerEfficiency(ht,leps[i1].pt,leps[i1].pdgId,leps[i2].pt,leps[i2].pdgId) for var in self.systsLEPSF: lepsf=[1]*2 for i,ind in enumerate([i1,i2]): lepsf[i] = leptonScaleFactor_UCSx(leps[ind].pdgId,leps[ind].pt,leps[ind].eta,ht) if not event.isData else 1 #if abs(var)==1: TO BE IMPLEMENTED, at the moment it's done with a flat uncertainty on the final datacard if self.isFastSim: addsf, addsferr = self.read_FastSim_lepSF(leps[ind].pdgId,leps[ind].pt,leps[ind].eta,event.nVert) if not event.isData else 1 if var==2: lepsf[i] *= (addsf*(1+addsferr)) if var==-2: lepsf[i] *= (addsf*(1-addsferr)) else: lepsf[i] *= addsf ret["leptonSF"+self.systsLEPSF[var]][npair] = lepsf[0]*lepsf[1] ret["maxDeltaPhiLepJet"][npair] = max([abs(deltaPhi(l.phi,j.phi)) for l in [leps[i1],leps[i2]] for j in jets40]+[-999]) ret["maxDeltaPhiLepBJet"][npair] = max([abs(deltaPhi(l.phi,j.phi)) for l in [leps[i1],leps[i2]] for j in bjets25]+[-999]) ret["maxDeltaPhiJetJet"][npair] = max([(abs(deltaPhi(j1.phi,j2.phi)) if j1!=j2 else -999) for j1 in jets40 for j2 in jets40]+[-999]) ret["minDeltaRLepJet"][npair] = min([abs(deltaR(l,j)) for l in [leps[i1],leps[i2]] for j in jets40]+[999]) ret["minDeltaRLepBJet"][npair] = min([abs(deltaR(l,j)) for l in [leps[i1],leps[i2]] for j in bjets25]+[999]) if self.apply: if self.whichApplication == self.appl_Fakes: for varFR in systsFR: if self.lepChoiceMethod==self.style_TT_loopTF_2FF: if ret["hasTT"]: ret["appWeight"+systsFR[varFR]][npair] = 0.0 elif ret["hasTF"]: prev = 1.0 if varFR not in _probs: _probs[varFR]=[] for x in _probs[varFR]: prev *= (1-x) prob = self.FRprob(leps[i2],ht,varFR) transf = self.FRtransfer_fromprob(prob) _probs[varFR].append(prob) ret["appWeight"+systsFR[varFR]][npair] = prev * transf ret["whoIsFake"][npair] = 2 if leps[i1].conePt>=leps[i2].conePt else 1 elif ret["hasFF"]: ret["appWeight"+systsFR[varFR]][npair] = -self.FRtransfer(leps[i1],ht,varFR)*self.FRtransfer(leps[i2],ht,varFR) if len(choice)<2 else 0.0 # throw away events with three FO non Tight elif self.lepChoiceMethod==self.style_sort_FO: if ret["hasTT"]: ret["appWeight"+systsFR[varFR]][npair] = 0.0 elif ret["hasTF"]: ret["appWeight"+systsFR[varFR]][npair] = self.FRtransfer(leps[i2 if tt_sort_FO[0] else i1],ht,varFR) ret["whoIsFake"][npair] = (2 if leps[i1].conePt>=leps[i2].conePt else 1) if tt_sort_FO[0] else (1 if leps[i1].conePt>=leps[i2].conePt else 2) elif ret["hasFF"]: ret["appWeight"+systsFR[varFR]][npair] = -self.FRtransfer(leps[i1],ht,varFR)*self.FRtransfer(leps[i2],ht,varFR) elif self.whichApplication == self.appl_Flips: ret["appWeight"][npair] = self.flipRate(leps[i1])+self.flipRate(leps[i2]) ### attach labels and return fullret = {} for k,v in ret.iteritems(): fullret[k+self.label] = v return fullret