def getAllTaus(c, ntaus ): res=[] for i in range(0, int(ntaus)): if goodTauID(c, i): res.append({'pt':getVarValue(c, 'tausPt', i),'eta':getVarValue(c, 'tausEta', i), 'phi':getVarValue(c, 'tausPhi', i),\ 'Pdg':getVarValue(c, 'tausPdg', i)}) res = sorted(res, key=lambda k: -k['pt']) return res
def wRecoPt(chain): lPt = getVarValue(chain, "leptonPt") lPhi = getVarValue(chain, "leptonPhi") metphi = getVarValue(chain, "type1phiMetphi") met = getVarValue(chain, "type1phiMet") cosLepPhi = cos(lPhi) sinLepPhi = sin(lPhi) mpx = met * cos(metphi) mpy = met * sin(metphi) return sqrt((lPt * cosLepPhi + mpx)**2 + (lPt * sinLepPhi + mpy)**2)
def cosDeltaPhiLepW(chain): lPt = getVarValue(chain, "leptonPt") lPhi = getVarValue(chain, "leptonPhi") mpx = getVarValue(chain, "type1phiMetpx") mpy = getVarValue(chain, "type1phiMetpy") cosLepPhi = cos(lPhi) sinLepPhi = sin(lPhi) pW = sqrt((lPt*cosLepPhi + mpx)**2 + (lPt*sinLepPhi + mpy)**2) return ((lPt*cosLepPhi + mpx)*cosLepPhi + (lPt*sinLepPhi + mpy)*sinLepPhi )/pW
def wRecoPt(chain): lPt = getVarValue(chain, "leptonPt") lPhi = getVarValue(chain, "leptonPhi") metphi = getVarValue(chain, "type1phiMetphi") met = getVarValue(chain, "type1phiMet") cosLepPhi = cos(lPhi) sinLepPhi = sin(lPhi) mpx = met*cos(metphi) mpy = met*sin(metphi) return sqrt((lPt*cosLepPhi + mpx)**2 + (lPt*sinLepPhi + mpy)**2)
def getSoftMuon(c): for imu in reversed(range(int(getVarValue(c, 'nmuCount')))): relIso = getVarValue(c, 'muRelIso', imu) pt = getVarValue(c, 'muPt', imu) isGlobal = getVarValue(c, 'muIsGlobal', imu) isTracker = getVarValue(c, 'muIsTracker', imu) dz = getVarValue(c, 'muDz', imu) eta=getVarValue(c, 'muEta', imu) if (isGlobal or isTracker) and pt>5. and abs(eta)<2.1 and abs(dz)<0.5: if (pt<20. and pt*relIso<10): return {'pt':pt, 'eta':eta, 'phi':getVarValue(c, 'muPhi', imu), 'pdg':getVarValue(c, 'muPdg', imu)}
def getAllMuons(c, nmuons ): res=[] for i in range(0, int(nmuons)): cand = getLooseMu(c, i) if cand: for v in ['Pdg', 'Dxy', 'NormChi2', 'NValMuonHits', 'NumMatchedStations', 'PixelHits', 'NumtrackerLayerWithMeasurement']: cand[v] = getVarValue(c, 'muons'+v, i) res.append(cand) res = sorted(res, key=lambda k: -k['pt']) return res
def getLooseMu(c, imu ): isPF = getVarValue(c, 'muonsisPF', imu) isGlobal = getVarValue(c, 'muonsisGlobal', imu) isTracker = getVarValue(c, 'muonsisTracker', imu) pt = getVarValue(c, 'muonsPt', imu) dz = getVarValue(c, 'muonsDz', imu) eta=getVarValue(c, 'muonsEta', imu) if isPF and (isGlobal or isTracker) and pt>5. and abs(eta)<2.1 and abs(dz)<0.5: return {'pt':pt, 'phi':getVarValue(c, 'muonsPhi', imu), 'eta':eta, 'IsGlobal':isGlobal, 'IsTracker':isTracker, 'IsPF':isPF, 'relIso':getVarValue(c, 'muonsPFRelIso', imu), 'Dz':dz}
def getNEvents(dir, flist): d = ROOT.TChain('Runs') for f in flist: d.Add('root://hephyse.oeaw.ac.at/'+dir+'/'+f) nevents = 0 nruns = d.GetEntries() for i in range(0, nruns): d.GetEntry(i) nevents += getVarValue(d,'uint_EventCounter_runCounts_PAT.obj') del d return int(nevents)
def goodTauID(c, itau ): return getVarValue(c, 'tausisPF', itau) and \ getVarValue(c, 'tausDecayModeFinding', itau) and \ getVarValue(c, 'tausAgainstMuonLoose', itau) and \ getVarValue(c, 'tausAgainstElectronLoose', itau) and \ getVarValue(c, 'tausByLooseCombinedIsolationDBSumPtCorr', itau) and \ getVarValue(c, 'tausPt', itau)>5.
nEvents = eList.GetN() if options.small: if nEvents>1001: nEvents=1001 #print "Reading percentage ",options.fromPercentage, "to",options.toPercentage, "which is range",start,"to",stop,"of",nEvents for i in range(nEvents): for k in vars.keys(): # print k, vars['ptZ'].value vars[k].value=0 c.GetEntry(eList.GetEntry(i)) if i%100==0: print "Event",i, "/",nEvents patMET = getVarValue(c, 'patPFMet') patMETphi = getVarValue(c, 'patPFMetphi') vars['patMEx'].value = patMET*cos(patMETphi) vars['patMEy'].value = patMET*sin(patMETphi) vars['ptZ'].value = sqrt((getVarValue(c, "muonsPt", 0)*cos(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*cos(getVarValue(c, "muonsPhi", 1)))**2\ +(getVarValue(c, "muonsPt", 0)*sin(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*sin(getVarValue(c, "muonsPhi", 1)))**2) vars['phiZ'].value = atan2( getVarValue(c, "muonsPt", 0)*sin(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*sin(getVarValue(c, "muonsPhi", 1)),\ getVarValue(c, "muonsPt", 0)*cos(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*cos(getVarValue(c, "muonsPhi", 1))) vars['ngoodVertices'].value = int(getVarValue(c, 'ngoodVertices')) events.to(eList.GetEntry(i)) events.getByLabel(labelpf,pfhandle) pfc = pfhandle.product() vecs={} for t in usedPFTypes: vecs[t] = [] for p in pfc:
def calcPolWeights(c): #find gen W ngp = int(getVarValue(c, 'ngp')) for gpLep in range(ngp): pdgLep = getVarValue(c, 'gpPdg', gpLep) staLep = getVarValue(c, 'gpSta', gpLep) if staLep==3 and (abs(pdgLep)==11 or abs(pdgLep)==13 or abs(pdgLep)==15): gpW = int(getVarValue(c, 'gpMo1', gpLep)) if abs(getVarValue(c, 'gpPdg', int(gpW)))==24: # print 'pdg',pdgLep,'sta',getVarValue(c, 'gpSta', gpLep),"gpW",gpW,getVarValue(c, 'gpPdg', int(gpW)) plus = pdgLep<0 WPt =getVarValue(c, "gpPt", gpW) WEta =getVarValue(c, "gpEta", gpW) WPhi =getVarValue(c, "gpPhi", gpW) genp4_W_ = ROOT.TLorentzVector() genp4_W_.SetPtEtaPhiM(WPt, WEta, WPhi, 80.4) LepPt =getVarValue(c, "gpPt", gpLep) LepEta =getVarValue(c, "gpEta", gpLep) LepPhi =getVarValue(c, "gpPhi", gpLep) genp4_l_ = ROOT.TLorentzVector() genp4_l_.SetPtEtaPhiM(LepPt, LepEta, LepPhi, 0.) # genp4_W_.Print() # genp4_l_.Print() WPol1Plus10_weight_flfr = 1 WPol1Minus10_weight_flfr = 1 WPol2PlusPlus5_weight_flfr = 1 WPol2PlusMinus5_weight_flfr = 1 WPol2MinusPlus5_weight_flfr = 1 WPol2MinusMinus5_weight_flfr = 1 WPol3Plus10_weight_flfr = 1 WPol3Minus10_weight_flfr = 1 if plus: WPol1Plus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,10,1); WPol1Minus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,-10,1); WPol2PlusPlus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,5,1); WPol2PlusMinus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,-5,1); WPol3Plus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0(genp4_W_,genp4_l_,+10,1); WPol3Minus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0(genp4_W_,genp4_l_,-10,1); else: WPol1Plus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,10,0); WPol1Minus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,-10,0); WPol2MinusPlus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,5,0); WPol2MinusMinus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR(genp4_W_,genp4_l_,-5,0); WPol3Plus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0(genp4_W_,genp4_l_,+10,0); WPol3Minus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0(genp4_W_,genp4_l_,-10,0); res = {\ "WPol1Plus10_weight_flfr":WPol1Plus10_weight_flfr, "WPol1Minus10_weight_flfr":WPol1Minus10_weight_flfr, "WPol2PlusPlus5_weight_flfr":WPol2PlusPlus5_weight_flfr, "WPol2PlusMinus5_weight_flfr":WPol2PlusMinus5_weight_flfr, "WPol2MinusPlus5_weight_flfr":WPol2MinusPlus5_weight_flfr, "WPol2MinusMinus5_weight_flfr":WPol2MinusMinus5_weight_flfr, "WPol3Plus10_weight_f0":WPol3Plus10_weight_f0, "WPol3Minus10_weight_f0":WPol3Minus10_weight_f0 } return res
print "cut",cut,"Entries", n #nEvents = min([1000, n]) nEvents = min([options.maxEvents, n]) for i in range(nEvents): c.GetEntry(eList.GetEntry(i)) if i%100==0:print "At",i,"/",nEvents print "At",i,"/",nEvents mexUncorr = 0. meyUncorr = 0. mexCorr = 0. meyCorr = 0. mexCorrPt = 0. meyCorrPt = 0. c.GetEntry(i) nCand = getVarValue (c, 'nCand') counter=0 for j in range(int(nCand)): if label[getVarValue(c, 'candId', j)] == map['type']: eta = getVarValue (c, 'candEta', j) pt = getVarValue (c, 'candPt', j) # pt = pt*cosh(eta) for ptb_ in ptBins: if pt>ptb_[0] and (pt<ptb_[1] or ptb_[1]<0): ptb=tuple(ptb_) break # if not ptb==(0,0.5):continue if eta>=map['binning'][1] and eta<map['binning'][2]: # counter+=1 etaBin = a.FindBin(eta) phi = getVarValue (c, 'candPhi', j)
def getGoodJets(c, crosscleanobjects, jermode=options.jermode, jesmode=options.jesmode): nSoftJets = getVarValue(c, 'nsoftjets') # jet.pt() > 10. res = [] bres = [] ht = 0. nbtags = 0 met_dx = 0. met_dy = 0. if jesmode.lower() != "none": if jesmode.lower() == 'up': sign = +1 if jesmode.lower() == 'down': sign = -1 delta_met_x_unclustered = getVarValue(c, 'deltaMETxUnclustered') delta_met_y_unclustered = getVarValue(c, 'deltaMETyUnclustered') met_dx += 0.1 * sign * delta_met_x_unclustered met_dy += 0.1 * sign * delta_met_y_unclustered for i in range(int(nSoftJets)): eta = getVarValue(c, 'jetsEta', i) pt = getVarValue(c, 'jetsPt', i) unc = getVarValue(c, 'jetsUnc', i) id = getVarValue(c, 'jetsID', i) phi = getVarValue(c, 'jetsPhi', i) # if max([jet['muef'],jet['elef']]) > 0.6 : print jet if jermode.lower() != "none": c_jet = jerDifferenceScaleFactor(eta, jermode) sigmaMCRel = jerSigmaMCRel(pt, eta) sigma = sqrt(c_jet**2 - 1) * sigmaMCRel scale = random.gauss(1, sigma) met_dx += (1 - scale) * cos(phi) * pt met_dy += (1 - scale) * sin(phi) * pt pt *= scale # print i,'pt', pt, 'eta',eta, 'sigmaMCRel', sigmaMCRel, 'c_jet',c_jet,'sigma', sigma,'scale',scale, 'met_dx',met_dx,'met_dy',met_dy if jesmode.lower() != "none": scale = 1. + sign * unc met_dx += (1 - scale) * cos(phi) * pt met_dy += (1 - scale) * sin(phi) * pt pt *= scale if abs(eta) <= 4.5 and pt > 40: parton = int(abs(getVarValue(c, 'jetsParton', i))) jet = {'pt':pt, 'eta':eta,'phi':phi, 'Pdg':parton,\ 'id':id, 'chef':getVarValue(c, 'jetsChargedHadronEnergyFraction', i), 'nhef':getVarValue(c, 'jetsNeutralHadronEnergyFraction', i),\ 'ceef':getVarValue(c, 'jetsChargedEmEnergyFraction', i), 'neef':getVarValue(c, 'jetsNeutralEmEnergyFraction', i), 'id':id,\ 'hfhef':getVarValue(c, 'jetsHFHadronEnergyFraction', i), 'hfeef':getVarValue(c, 'jetsHFEMEnergyFraction', i),\ 'muef':getVarValue(c, 'jetsMuonEnergyFraction', i), 'elef':getVarValue(c, 'jetsElectronEnergyFraction', i), 'phef':getVarValue(c, 'jetsPhotonEnergyFraction', i),\ 'jetCutBasedPUJetIDFlag':getVarValue(c, 'jetsCutBasedPUJetIDFlag', i),'jetMET53XPUJetIDFlag':getVarValue(c, 'jetsMET53XPUJetIDFlag', i),'jetFull53XPUJetIDFlag':getVarValue(c, 'jetsFull53XPUJetIDFlag', i), 'btag': getVarValue(c, 'jetsBtag', i), 'unc': unc } isolated = True for obj in crosscleanobjects: #Jet cross-cleaning if deltaR( jet, obj ) < 0.3: # and obj['relIso']< relIsoCleaningRequ: #(obj['pt']/jet['pt']) > 0.4: isolated = False # print "Cleaned", 'deltaR', deltaR(jet, obj), 'maxfrac', max([jet['muef'],jet['elef']]), 'pt:jet/obj', jet['pt'], obj['pt'], "relIso", obj['relIso'], 'btag',getVarValue(c, 'jetsBtag', i), "parton", parton # print 'Not this one!', jet, obj, deltaR(jet, obj) break jet['isolated'] = isolated res.append(jet) res = sorted(res, key=lambda k: -k['pt']) return {'jets': res, 'met_dx': met_dx, 'met_dy': met_dy}
newList.append(f) break filelist = newList print "Doing only", newList if options.small: filelist = filelist[:10] for f in filelist: sample['filenames'][bin].append(subdirname+f) d = ROOT.TChain('Runs') for f in sample['filenames'][bin]: print "Adding",f d.Add(prefix+f) nevents = 0 nruns = d.GetEntries() for i in range(0, nruns): d.GetEntry(i) nevents += getVarValue(d,'uint_EventCounter_runCounts_PAT.obj') del d if not bin.lower().count('run'): if nevents>0: weight = xsec[bin]*target_lumi/nevents else: weight=0 print 'Sample', sample['name'], 'bin', bin,'xsec',xsec[bin], 'n-events',nevents,'weight',weight else: weight = 1. print 'Sample', sample['name'], 'bin', bin, 'n-events',nevents,'weight',weight sample["weight"][bin]=weight if not os.path.isdir(outputDir): os.system('mkdir -p '+outputDir) outSubDir = options.chmode
def cosDeltaPhiLepMET(chain): lepton_pt = getVarValue(chain, "leptonPt") met = getVarValue(chain, "met") mT = getVarValue(chain, "mT") res = 1. - mT**2/(2.*met*lepton_pt) return res
def getVetoEle(c, iele): # POG Ele veto https://twiki.cern.ch/twiki/bin/viewauth/CMS/EgammaCutBasedIdentification eta = getVarValue(c, 'elesEta', iele) pdg = getVarValue(c, 'elesPdg', iele) sietaieta = getVarValue(c, 'elesSigmaIEtaIEta', iele) dphi = getVarValue(c, 'elesDPhi', iele) deta = getVarValue(c, 'elesDEta', iele) HoE = getVarValue(c, 'elesHoE', iele) isEB = abs(eta) < 1.479 isEE = abs(eta) > 1.479 and abs(eta) < 2.5 relIso = getVarValue(c, 'elesPfRelIso', iele) pt = getVarValue(c, 'elesPt', iele) dxy = getVarValue(c, 'elesDxy', iele) dz = getVarValue(c, 'elesDz', iele) oneOverEMinusOneOverP = getVarValue(c, 'elesOneOverEMinusOneOverP', iele) convRej = getVarValue(c, 'elesPassConversionRejection', iele) missingHits = getVarValue(c, 'elesMissingHits', iele) relIsoCut = 0.15 if ( isEE or isEB)\ and ((isEB and dphi < 0.8) or (isEE and dphi < 0.7)) and ( (isEB and deta < 0.007) or (isEE and deta < 0.01) )\ and ((isEB and sietaieta < 0.01 ) or (isEE and sietaieta < 0.03))\ and ( isEB and HoE < 0.15 )\ and abs(dxy) < 0.04 and abs(dz) < 0.2 \ and ( relIso < relIsoCut ) \ and pt>5.: return {'pt':pt, 'phi':getVarValue(c, 'elesPhi', iele), 'Pdg':pdg, 'eta':eta, 'sIEtaIEta':sietaieta, 'DPhi':dphi, \ 'DEta':deta, 'HoE':HoE, 'OneOverEMinusOneOverP':oneOverEMinusOneOverP, 'ConvRejection':convRej, 'MissingHits':missingHits,\ 'isEB':isEB, 'isEE':isEE, 'relIso':relIso, 'Dxy':dxy, 'Dz':dz}
def getGoodJets(c, crosscleanobjects=[]): nSoftJets = getVarValue(c, 'nsoftjets') # jet.pt() > 10. res = [] bres = [] ht = 0. nbtags = 0 for i in range(int(nSoftJets)): eta = getVarValue(c, 'jetsEta', i) pt = getVarValue(c, 'jetsPt', i) if abs(eta) <= 4.5: unc = getVarValue(c, 'jetsUnc', i) id = getVarValue(c, 'jetsID', i) phi = getVarValue(c, 'jetsPhi', i) # if max([jet['muef'],jet['elef']]) > 0.6 : print jet if pt>30: parton = int(abs(getVarValue(c, 'jetsParton', i))) jet = {'pt':pt, 'eta':eta,'phi':phi, 'Pdg':parton,\ 'id':id, 'chef':getVarValue(c, 'jetsChargedHadronEnergyFraction', i), 'nhef':getVarValue(c, 'jetsNeutralHadronEnergyFraction', i),\ 'ceef':getVarValue(c, 'jetsChargedEmEnergyFraction', i), 'neef':getVarValue(c, 'jetsNeutralEmEnergyFraction', i), 'id':id,\ 'hfhef':getVarValue(c, 'jetsHFHadronEnergyFraction', i), 'hfeef':getVarValue(c, 'jetsHFEMEnergyFraction', i),\ 'muef':getVarValue(c, 'jetsMuonEnergyFraction', i), 'elef':getVarValue(c, 'jetsElectronEnergyFraction', i), 'phef':getVarValue(c, 'jetsPhotonEnergyFraction', i),\ # 'jetCutBasedPUJetIDFlag':getVarValue(c, 'jetsCutBasedPUJetIDFlag', i),'jetMET53XPUJetIDFlag':getVarValue(c, 'jetsMET53XPUJetIDFlag', i),'jetFull53XPUJetIDFlag':getVarValue(c, 'jetsFull53XPUJetIDFlag', i), 'btag': getVarValue(c, 'jetsBtag', i), 'unc': unc } isolated = True for obj in crosscleanobjects: #Jet cross-cleaning if deltaR(jet, obj) < 0.3:# and obj['relIso']< relIsoCleaningRequ: #(obj['pt']/jet['pt']) > 0.4: isolated = False # print "Cleaned", 'deltaR', deltaR(jet, obj), 'maxfrac', max([jet['muef'],jet['elef']]), 'pt:jet/obj', jet['pt'], obj['pt'], "relIso", obj['relIso'], 'btag',getVarValue(c, 'jetsBtag', i), "parton", parton # print 'Not this one!', jet, obj, deltaR(jet, obj) break jet['isolated'] = isolated res.append(jet) res = sorted(res, key=lambda k: -k['pt']) return {'jets':res}#,'met_dx':met_dx, 'met_dy':met_dy}
res={} for j, c in enumerate([cWJetsHT150v2, data]) : c.Draw('>>eList', cut) eList = ROOT.gDirectory.Get('eList') if j==0: y={} ye={} else: y=0 ye=0 for i in range(eList.GetN()): # if not i%10000: print "At",i,"/",eList.GetN() c.GetEntry(eList.GetEntry(i)) mu = getSoftMuon(c) if mu and muRequirement(mu) and pdgSign==mu['pdg']/abs(mu['pdg']): mT = calcMT(mu, {'pt':getVarValue(c, 'type1phiMet'), 'phi':getVarValue(c, 'type1phiMetphi')}) if mT>60 and mT<88: if j==0: w = getVarValue(c, 'puWeight') pol = calcPolWeights(c) for k in pol.keys(): if not y.has_key(k): y[k] = w*pol[k] ye[k] = (w*pol[k])**2 else: y[k] += w*pol[k] ye[k] += (w*pol[k])**2 else: y+=1 ye+=1 # res[j]={'v':y,'sigma':sqrt(ye)}
def getGoodJets(c, crosscleanobjects, jermode=options.jermode, jesmode=options.jesmode): nSoftJets = getVarValue(c, 'nsoftjets') # jet.pt() > 10. res = [] bres = [] ht = 0. nbtags = 0 met_dx = 0. met_dy = 0. if jesmode.lower()!="none": if jesmode.lower()=='up': sign=+1 if jesmode.lower()=='down': sign=-1 delta_met_x_unclustered = getVarValue(c, 'deltaMETxUnclustered') delta_met_y_unclustered = getVarValue(c, 'deltaMETyUnclustered') met_dx+=0.1*sign*delta_met_x_unclustered met_dy+=0.1*sign*delta_met_y_unclustered for i in range(int(nSoftJets)): eta = getVarValue(c, 'jetsEta', i) pt = getVarValue(c, 'jetsPt', i) unc = getVarValue(c, 'jetsUnc', i) id = getVarValue(c, 'jetsID', i) phi = getVarValue(c, 'jetsPhi', i) # if max([jet['muef'],jet['elef']]) > 0.6 : print jet if jermode.lower()!="none": c_jet = jerDifferenceScaleFactor(eta, jermode) sigmaMCRel = jerSigmaMCRel(pt, eta) sigma = sqrt(c_jet**2 - 1)*sigmaMCRel scale = random.gauss(1,sigma) met_dx+=(1-scale)*cos(phi)*pt met_dy+=(1-scale)*sin(phi)*pt pt*=scale # print i,'pt', pt, 'eta',eta, 'sigmaMCRel', sigmaMCRel, 'c_jet',c_jet,'sigma', sigma,'scale',scale, 'met_dx',met_dx,'met_dy',met_dy if jesmode.lower()!="none": scale = 1. + sign*unc met_dx+=(1-scale)*cos(phi)*pt met_dy+=(1-scale)*sin(phi)*pt pt*=scale if abs(eta) <= 4.5 and pt>40: parton = int(abs(getVarValue(c, 'jetsParton', i))) jet = {'pt':pt, 'eta':eta,'phi':phi, 'Pdg':parton,\ 'id':id, 'chef':getVarValue(c, 'jetsChargedHadronEnergyFraction', i), 'nhef':getVarValue(c, 'jetsNeutralHadronEnergyFraction', i),\ 'ceef':getVarValue(c, 'jetsChargedEmEnergyFraction', i), 'neef':getVarValue(c, 'jetsNeutralEmEnergyFraction', i), 'id':id,\ 'hfhef':getVarValue(c, 'jetsHFHadronEnergyFraction', i), 'hfeef':getVarValue(c, 'jetsHFEMEnergyFraction', i),\ 'muef':getVarValue(c, 'jetsMuonEnergyFraction', i), 'elef':getVarValue(c, 'jetsElectronEnergyFraction', i), 'phef':getVarValue(c, 'jetsPhotonEnergyFraction', i),\ 'jetCutBasedPUJetIDFlag':getVarValue(c, 'jetsCutBasedPUJetIDFlag', i),'jetMET53XPUJetIDFlag':getVarValue(c, 'jetsMET53XPUJetIDFlag', i),'jetFull53XPUJetIDFlag':getVarValue(c, 'jetsFull53XPUJetIDFlag', i), 'btag': getVarValue(c, 'jetsBtag', i), 'unc': unc } isolated = True for obj in crosscleanobjects: #Jet cross-cleaning if deltaR(jet, obj) < 0.3:# and obj['relIso']< relIsoCleaningRequ: #(obj['pt']/jet['pt']) > 0.4: isolated = False # print "Cleaned", 'deltaR', deltaR(jet, obj), 'maxfrac', max([jet['muef'],jet['elef']]), 'pt:jet/obj', jet['pt'], obj['pt'], "relIso", obj['relIso'], 'btag',getVarValue(c, 'jetsBtag', i), "parton", parton # print 'Not this one!', jet, obj, deltaR(jet, obj) break jet['isolated'] = isolated res.append(jet) res = sorted(res, key=lambda k: -k['pt']) return {'jets':res,'met_dx':met_dx, 'met_dy':met_dy}
def getVetoEle(c, iele): # POG Ele veto https://twiki.cern.ch/twiki/bin/viewauth/CMS/EgammaCutBasedIdentification eta = getVarValue(c, 'elesEta', iele) pdg = getVarValue(c, 'elesPdg', iele) sietaieta = getVarValue(c, 'elesSigmaIEtaIEta', iele) dphi = getVarValue(c, 'elesDPhi', iele) deta = getVarValue(c, 'elesDEta', iele) HoE = getVarValue(c, 'elesHoE', iele) isEB = abs(eta) < 1.479 isEE = abs(eta) > 1.479 and abs(eta) < 2.5 relIso = getVarValue(c, 'elesPfRelIso', iele) pt = getVarValue(c, 'elesPt', iele) dxy = getVarValue(c, 'elesDxy', iele) dz = getVarValue(c, 'elesDz', iele) oneOverEMinusOneOverP = getVarValue(c, 'elesOneOverEMinusOneOverP', iele) convRej = getVarValue(c, 'elesPassConversionRejection', iele) missingHits = getVarValue(c, 'elesMissingHits', iele) relIsoCut = 0.15 if ( isEE or isEB)\ and ((isEB and dphi < 0.8) or (isEE and dphi < 0.7)) and ( (isEB and deta < 0.007) or (isEE and deta < 0.01) )\ and ((isEB and sietaieta < 0.01 ) or (isEE and sietaieta < 0.03))\ and ( isEB and HoE < 0.15 )\ and dxy < 0.04 and dz < 0.2 \ and ( relIso < relIsoCut ) \ and pt>5.: return {'pt':pt, 'phi':getVarValue(c, 'elesPhi', iele), 'Pdg':pdg, 'eta':eta, 'sIEtaIEta':sietaieta, 'DPhi':dphi, \ 'DEta':deta, 'HoE':HoE, 'OneOverEMinusOneOverP':oneOverEMinusOneOverP, 'ConvRejection':convRej, 'MissingHits':missingHits,\ 'isEB':isEB, 'isEE':isEE, 'relIso':relIso, 'Dxy':dxy, 'Dz':dz}
c = ROOT.TChain(sample['Chain']) for tfile in sample['filenames'][bin]: print "Adding", prefix + tfile c.Add(prefix + tfile) nevents = c.GetEntries(sample['additionalCut']) print nevents, sample['additionalCut'] del c else: d = ROOT.TChain('Runs') for tfile in sample['filenames'][bin]: d.Add(prefix + tfile) nevents = 0 nruns = d.GetEntries() for i in range(0, nruns): d.GetEntry(i) nevents += getVarValue(d, 'uint_EventCounter_runCounts_PAT.obj') del d if not bin.lower().count('run'): if nevents > 0: weight = xsec[bin] * target_lumi / nevents else: weight = 0 print 'Sample', sample['name'], 'bin', bin, 'xsec', xsec[ bin], 'n-events', nevents, 'weight', weight else: weight = 1. print 'Sample', sample[ 'name'], 'bin', bin, 'n-events', nevents, 'weight', weight sample["weight"][bin] = weight
print "cut", cut, "Entries", n #nEvents = min([1000, n]) nEvents = min([options.maxEvents, n]) for i in range(nEvents): c.GetEntry(eList.GetEntry(i)) if i % 100 == 0: print "At", i, "/", nEvents print "At", i, "/", nEvents mexUncorr = 0. meyUncorr = 0. mexCorr = 0. meyCorr = 0. mexCorrPt = 0. meyCorrPt = 0. c.GetEntry(i) nCand = getVarValue(c, 'nCand') counter = 0 for j in range(int(nCand)): if label[getVarValue(c, 'candId', j)] == map['type']: eta = getVarValue(c, 'candEta', j) pt = getVarValue(c, 'candPt', j) # pt = pt*cosh(eta) for ptb_ in ptBins: if pt > ptb_[0] and (pt < ptb_[1] or ptb_[1] < 0): ptb = tuple(ptb_) break # if not ptb==(0,0.5):continue if eta >= map['binning'][1] and eta < map['binning'][2]: # counter+=1 etaBin = a.FindBin(eta) phi = getVarValue(c, 'candPhi', j)
nEvents = eList.GetN() if options.small: if nEvents > 1001: nEvents = 1001 #print "Reading percentage ",options.fromPercentage, "to",options.toPercentage, "which is range",start,"to",stop,"of",nEvents for i in range(nEvents): for k in vars.keys(): # print k, vars['ptZ'].value vars[k].value = 0 c.GetEntry(eList.GetEntry(i)) if i % 100 == 0: print "Event", i, "/", nEvents patMET = getVarValue(c, 'patPFMet') patMETphi = getVarValue(c, 'patPFMetphi') vars['patMEx'].value = patMET * cos(patMETphi) vars['patMEy'].value = patMET * sin(patMETphi) vars['ptZ'].value = sqrt((getVarValue(c, "muonsPt", 0)*cos(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*cos(getVarValue(c, "muonsPhi", 1)))**2\ +(getVarValue(c, "muonsPt", 0)*sin(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*sin(getVarValue(c, "muonsPhi", 1)))**2) vars['phiZ'].value = atan2( getVarValue(c, "muonsPt", 0)*sin(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*sin(getVarValue(c, "muonsPhi", 1)),\ getVarValue(c, "muonsPt", 0)*cos(getVarValue(c, "muonsPhi", 0)) + getVarValue(c, "muonsPt", 1)*cos(getVarValue(c, "muonsPhi", 1))) vars['ngoodVertices'].value = int(getVarValue(c, 'ngoodVertices')) events.to(eList.GetEntry(i)) events.getByLabel(labelpf, pfhandle) pfc = pfhandle.product() vecs = {} for t in usedPFTypes: vecs[t] = [] for p in pfc:
def calcPolWeights(c): #find gen W ngp = int(getVarValue(c, 'ngp')) for gpLep in range(ngp): pdgLep = getVarValue(c, 'gpPdg', gpLep) staLep = getVarValue(c, 'gpSta', gpLep) if staLep == 3 and (abs(pdgLep) == 11 or abs(pdgLep) == 13 or abs(pdgLep) == 15): gpW = int(getVarValue(c, 'gpMo1', gpLep)) if abs(getVarValue(c, 'gpPdg', int(gpW))) == 24: # print 'pdg',pdgLep,'sta',getVarValue(c, 'gpSta', gpLep),"gpW",gpW,getVarValue(c, 'gpPdg', int(gpW)) plus = pdgLep < 0 WPt = getVarValue(c, "gpPt", gpW) WEta = getVarValue(c, "gpEta", gpW) WPhi = getVarValue(c, "gpPhi", gpW) genp4_W_ = ROOT.TLorentzVector() genp4_W_.SetPtEtaPhiM(WPt, WEta, WPhi, 80.4) LepPt = getVarValue(c, "gpPt", gpLep) LepEta = getVarValue(c, "gpEta", gpLep) LepPhi = getVarValue(c, "gpPhi", gpLep) genp4_l_ = ROOT.TLorentzVector() genp4_l_.SetPtEtaPhiM(LepPt, LepEta, LepPhi, 0.) # genp4_W_.Print() # genp4_l_.Print() WPol1Plus10_weight_flfr = 1 WPol1Minus10_weight_flfr = 1 WPol2PlusPlus5_weight_flfr = 1 WPol2PlusMinus5_weight_flfr = 1 WPol2MinusPlus5_weight_flfr = 1 WPol2MinusMinus5_weight_flfr = 1 WPol3Plus10_weight_flfr = 1 WPol3Minus10_weight_flfr = 1 if plus: WPol1Plus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, 10, 1) WPol1Minus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, -10, 1) WPol2PlusPlus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, 5, 1) WPol2PlusMinus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, -5, 1) WPol3Plus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0( genp4_W_, genp4_l_, +10, 1) WPol3Minus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0( genp4_W_, genp4_l_, -10, 1) else: WPol1Plus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, 10, 0) WPol1Minus10_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, -10, 0) WPol2MinusPlus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, 5, 0) WPol2MinusMinus5_weight_flfr = ROOT.GetWeightWjetsPolarizationFLminusFR( genp4_W_, genp4_l_, -5, 0) WPol3Plus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0( genp4_W_, genp4_l_, +10, 0) WPol3Minus10_weight_f0 = ROOT.GetWeightWjetsPolarizationF0( genp4_W_, genp4_l_, -10, 0) res = {\ "WPol1Plus10_weight_flfr":WPol1Plus10_weight_flfr, "WPol1Minus10_weight_flfr":WPol1Minus10_weight_flfr, "WPol2PlusPlus5_weight_flfr":WPol2PlusPlus5_weight_flfr, "WPol2PlusMinus5_weight_flfr":WPol2PlusMinus5_weight_flfr, "WPol2MinusPlus5_weight_flfr":WPol2MinusPlus5_weight_flfr, "WPol2MinusMinus5_weight_flfr":WPol2MinusMinus5_weight_flfr, "WPol3Plus10_weight_f0":WPol3Plus10_weight_f0, "WPol3Minus10_weight_f0":WPol3Minus10_weight_f0 } return res
c = ROOT.TChain(sample['Chain']) for tfile in sample['filenames'][bin]: print "Adding",prefix+tfile c.Add(prefix+tfile) nevents = c.GetEntries(sample['additionalCut']) print nevents, sample['additionalCut'] del c else: d = ROOT.TChain('Runs') for tfile in sample['filenames'][bin]: d.Add(prefix+tfile) nevents = 0 nruns = d.GetEntries() for i in range(0, nruns): d.GetEntry(i) nevents += getVarValue(d,'uint_EventCounter_runCounts_PAT.obj') del d if not bin.lower().count('run'): if nevents>0: weight = xsec[bin]*target_lumi/nevents else: weight=0 print 'Sample', sample['name'], 'bin', bin,'xsec',xsec[bin], 'n-events',nevents,'weight',weight else: weight = 1. print 'Sample', sample['name'], 'bin', bin, 'n-events',nevents,'weight',weight sample["weight"][bin]=weight if not os.path.isdir(outputDir): os.system('mkdir -p '+outputDir)