def loopOneFile(iSample, varName, hist, fs, varBins = 0): file = r.TFile(iSample) tree = file.Get('Ntuple') weight = file.Get('eventCountWeighted').GetBinContent(1) print weight nEntries = tree.GetEntries() hist.Sumw2() for i in range(nEntries): tree.GetEntry(i) if passCut(tree, fs): value = -1 if varName != 'nTruePU': wegiht = weight*cutSampleTools.getPUWeight(tree.nTruePU) if varName == 'm_eff': met.SetCoordinates(tree.pfMetEt, 0.0, tree.pfMetPhi, 0) l1.SetCoordinates(tree.pt_1, tree.eta_1, tree.phi_1, tree.m_1) l2.SetCoordinates(tree.pt_2, tree.eta_2, tree.phi_2, tree.m_2) value = (l1 + l2 + met).mass() if value > varBins[len(varBins)-1]: value = (varBins[len(varBins)-1]+varBins[len(varBins)-2]+0.0)/2.0 else: value = getattr(tree, varName) hist.Fill(value, 1.0/weight)
def loopOneFile(iSample, varName, hist, fs, varBins = 0): file = r.TFile(iSample) tree = file.Get('Ntuple') sumWeights = file.Get('eventCountWeighted').GetBinContent(1) print sumWeights nEntries = tree.GetEntries() hist.Sumw2() counter = 0 for i in range(nEntries): tree.GetEntry(i) wegiht = 1.0 if passCut(tree, fs): value = -1 weight = cutSampleTools.getPUWeight(tree.nTruePU)*Lumi*tree.xs*tree.genEventWeight*tree.trigweight_1*tree.trigweight_2/(sumWeights+0.0) value = getattr(tree, varName) if value > 1200: counter += 1 if value < varBins[0]: value = (varBins[0]+varBins[1])/2.0 elif value > varBins[len(varBins)-1]: value = (varBins[len(varBins)-1]+varBins[len(varBins)-2]+0.0)/2.0 hist.Fill(value, weight) print 'nevents with m_eff > 1200: ', counter
def loopOneFile(iSample, varName, hist, fs, varBins=0): file = r.TFile(iSample) tree = file.Get('Ntuple') weight = file.Get('eventCountWeighted').GetBinContent(1) print weight nEntries = tree.GetEntries() hist.Sumw2() for i in range(nEntries): tree.GetEntry(i) if passCut(tree, fs): value = -1 if varName != 'nTruePU': wegiht = weight * cutSampleTools.getPUWeight(tree.nTruePU) if varName == 'm_eff': met.SetCoordinates(tree.pfMetEt, 0.0, tree.pfMetPhi, 0) l1.SetCoordinates(tree.pt_1, tree.eta_1, tree.phi_1, tree.m_1) l2.SetCoordinates(tree.pt_2, tree.eta_2, tree.phi_2, tree.m_2) value = (l1 + l2 + met).mass() if value > varBins[len(varBins) - 1]: value = (varBins[len(varBins) - 1] + varBins[len(varBins) - 2] + 0.0) / 2.0 else: value = getattr(tree, varName) hist.Fill(value, 1.0 / weight)
def loop_one_sample(iSample, iLocation, iCat, oTree, oTree_tmp, floatVarsDict, intVarsDict, charVarsDict, iFS, yieldDict, histDict): print 'combininig sample [%s] for datacard' %(iSample) if 'data' in iSample: isData = True else: isData = False if 'emb' in iSample: isEmbedded = True else: isEmbedded = False if ("Z'" in iSample): isSignal = True else: isSignal = False iFile = r.TFile(iLocation) iTree = iFile.Get("Ntuple") nEntries = iTree.GetEntries() iTree.SetBranchStatus("*",1) iTree.SetBranchStatus("sampleName",0) eventCount = iFile.Get('eventCount') eventCountWeighted = iFile.Get('eventCountWeighted') sumPtWeights = -1.0 if "TTJets" in iSample: sumPtWeights = iFile.Get('eventCountPtWeighted').GetBinContent(1) yieldEstimator_OS = 0.0 yieldEstimator_SS = 0.0 fillcounter=0 met = lvClass() for iEntry in range(nEntries): iTree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'looping over file %s' %(iSample), iEntry-1) uncWeight = 1.0 region = 'none' met.SetCoordinates(iTree.met, 0.0, iTree.metphi, 0) l1.SetCoordinates(iTree.pt_1, iTree.eta_1, iTree.phi_1, iTree.m_1) l2.SetCoordinates(iTree.pt_2, iTree.eta_2, iTree.phi_2, iTree.m_2) if not plots_dataDrivenQCDandWJ.passCut(iTree, iFS, isData, options.sys): continue if plots_dataDrivenQCDandWJ.regionSelection(iTree, iFS, "signal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isData: charVarsDict['sampleName'][:31] = 'data' + signalRegionName else: charVarsDict['sampleName'][:31] = iSample if isSignal: charVarsDict['sampleName'][:31] = "Zprime_%s" %iSample[iSample.find("(")+1: iSample.find(")")] if iTree.q_1 != iTree.q_2: region = 'A' else: if isSignal: continue region = 'B' elif plots_dataDrivenQCDandWJ.regionSelection(iTree, iFS, "control_iso", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isSignal: continue if iTree.q_1 == -iTree.q_2: #C region if iFS != "em": if "WJets" in iSample: continue region = 'C' else: #D region charVarsDict['sampleName'][:31] = 'QCD_in_D' region = 'D' elif iFS != 'em' and plots_dataDrivenQCDandWJ.regionSelection(iTree, iFS, "control_anti_iso", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isSignal: continue if not isData: continue if iTree.q_1 == -iTree.q_2: #E region charVarsDict['sampleName'][:31] = 'QCDLoose' region = 'E' else: charVarsDict['sampleName'][:31] = 'QCD_in_F' region = 'F' else: continue floatVarsDict['pt_1'][0] = iTree.pt_1 floatVarsDict['pt_2'][0] = iTree.pt_2 floatVarsDict['eta_1'][0] = iTree.eta_1 floatVarsDict['eta_2'][0] = iTree.eta_2 floatVarsDict['mt_1'][0] = iTree.mt_1 floatVarsDict['genMass'][0] = 0.0 if hasattr(iTree, 'X_to_ll'): floatVarsDict['genMass'][0] = iTree.X_to_ll if hasattr(iTree, 'BDT_both'): floatVarsDict['BDT'][0] = iTree.BDT_both else: floatVarsDict['BDT'][0] = -999 floatVarsDict['triggerEff'][0] = iTree.trigweight_1*iTree.trigweight_2 charVarsDict['Category'][:31] = iFS floatVarsDict['xs'][0] = iTree.xs if 'WJets' in iSample: floatVarsDict['xs'][0] = floatVarsDict['xs'][0]*plots_cfg.WJetsScanRange[0] if "Zprime" in iSample: floatVarsDict['xs'][0] = plots.getZPrimeXS(iSample[7:]) if eventCount: intVarsDict['initEvents'][0] = int(eventCount.GetBinContent(1)) else: intVarsDict['initEvents'][0] = int(iTree.initEvents) if eventCountWeighted: intVarsDict['initSumWeights'][0] = int(eventCountWeighted.GetBinContent(1)) else: intVarsDict['initSumWeights'][0] = int(iTree.initWeightedEvents) # floatVarsDict['m_svfit'][0] = iTree.pfmet_svmc_mass floatVarsDict['m_effective'][0] = (l1 + l2 + met).mass() floatVarsDict['m_vis'][0] = (l1 + l2).mass() # floatVarsDict['m_tt'][0] = iTree.m_tt if (options.sys == 'up' or options.sys == 'down') and (not isData): floatVarsDict['xs'][0] = floatVarsDict['xs'][0]*getPDFWeight(iFS, iSample, iCat, options.sys, floatVarsDict['m_effective'][0]) intVarsDict['nCSVL'][0] = int(iTree.nCSVL) if iFS == 'et' or iFS == "mt": intVarsDict['tauDecayMode'][0] = int(iTree.tDecayMode) floatVarsDict['tauTightIso'][0] = iTree.tByTightCombinedIsolationDeltaBetaCorr3Hits floatVarsDict['tauMediumIso'][0] = iTree.tByMediumCombinedIsolationDeltaBetaCorr3Hits floatVarsDict['tauLooseIso'][0] = iTree.tByLooseCombinedIsolationDeltaBetaCorr3Hits if options.sys == 'tauUncUp': uncWeight += 0.05*iTree.pt_2/1000. if options.sys == 'tauUncDown': uncWeight -= 0.35*iTree.pt_2/1000. floatVarsDict['cosDPhi'][0] = math.cos(iTree.phi_1 - iTree.phi_2) floatVarsDict['pZetaCut'][0] = getattr(iTree, "%s_%s_PZeta" %(iFS[0], iFS[1])) - 3.1*getattr(iTree, "%s_%s_PZetaVis" %(iFS[0], iFS[1])) floatVarsDict['pfMEt'][0] = iTree.met # floatVarsDict['eleRelIso'][0] = iTree.eRelIso if options.PUWeight and not isData: floatVarsDict['PUWeight'][0] = cutSampleTools.getPUWeight(iTree.nTruePU) floatVarsDict['npv'][0] = iTree.nTruePU else: floatVarsDict['PUWeight'][0] = 1.0 floatVarsDict['npv'][0] = 0 if isData: floatVarsDict['genEventWeight'][0] = 1.0 intVarsDict['initSumWeights'][0] = 1 floatVarsDict['xs'][0] = 1.0/plots.lumi floatVarsDict['triggerEff'][0] = 1.0 else: floatVarsDict['genEventWeight'][0] = uncWeight*iTree.genEventWeight if options.topPt and sumPtWeights != -1.0: intVarsDict['initSumWeights'][0] = int(sumPtWeights) floatVarsDict['genEventWeight'][0] = iTree.topPtWeight*floatVarsDict['genEventWeight'][0] if (iFS == 'et' or iFS == 'mt') and not isData: floatVarsDict['genEventWeight'][0] = floatVarsDict['genEventWeight'][0]*0.95 #tauID if options.againstLeptonSF: if iFS == 'et': if (iTree.tIsPromptElectron): floatVarsDict['genEventWeight'][0] = floatVarsDict['genEventWeight'][0]*plots_dataDrivenQCDandWJ.getAgainstLeptonSF('electron', 'Tight', abs(iTree.eta_2)) if (iTree.tIsPromptMuon): floatVarsDict['genEventWeight'][0] = floatVarsDict['genEventWeight'][0]*plots_dataDrivenQCDandWJ.getAgainstLeptonSF('muon', 'Loose', abs(iTree.eta_2)) if iFS == 'mt': if (iTree.tIsPromptElectron): floatVarsDict['genEventWeight'][0] = floatVarsDict['genEventWeight'][0]*plots_dataDrivenQCDandWJ.getAgainstLeptonSF('electron', 'VLoose', abs(iTree.eta_2)) if (iTree.tIsPromptMuon): floatVarsDict['genEventWeight'][0] = floatVarsDict['genEventWeight'][0]*plots_dataDrivenQCDandWJ.getAgainstLeptonSF('muon', 'Tight', abs(iTree.eta_2)) if region == 'none': print "ERROR!!!!!" if region == "A": if charVarsDict['sampleName'][:31] in yieldDict.keys(): yieldDict[str(charVarsDict['sampleName'][:31])] += floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) else: yieldDict[str(charVarsDict['sampleName'][:31])] = floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) if "WJets" in iSample: histDict["WJetsOSTight_MC"].Fill(1, floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0)) yieldDict["WJetsOSTight_MC"] += floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) oTree.Fill() elif region == "B": weight = 1 if not isData: weight = -floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) yieldDict["QCD_in_B"] += weight histDict["QCD_in_B"].Fill(1, weight) elif region == "C": if iFS == "em": charVarsDict['sampleName'][:31] = 'QCDLoose' if isData: yieldDict["QCDLoose"] += 1 else: yieldDict["QCDLoose"] -= floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) floatVarsDict['genEventWeight'][0] = -1.0*floatVarsDict['genEventWeight'][0] oTree.Fill() if "WJets" in iSample: floatVarsDict['genEventWeight'][0] = -1.0*floatVarsDict['genEventWeight'][0] yieldDict['WJets' + controlRegionName] += floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) histDict["WJetsOSLoose_MC"].Fill(1, floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0)) charVarsDict['sampleName'][:31] = 'WJets' + controlRegionName oTree.Fill() else: charVarsDict['sampleName'][:31] = 'WJetsLoose' if isData: yieldDict['WJetsLoose'] += 1 else: floatVarsDict['genEventWeight'][0] = -1.0*floatVarsDict['genEventWeight'][0] yieldDict['WJetsLoose'] += floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) oTree.Fill() elif region == "D": weight = 1 if not isData: weight = -floatVarsDict['triggerEff'][0]*floatVarsDict['PUWeight'][0]*floatVarsDict['genEventWeight'][0]*floatVarsDict['xs'][0]*plots.lumi/(intVarsDict['initSumWeights'][0]+ 0.0) yieldDict["QCD_in_D"] += weight histDict["QCD_in_D"].Fill(1, weight) elif region == "E" and iFS != 'em' and isData: yieldDict["QCDLoose"] += 1 oTree_tmp.Fill() oTree.Fill() elif region == "F" and iFS != 'em' and isData: yieldDict["QCD_in_F"] += 1 histDict["QCD_in_F"].Fill(1, 1) return yieldDict, histDict
def loop_one_sample(iSample, iCategory, histDict, varName, varBins, FS, scanPoints): if options.antiIso: iSample += "%s_antiIso.root" %FS elif options.antiEIso: iSample += "%s_antiEIso.root" %FS elif options.antiMIso: iSample += "%s_antiMIso.root" %FS elif options.noIso: iSample += "%s_noIso.root" %FS elif options.BSM3G: iSample += "%s_noIso.root" %FS else: iSample += "%s_inclusive.root" %FS file = r.TFile(iSample) tree = file.Get('Ntuple') eventCount = file.Get('eventCount') eventCountWeighted = file.Get('eventCountWeighted') sumPtWeights = -1 if "TT" in iSample and options.sys == 'topPt': sumPtWeights = file.Get('eventCountPtWeighted').GetBinContent(1) nEntries = tree.GetEntries() # if nEntries > 100: # nEntries = 100 tmpHist = r.TH1F("tmp_%s_%s" %(iCategory, varName), '', len(varBins)-1, varBins) tmpHist.Sumw2() tmpHist_forROOTFile = r.TH1F("%s" %(varName), '', len(varBins)-1, varBins) tmpHist_forROOTFile.Sumw2() isData = False isSignal = False if eventCount: initEvents = eventCount.GetBinContent(1) else: initEvents = tree.initEvents if eventCountWeighted: sumWeights = eventCountWeighted.GetBinContent(1) else: sumWeights = tree.initWeightedEvents if iCategory == 'Observed': isData = True for iEntry in range(nEntries): tree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'Looping sample %s' %(iSample), iEntry-1) weight = 1.0 QCD_weight = 1.0 if not passCut(tree, FS, isData, options.sys): continue if not isData: xs = tree.xs if (80.94 < xs < 80.96) or (136.01 < xs < 136.03): xs = xs*0.108*3 if options.sys == 'topPt' and sumPtWeights != -1.0: sumWeights = sumPtWeights weight = Lumi*xs*tree.genEventWeight*tree.trigweight_1*tree.trigweight_2*tree.topPtWeight/(sumWeights+0.0) else: weight = Lumi*xs*tree.genEventWeight*tree.trigweight_1*tree.trigweight_2/(sumWeights+0.0) if options.PUWeight: weight = weight*cutSampleTools.getPUWeight(tree.nTruePU) if 'WJets' in iSample: weight = 1.0*weight if 'ZPrime' in iSample: weight = getZPrimeXS(iSample[iSample.rfind("/")+8:iSample.rfind("_all")])*weight isSignal = True if varName == 'm_withMET': value = tree.m_eff elif varName == 'mVis': value = tree.m_vis elif varName == 'pZeta - 3.1pZetaVis': value = tree.ZetaCut elif varName == "nCSVL": value = tree.nCSVL elif varName == "cos_phi_tau1_tau2": value = math.cos(tree.phi_1 - tree.phi_2) elif varName == "j1Pt": jets = getJets(tree) if len(jets)>0: value = jets[0].pt() else: value = -1 elif varName == 'm_gen': if tree.eGenTauMass < 0 or tree.tGenMass < 0: continue l1.SetCoordinates(tree.eGenTauPt, tree.eGenTauEta, tree.eGenTauPhi, tree.eGenTauMass) l2.SetCoordinates(tree.tGenPt, tree.tGenEta, tree.tGenPhi, tree.tGenMass) value = (l1 + l2).mass() else: if hasattr(tree, varName): value = getattr(tree, varName) else: value = -1 if options.overFlow: if value > varBins[len(varBins)-1]: value = (varBins[len(varBins)-1]+varBins[len(varBins)-2]+0.0)/2.0 if regionSelection(tree, FS, "OSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): fill = True if isData: fill = False if options.unblind or (options.unblindPartial and passUnblindPartial(varName, value)): fill = True histDict['WJets_OSsignal'].Fill(value, weight) if fill: tmpHist.Fill(value, weight) tmpHist_forROOTFile.Fill(value, weight) if iCategory != 'WJets' and (not isData) and (not isSignal): histDict['WJets_OSsignal'].Fill(value, -weight) else: if isSignal: continue #region B if regionSelection(tree, FS, "SSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_B'].Fill(value, -weight*QCD_weight) else: histDict['QCD_B'].Fill(value, weight*QCD_weight) #region C if regionSelection(tree, FS, "OScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_C'].Fill(value, -weight*QCD_weight) if iCategory != 'WJets': histDict['WJets_OScontrol'].Fill(value, -weight) else: histDict['QCD_C'].Fill(value, weight*QCD_weight) histDict['WJets_OScontrol'].Fill(value, weight) #region D if regionSelection(tree, FS, "SScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_D'].Fill(value, -weight*QCD_weight) histDict['QCD_D_for_C'].Fill(value, -weight*QCD_weight) histDict['QCD_D_for_A'].Fill(value, -weight*QCD_weight) else: histDict['QCD_D'].Fill(value, weight*QCD_weight) histDict['QCD_D_for_C'].Fill(value, weight*QCD_weight) histDict['QCD_D_for_A'].Fill(value, weight*QCD_weight) print iCategory, tmpHist.Integral(0, len(varBins)+1) histDict[iCategory].Add(tmpHist) del tmpHist print ''
def runOneFile(iFS, inputFile, signSelect, isoSelect, output_dir, split): inputFile = inputFile + "%s_noIso.root" %iFS ifile = r.TFile(inputFile) iTree = ifile.Get("Ntuple") eventCountWeighted = ifile.Get('eventCountWeighted') oString = signSelect + isoSelect outputFile = "%s_%s.root" %(output_dir+inputFile[inputFile.rfind("/"):inputFile.find(".root")], oString) oFile = r.TFile(outputFile,"recreate") iTree.SetBranchStatus("*",1) oTree = r.TTree('eventTree','') oTree_train = r.TTree('eventTree_train','') oTree_test = r.TTree('eventTree_test','') total = iTree.GetEntries() Lumi = plots.lumi isData = False isSignal = False floatVarsDict = setUpFloatVarsDict() for iVar in floatVarsDict.keys(): oTree.Branch("%s" %iVar, floatVarsDict[iVar], "%s/f" %iVar) oTree_train.Branch("%s" %iVar, floatVarsDict[iVar], "%s/f" %iVar) oTree_test.Branch("%s" %iVar, floatVarsDict[iVar], "%s/f" %iVar) isoSelection = 'control' if isoSelect == 'Tight': isoSelection = 'signal' for i in range(total): r.gStyle.SetOptStat(0) tool.printProcessStatus(iCurrent=i, total=total, processName = 'Looping sample %s' %inputFile) iTree.GetEntry(i) if 'data' in iTree.sampleName: isData = True if 'ZPrime' in iTree.sampleName: isSignal = True if signSelect == 'OS' and iTree.q_1 == iTree.q_2: continue elif signSelect == 'SS' and iTree.q_1 != iTree.q_2: continue if plots.regionSelection(iTree, iFS, isoSelection, 'Loose', plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isData: floatVarsDict['weightWithPU'][0] = 1.0 else: floatVarsDict['weightWithPU'][0] = cutSampleTools.getPUWeight(iTree.nTruePU)*iTree.xs*Lumi*iTree.genEventWeight/(int(eventCountWeighted.GetBinContent(1))) if split: floatVarsDict['weightWithPU'][0] = 2*floatVarsDict['weightWithPU'][0] met.SetCoordinates(iTree.pfMetEt, 0.0, iTree.pfMetPhi, 0) l1.SetCoordinates(iTree.pt_1, iTree.eta_1, iTree.phi_1, iTree.m_1) l2.SetCoordinates(iTree.pt_2, iTree.eta_2, iTree.phi_2, iTree.m_2) floatVarsDict['pt_1'][0] = iTree.pt_1 floatVarsDict['eta_1'][0] = iTree.eta_1 floatVarsDict['phi_1'][0] = iTree.phi_1 floatVarsDict['m_1'][0] = iTree.m_1 floatVarsDict['pt_2'][0] = iTree.pt_2 floatVarsDict['eta_2'][0] = iTree.eta_2 floatVarsDict['phi_2'][0] = iTree.phi_2 floatVarsDict['m_2'][0] = iTree.m_2 floatVarsDict['met'][0] = iTree.pfMetEt floatVarsDict['met_phi'][0] = iTree.pfMetPhi floatVarsDict['jpt_1'][0] = iTree.jpt_1 if iTree.jpt_1 > 0: floatVarsDict['jeta_1'][0] = iTree.jeta_1 floatVarsDict['jphi_1'][0] = iTree.jphi_1 else: floatVarsDict['jeta_1'][0] = -999.0 floatVarsDict['jphi_1'][0] = -999.0 floatVarsDict['jpt_2'][0] = iTree.jpt_2 if iTree.jpt_2 > 0: floatVarsDict['jeta_2'][0] = iTree.jeta_2 floatVarsDict['jphi_2'][0] = iTree.jphi_2 else: floatVarsDict['jeta_2'][0] = -999.0 floatVarsDict['jphi_2'][0] = -999.0 floatVarsDict['pZetaCut'][0] = iTree.pZetaCut floatVarsDict['m_eff'][0] = iTree.m_eff oTree.Fill() if random.random() < 0.5: oTree_train.Fill() else: oTree_test.Fill() print '' oFile.cd() if not split: oTree.Write() nSaved = oTree.GetEntries() print 'saved %i events out of %i at: %s' %(nSaved,total,outputFile) else: oTree_train.Write() nSaved = oTree_train.GetEntries() print 'for training: saved %i events out of %i at: %s' %(nSaved,total,outputFile) oTree_test.Write() nSaved = oTree_test.GetEntries() print 'for testing: saved %i events out of %i at: %s' %(nSaved,total,outputFile) oFile.Close()
def loop_one_sample(iSample, iCategory, histDict, varName, varBins, FS, scanPoints): if options.antiIso: iSample += "%s_antiIso.root" %FS elif options.antiEIso: iSample += "%s_antiEIso.root" %FS elif options.antiMIso: iSample += "%s_antiMIso.root" %FS elif options.noIso: iSample += "%s_noIso.root" %FS elif options.BSM3G: iSample += "%s_noIso.root" %FS else: iSample += "%s_inclusive.root" %FS file = r.TFile(iSample) tree = file.Get('Ntuple') eventCount = file.Get('eventCount') eventCountWeighted = file.Get('eventCountWeighted') nEntries = tree.GetEntries() # if nEntries > 100: # nEntries = 100 tmpHist = r.TH1F("tmp_%s_%s" %(iCategory, varName), '', len(varBins)-1, varBins) tmpHist.Sumw2() tmpHist_forROOTFile = r.TH1F("%s" %(varName), '', len(varBins)-1, varBins) tmpHist_forROOTFile.Sumw2() isData = False isSignal = False if iCategory == 'Observed': isData = True for iEntry in range(nEntries): tree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'Looping sample %s' %(iSample), iEntry-1) weight = 1.0 QCD_weight = 1.0 if options.sys == 'jetECUp' and not isData: met.SetCoordinates(tree.pfMet_jesUp_Et, 0.0, tree.pfMet_jesUp_Phi, 0) elif options.sys == 'jetECDown' and not isData: met.SetCoordinates(tree.pfMet_jesDown_Et, 0.0, tree.pfMet_jesDown_Phi, 0) elif options.sys == 'tauECUp' and not isData: met.SetCoordinates(tree.pfMet_tesUp_Et, 0.0, tree.pfMet_tesUp_Phi, 0) elif options.sys == 'tauECDown' and not isData: met.SetCoordinates(tree.pfMet_tesDown_Et, 0.0, tree.pfMet_tesDown_Phi, 0) else: met.SetCoordinates(tree.pfMetEt, 0.0, tree.pfMetPhi, 0) l1.SetCoordinates(tree.pt_1, tree.eta_1, tree.phi_1, tree.m_1) l2.SetCoordinates(tree.pt_2, tree.eta_2, tree.phi_2, tree.m_2) if eventCount: initEvents = eventCount.GetBinContent(1) else: initEvents = tree.initEvents if eventCountWeighted: sumWeights = eventCountWeighted.GetBinContent(1) else: sumWeights = tree.initWeightedEvents if not passCut(tree, FS, isData, l1, l2, met, options.sys): continue if not isData: xs = tree.xs if (80.94 < xs < 80.96) or (136.01 < xs < 136.03): xs = xs*0.108*3 weight = Lumi*xs*tree.genEventWeight*tree.trigweight_1*tree.trigweight_2/(sumWeights+0.0) if options.PUWeight: weight = weight*cutSampleTools.getPUWeight(tree.nTruePU) if options.diffQCD: if tree.tDecayMode < 4: QCD_weight = plots_cfg.SF_prong1[0] elif tree.tDecayMode > 8: QCD_weight = plots_cfg.SF_prong3[0] if 'WJets' in iSample: weight = 1.0*weight if 'ZPrime' in iSample: weight = getZPrimeXS(iCategory[7:])*weight isSignal = True if varName == 'm_withMET': value = (l1 + l2 + met).mass() elif varName == 'mVis': value = (l1 + l2).mass() elif varName == 'pZeta - 3.1pZetaVis': value = pZetaCut(l1, l2, met) elif varName == "nCSVL": value = getNCSVLJets(tree, options.sys, isData, True) elif varName == "cos_phi_tau1_tau2": value = math.cos(tree.phi_1 - tree.phi_2) elif varName == "j1Pt": jets = getJets(tree) if len(jets)>0: value = jets[0].pt() else: value = -1 elif varName == 'm_gen': if tree.eGenTauMass < 0 or tree.tGenMass < 0: continue l1.SetCoordinates(tree.eGenTauPt, tree.eGenTauEta, tree.eGenTauPhi, tree.eGenTauMass) l2.SetCoordinates(tree.tGenPt, tree.tGenEta, tree.tGenPhi, tree.tGenMass) value = (l1 + l2).mass() else: if hasattr(tree, varName): value = getattr(tree, varName) else: value = -1 if options.overFlow: if value > varBins[len(varBins)-1]: value = (varBins[len(varBins)-1]+varBins[len(varBins)-2]+0.0)/2.0 if regionSelection(tree, FS, "OSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): fill = True if isData: fill = False if options.unblind or (options.unblindPartial and passUnblindPartial(varName, value)): fill = True histDict['WJets_OSsignal'].Fill(value, weight) if fill: tmpHist.Fill(value, weight) tmpHist_forROOTFile.Fill(value, weight) if iCategory != 'WJets' and (not isData) and (not isSignal): histDict['WJets_OSsignal'].Fill(value, -weight) else: if isSignal: continue #region B if regionSelection(tree, FS, "SSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_B'].Fill(value, -weight*QCD_weight) else: histDict['QCD_B'].Fill(value, weight*QCD_weight) #region C if regionSelection(tree, FS, "OScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_C'].Fill(value, -weight*QCD_weight) histDict['QCD_C_for_A'].Fill(value, -weight*QCD_weight) if iCategory != 'WJets': histDict['WJets_OScontrol'].Fill(value, -weight) else: histDict['QCD_C'].Fill(value, weight*QCD_weight) histDict['QCD_C_for_A'].Fill(value, weight*QCD_weight) histDict['WJets_OScontrol'].Fill(value, weight) #region D if regionSelection(tree, FS, "SScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_D'].Fill(value, -weight*QCD_weight) histDict['QCD_D_for_C'].Fill(value, -weight*QCD_weight) histDict['QCD_D_for_A'].Fill(value, -weight*QCD_weight) else: histDict['QCD_D'].Fill(value, weight*QCD_weight) histDict['QCD_D_for_C'].Fill(value, weight*QCD_weight) histDict['QCD_D_for_A'].Fill(value, weight*QCD_weight) #highMT signal region if regionSelection(tree, FS, "OSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1], True): if not isData: if iCategory != 'WJets': histDict['WJets_A_'].Fill(value, -weight) else: histDict['WJets_MC_A_'].Fill(value, weight) else: histDict['WJets_A_'].Fill(value, weight) #highMT C region if regionSelection(tree, FS, "OScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1], True): if not isData: histDict['QCD_C_for_A_'].Fill(value, -weight*QCD_weight) else: histDict['QCD_C_for_A_'].Fill(value, weight*QCD_weight) print iCategory, tmpHist.Integral(0, len(varBins)+1) histDict[iCategory].Add(tmpHist) del tmpHist if options.saveHisto: oFile = r.TFile('/user_data/zmao/ZPrimeHistos/%s/%s_%s.root' %(FS, iSample[iSample.rfind('/'): iSample.rfind('_SYNC')], varName),"recreate") oFile.cd() tmpHist_forROOTFile = fixNegativBins(tmpHist_forROOTFile) tmpHist_forROOTFile.Write() oFile.Close() del tmpHist_forROOTFile print ''
def loop_one_sample(iSample, iLocation, iCat, oTree, oTree_tmp, floatVarsDict, intVarsDict, charVarsDict, iFS, yieldDict, histDict): print 'combininig sample [%s] for datacard' % (iSample) if 'data' in iSample: isData = True else: isData = False if 'emb' in iSample: isEmbedded = True else: isEmbedded = False if ("Z'" in iSample): isSignal = True else: isSignal = False iFile = r.TFile(iLocation) iTree = iFile.Get("Ntuple") nEntries = iTree.GetEntries() iTree.SetBranchStatus("*", 1) iTree.SetBranchStatus("sampleName", 0) eventCount = iFile.Get('eventCount') eventCountWeighted = iFile.Get('eventCountWeighted') sumPtWeights = -1.0 if "TTJets" in iSample: sumPtWeights = iFile.Get('eventCountPtWeighted').GetBinContent(1) yieldEstimator_OS = 0.0 yieldEstimator_SS = 0.0 fillcounter = 0 met = lvClass() for iEntry in range(nEntries): iTree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'looping over file %s' % (iSample), iEntry - 1) uncWeight = 1.0 region = 'none' met.SetCoordinates(iTree.met, 0.0, iTree.metphi, 0) l1.SetCoordinates(iTree.pt_1, iTree.eta_1, iTree.phi_1, iTree.m_1) l2.SetCoordinates(iTree.pt_2, iTree.eta_2, iTree.phi_2, iTree.m_2) if not plots_dataDrivenQCDandWJ.passCut(iTree, iFS, isData, options.sys): continue if plots_dataDrivenQCDandWJ.regionSelection(iTree, iFS, "signal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isData: charVarsDict['sampleName'][:31] = 'data' + signalRegionName else: charVarsDict['sampleName'][:31] = iSample if isSignal: charVarsDict['sampleName'][:31] = "Zprime_%s" % iSample[ iSample.find("(") + 1:iSample.find(")")] if iTree.q_1 != iTree.q_2: region = 'A' else: if isSignal: continue region = 'B' elif plots_dataDrivenQCDandWJ.regionSelection(iTree, iFS, "control_iso", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isSignal: continue if iTree.q_1 == -iTree.q_2: #C region if iFS != "em": if "WJets" in iSample: continue region = 'C' else: #D region charVarsDict['sampleName'][:31] = 'QCD_in_D' region = 'D' elif iFS != 'em' and plots_dataDrivenQCDandWJ.regionSelection( iTree, iFS, "control_anti_iso", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if isSignal: continue if not isData: continue if iTree.q_1 == -iTree.q_2: #E region charVarsDict['sampleName'][:31] = 'QCDLoose' region = 'E' else: charVarsDict['sampleName'][:31] = 'QCD_in_F' region = 'F' else: continue floatVarsDict['pt_1'][0] = iTree.pt_1 floatVarsDict['pt_2'][0] = iTree.pt_2 floatVarsDict['eta_1'][0] = iTree.eta_1 floatVarsDict['eta_2'][0] = iTree.eta_2 floatVarsDict['mt_1'][0] = iTree.mt_1 floatVarsDict['genMass'][0] = 0.0 if hasattr(iTree, 'X_to_ll'): floatVarsDict['genMass'][0] = iTree.X_to_ll if hasattr(iTree, 'BDT_both'): floatVarsDict['BDT'][0] = iTree.BDT_both else: floatVarsDict['BDT'][0] = -999 floatVarsDict['triggerEff'][ 0] = iTree.trigweight_1 * iTree.trigweight_2 charVarsDict['Category'][:31] = iFS floatVarsDict['xs'][0] = iTree.xs if 'WJets' in iSample: floatVarsDict['xs'][ 0] = floatVarsDict['xs'][0] * plots_cfg.WJetsScanRange[0] if "Zprime" in iSample: floatVarsDict['xs'][0] = plots.getZPrimeXS(iSample[7:]) if eventCount: intVarsDict['initEvents'][0] = int(eventCount.GetBinContent(1)) else: intVarsDict['initEvents'][0] = int(iTree.initEvents) if eventCountWeighted: intVarsDict['initSumWeights'][0] = int( eventCountWeighted.GetBinContent(1)) else: intVarsDict['initSumWeights'][0] = int(iTree.initWeightedEvents) # floatVarsDict['m_svfit'][0] = iTree.pfmet_svmc_mass floatVarsDict['m_effective'][0] = (l1 + l2 + met).mass() floatVarsDict['m_vis'][0] = (l1 + l2).mass() # floatVarsDict['m_tt'][0] = iTree.m_tt if (options.sys == 'up' or options.sys == 'down') and (not isData): floatVarsDict['xs'][0] = floatVarsDict['xs'][0] * getPDFWeight( iFS, iSample, iCat, options.sys, floatVarsDict['m_effective'][0]) intVarsDict['nCSVL'][0] = int(iTree.nCSVL) if iFS == 'et' or iFS == "mt": intVarsDict['tauDecayMode'][0] = int(iTree.tDecayMode) floatVarsDict['tauTightIso'][ 0] = iTree.tByTightCombinedIsolationDeltaBetaCorr3Hits floatVarsDict['tauMediumIso'][ 0] = iTree.tByMediumCombinedIsolationDeltaBetaCorr3Hits floatVarsDict['tauLooseIso'][ 0] = iTree.tByLooseCombinedIsolationDeltaBetaCorr3Hits if options.sys == 'tauUncUp': uncWeight += 0.05 * iTree.pt_2 / 1000. if options.sys == 'tauUncDown': uncWeight -= 0.35 * iTree.pt_2 / 1000. floatVarsDict['cosDPhi'][0] = math.cos(iTree.phi_1 - iTree.phi_2) floatVarsDict['pZetaCut'][0] = getattr( iTree, "%s_%s_PZeta" % (iFS[0], iFS[1])) - 3.1 * getattr( iTree, "%s_%s_PZetaVis" % (iFS[0], iFS[1])) floatVarsDict['pfMEt'][0] = iTree.met # floatVarsDict['eleRelIso'][0] = iTree.eRelIso if options.PUWeight and not isData: floatVarsDict['PUWeight'][0] = cutSampleTools.getPUWeight( iTree.nTruePU) floatVarsDict['npv'][0] = iTree.nTruePU else: floatVarsDict['PUWeight'][0] = 1.0 floatVarsDict['npv'][0] = 0 if isData: floatVarsDict['genEventWeight'][0] = 1.0 intVarsDict['initSumWeights'][0] = 1 floatVarsDict['xs'][0] = 1.0 / plots.lumi floatVarsDict['triggerEff'][0] = 1.0 else: floatVarsDict['genEventWeight'][ 0] = uncWeight * iTree.genEventWeight if options.topPt and sumPtWeights != -1.0: intVarsDict['initSumWeights'][0] = int(sumPtWeights) floatVarsDict['genEventWeight'][ 0] = iTree.topPtWeight * floatVarsDict['genEventWeight'][0] if (iFS == 'et' or iFS == 'mt') and not isData: floatVarsDict['genEventWeight'][ 0] = floatVarsDict['genEventWeight'][0] * 0.95 #tauID if options.againstLeptonSF: if iFS == 'et': if (iTree.tIsPromptElectron): floatVarsDict['genEventWeight'][ 0] = floatVarsDict['genEventWeight'][ 0] * plots_dataDrivenQCDandWJ.getAgainstLeptonSF( 'electron', 'Tight', abs(iTree.eta_2)) if (iTree.tIsPromptMuon): floatVarsDict['genEventWeight'][ 0] = floatVarsDict['genEventWeight'][ 0] * plots_dataDrivenQCDandWJ.getAgainstLeptonSF( 'muon', 'Loose', abs(iTree.eta_2)) if iFS == 'mt': if (iTree.tIsPromptElectron): floatVarsDict['genEventWeight'][ 0] = floatVarsDict['genEventWeight'][ 0] * plots_dataDrivenQCDandWJ.getAgainstLeptonSF( 'electron', 'VLoose', abs(iTree.eta_2)) if (iTree.tIsPromptMuon): floatVarsDict['genEventWeight'][ 0] = floatVarsDict['genEventWeight'][ 0] * plots_dataDrivenQCDandWJ.getAgainstLeptonSF( 'muon', 'Tight', abs(iTree.eta_2)) if region == 'none': print "ERROR!!!!!" if region == "A": if charVarsDict['sampleName'][:31] in yieldDict.keys(): yieldDict[str( charVarsDict['sampleName'] [:31])] += floatVarsDict['triggerEff'][0] * floatVarsDict[ 'PUWeight'][0] * floatVarsDict['genEventWeight'][ 0] * floatVarsDict['xs'][0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) else: yieldDict[str( charVarsDict['sampleName'] [:31])] = floatVarsDict['triggerEff'][0] * floatVarsDict[ 'PUWeight'][0] * floatVarsDict['genEventWeight'][ 0] * floatVarsDict['xs'][0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) if "WJets" in iSample: histDict["WJetsOSTight_MC"].Fill( 1, floatVarsDict['triggerEff'][0] * floatVarsDict['PUWeight'][0] * floatVarsDict['genEventWeight'][0] * floatVarsDict['xs'][0] * plots.lumi / (intVarsDict['initSumWeights'][0] + 0.0)) yieldDict["WJetsOSTight_MC"] += floatVarsDict['triggerEff'][ 0] * floatVarsDict['PUWeight'][0] * floatVarsDict[ 'genEventWeight'][0] * floatVarsDict['xs'][ 0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) oTree.Fill() elif region == "B": weight = 1 if not isData: weight = -floatVarsDict['triggerEff'][0] * floatVarsDict[ 'PUWeight'][0] * floatVarsDict['genEventWeight'][ 0] * floatVarsDict['xs'][0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) yieldDict["QCD_in_B"] += weight histDict["QCD_in_B"].Fill(1, weight) elif region == "C": if iFS == "em": charVarsDict['sampleName'][:31] = 'QCDLoose' if isData: yieldDict["QCDLoose"] += 1 else: yieldDict["QCDLoose"] -= floatVarsDict['triggerEff'][ 0] * floatVarsDict['PUWeight'][0] * floatVarsDict[ 'genEventWeight'][0] * floatVarsDict['xs'][ 0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) floatVarsDict['genEventWeight'][ 0] = -1.0 * floatVarsDict['genEventWeight'][0] oTree.Fill() if "WJets" in iSample: floatVarsDict['genEventWeight'][ 0] = -1.0 * floatVarsDict['genEventWeight'][0] yieldDict['WJets' + controlRegionName] += floatVarsDict[ 'triggerEff'][0] * floatVarsDict['PUWeight'][ 0] * floatVarsDict['genEventWeight'][ 0] * floatVarsDict['xs'][0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) histDict["WJetsOSLoose_MC"].Fill( 1, floatVarsDict['triggerEff'][0] * floatVarsDict['PUWeight'][0] * floatVarsDict['genEventWeight'][0] * floatVarsDict['xs'][0] * plots.lumi / (intVarsDict['initSumWeights'][0] + 0.0)) charVarsDict[ 'sampleName'][:31] = 'WJets' + controlRegionName oTree.Fill() else: charVarsDict['sampleName'][:31] = 'WJetsLoose' if isData: yieldDict['WJetsLoose'] += 1 else: floatVarsDict['genEventWeight'][ 0] = -1.0 * floatVarsDict['genEventWeight'][0] yieldDict['WJetsLoose'] += floatVarsDict['triggerEff'][ 0] * floatVarsDict['PUWeight'][0] * floatVarsDict[ 'genEventWeight'][0] * floatVarsDict['xs'][ 0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) oTree.Fill() elif region == "D": weight = 1 if not isData: weight = -floatVarsDict['triggerEff'][0] * floatVarsDict[ 'PUWeight'][0] * floatVarsDict['genEventWeight'][ 0] * floatVarsDict['xs'][0] * plots.lumi / ( intVarsDict['initSumWeights'][0] + 0.0) yieldDict["QCD_in_D"] += weight histDict["QCD_in_D"].Fill(1, weight) elif region == "E" and iFS != 'em' and isData: yieldDict["QCDLoose"] += 1 oTree_tmp.Fill() oTree.Fill() elif region == "F" and iFS != 'em' and isData: yieldDict["QCD_in_F"] += 1 histDict["QCD_in_F"].Fill(1, 1) return yieldDict, histDict
def loop_one_sample(iSample, varName, hist1, hist2, category = 'mt', isData = False, region = 'barrel', triggerLeg = 'e'): file = r.TFile(iSample) tree = file.Get('Ntuple') nEntries = tree.GetEntries() hist1.Sumw2() hist2.Sumw2() eventCountWeighted = file.Get('eventCountWeighted') # if not isData and nEntries > 50000: # nEntries = 50000 for iEntry in range(nEntries): tree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'Looping sample %s' %(iSample), iEntry-1) weight = 1.0 if not isData: puweight = cutSampleTools.getPUWeight(tree.nTruePU) sumWeights = eventCountWeighted.GetBinContent(1) weight = 10000*tree.xs*tree.genEventWeight*puweight/(sumWeights+0.0) if category == 'mt': if tree.mPt <= 25: continue if not (tree.singleMu24Pass and tree.mIsoMu24): continue elif category == 'et': if tree.ePt <= 33: continue if isData: if not (tree.singleETightPass and tree.eSingleEleTight): continue else: if not (tree.singleEPass and tree.eSingleEle): continue elif category == 'ee': if isData: if 'e2' in varName: if tree.singleETightPass and tree.e1SingleEleTight: if tree.e1Pt <= 34: continue else: continue if 'e1' in varName: if tree.singleETightPass and tree.e2SingleEleTight: if tree.e2Pt <= 34: continue else: continue else: if 'e2' in varName: if tree.singleEPass and tree.e1SingleEle: if tree.e1Pt <= 34: continue else: continue if 'e1' in varName: if tree.singleEPass and tree.e2SingleEle: if tree.e2Pt <= 34: continue else: continue elif category == 'em': if abs(tree.eEta) >= 2.1: continue if abs(tree.mEta) >= 2.1: continue if triggerLeg == 'e': if tree.mPt <= 24: continue if region == 'barrel' and abs(tree.eEta) > 1.479: continue elif region == 'endcap' and abs(tree.eEta) <= 1.479: continue else: if region == 'barrel' and abs(tree.mEta) > 0.8: continue elif region == 'middle' and not (0.8 < abs(tree.mEta) < 1.24): continue elif region == 'endcap' and not (1.24 < abs(tree.mEta) < 2.4): continue if tree.ePt <= 28: continue if tree.q_1 == tree.q_2: continue value = getattr(tree, varName) hist1.Fill(value) if category == 'mt': if tree.muTauPass and tree.mMuTau and tree.mMuTauOverlap and tree.tTau20AgainstMuon and tree.tTauOverlapMu: hist2.Fill(value) elif category == 'et': if isData: if tree.eTau_WPLoosePass and tree.eEle22Loose and tree.eOverlapEle22Loose and tree.tTau20 and tree.tTauOverlapEleLoose: hist2.Fill(value) else: if tree.eTauPass and tree.eEle22 and tree.eOverlapEle22 and tree.tTau20 and tree.tTauOverlapEle: hist2.Fill(value) elif category == 'ee': if isData: if tree.doubleE_WPLoosePass: if (tree.e1DoubleE_WPLooseLeg1 and tree.e2DoubleE_WPLooseLeg2) or (tree.e1DoubleE_WPLooseLeg2 and tree.e2DoubleE_WPLooseLeg1): if tree.e2Pt > 25 and tree.e1Pt > 25: hist2.Fill(value) else: if tree.doubleE_WP75Pass: if (tree.e1DoubleE_WP75Leg1 and tree.e2DoubleE_WP75Leg2) or (tree.e1DoubleE_WP75Leg2 and tree.e2DoubleE_WP75Leg1): if tree.e2Pt > 25 and tree.e1Pt > 25: hist2.Fill(value) elif category == 'em': if triggerLeg == 'e': if tree.singleE27_2p1_WPTightPass and tree.eSingleEle27_2p1_WPTight: hist2.Fill(value) elif triggerLeg == 'mu': if tree.singleMu24Pass and tree.mIsoMu24: hist2.Fill(value) print ''
def loop_one_sample(iSample, iCategory, histDict, varName, varBins, FS, scanPoints): if options.antiIso: iSample += "%s_antiIso.root" % FS elif options.antiEIso: iSample += "%s_antiEIso.root" % FS elif options.antiMIso: iSample += "%s_antiMIso.root" % FS elif options.noIso: iSample += "%s_noIso.root" % FS elif options.BSM3G: iSample += "%s_noIso.root" % FS else: iSample += "%s_inclusive.root" % FS file = r.TFile(iSample) tree = file.Get('Ntuple') eventCount = file.Get('eventCount') eventCountWeighted = file.Get('eventCountWeighted') sumPtWeights = -1 if "TT" in iSample and options.sys == 'topPt': sumPtWeights = file.Get('eventCountPtWeighted').GetBinContent(1) nEntries = tree.GetEntries() # if nEntries > 100: # nEntries = 100 tmpHist = r.TH1F("tmp_%s_%s" % (iCategory, varName), '', len(varBins) - 1, varBins) tmpHist.Sumw2() tmpHist_forROOTFile = r.TH1F("%s" % (varName), '', len(varBins) - 1, varBins) tmpHist_forROOTFile.Sumw2() isData = False isSignal = False if eventCount: initEvents = eventCount.GetBinContent(1) else: initEvents = tree.initEvents if eventCountWeighted: sumWeights = eventCountWeighted.GetBinContent(1) else: sumWeights = tree.initWeightedEvents if iCategory == 'Observed': isData = True for iEntry in range(nEntries): tree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'Looping sample %s' % (iSample), iEntry - 1) weight = 1.0 QCD_weight = 1.0 if not passCut(tree, FS, isData, options.sys): continue if not isData: xs = tree.xs if (80.94 < xs < 80.96) or (136.01 < xs < 136.03): xs = xs * 0.108 * 3 if options.sys == 'topPt' and sumPtWeights != -1.0: sumWeights = sumPtWeights weight = Lumi * xs * tree.genEventWeight * tree.trigweight_1 * tree.trigweight_2 * tree.topPtWeight / ( sumWeights + 0.0) else: weight = Lumi * xs * tree.genEventWeight * tree.trigweight_1 * tree.trigweight_2 / ( sumWeights + 0.0) if options.PUWeight: weight = weight * cutSampleTools.getPUWeight(tree.nTruePU) if 'WJets' in iSample: weight = 1.0 * weight if 'ZPrime' in iSample: weight = getZPrimeXS( iSample[iSample.rfind("/") + 8:iSample.rfind("_all")]) * weight isSignal = True if varName == 'm_withMET': value = tree.m_eff elif varName == 'mVis': value = tree.m_vis elif varName == 'pZeta - 3.1pZetaVis': value = tree.ZetaCut elif varName == "nCSVL": value = tree.nCSVL elif varName == "cos_phi_tau1_tau2": value = math.cos(tree.phi_1 - tree.phi_2) elif varName == "j1Pt": jets = getJets(tree) if len(jets) > 0: value = jets[0].pt() else: value = -1 elif varName == 'm_gen': if tree.eGenTauMass < 0 or tree.tGenMass < 0: continue l1.SetCoordinates(tree.eGenTauPt, tree.eGenTauEta, tree.eGenTauPhi, tree.eGenTauMass) l2.SetCoordinates(tree.tGenPt, tree.tGenEta, tree.tGenPhi, tree.tGenMass) value = (l1 + l2).mass() else: if hasattr(tree, varName): value = getattr(tree, varName) else: value = -1 if options.overFlow: if value > varBins[len(varBins) - 1]: value = (varBins[len(varBins) - 1] + varBins[len(varBins) - 2] + 0.0) / 2.0 if regionSelection(tree, FS, "OSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): fill = True if isData: fill = False if options.unblind or (options.unblindPartial and passUnblindPartial(varName, value)): fill = True histDict['WJets_OSsignal'].Fill(value, weight) if fill: tmpHist.Fill(value, weight) tmpHist_forROOTFile.Fill(value, weight) if iCategory != 'WJets' and (not isData) and (not isSignal): histDict['WJets_OSsignal'].Fill(value, -weight) else: if isSignal: continue #region B if regionSelection(tree, FS, "SSsignal", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_B'].Fill(value, -weight * QCD_weight) else: histDict['QCD_B'].Fill(value, weight * QCD_weight) #region C if regionSelection(tree, FS, "OScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_C'].Fill(value, -weight * QCD_weight) if iCategory != 'WJets': histDict['WJets_OScontrol'].Fill(value, -weight) else: histDict['QCD_C'].Fill(value, weight * QCD_weight) histDict['WJets_OScontrol'].Fill(value, weight) #region D if regionSelection(tree, FS, "SScontrol", plots_cfg.scanRange[0], plots_cfg.scanRange[1]): if not isData: histDict['QCD_D'].Fill(value, -weight * QCD_weight) histDict['QCD_D_for_C'].Fill(value, -weight * QCD_weight) histDict['QCD_D_for_A'].Fill(value, -weight * QCD_weight) else: histDict['QCD_D'].Fill(value, weight * QCD_weight) histDict['QCD_D_for_C'].Fill(value, weight * QCD_weight) histDict['QCD_D_for_A'].Fill(value, weight * QCD_weight) print iCategory, tmpHist.Integral(0, len(varBins) + 1) histDict[iCategory].Add(tmpHist) del tmpHist print ''
def loop_one_sample(iSample, varName, hist1, hist2, category='mt', isData=False, region='barrel', triggerLeg='e'): file = r.TFile(iSample) tree = file.Get('Ntuple') nEntries = tree.GetEntries() hist1.Sumw2() hist2.Sumw2() eventCountWeighted = file.Get('eventCountWeighted') # if not isData and nEntries > 50000: # nEntries = 50000 for iEntry in range(nEntries): tree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'Looping sample %s' % (iSample), iEntry - 1) weight = 1.0 if not isData: puweight = cutSampleTools.getPUWeight(tree.nTruePU) sumWeights = eventCountWeighted.GetBinContent(1) weight = 10000 * tree.xs * tree.genEventWeight * puweight / ( sumWeights + 0.0) if category == 'mt': if tree.mPt <= 25: continue if not (tree.singleMu24Pass and tree.mIsoMu24): continue elif category == 'et': if tree.ePt <= 33: continue if isData: if not (tree.singleETightPass and tree.eSingleEleTight): continue else: if not (tree.singleEPass and tree.eSingleEle): continue elif category == 'ee': if isData: if 'e2' in varName: if tree.singleETightPass and tree.e1SingleEleTight: if tree.e1Pt <= 34: continue else: continue if 'e1' in varName: if tree.singleETightPass and tree.e2SingleEleTight: if tree.e2Pt <= 34: continue else: continue else: if 'e2' in varName: if tree.singleEPass and tree.e1SingleEle: if tree.e1Pt <= 34: continue else: continue if 'e1' in varName: if tree.singleEPass and tree.e2SingleEle: if tree.e2Pt <= 34: continue else: continue elif category == 'em': if abs(tree.eEta) >= 2.1: continue if abs(tree.mEta) >= 2.1: continue if triggerLeg == 'e': if tree.mPt <= 24: continue if region == 'barrel' and abs(tree.eEta) > 1.479: continue elif region == 'endcap' and abs(tree.eEta) <= 1.479: continue else: if region == 'barrel' and abs(tree.mEta) > 0.8: continue elif region == 'middle' and not (0.8 < abs(tree.mEta) < 1.24): continue elif region == 'endcap' and not (1.24 < abs(tree.mEta) < 2.4): continue if tree.ePt <= 28: continue if tree.q_1 == tree.q_2: continue value = getattr(tree, varName) hist1.Fill(value) if category == 'mt': if tree.muTauPass and tree.mMuTau and tree.mMuTauOverlap and tree.tTau20AgainstMuon and tree.tTauOverlapMu: hist2.Fill(value) elif category == 'et': if isData: if tree.eTau_WPLoosePass and tree.eEle22Loose and tree.eOverlapEle22Loose and tree.tTau20 and tree.tTauOverlapEleLoose: hist2.Fill(value) else: if tree.eTauPass and tree.eEle22 and tree.eOverlapEle22 and tree.tTau20 and tree.tTauOverlapEle: hist2.Fill(value) elif category == 'ee': if isData: if tree.doubleE_WPLoosePass: if (tree.e1DoubleE_WPLooseLeg1 and tree.e2DoubleE_WPLooseLeg2) or ( tree.e1DoubleE_WPLooseLeg2 and tree.e2DoubleE_WPLooseLeg1): if tree.e2Pt > 25 and tree.e1Pt > 25: hist2.Fill(value) else: if tree.doubleE_WP75Pass: if (tree.e1DoubleE_WP75Leg1 and tree.e2DoubleE_WP75Leg2 ) or (tree.e1DoubleE_WP75Leg2 and tree.e2DoubleE_WP75Leg1): if tree.e2Pt > 25 and tree.e1Pt > 25: hist2.Fill(value) elif category == 'em': if triggerLeg == 'e': if tree.singleE27_2p1_WPTightPass and tree.eSingleEle27_2p1_WPTight: hist2.Fill(value) elif triggerLeg == 'mu': if tree.singleMu24Pass and tree.mIsoMu24: hist2.Fill(value) print ''
def loop_one_sample(iSample, iLocation, iFS): print 'combining sample [%s] for LUT' % (iSample) if 'data' in iSample: return 0 if ('H2hh' in iSample) or ('ggH' in iSample) or ('Zprime' in iSample): return 0 #pdfWeights pdfHists = [] nWeights = 101 initSumPDF = [] for i in range(nWeights): pdfHists.append(r.TH1F("pdf_%i" % i, "", len(bins) - 1, bins)) pdfHists[i].Sumw2() iFile = r.TFile(iLocation) iTree = iFile.Get("Ntuple") nEntries = iTree.GetEntries() iTree.SetBranchStatus("*", 1) iTree.SetBranchStatus("sampleName", 0) eventCount = iFile.Get('eventCount') eventCountWeighted = iFile.Get('eventCountWeighted') if options.signal: for i in range(100): initSumPDF.append( int((iFile.Get('eventCountWeightedPDF_%i' % i)).GetBinContent(1))) hist_highPUWeight = r.TH1F("hist_highPUWeight", "", 50, 0.5, 1.5) hist_lowPUWeight = r.TH1F("hist_lowPUWeight", "", 50, 0.5, 1.5) yieldEstimator_OS = 0.0 yieldEstimator_SS = 0.0 fillcounter = 0 met = lvClass() nBins = len(plots_cfg.list) # nEntries = int(nEntries/3.) for iEntry in range(nEntries): iTree.GetEntry(iEntry) tool.printProcessStatus(iEntry, nEntries, 'looping over file %s' % (iSample), iEntry - 1) met.SetCoordinates(iTree.pfMetEt, 0.0, iTree.pfMetPhi, 0) l1.SetCoordinates(iTree.pt_1, iTree.eta_1, iTree.phi_1, iTree.m_1) l2.SetCoordinates(iTree.pt_2, iTree.eta_2, iTree.phi_2, iTree.m_2) if not plots.passCut(iTree, iFS, False, l1, l2, met, ''): continue if options.method != 'SS' and iTree.q_1 == iTree.q_2: continue if plots.regionSelection(iTree, iFS, "control", options.method, plots_cfg.scanRange[0], plots_cfg.scanRange[1]): sampleName = 'MC' + controlRegionName elif plots.regionSelection(iTree, iFS, "signal", options.method, plots_cfg.scanRange[0], plots_cfg.scanRange[1]): sampleName = iSample else: continue triggerEff = iTree.trigweight_1 * iTree.trigweight_2 xs = iTree.xs if 'WJets' in iSample: xs = xs * plots_cfg.WJetsScanRange[0] if "Zprime" in iSample: xs = plots.getZPrimeXS(iSample[7:]) if eventCount: initEvents = int(eventCount.GetBinContent(1)) else: initEvents = int(iTree.initEvents) if eventCountWeighted: initSumWeights = int(eventCountWeighted.GetBinContent(1)) else: initSumWeights = int(iTree.initWeightedEvents) m_effective = (l1 + l2 + met).mass() #overflow and underflow if m_effective < plots_cfg.list[0]: m_effective = (plots_cfg.list[0] + plots_cfg.list[1]) / 2.0 if m_effective > plots_cfg.list[nBins - 1]: m_effective = (plots_cfg.list[nBins - 1] + plots_cfg.list[nBins - 2]) / 2.0 if options.PUWeight: PUWeight = cutSampleTools.getPUWeight(iTree.nTruePU) else: PUWeight = 1.0 genEventWeight = iTree.genEventWeight if options.noNegGenEvts and genEventWeight < 0: continue if options.noGenEvtWeight: genEventWeight = 1.0 if (sampleName == 'MC' + controlRegionName) and ("WJets" in iSample): totalNormalWeights = triggerEff * xs * ( plots.lumi) * genEventWeight * PUWeight pdfHists[0].Fill(m_effective, totalNormalWeights) for i in range(1, nWeights): pdfID = getPDFWeightID(i, iSample, iTree.pdfID) pdfHists[i].Fill( m_effective, totalNormalWeights * iTree.pdfWeight.at(pdfID)) elif (controlRegionName not in sampleName) and ("WJets" not in iSample): totalNormalWeights = triggerEff * xs * ( plots.lumi) * genEventWeight * PUWeight absMax = 0 for i in range(1, nWeights): pdfID = getPDFWeightID(i, iSample, iTree.pdfID) if abs(iTree.pdfWeight.at(pdfID)) > absMax: absMax = iTree.pdfWeight.at(pdfID) if absMax > 2.5: continue pdfHists[0].Fill(m_effective, totalNormalWeights) for i in range(1, nWeights): pdfID = getPDFWeightID(i, iSample, iTree.pdfID) pdfHists[i].Fill( m_effective, totalNormalWeights * iTree.pdfWeight.at(pdfID)) #hist_highPUWeight.Fill(PUWeight) if PUWeight > 1.0: hist_highPUWeight.Fill(iTree.pdfWeight.at(pdfID), genEventWeight * PUWeight) else: hist_lowPUWeight.Fill(iTree.pdfWeight.at(pdfID), genEventWeight * PUWeight) c = r.TCanvas("c", "Test", 600, 600) c.Range(0, 0, 1, 1) c.SetFillColor(0) # r.gPad.SetLogy() hist_highPUWeight.SetLineColor(r.kBlue) hist_lowPUWeight.SetLineColor(r.kRed) # hist_lowPUWeight.Scale(1./hist_lowPUWeight.Integral(0, hist_lowPUWeight.GetNbinsX()+1)) hist_highPUWeight.Draw() hist_lowPUWeight.Draw("same") position = (0.15, 0.85 - 0.05 * 2, 0.47, 0.85) histList = [] histList.append((hist_highPUWeight, "high PU-weight events", 'l')) histList.append((hist_lowPUWeight, "low PU-weight events", 'l')) legends = tool.setMyLegend(position, histList) legends.Draw("same") c.Print('pdfSys.pdf') return getVariations(pdfHists, initSumPDF, initSumWeights)