def setDefaults(greyScale = False): AtlasStyle.setAtlasDefaults(not LATEX); gROOT.SetBatch(True) #gStyle.SetPalette(1) #gStyle.SetOptTitle(1) #gStyle.SetTitleAlign(33) global CANVAS CANVAS = TCanvas('c', 'global canvas', 800, 600) if greyScale: CANVAS.SetGrayscale()
def get_met_filter_acceptance(passed_list, total, parameters): print('') print('*** start get_met_filter_acceptance') canvas = ROOT.TCanvas("c", "c", 1000, 600) canvas.SetLogy() n_bins = len(parameters['from_to']) bin_width = (parameters['from_to'][-1] - parameters['from_to'][0]) / n_bins histo = [ ROOT.TH1F('histo' + str(x), ';MET_LocHadTopo Threshold [GeV];Acceptance', n_bins, parameters['from_to'][0] - bin_width / 2., parameters['from_to'][-1] + bin_width / 2.) for x in range(len(passed_list)) ] legend = ROOT.TLegend(0.6, 0.50, 0.85, 0.85) denominator = total.Integral(0, -1) for (ii, passed) in enumerate(passed_list): utils.decorate_histogram(histo[ii], BasicConfig.colors[ii]) for threshold in parameters['from_to']: numerator = passed.Integral(passed.FindBin(threshold), -1) acceptance, error = utils.division_error_propagation( numerator, denominator) histo[ii].SetBinContent(histo[ii].FindBin(threshold), acceptance) histo[ii].SetBinError(histo[ii].FindBin(threshold), error) if ii == 0: if 'max_min' in parameters: histo[ii].SetMaximum(parameters['max_min'][0]) histo[ii].SetMinimum(parameters['max_min'][1]) histo[ii].Draw('e1') else: histo[ii].Draw('same,e1') legend.AddEntry(histo[ii], parameters['legend'][ii], 'ple') if 'reference' in parameters: ref_acceptance = parameters['reference'].Integral(0, -1) / denominator print ref_acceptance canvas.Update() tl = ROOT.TLine(canvas.GetUxmin(), ref_acceptance, canvas.GetUxmax(), ref_acceptance) tl.SetLineColor(ROOT.kGray + 1) tl.SetLineStyle(2) tl.Draw() utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.85, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, ROOT.kBlack, parameters['label'], 0.035) if 'label2' in parameters: AtlasStyle.myText(0.20, 0.74, ROOT.kBlack, parameters['label2'], 0.035) utils.save_as( canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def overlay_histograms(histograms, legends, parameters): print('') print('*** start overlay_histograms') canvas = ROOT.TCanvas("c", "c", 1000, 600) y_max = histograms[0].GetMaximum() * 2 y_min = 0.8 leg = ROOT.TLegend(0.55, 0.7, 0.86, 0.90) for ii, (histogram, legend) in enumerate(zip(histograms, legends)): print(ii) decorate_histogram(histogram, BasicConfig.colors[ii]) if 'x_max' in parameters: histogram.GetXaxis().SetRangeUser(0, parameters['x_max']) if ii == 0: if 'doNorm' not in parameters: histograms[0].SetMinimum(y_min) histograms[0].SetMaximum(histograms[0].GetMaximum() * 30) else: histograms[0].Scale(1. / histograms[0].Integral()) histograms[0].SetMaximum(histograms[0].GetMaximum() * 1.5) if 'noLogy' in parameters: histograms[0].SetMinimum(0) histogram.Draw() else: if histogram.GetMaximum() > y_max and 'doNorm' not in parameters: y_max = histogram.GetMaximum() if 'noLogy' not in parameters: histograms[0].SetMaximum(y_max * 15) histogram.Draw('same') elif 'doNorm' in parameters: histogram.Scale(1. / histogram.Integral()) if histogram.GetMaximum() > histograms[0].GetMaximum(): histograms[0].SetMaximum(histogram.GetMaximum() * 1.5) if 'noLogy' not in parameters: histograms[0].SetMaximum(histogram.GetMaximum() * 15) histogram.Draw('same') leg.AddEntry(histogram, legend, 'l') canvas.Update() canvas.SetLogy() if 'noLogy' in parameters: canvas.SetLogy(parameters['noLogy']) decorate_legend(leg) leg.Draw() AtlasStyle.ATLASLabel(0.20, 0.85, 'Work in Progress') AtlasStyle.myText(0.22, 0.80, ROOT.kBlack, parameters['label'], 0.038) if 'label2' in parameters: AtlasStyle.myText(0.25, 0.76, ROOT.kBlack, parameters['label2'], 0.038) save_as( canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def overlay_histograms(histograms, legends, parameters): print('') print('*** start overlay_histograms') canvas = ROOT.TCanvas("c", "c", 1000, 600) y_max = histograms[0].GetMaximum() * 2 y_min = 0.8 leg = ROOT.TLegend(0.55, 0.7, 0.86, 0.90) for ii, (histogram, legend) in enumerate(zip(histograms, legends)): print(ii) decorate_histogram(histogram, BasicConfig.colors[ii]) if 'x_max' in parameters: histogram.GetXaxis().SetRangeUser(0, parameters['x_max']) if ii == 0: if 'doNorm' not in parameters: histograms[0].SetMinimum(y_min) histograms[0].SetMaximum(histograms[0].GetMaximum() * 30) else: histograms[0].Scale(1./histograms[0].Integral()) histograms[0].SetMaximum(histograms[0].GetMaximum() * 1.5) if 'noLogy' in parameters: histograms[0].SetMinimum(0) histogram.Draw() else: if histogram.GetMaximum() > y_max and 'doNorm' not in parameters: y_max = histogram.GetMaximum() if 'noLogy' not in parameters: histograms[0].SetMaximum(y_max * 15) histogram.Draw('same') elif 'doNorm' in parameters: histogram.Scale(1./histogram.Integral()) if histogram.GetMaximum() > histograms[0].GetMaximum(): histograms[0].SetMaximum(histogram.GetMaximum() * 1.5) if 'noLogy' not in parameters: histograms[0].SetMaximum(histogram.GetMaximum() * 15) histogram.Draw('same') leg.AddEntry(histogram, legend, 'l') canvas.Update() canvas.SetLogy() if 'noLogy' in parameters: canvas.SetLogy(parameters['noLogy']) decorate_legend(leg) leg.Draw() AtlasStyle.ATLASLabel(0.20, 0.85, 'Work in Progress') AtlasStyle.myText(0.22, 0.80, ROOT.kBlack, parameters['label'], 0.038) if 'label2' in parameters: AtlasStyle.myText(0.25, 0.76, ROOT.kBlack, parameters['label2'], 0.038) save_as(canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def draw_mass_distributions_and_ratio(self, ntrk, region, h_data, h_model): canvas2 = ROOT.TCanvas('canvas2', 'canvas2', 1000, 750) # mass distributions pad1 = ROOT.TPad('pad1', 'pad1', 0, 0.3, 1, 1.0) pad2 = ROOT.TPad('pad2', 'pad2', 0, 0.05, 1, 0.3) self.prepare_pads(canvas2, pad1, pad2) pad1.cd() self.configure_data_histogram(h_data) h_data.SetMaximum(h_data.GetMaximum() * 8) self.configure_model_histogram(h_model) h_data.Draw('e') h_model.SetLineWidth(2) h_model.Draw('same,hist') h_data.Draw('same,e') # list_diff3[ipad-1].SetLineWidth(2) AtlasStyle.ATLASLabel(self.ax, self.ay, 'Work in Progress') AtlasStyle.myText(self.tx, self.ty, ROOT.kBlack, self.beam_condition, 0.038) leg = ROOT.TLegend(self.x_min, self.y_min, self.x_max, self.y_max) leg.AddEntry(h_data, str(ntrk) + '-trk vertices Region' + str(region), 'lep') leg.AddEntry(h_model, str(ntrk - 1) + '-trk vert + 1 random track', 'f') utils.decorate_legend(leg) leg.Draw() line = ROOT.TLine(self.m_cut, h_data.GetMinimum(), self.m_cut, h_data.GetMaximum() * 0.1) utils.decorate_line(line, ROOT.kGray + 1, 5) line.Draw() # Ratio plot pad2.cd() h_ratio = h_data.Clone(str(ntrk) + 'trk_ratio' + str(region)) h_ratio.Sumw2() h_ratio.Divide(h_model) self.decorate_ratio_plot(h_ratio, 0.1, 1.9) h_ratio.Draw('e2p') line2 = ROOT.TLine(self.m_cut, h_ratio.GetMinimum(), self.m_cut, h_ratio.GetMaximum()) utils.decorate_line(line2, ROOT.kGray + 1, 5) line2.Draw() directory = BasicConfig.plotdir + 'bg_est/' + str(date.today()) os.system('mkdir -p ' + directory) utils.save_as( canvas2, directory + '/pseudoFit_' + str(ntrk) + 'Trk_Region' + str(region)) canvas2.Close()
def get_met_filter_acceptance(passed_list, total, parameters): print('') print('*** start get_met_filter_acceptance') canvas = ROOT.TCanvas("c", "c", 1000, 600) canvas.SetLogy() n_bins = len(parameters['from_to']) bin_width = (parameters['from_to'][-1] - parameters['from_to'][0]) / n_bins histo = [ROOT.TH1F('histo' + str(x), ';MET_LocHadTopo Threshold [GeV];Acceptance', n_bins, parameters['from_to'][0] - bin_width / 2., parameters['from_to'][-1] + bin_width / 2.) for x in range(len(passed_list))] legend = ROOT.TLegend(0.6, 0.50, 0.85, 0.85) denominator = total.Integral(0, -1) for (ii, passed) in enumerate(passed_list): utils.decorate_histogram(histo[ii], BasicConfig.colors[ii]) for threshold in parameters['from_to']: numerator = passed.Integral(passed.FindBin(threshold), -1) acceptance, error = utils.division_error_propagation(numerator, denominator) histo[ii].SetBinContent(histo[ii].FindBin(threshold), acceptance) histo[ii].SetBinError(histo[ii].FindBin(threshold), error) if ii == 0: if 'max_min' in parameters: histo[ii].SetMaximum(parameters['max_min'][0]) histo[ii].SetMinimum(parameters['max_min'][1]) histo[ii].Draw('e1') else: histo[ii].Draw('same,e1') legend.AddEntry(histo[ii], parameters['legend'][ii], 'ple') if 'reference' in parameters: ref_acceptance = parameters['reference'].Integral(0, -1) / denominator print ref_acceptance canvas.Update() tl = ROOT.TLine(canvas.GetUxmin(), ref_acceptance, canvas.GetUxmax(), ref_acceptance) tl.SetLineColor(ROOT.kGray+1) tl.SetLineStyle(2) tl.Draw() utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.85, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, ROOT.kBlack, parameters['label'], 0.035) if 'label2' in parameters: AtlasStyle.myText(0.20, 0.74, ROOT.kBlack, parameters['label2'], 0.035) utils.save_as(canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def plotAll(file): outDir = file[:-5] + '/' if not os.path.exists(outDir): os.mkdir(outDir) outDir += "plotAll/" if not os.path.exists(outDir): os.mkdir(outDir) AtlasStyle.SetAtlasStyle() gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info inFile = TFile.Open(file, "READ") keyList = [key.GetName() for key in inFile.GetListOfKeys()] #List of top level objects dirList = [key for key in keyList if "Iteration" in key] #List of all directories nomDir = [dir for dir in dirList if "Nominal" in dir] if (not len(nomDir) == 1): print "Error, nominal directories are ", nomDir return else: nomDir = inFile.Get(nomDir[0]) c1 = TCanvas() ##################### Plot every single histogram ################################# print "Plotting All Histograms for " for dir in dirList: if not "Nominal" in dir: continue print " ", dir if not os.path.exists(outDir + dir): os.mkdir(outDir + dir) if not os.path.exists(outDir + dir + '/eps'): os.mkdir(outDir + dir + '/eps') thisDir = inFile.Get(dir) histList = [key.GetName() for key in thisDir.GetListOfKeys()] ### Save all histograms ### for histName in histList: print dir, histName thisHist = thisDir.Get(histName) if ("Pt" in histName): thisHist.GetXaxis().SetRangeUser(0, 2000.) if (not "Nominal" in dir): nomHist = nomDir.Get(histName) thisHist.Add(nomHist, -1) thisHist.Divide(nomHist) thisHist.GetYaxis().SetTitle("Relative Difference w/ Nominal") if type(thisHist) == TH2F or type(thisHist) == TH3F: thisHist.Draw("colz") else: thisHist.Draw() c1.SaveAs(outDir + dir + "/" + thisHist.GetName() + ".png") c1.SaveAs(outDir + dir + "/eps/" + thisHist.GetName() + ".eps") c1.Clear() inFile.Close()
def main(): AtlasStyle.SetAtlasStyle() gROOT.SetBatch() parser = argparse.ArgumentParser(description='Error Analysis for TRT logs, input grafana mu dumps and ers log.') parser.add_argument('-d', '--dir', dest='dir', default='2018', type=str, help="Directory of grafana mu dumps") parser.add_argument('-f', '--file', dest='log_file', default='', type=str, help="Log file from ERS.") parser.add_argument('-s', '--print_sb', dest='print_sb', default=False, action='store_true', help="print sb times") parser.add_argument('-n', '--non_sb',dest='non_sb', default=False, action='store_true', help="get errors for non-sb times") parser.add_argument('-t', '--text',dest='text_filter', default='rocketio problem', type=str, help="text to filter query") parser.add_argument('-b', '--date_beg',dest='date_beg', default='2015-04-01 00:00:00', type=str, help="date begin. Formatted yyyy-mm-dd hh:mm:ss format") parser.add_argument('-e', '--date_end',dest='date_end', default='2100-01-01 00:00:00', type=str, help="date end. Formatted yyyy-mm-dd hh:mm:ss format") args = parser.parse_args() if args.print_sb: beg_sb, end_sb = getsb.get_sb_list(args.dir, args.print_sb) if args.log_file != '': beg_sb, end_sb = getsb.get_sb_list(args.dir, args.print_sb) dbhandler.add_to_db(beg_sb, end_sb, args.log_file) query_errors = dbhandler.get_query(args.non_sb, args.text_filter, args.date_beg, args.date_end) print "Query returned " + repr(len(query_errors)) + " results!" if('rocketio' in args.text_filter): parse_rocketio_errors(query_errors)
def mll_C1_test(): path = "/afs/cern.ch/work/y/yushen/private/Higgsino/SimpleAnalysis/Results/" f_original = path + "20170619/user.chris.100k.CC.filtered.TestJob.root" f_C1_at_middle = path + "20170620/user.chris.C1.middle.TestJob.root" f_no_C1 = path + "20170619/user.chris.10k.CC.no.x1.filtered.TestJob.root" f1 = ROOT.TFile(f_original) f2 = ROOT.TFile(f_C1_at_middle) f3 = ROOT.TFile(f_no_C1) t1 = f1.Get("EwkHiggsino2016__ntuple") h1 = ROOT.TH1F("h1", "mll", 100, 0, 50) t1.Project("h1", "mll", "mll>0") h1.SetDirectory(ROOT.gROOT) t2 = f2.Get("EwkHiggsino2016__ntuple") h2 = ROOT.TH1F("h2", "mll", 100, 0, 50) t2.Project("h2", "mll", "mll>0") h2.SetDirectory(ROOT.gROOT) t3 = f3.Get("EwkHiggsino2016__ntuple") h3 = ROOT.TH1F("h3", "mll", 100, 0, 50) t3.Project("h3", "mll", "mll>0") h3.SetDirectory(ROOT.gROOT) h1.Scale(1 / h1.Integral()) h2.Scale(1 / h2.Integral()) h3.Scale(1 / h3.Integral()) h1.SetLineColor(ROOT.kBlack) h2.SetLineColor(ROOT.kRed) h3.SetLineColor(ROOT.kBlue) c1 = ROOT.TCanvas("c1", "c1", 800, 800) ROOT.gPad.SetLogy() h1.SetStats(0) # no stats box h1.SetXTitle("M_{ll} [GeV]") h1.SetMaximum(h1.GetMaximum() * 20) h1.Draw() h2.Draw("same") h3.Draw("same") legend = ROOT.TLegend(0.5, 0.6, 0.9, 0.8) legend.AddEntry(h1, "NUHM2, 100k, Original", "l") legend.AddEntry(h2, "NUHM2, 10k, C1 at middle", "l") legend.AddEntry(h3, "NUHM2, 10k, No C1", "l") legend.SetBorderSize(0) legend.SetTextFont(42) legend.SetTextSize(0.02) legend.SetFillColor(0) legend.SetFillStyle(0) legend.Draw() AtlasStyle.ATLASLabel(0.15, 0.85, "internal", ROOT.kBlack) c1.SaveAs("mll_C1_test.pdf")
def superpose_tocs(passed_list, total, parameters): print('') print('*** start create_toc_plot') #if len(passed_list) != len(total_list): # print('lengths of passed_list and total_list are different.') # print('aborted.') # sys.exit() canvas = ROOT.TCanvas("c", "c", 1000, 600) legend = ROOT.TLegend(0.6, 0.20, 0.85, 0.60) tgs = [] for index, passed in enumerate(passed_list): if 'rebin' in parameters: passed.Rebin(parameters['rebin']) if index == 0: # only once if passed.GetNbinsX() != total.GetNbinsX(): total.Rebin(parameters['rebin']) tg = ROOT.TGraphAsymmErrors(passed, total) utils.decorate_histogram(tg, BasicConfig.colors[index]) if index == 0: tg.SetMaximum(1.09) tg.GetXaxis().SetRangeUser(0, 400) tg.GetYaxis().SetTitle('Efficiency') if 'x_title' in parameters: tg.GetXaxis().SetTitle(parameters['x_title']) tg.Draw('AP') else: tg.Draw('P,same') legend.AddEntry(tg, parameters['legend'][index], 'pl') tgs.append(tg) # stored temporarily utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.85, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, ROOT.kBlack, parameters['label'], 0.035) if 'label2' in parameters: AtlasStyle.myText(0.20, 0.74, ROOT.kBlack, parameters['label2'], 0.035) utils.save_as( canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def fit_plot(file, n2_n1): f = ROOT.TFile(file) t = f.Get("MGPy8EG_A14N23LO_SM_Higgsino_" + n2_n1 + "_2LMET50_MadSpin_NoSys") h = ROOT.TH1F("h_" + n2_n1, "truthMll", 100, 0, 100) t.Project("h_" + n2_n1, "truthMll", "truthMll>0") n2 = float((n2_n1.split("_"))[0]) n1 = float((n2_n1.split("_"))[1]) dm = n2 - n1 print n2, n1, dm fit_func = ROOT.TF1("fit_func", funcMllDistr, 0, dm, 3) fit_func.SetParameters(1., n1, -1. * n2) area_fit_func = fit_func.Integral(0., dm) fit_func.SetParameter(0, 1. / area_fit_func) # normalize fit_func fit_func.FixParameter(1, n1) fit_func.FixParameter(2, -1. * n2) fit_func.SetLineColor(ROOT.kRed) fit_func.SetLineStyle(2) canvas = ROOT.TCanvas("c", "", 800, 600) canvas.SetLeftMargin(0.12) logY = True if logY: ROOT.gPad.SetLogy() ROOT.gStyle.SetOptFit(1111) h.SetXTitle("m_{ll} [GeV]") h.SetYTitle("Events / 1GeV") h.GetYaxis().SetTitleOffset(1.2) h.SetLineColor(ROOT.kBlue) h.Draw("hist") h.Fit(fit_func, "R0") h.Fit(fit_func, "R0") h.Fit(fit_func, "R+") fit_func.Draw("same") AtlasStyle.ATLASLabel(0.15, 0.85, "internal", ROOT.kBlack) legend = ROOT.TLegend(0.5, 0.4, 0.9, 0.5) legend.AddEntry(h, "Higgsino_" + n2_n1, "l") # legend.AddEntry(fit_func, "Theoretical Higgsino_" + n2_n1 + "(fix N2 and N1)", "l") legend.AddEntry(fit_func, "Theoretical Higgsino_" + n2_n1, "l") legend.SetBorderSize(0) legend.SetTextFont(42) legend.SetTextSize(0.03) legend.SetFillColor(0) legend.SetFillStyle(0) legend.Draw() output = "fit_Higgsino_common_ntuple_v1.8b_" + n2_n1 + ".pdf" canvas.SaveAs(output)
def draw_mass_distributions_and_ratio(self, ntrk, region, h_data, h_model): canvas2 = ROOT.TCanvas('canvas2', 'canvas2', 1000, 750) # mass distributions pad1 = ROOT.TPad('pad1', 'pad1', 0, 0.3, 1, 1.0) pad2 = ROOT.TPad('pad2', 'pad2', 0, 0.05, 1, 0.3) self.prepare_pads(canvas2, pad1, pad2) pad1.cd() self.configure_data_histogram(h_data) h_data.SetMaximum(h_data.GetMaximum() * 8) self.configure_model_histogram(h_model) h_data.Draw('e') h_model.SetLineWidth(2) h_model.Draw('same,hist') h_data.Draw('same,e') # list_diff3[ipad-1].SetLineWidth(2) AtlasStyle.ATLASLabel(self.ax, self.ay, 'Work in Progress') AtlasStyle.myText(self.tx, self.ty, ROOT.kBlack, self.beam_condition, 0.038) leg = ROOT.TLegend(self.x_min, self.y_min, self.x_max, self.y_max) leg.AddEntry(h_data, str(ntrk) + '-trk vertices Region' + str(region), 'lep') leg.AddEntry(h_model, str(ntrk-1) + '-trk vert + 1 random track', 'f') utils.decorate_legend(leg) leg.Draw() line = ROOT.TLine(self.m_cut, h_data.GetMinimum(), self.m_cut, h_data.GetMaximum() * 0.1) utils.decorate_line(line, ROOT.kGray+1, 5) line.Draw() # Ratio plot pad2.cd() h_ratio = h_data.Clone(str(ntrk) + 'trk_ratio' + str(region)) h_ratio.Sumw2() h_ratio.Divide(h_model) self.decorate_ratio_plot(h_ratio, 0.1, 1.9) h_ratio.Draw('e2p') line2 = ROOT.TLine(self.m_cut, h_ratio.GetMinimum(), self.m_cut, h_ratio.GetMaximum()) utils.decorate_line(line2, ROOT.kGray+1, 5) line2.Draw() directory = BasicConfig.plotdir + 'bg_est/' + str(date.today()) os.system('mkdir -p ' + directory) utils.save_as(canvas2, directory + '/pseudoFit_' + str(ntrk) + 'Trk_Region' + str(region)) canvas2.Close()
def superpose_tocs(passed_list, total, parameters): print('') print('*** start create_toc_plot') #if len(passed_list) != len(total_list): # print('lengths of passed_list and total_list are different.') # print('aborted.') # sys.exit() canvas = ROOT.TCanvas("c", "c", 1000, 600) legend = ROOT.TLegend(0.6, 0.20, 0.85, 0.60) tgs = [] for index, passed in enumerate(passed_list): if 'rebin' in parameters: passed.Rebin(parameters['rebin']) if index == 0: # only once if passed.GetNbinsX() != total.GetNbinsX(): total.Rebin(parameters['rebin']) tg = ROOT.TGraphAsymmErrors(passed, total) utils.decorate_histogram(tg, BasicConfig.colors[index]) if index == 0: tg.SetMaximum(1.09) tg.GetXaxis().SetRangeUser(0, 400) tg.GetYaxis().SetTitle('Efficiency') if 'x_title' in parameters: tg.GetXaxis().SetTitle(parameters['x_title']) tg.Draw('AP') else: tg.Draw('P,same') legend.AddEntry(tg, parameters['legend'][index], 'pl') tgs.append(tg) # stored temporarily utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.85, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, ROOT.kBlack, parameters['label'], 0.035) if 'label2' in parameters: AtlasStyle.myText(0.20, 0.74, ROOT.kBlack, parameters['label2'], 0.035) utils.save_as(canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def compare_two_curves(file1, file2, var, normalize): canvas = ROOT.TCanvas("c", "", 800, 600) if var not in [ "h_NJets", "h_NJet30", "h_NJet25", "h_Nbjets", "h_NLepts_baseline", "h_NLepts_signal" ]: ROOT.gPad.SetLogy() f1 = ROOT.TFile(file1) var_in_f1 = f1.Get(var) integral1 = var_in_f1.Integral() if normalize is True: var_in_f1.Scale(1 / integral1) f2 = ROOT.TFile(file2) var_in_f2 = f2.Get(var) integral2 = var_in_f2.Integral() if normalize is True: var_in_f2.Scale(1 / integral2) y_maximum = max(var_in_f1.GetMaximum(), var_in_f2.GetMaximum()) var_in_f1.SetStats(0) var_in_f1.SetLineColor(ROOT.kBlue) if normalize is True: var_in_f1.SetYTitle("Normalized event counts") else: var_in_f1.SetYTitle("Event counts") var_in_f1.SetMaximum(y_maximum * 1.2) if var is "h_mll": var_in_f1.GetXaxis().SetRangeUser(0, 300) var_in_f1.Draw("hist") var_in_f2.SetLineColor(ROOT.kRed) var_in_f2.Draw("hist,same") legend = ROOT.TLegend(0.6, 0.7, 0.9, 0.8) legend.AddEntry(var_in_f1, "NUHM2_m12_600", "l") legend.AddEntry(var_in_f2, "Higgsino_190_150", "l") legend.SetBorderSize(0) legend.SetTextFont(42) legend.SetTextSize(0.03) legend.SetFillColor(0) legend.SetFillStyle(0) legend.Draw() AtlasStyle.ATLASLabel(0.15, 0.85, "internal", ROOT.kBlack) # if normalize is not True: # AtlasStyle.myText(0.3, 0.85, ROOT.kBlack, "36.1fb^{-1}") #AtlasStyle.myText(0.6, 0.4, ROOT.kBlack, "#sqrt{s}=13TeV") output = var + ".pdf" canvas.SaveAs(output)
def create_toc_plot(passed, total, parameters): print('') print('*** start create_toc_plot') canvas = ROOT.TCanvas("c", "c", 1000, 600) if 'rebin' in parameters: passed.Rebin(parameters['rebin']) total.Rebin(parameters['rebin']) tg = ROOT.TGraphAsymmErrors(passed, total) tg.GetXaxis().SetRangeUser(0, 400) tg.GetYaxis().SetTitle('Efficiency') if 'x_title' in parameters: tg.GetXaxis().SetTitle(parameters['x_title']) tg.Draw('AP') AtlasStyle.ATLASLabel(0.50, 0.4, 'Work in Progress') AtlasStyle.myText(0.55, 0.32, ROOT.kBlack, parameters['label'], 0.038) if 'label2' in parameters: AtlasStyle.myText(0.55, 0.24, ROOT.kBlack, parameters['label2'], 0.038) save_as(canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def create_toc_plot(passed, total, parameters): print('') print('*** start create_toc_plot') canvas = ROOT.TCanvas("c", "c", 1000, 600) if 'rebin' in parameters: passed.Rebin(parameters['rebin']) total.Rebin(parameters['rebin']) tg = ROOT.TGraphAsymmErrors(passed, total) tg.GetXaxis().SetRangeUser(0, 400) tg.GetYaxis().SetTitle('Efficiency') if 'x_title' in parameters: tg.GetXaxis().SetTitle(parameters['x_title']) tg.Draw('AP') AtlasStyle.ATLASLabel(0.50, 0.4, 'Work in Progress') AtlasStyle.myText(0.55, 0.32, ROOT.kBlack, parameters['label'], 0.038) if 'label2' in parameters: AtlasStyle.myText(0.55, 0.24, ROOT.kBlack, parameters['label2'], 0.038) save_as( canvas, BasicConfig.plotdir + parameters['plot_name'] + '_' + str(date.today()))
def draw_distributions_and_ratio(h_data, h_model, parameters): canvas2 = ROOT.TCanvas('canvas2', 'canvas2', 1000, 750) # mass distributions pad1 = ROOT.TPad('pad1', 'pad1', 0, 0.3, 1, 1.0) pad2 = ROOT.TPad('pad2', 'pad2', 0, 0.05, 1, 0.3) prepare_pads(canvas2, pad1, pad2) pad1.cd() decorate_histogram(h_data, ROOT.kGray + 3) h_data.Sumw2() h_data.Scale(1. / h_data.GetEntries()) h_data.SetMaximum(h_data.GetMaximum() * 10) h_data.Draw('e') #h_model.SetLineWidth(2) decorate_histogram(h_model, ROOT.kRed + 1) h_model.Sumw2() h_model.Scale(1. / h_model.GetEntries()) h_model.Draw('same,e') # list_diff3[ipad-1].SetLineWidth(2) AtlasStyle.ATLASLabel(0.2, 0.85, 'Work in Progress') #AtlasStyle.myText(0, self.ty, ROOT.kBlack, self.beam_condition, 0.038) leg = ROOT.TLegend(0.55, 0.6, 0.85, 0.85) leg.AddEntry(h_data, parameters['legend1'], 'lep') leg.AddEntry(h_model, parameters['legend2'], 'lep') decorate_legend(leg) leg.Draw() #line = ROOT.TLine(self.m_cut, h_data.GetMinimum(), self.m_cut, h_data.GetMaximum() * 0.1) #decorate_line(line, ROOT.kGray + 1, 5) #line.Draw() # Ratio plot pad2.cd() h_ratio = h_data.Clone('ratio') h_ratio.Sumw2() h_ratio.Divide(h_model) decorate_ratio_plot(h_ratio, 0.1, 1.9) h_ratio.Draw('e2p') #line2 = ROOT.TLine(self.m_cut, h_ratio.GetMinimum(), self.m_cut, h_ratio.GetMaximum()) #decorate_line(line2, ROOT.kGray + 1, 5) #line2.Draw() directory = BasicConfig.plotdir + 'validation/' + str(date.today()) os.system('mkdir -p ' + directory) save_as(canvas2, directory + '/' + parameters['file_name']) canvas2.Close()
def MultipleFlatEffCurve(outputName, approachList, bins, binslong,flav = "L"): markerlist = [21, 8 , 22, 23, 29, 34] fout = ROOT.TFile(outputName, "recreate") fout.cd() Canv = ROOT.TCanvas("EffComb", "EffComb", 0, 800, 0, 800) Canv.cd() EffCurves = [] for scoreList, varList, label, scoreCutList in approachList: heff = getFixEffCurve(scoreList, varList, label, binslong, fix_eff_target=0.7, scoreCutList=scoreCutList, onlyReturnCutList=False) EffCurves.append(heff) legend = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) legend_rel = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) ROCs = [] mg = ROOT.TMultiGraph() for i in range(len(EffCurves)): ROC = ConvertEffToGraph(EffCurves[i],bins, False) ROC.SetLineWidth(2) ROC.SetLineColor(colorlist[i]) ROC.SetMarkerColor(colorlist[i]) ROC.SetMarkerSize(1) ROC.SetMarkerStyle(markerlist[i]) ROC.SetLineStyle(i+1) mg.Add(ROC) legend.AddEntry(ROC, approachList[i][2][1], "lp") ROCs.append(ROC) mg.Draw("AP") mg.GetXaxis().SetTitle("b-jet p_{T} [GeV]") if flav == "L": mg.GetYaxis().SetTitle("light-jet Rejection, 1/#varepsilon_{l}") if flav == "C": mg.GetYaxis().SetTitle("c-jet Rejection, 1/#varepsilon_{c}") legend.Draw("same") Atlas.ATLASLabel(0.2, 0.88,0.13, "Simulation Internal",color=1) Atlas.myText(0.2, 0.81 ,color=1, size=0.04,text="#sqrt{s}=13 TeV, t#bar{t}") Atlas.myText(0.2, 0.75 ,color=1, size=0.04,text="p_{T}>20 GeV, |#eta|<2.5") Atlas.myText(0.2, 0.69 ,color=1, size=0.04,text="Flat 70% b-tagging WP") Canv.Write() fout.Close()
def draw_atlas_details(labels=[], x_pos=0.2, y_pos=0.87, dy=0.04 * 0.9, text_size=0.035 * 0.9, sampleName="", height=1.0, isSimulation=True): if sampleName != "": sampleName = ", " + c.samples[sampleName]["Name"] text_size = text_size / height dy = dy / height if not isSimulation: AS.ATLASLabel(x_pos, y_pos, 1, 0.1, text_size, "Internal") else: AS.ATLASLabel(x_pos, y_pos, 1, 0.1, text_size, "Simulation Internal") y_pos -= dy AS.myText(x_pos, y_pos, 1, text_size, "#sqrt{s} = 13 TeV %s" % (sampleName)) y_pos -= dy for label in labels: AS.myText(x_pos, y_pos, 1, text_size, "%s" % label) y_pos -= dy
def make_time_rod_evo(error_dict, rod_dict, results, doLock): c2 = TCanvas( 'c2', 'c2', 1000, 600) leg = TLegend(0.18,0.85,0.45,0.55) leg.SetLineColor(0) leg.SetFillStyle(0) leg.SetShadowColor(0) leg.SetBorderSize(0) leg.SetNColumns(2) R15 = TLine(431,0,431,60) R15.SetLineColorAlpha(kPink+10,0.4) R15.SetLineWidth(4) R16 = TLine(1820,0,1820,60) R16.SetLineColorAlpha(kMagenta+10,0.4) R16.SetLineWidth(4) R17 = TLine(3376,0,3376,60) R17.SetLineColorAlpha(kGreen-3,0.4) R17.SetLineWidth(4) TS1 = TLine(431,0,432,60) TS1.SetLineColorAlpha(kPink+10,0.5) TS1.SetLineWidth(5) TS2 = TLine(1415,0,1415,60) TS2.SetLineColorAlpha(kMagenta+3,0.5) TS2.SetLineWidth(5) leg2 = TLegend(0.18,0.45,0.35,0.55) leg2.SetLineColor(0) leg2.SetFillStyle(0) leg2.SetShadowColor(0) leg2.SetBorderSize(0) gStyle.SetLegendTextSize(0.030) leg2.AddEntry(R15, "End of 2015", 'lf') leg2.AddEntry(R16, "End of 2016", 'lf') leg2.AddEntry(R17, "End of 2017", 'lf') #leg2.AddEntry(TS2, "TS2", 'lf') for key,val in rod_dict.items(): TS1.SetY2(val*0.5) TS2.SetY2(val+1) R15.SetY2(val*0.3) R16.SetY2(val*0.5) R17.SetY2(val*0.8) times = {} times.clear() for e in error_bits: times['0x'+e] = [0] for error in results: pos_rod = error.text.find("ROD") + 4 pos_lock = error.text.find("Lock") + 15 pos_buff = error.text.find("buffer") + 17 if error.msgID == 'TRT::ROD05Module': rod = '0x'+str(error.text[pos_rod:pos_rod+6]) else: rod = str(error.text[pos_rod:pos_rod+8]) lock = str(error.text[pos_lock:pos_lock+3]) buff = str(error.text[pos_buff:pos_buff+3]) if key == rod and doLock and lock != '0xf': times[lock].append(error.sb_total_time) leg.Clear() mg = TMultiGraph() for e in error_bits: errs = [] for i,x in enumerate(times['0x'+e]): errs.append(i+0.0) errs.append(errs[-1]) #times['0x'+e].append(1800.0) times['0x'+e].append(results[-1].sb_total_time) gr = TGraph(len(times['0x'+e]), array(times['0x'+e]), array(errs)) gr.SetMarkerSize(0.7) if bin(int('0x'+e, 16))[2:].zfill(4) == '0111': leg.AddEntry(gr,'GOL 3',"lp"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1011': leg.AddEntry(gr,'GOL 2',"lp"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1101': leg.AddEntry(gr,'GOL 1',"lp"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1110': leg.AddEntry(gr,'GOL 0',"lp"); else: leg.AddEntry(gr,bin(int('0x'+e, 16))[2:].zfill(4),"lp"); mg.Add(gr,"pl"); mg.SetTitle("; Hours of stable beams; # of rocketio io lock errors"); mg.Draw("PMC PLC a"); R15.Draw() R16.Draw() R17.Draw() #TS1.Draw() #TS2.Draw() AtlasStyle.ATLAS_LABEL(0.19,.88, 1, "Internal") leg.Draw() leg2.Draw() AtlasStyle.myText(0.4, 0.88, kBlack, "ROD: " + key) leg.SetMargin(0.5) gPad.Modified() mg.GetXaxis().SetLimits(0,results[-1].sb_total_time) mg.SetMinimum(0.) mg.SetMaximum(val+1) c2.Update() c2.Print("plots/time_"+key+".pdf") c2.Clear()
def make_minute_plots(buff_dict, lock_dict, results): hl0 = TH1F('hl0','hl0', 50, 0, 1) hl4 = TH1F('hl4','hl4', 50, 0, 1) hl8 = TH1F('hl8','hl8', 50, 0, 1) hl8.GetXaxis().SetTitle("Fraction of fill length") hl8.GetYaxis().SetTitle("Arbitrary units/ 0.02") hb0 = TH1F('hb0','h0', 50, 0, 1) hb4 = TH1F('hb4','h4', 50, 0, 1) hb8 = TH1F('hb8','h8', 50, 0, 1) hb8.GetXaxis().SetTitle("Fraction of fill length") hb8.GetYaxis().SetTitle("Arbitrary units/ 0.02") change_colors_min(hl0, hl4, hl8) change_colors_min(hb0, hb4, hb8) h0_lock_beg = [] h0_lock_end = [] h4_lock_beg = [] h4_lock_end = [] h8_lock_beg = [] h8_lock_end = [] h0_buff_beg = [] h0_buff_end = [] h4_buff_beg = [] h4_buff_end = [] h8_buff_beg = [] h8_buff_end = [] for r in lock_dict: temp0 = TH1F('htl0'+r,'htl0'+r, 80, 0, 240) temp1 = TH1F('htl1'+r,'htl1'+r, 80, 0, 240) temp2 = TH1F('htl2'+r,'htl2'+r, 96, 0, 480) temp3 = TH1F('htl3'+r,'htl3'+r, 96, 0, 480) temp4 = TH1F('htl4'+r,'htl4'+r, 180,0, 900) temp5 = TH1F('htl5'+r,'htl5'+r, 180,0, 900) temp0.SetLineWidth(0) temp0.SetFillStyle(1001) temp1.SetLineWidth(0) temp1.SetFillStyle(1001) temp2.SetLineWidth(0) temp2.SetFillStyle(1001) temp3.SetLineWidth(0) temp3.SetFillStyle(1001) temp4.SetLineWidth(0) temp4.SetFillStyle(1001) temp5.SetLineWidth(0) temp5.SetFillStyle(1001) h0_lock_beg.append(temp0) h0_lock_end.append(temp1) h0_lock_beg[-1].SetDirectory(0) h0_lock_end[-1].SetDirectory(0) h4_lock_beg.append(temp2) h4_lock_end.append(temp3) h4_lock_beg[-1].SetDirectory(0) h4_lock_end[-1].SetDirectory(0) h8_lock_beg.append(temp4) h8_lock_end.append(temp5) h8_lock_beg[-1].SetDirectory(0) h8_lock_end[-1].SetDirectory(0) for r in buff_dict: temp0 = TH1F('htb0'+r,'htb0'+r, 80, 0, 240) temp1 = TH1F('htb1'+r,'htb1'+r, 80, 0, 240) temp2 = TH1F('htb2'+r,'htb2'+r, 96, 0, 480) temp3 = TH1F('htb3'+r,'htb3'+r, 96, 0, 480) temp4 = TH1F('htb4'+r,'htb4'+r, 180,0, 900) temp5 = TH1F('htb5'+r,'htb5'+r, 180,0, 900) temp0.SetLineWidth(0) temp0.SetFillStyle(1001) temp1.SetLineWidth(0) temp1.SetFillStyle(1001) temp2.SetLineWidth(0) temp2.SetFillStyle(1001) temp3.SetLineWidth(0) temp3.SetFillStyle(1001) temp4.SetLineWidth(0) temp4.SetFillStyle(1001) temp5.SetLineWidth(0) temp5.SetFillStyle(1001) h0_buff_beg.append(temp0) h0_buff_end.append(temp1) h0_buff_beg[-1].SetDirectory(0) h0_buff_end[-1].SetDirectory(0) h4_buff_beg.append(temp2) h4_buff_end.append(temp3) h4_buff_beg[-1].SetDirectory(0) h4_buff_end[-1].SetDirectory(0) h8_buff_beg.append(temp4) h8_buff_end.append(temp5) h8_buff_beg[-1].SetDirectory(0) h8_buff_end[-1].SetDirectory(0) for error in results: pos_rod = error.text.find("ROD") + 4 pos_lock = error.text.find("Lock") + 15 pos_buff = error.text.find("buffer") + 17 if error.msgID == 'TRT::ROD05Module': rod = '0x'+str(error.text[pos_rod:pos_rod+6]) else: rod = str(error.text[pos_rod:pos_rod+8]) lock = str(error.text[pos_lock:pos_lock+3]) buff = str(error.text[pos_buff:pos_buff+3]) for i,key in enumerate(lock_dict): if key == rod and lock != '0xf' and error.sb_length > 0.5 and error.sb == 1: frac = error.sb_time_run/error.sb_length if error.sb_length < 4: hl0.Fill(frac) h0_lock_beg[i].Fill(error.sb_time_run*60) h0_lock_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) elif error.sb_length < 8: hl4.Fill(frac) h4_lock_beg[i].Fill(error.sb_time_run*60) h4_lock_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) else: hl8.Fill(frac) h8_lock_beg[i].Fill(error.sb_time_run*60) h8_lock_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) for i,key2 in enumerate(buff_dict): if key2 == rod and buff != '0xf' and error.sb_length > 0.5 and error.sb == 1: frac = error.sb_time_run/error.sb_length if error.sb_length < 4: hb0.Fill(frac) h0_buff_beg[i].Fill(error.sb_time_run*60) h0_buff_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) elif error.sb_length < 8: hb4.Fill(frac) h4_buff_beg[i].Fill(error.sb_time_run*60) h4_buff_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) else: hb8.Fill(frac) h8_buff_beg[i].Fill(error.sb_time_run*60) h8_buff_end[i].Fill(error.sb_length*60 - error.sb_time_run*60) leg3 = TLegend(0.23,0.85,0.45,0.75) leg3.SetLineColor(0) leg3.SetFillStyle(0) leg3.SetShadowColor(0) leg3.SetBorderSize(0) leg3.AddEntry(hl0, "Fill Length: 0-4 hours", "lf") leg3.AddEntry(hl4, "Fill Length: 4-8 hours", "lf") leg3.AddEntry(hl8, "Fill Length: 8+ hours", "lf") c3 = TCanvas( 'c3', 'c3', 1000, 600) hl8.DrawNormalized("HIST",1) hl0.DrawNormalized("HISTSAME",1) hl4.DrawNormalized("HISTSAME",1) AtlasStyle.ATLAS_LABEL(0.24,.88, 1, "Internal") leg3.Draw() AtlasStyle.myText(0.5, 0.88, kBlack, "All RODs: lock errors") c3.Update() c3.Print("plots/frac_all_lock.pdf") c3.Clear() hb8.DrawNormalized("HIST",1) hb0.DrawNormalized("HISTSAME",1) hb4.DrawNormalized("HISTSAME",1) AtlasStyle.ATLAS_LABEL(0.24,.88, 1, "Internal") leg3.Draw() AtlasStyle.myText(0.5, 0.88, kBlack, "All RODs: buffer errors") c3.Update() c3.Print("plots/frac_all_buff.pdf") print_single_min(h0_lock_beg, h0_lock_end, "min_lock_0_4", "Lock errors, 0-4 hour fills") print_single_min(h4_lock_beg, h4_lock_end, "min_lock_4_8", "Lock errors, 4-8 hour fills") print_single_min(h8_lock_beg, h8_lock_end, "min_lock_8", "Lock errors, 8+ hour fills") print_single_min(h0_buff_beg, h0_buff_end, "min_buff_0_4", "Buffer errors, 0-4 hour fills") print_single_min(h4_buff_beg, h4_buff_end, "min_buff_4_8", "Buffer errors, 4-8 hour fills") print_single_min(h8_buff_beg, h8_buff_end, "min_buff_8", "Buffer errors, 8+ hour fills")
def print_single_min(vec_beg, vec_end, name, text): total_beg = vec_beg[0].Clone() c = 0 for v in vec_beg: if c != 0: total_beg.Add(vec_beg[c]) c = c + 1 norm = 1/total_beg.Integral() total_beg.Scale(norm) if 'buff' in name: legb = TLegend(0.25,0.93,0.90,0.60) legb.SetNColumns(5) else: legb = TLegend(0.60,0.80,0.90,0.60) legb.SetNColumns(2) legb.SetLineColor(0) legb.SetFillStyle(0) legb.SetShadowColor(0) legb.SetBorderSize(0) if 'buff' in name: lege = TLegend(0.25,0.93,0.90,0.60) lege.SetNColumns(5) else: lege = TLegend(0.22,0.90,0.52,0.70) lege.SetNColumns(2) lege.SetLineColor(0) lege.SetFillStyle(0) lege.SetShadowColor(0) lege.SetBorderSize(0) #vec_beg = sorted(vec_beg, key=lambda x: x.Integral(), reverse=True) stack1 = THStack("stack1","stack1") for v in vec_beg: if v.Integral() != 0: v.Scale(norm) legb.AddEntry(v, v.GetTitle()[4:], "f") stack1.Add(v) total_end = vec_end[0].Clone() c = 0 for v in vec_end: if c != 0: total_end.Add(vec_end[c]) c = c + 1 norm = 1/total_end.Integral() total_end.Scale(norm) stack2 = THStack("stack2","stack2") for v in vec_end: if v.Integral() != 0: v.Scale(norm) lege.AddEntry(v, v.GetTitle()[4:], "f") stack2.Add(v) total_beg.SetLineColor(kRed) total_beg.SetLineWidth(1) total_end.SetLineColor(kRed) total_end.SetLineWidth(1) total_beg.GetXaxis().SetTitle("Minutes after stable beams declared [min]") total_end.GetXaxis().SetTitle("Minutes before stable beams ended [min]") if '0_4' in name: total_beg.GetYaxis().SetTitle("Arbitrary units / 3 mins") total_end.GetYaxis().SetTitle("Arbitrary units / 3 mins") else: total_beg.GetYaxis().SetTitle("Arbitrary units / 5 mins") total_end.GetYaxis().SetTitle("Arbitrary units / 5 mins") c4 = TCanvas( 'c4', 'c4',1000, 1200) c4.Divide(1,2) c4.cd(1) total_beg.Draw("HIST") stack1.Draw("PFC PLC SAME HIST") total_beg.Draw("SAMEHIST") AtlasStyle.ATLAS_LABEL(0.18,.96, 1, "Internal") AtlasStyle.myText(0.40, 0.96, kBlack, text) legb.Draw() c4.cd(2) total_end.Draw("HIST") stack2.Draw("PFC PLC SAME HIST") total_end.Draw("SAMEHIST") AtlasStyle.ATLAS_LABEL(0.18,.96, 1, "Internal") AtlasStyle.myText(0.40, 0.96, kBlack, text) lege.Draw() c4.Update() c4.Print("plots/"+name+".pdf")
def draw_cross_section_limit(tree, mass_g, flavor_of_sample='MET_TLJets'): AtlasStyle.SetAtlasStyle() entries = tree.GetEntries() current_delta_mass = 0 upper_limits = [] dM = [] index = -1 for entry in range(entries): tree.GetEntry(entry) if tree.mGluino == mass_g: if current_delta_mass != tree.deltaM: print('*** {0}, {1}'.format(tree.mGluino, tree.deltaM)) upper_limits.append(TGraphErrors()) dM.append(int(tree.deltaM)) index += 1 point = 0 current_delta_mass = tree.deltaM #if current_delta_mass < 100: # continue upper_limits[index].SetPoint(point, tree.ctau * 1e3, tree.xsUL) upper_limits[index].SetPointError(point, 0, tree.xsUL*tree.effRelStatErr+tree.xsUL*tree.effRelSystErr) point += 1 print(tree.ctau, tree.xsUL) canvas = TCanvas('c', 'c', 1000, 800) canvas.SetLogx() canvas.SetLogy() #upper_limits[0].SetMinimum(0.8) #upper_limits[0].SetMinimum(0.05) #upper_limits[0].SetMaximum(30000) #upper_limits[0].GetXaxis().SetRangeUser(0.9, 310) #upper_limits[0].GetXaxis().SetTitle('c#tau [mm]') #upper_limits[0].GetYaxis().SetTitle('Cross Section [fb]') #upper_limits[0].Draw('A3') #if mass_g == 1400: # upper_limits[1].RemovePoint(0) #upper_limits[0].RemovePoint(2) #upper_limits[1].RemovePoint(0) h_xs = TH1F('xs', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310) h_xs_line = TH1F('xs_line', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310) print(mc.mass_xs_err[mass_g]['xs'] * 1e3) for bin in range(1, 1000+1): h_xs.SetBinContent(bin, mc.mass_xs_err[mass_g]['xs'] * 1e3) h_xs_line.SetBinContent(bin, mc.mass_xs_err[mass_g]['xs'] * 1e3) h_xs.SetBinError(bin, mc.mass_xs_err[mass_g]['xs'] * 1e3 * mc.mass_xs_err[mass_g]['xs_err'] * 0.01) h_xs.SetMarkerSize(0) h_xs.SetFillStyle(3001) h_xs.SetFillColor(kGray+2) #h_xs.SetMinimum(0.8) h_xs.SetMinimum(0.05) h_xs.SetMaximum(30000) #h_xs.Draw('same,e2') h_xs.Draw('e2') h_xs_line.SetLineColor(kGray+3) h_xs_line.SetLineStyle(2) h_xs_line.Draw('same') legend = TLegend(0.60, 0.75, 0.83, 0.90) for ii, upper_limit in enumerate(upper_limits): #upper_limit.RemovePoint(0) upper_limit.SetMarkerSize(0) upper_limit.SetFillStyle(3001) index = ii if dM[ii] == 130: index = 1 elif dM[ii] == 80: index = 2 continue elif dM[ii] == 50: index = 3 elif dM[ii] == 30: index = 4 upper_limit.SetFillColor(BasicConfig.colors[index+1]) upper_limit.SetLineColor(BasicConfig.colors[index+1]) upper_limit.Draw('3,same') #upper_limit.Draw('c,same') #if dM[ii] > 100: # #legend.AddEntry(upper_limit, 'M_{#tilde{g}} = '+str(mass_g)+' GeV, #DeltaM = '+str(dM[ii])+' GeV', 'lf') # legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf') legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf') utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.87, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, kBlack, '#sqrt{s} = 13 TeV, #int L dt = 30 fb^{-1}', 0.035) AtlasStyle.myText(0.20, 0.73, kBlack, 'Split-SUSY Model, M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.032) #AtlasStyle.myText(0.20, 0.67, kBlack, 'M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.035) utils.save_as(canvas, BasicConfig.plotdir + 'xs_limit_mGluino' + str(mass_g) + flavor_of_sample)
def plot1D( Hists, SampleTypes, SampleNames ): histName = '_'.join(Hists[0].GetName().split('_')[1:] ) plotRatio = args.plotRatio ## Setup Canvas ## c0 = ROOT.TCanvas(histName) logx = False logy = False # TODO need a logXList of variables for choosing this. Get from a config file? ## #extraText=[] ## #logXList = [ "mjj", "m3j", "chi", "m12", "m13", "m23" ] # madd all two-jet mass plots have logX ## logXList = [ "chi" ] # madd all two-jet mass plots have logX ## for v in logXList: ## if v == hName: logx = True ### elif v in hName and "for_mjj" in hName : logx = True ## elif v in hName and not "__" in hName: logx = True ## if logx: hists[i].GetYaxis().SetMoreLogLabels(1) ## ## if logx: ## c0.SetLogx() ## if (plotRatio): ## pad1.SetLogx() ## pad2.SetLogx() ## else: ## c0.SetLogx(0) ## if (plotRatio): ## pad1.SetLogx(0) ## pad2.SetLogx(0) setMaximum(Hists) if plotRatio: pad1, pad2, zeroLine, oneLine = getRatioObjects(c0, logx, logy) pad1.cd() leg = configureLegend(SampleTypes, Hists, SampleNames) ### Configure draw string ### for iH, hist in enumerate(Hists): drawString = "" if iH != 0: drawString += 'same' drawString += 'hist' #drawString += 'histe' if not "data" == SampleTypes[iH]: #if args.stackBkg: # drawString += 'fe' #else: drawString += '' else: drawString += 'ep' print drawString Hists[iH].Draw( drawString ) ## Setup Ratio Plots if plotRatio: ratioHists = [] #Ratio histograms iRatioHist = SampleTypes.index(args.ratioWRT) for iHist, hist in enumerate(Hists): if iHist == iRatioHist: continue ## get relevant hists ## tmpRatioHist = Hists[iRatioHist].Clone( hist.GetName()+'_ratio' ) tmpHist = hist.Clone( 'tmp' ) ## create ratio ## if type(tmpHist) == ROOT.THStack: tmpStackHist = getCombinedStack( tmpHist ) for iStack, stackHist in enumerate(tmpHist.GetHists() ): if iStack == 0: tmpStackHist = stackHist.Clone('tmp2') else: tmpStackHist.Add( stackHist ) tmpRatioHist.Add( tmpStackHist, -1. ) tmpRatioHist.Divide( tmpStackHist ) else: tmpRatioHist.Add( tmpHist, -1. ) tmpRatioHist.Divide( tmpHist ) # flip it so excess is still positive on ratio if args.ratioWRT == 'bkg' or args.ratioWRT == 'stack': tmpRatioHist.Scale( -1.0 ) ## If ratio value is 0 then there should be no ratio drawn for iBin in range(1, tmpRatioHist.GetNbinsX()+1): if Hists[iRatioHist].GetBinContent(iBin) == 0: tmpRatioHist.SetBinContent(iBin, 0) tmpRatioHist.SetBinError(iBin, 0) configureRatioHist(tmpHist, tmpRatioHist) ratioHists.append( tmpRatioHist ) ## Draw Ratio Plots if plotRatio: pad2.cd() for iHist, ratioHist in enumerate(ratioHists): ratioHists[iHist].SetStats(0) if iHist == 0: ratioHists[iHist].DrawCopy("p") ratioHists[iHist].SetMarkerSize(0) ratioHists[iHist].DrawCopy("same e0") else: ratioHists[iHist].Draw( "same fhist" ) zeroLine.Draw("same") c0.cd() leg.Draw("same") AtlasStyle.ATLAS_LABEL(0.20,0.88, 1, "Internal") sqrtSLumiText = getSqrtSLumiText( args.lumi ) AtlasStyle.myText(0.20,0.82,1, sqrtSLumiText) if len(args.plotText)>0: AtlasStyle.myText(0.20,0.76,1, args.plotText) if any( extraTextString.split(',')[0] in histName for extraTextString in extraText ): extraTextString = [extraTextString for extraTextString in extraText if extraTextString.split(',')[0] in histName][0] if len(args.plotText)>0: AtlasStyle.myText(0.20,0.64, 1,extraTextString.split(',')[1]) else: AtlasStyle.myText(0.20,0.75, 1,extraTextString.split(',')[1]) c0.Print( args.outDir + "/" + args.outputTag + "_" + histName + args.outputVersion + ".png","png") #,"png") ## Draw y-log plots ## if plotRatio: pad1.SetLogy() pad1.cd() else: c0.SetLogy() setMaximum(Hists, True) ### Redraw using new logY maximum ### for iH, hist in enumerate(Hists): drawString = "" if iH != 0: drawString += 'same' drawString += 'histe' if not "data" == SampleTypes[iH]: drawString += 'fe' else: drawString += 'ep' Hists[iH].Draw( drawString ) c0.Print( args.outDir + "/" + args.outputTag + "_" + histName + args.outputVersion + "_logY.png","png") #,"png") ## TODO Draw log versions? if plotRatio: pad1.Delete() pad2.Delete() # c0.Delete() return
def plotFiles(filenames, variables, key, weight_plots=False, weight_plots_tx=True, plot_dict={}, tex_dict={}): # plot a bunch of files and get stats of variables ROOT.gROOT.SetBatch(True) atlas.SetAtlasStyle() print filenames # check how many cv folds are represented in this list of files cv_folds = filter( lambda x: x.find('cv') != -1 and x.endswith('root') and x.find('train') != -1, filenames) # get the cv_xxx name and only return unique values cv_nums = list( set( map(lambda x: 'cv_' + x.split('_')[-1].replace('.root', ''), cv_folds))) print cv_nums # create a dictionary for the stats of each cv fold stats = {} # create a dict for the event counts of each cv fold event_counts = {} for c in cv_nums: stats[c] = {'Train': {}, 'Valid': {}} event_counts[c] = {'Train': {}, 'Valid': {}} # dict for the stats of the full dataset stats['Full'] = {} event_counts['Full'] = {} # add this to the end of the filenames to differentiate between weighted and not weighted weight_id = '_weighted' if weight_plots or weight_plots_tx else '' for i, f in enumerate(filenames): pt4_16 = False pt8_12 = False if f.find('400_1600') != -1 or f.find('4_16') != -1: pt4_16 = True elif f.find('800_1200') != -1 or f.find('8_12') != -1: pt8_12 = True # is this a test or train file? file_type = "Full" if f.lower().find('train') != -1: file_type = "Train" elif f.lower().find('test') != -1: file_type = 'Valid' # what cv split is it? look for cv_xyz.root cv_num = '' if file_type != 'Full': cv_num = 'cv_' + f.split('_')[-1].replace('.root', '') # open the file and get the tree f_open = ROOT.TFile.Open('folds/' + f) tree = f_open.Get('outputTree') leg = ROOT.TLegend(0.7, 0.60, 0.9, 0.50) leg.SetFillColor(ROOT.kWhite) c = ROOT.TCanvas(f) total_events = tree.GetEntries() signal_events = tree.GetEntries("label==1") bkg_events = tree.GetEntries("label==0") # this is the full dataset if cv_num != '': event_counts[cv_num][ file_type] = '{0:15} {1:10} {2:14}{3:10}'.format( file_type + ' ' + cv_num, str(signal_events), str(bkg_events), str(total_events)) else: # this is a cv split event_counts['Full'] = '{0:15} {1:10} {2:14}{3:10}'.format( file_type, str(signal_events), str(bkg_events), str(total_events)) # create histograms for each variable hists = {} for v in variables: # set up the names for the different histograms ispt = False if v.find('pt') != -1: ispt = True hist_full_name = v hist_sig_name = 'sig_' + v hist_bkg_name = 'bkg_' + v # first get the full histogram to get an idea of the combined mean and rms if ispt: tree.Draw(v + '/1000>>' + v) else: tree.Draw(v + '>>' + v) # pull from the global space hist_full = ROOT.gDirectory.Get(hist_full_name).Clone() mean = '{0:.4f}'.format(float(hist_full.GetMean())) std = '{0:.4f}'.format(float(hist_full.GetRMS())) # for some of the variables the labels overlap with the distributions. Visually inspecting this it doesn't # look like there is an easy fix. I think maybe the best is to rebook the histogram and change the limits to # something higher on the x axis.... # eec c2 # # set up the variable expression that gets used in the Draw function if not ispt: varexp = v + ">>" + hist_sig_name else: varexp = v + "/1000>>" + hist_sig_name # cut string to select signal only. An additional string can be added here to apply the weights. cutstring = '(label==1)' #*(weight)' #if weight_plots: #cutstring += ('*atan(1/weight)') cutstring += '*(weight)' # create the signal histogram and retrieve it tree.Draw(varexp, cutstring) mult = 1.0 xmax = -1 addBins = 0 if v.find('eec_c2_1') != -1 or v.find('mu12') != -1 or v.find( 'planarflow') != -1 or v.find( 'tauwta2tauwta1') != -1 or v.find( 'zcut12') != -1 or ispt: hist_sig_tmp = ROOT.gDirectory.Get(hist_sig_name).Clone() xmax = hist_sig_tmp.GetXaxis().GetXmax() width = float(hist_sig_tmp.GetXaxis().GetBinWidth(1)) # tauwta21 and planarflow need more than 1.2... if v.find('tauwta2tauwta1') != -1: mult = 1.65 elif v.find('planarflow') != -1: mult = 1.9 elif ispt and pt4_16: mult = 1.35 else: mult = 1.2 addBins = float((xmax * mult - xmax)) / width hist_sig = ROOT.TH1F('hist_sig', 'hist_sig', int(hist_sig_tmp.GetNbinsX() + addBins), hist_sig_tmp.GetXaxis().GetXmin(), xmax * mult) # how many bins? :( #fill it for n in xrange(1, hist_sig_tmp.GetNbinsX() + 1): hist_sig.SetBinContent(n, hist_sig_tmp.GetBinContent(n)) else: hist_sig = ROOT.gDirectory.Get(hist_sig_name).Clone() # stats sig_mean = '{0:.4f}'.format(float(hist_sig.GetMean())) sig_std = '{0:.4f}'.format(float(hist_sig.GetRMS())) # normalise if hist_sig.Integral() != 0: hist_sig.Scale(1 / hist_sig.Integral()) # set some drawing options and titles hist_sig.SetLineColor(2) hist_sig.SetTitle('Signal') hist_sig.GetXaxis().SetTitle(v) # now get the background histogram cutstring = cutstring.replace('==1', '==0') #cutstring += ('*(weight)') #if weight_plots_tx: # cutstring = cutstring.replace('*weight','*atan(1/weight)') tree.Draw(varexp.replace('sig', 'bkg'), cutstring) if v.find('eec_c2_1') != -1 or v.find('mu12') != -1 or v.find( 'planarflow') != -1 or v.find( 'tauwta2tauwta1') != -1 or v.find( 'zcut12') != -1 or ispt: hist_bkg_tmp = ROOT.gDirectory.Get( hist_bkg_name).Clone() # pull from global hist_bkg = ROOT.TH1F('hist_bkg', 'hist_bkg', int(hist_sig.GetNbinsX()), hist_sig.GetXaxis().GetXmin(), xmax * mult) for n in xrange(1, hist_bkg_tmp.GetNbinsX() + 1): hist_bkg.SetBinContent(n, hist_bkg_tmp.GetBinContent(n)) else: hist_bkg = ROOT.gDirectory.Get( hist_bkg_name).Clone() # pull from global # stats bkg_mean = '{0:.4f}'.format(float(hist_bkg.GetMean())) bkg_std = '{0:.4f}'.format(float(hist_bkg.GetRMS())) # normalise if hist_bkg.Integral() != 0: hist_bkg.Scale(1 / hist_bkg.Integral()) # drawing options and titles hist_bkg.SetLineColor(4) hist_bkg.SetTitle('Background') hist_sig.GetXaxis().SetTitle(v) leg.Clear() # add the legend entries leg.AddEntry(hist_sig, 'Signal', 'l') leg.AddEntry(hist_bkg, 'Background', 'l') # find the maximum for when we draw them together on a single canvas sig_max = hist_sig.GetMaximum() bkg_max = hist_bkg.GetMaximum() max_val = max(sig_max, bkg_max) hist_sig.SetMaximum(max_val * 1.1) hist_bkg.SetMaximum(max_val * 1.1) if v.strip() in plot_dict.keys(): hist_sig.GetXaxis().SetTitle(plot_dict[v]) hist_bkg.GetXaxis().SetTitle(plot_dict[v]) fnum = 'Full' if cv_num == '' else cv_num hist_sig.SetTitle('Fold ' + fnum + ': ' + plot_dict[v]) hist_bkg.SetTitle('Fold ' + fnum + ': ' + plot_dict[v]) hist_sig.Draw() hist_bkg.Draw('same') leg.Draw('same') # add the grooming algorithm too galg = ROOT.TLatex() galg.SetNDC() galg.SetTextFont(42) galg.SetTextSize(0.03) galg.SetTextColor(ROOT.kBlack) galg.DrawLatex( 0.7, 0.71, "#splitline{anti-k_{t} R=1.0 jets}{#splitline{Trimmed}{f_{cut}=5%,R_{sub}=0.2}}" ) scl = ROOT.TLatex() scl.SetNDC() scl.SetTextFont(42) scl.SetTextSize(0.03) scl.SetTextColor(ROOT.kBlack) scl.DrawLatex(0.7, 0.61, "Standardised") e = ROOT.TLatex() e.SetNDC() e.SetTextFont(42) e.SetTextSize(0.035) e.SetTextColor(ROOT.kBlack) e.DrawLatex(0.7, 0.88, "#sqrt{s}=13 TeV") m = ROOT.TLatex() m.SetNDC() m.SetTextFont(42) m.SetTextSize(0.035) m.SetTextColor(ROOT.kBlack) m.DrawLatex(0.7, 0.78, "68% mass window") # need to put the pt range on here.... # okay, so this is nasty and poor form, but I'm super stressed and running out of time # to finish my thesis, so whatever. The algorithm name should have the pt range in it in gev # At this point things are narrowed down to the point where we are only considering two # pt ranges: 400-1600 GeV or 800-1200 GeV, so just look for those. ptrange = '' if pt4_16: ptrange = '400<p_{T}^{Truth}<1600 GeV' elif pt8_12: ptrange = '800<p_{T}^{Truth}<1200 GeV' if ptrange != '': # draw it ptl = ROOT.TLatex() ptl.SetNDC() ptl.SetTextFont(42) ptl.SetTextSize(0.035) ptl.SetTextColor(ROOT.kBlack) ptl.DrawLatex(0.7, 0.83, ptrange) #"Internal Simulation"); if cv_num == '': c.SaveAs('fold_plots/' + key + '_Full_' + v + weight_id + '.pdf') else: c.SaveAs('fold_plots/' + key + '_Full_' + cv_num + '_' + v + weight_id + '.pdf') # write the means and std to the stats file result = '{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'.format( file_type + ' ' + cv_num, str(mean), str(std), str(sig_mean), str(sig_std), str(bkg_mean), str(bkg_std)) # check that this variable has a dictionary entry #if v not in stats[cv_num][file_type].keys(): #stats[cv_num][file_type][v] = {} if cv_num != '': # full dataset stats[cv_num][file_type][v] = result else: # cv split stats['Full'][v] = result # now all of the stats can be written to file! # first write the combined stats combined_stats = open( 'fold_stats/combined_stats_' + key + weight_id + '.txt', 'w') combined_stats.write('{0:15} {1:10} {2:14}{3:10}'.format( 'Sample', 'Signal', 'Background', 'Total') + '\n') print event_counts['Full'] combined_stats.write(str(event_counts['Full']) + '\n') # write out all of the cv splits for cv in cv_nums: for f in ['Train', 'Valid']: combined_stats.write(str(event_counts[cv][f]) + '\n') combined_stats.write('\n' + '\n') # now start doing the variables combined_stats.write('\n{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'. format('Variable', 'Mean', 'Std', 'Mean Sig', 'Std Sig', 'Mean Bkg', 'Std Bkg') + '\n\n') print 'stats full keys', stats['Full'].keys() print 'stats keys', stats.keys() print 'stats train keys', stats['cv_000']['Train'].keys() for v in variables: print v v = v.strip() if not v.strip() in stats['Full'].keys(): continue if v.strip() in tex_dict.keys(): combined_stats.write(tex_dict[v] + '\n') else: combined_stats.write(v + '\n') combined_stats.write(stats['Full'][v] + '\n') for c in cv_nums: combined_stats.write(str(stats[c]['Train'][v]) + '\n') combined_stats.write(str(stats[c]['Valid'][v]) + '\n') combined_stats.write('\n') combined_stats.close() # write the stats for each cv fold for cv in cv_nums: print cv stats_file = open('fold_stats/' + key + '_' + cv + weight_id + '.txt', 'w') stats_file.write('{0:15} {1:10} {2:14}{3:10}'.format( 'Sample', 'Signal', 'Background', 'Total') + '\n') stats_file.write(str(event_counts['Full']) + '\n') stats_file.write(str(event_counts[cv]['Train']) + '\n') stats_file.write(str(event_counts[cv]['Valid']) + '\n') stats_file.write('\n{0:15}: {1:10} {2:10} {3:10} {4:10} {5:10} {6:10}'. format('Variable', 'Mean', 'Std', 'Mean Sig', 'Std Sig', 'Mean Bkg', 'Std Bkg') + '\n\n') # now write each variable for v in variables: if v.strip() in tex_dict.keys(): stats_file.write(tex_dict[v] + '\n') else: stats_file.write(v + '\n') stats_file.write(stats['Full'][v] + '\n') stats_file.write(stats[cv]['Train'][v] + '\n') stats_file.write(stats[cv]['Valid'][v] + '\n') stats_file.write('\n') stats_file.close()
def createPlot(fname, plotType = 'discriminant', parameter_file = '', pt_range=[0,0], bdt=False, extraFiles=''): # values for the plotting of labels and stuff labelx = labelx_dnn = 0.45 labely = labely_dnn = 0.88 labelx_bdt = 0.2 labely_bdt = 0.88 legendx1 = legendx1_dnn = 0.65 legendy1 = legendy1_dnn = 0.6 legendx2 = legendx2_dnn = 0.9 legendy2 = legendy2_dnn = 0.75 xlow = 0.0 xhigh = 1.0 legendx1_bdt = 0.68 legendy1_bdt = 0.75 legendx2_bdt = 0.93 legendy2_bdt = 0.95 paramx = paramx_dnn = 0.4 paramy = paramy_dnn = 0.8 paramx_bdt = 0.2 paramy_bdt = 0.8 if bdt: labelx = labelx_bdt labely = labely_bdt legendx1 = legendx1_bdt legendx2 = legendx2_bdt legendy1 = legendy1_bdt legendy2 = legendy2_bdt paramx = paramx_bdt paramy = paramy_bdt #if plotType != 'discriminant': # legendx1 = 0.65 # legendx2 = 0.9 # legendy1 = 0.68 # legendy2 = 0.88 xtitle = 'Signal Probability' # need to get the tagger id from the file name pos = fname.find('paramID_')+8 pos_end = copy.deepcopy(pos) while fname[pos_end].isdigit(): pos_end+=1 taggerid = fname[pos:pos_end] #print fname, taggerid cv_pos = fname.find('ID',pos_end+1,len(fname))+3 cv = fname[cv_pos] print 'cv:',cv f = rt.TFile.Open(fname,mode)#'UPDATE' if plotType == 'discriminant': distrib = 'Discriminant' combined = 'Discriminant' file_flag = '_disc' else: distrib = 'Decision Function' combined = 'Decision Functions Norm' xtitle = 'Decision Function' file_flag = '_df' xlow = -1 keys = f.GetListOfKeys() has_sigdf = False has_bkgdf = False has_df = False for k in keys: if k.GetName() == 'Signal '+distrib: has_sigdf = True if k.GetName() == 'Background '+distrib: has_bkgdf = True if k.GetName() == combined: has_df = True if not has_sigdf or not has_bkgdf: print 'keys do not exist' return if has_df and mode=='UPDATE': print 'already has the df canvas' return sigdf = f.Get('Signal '+distrib).Clone() sigdf.SetDirectory(0) sigdf.SetLineColor(rt.kRed) sigdf.SetMarkerSize(1) sigdf.SetTitle(xtitle) #sigdf.Rebin(5) sigdf.GetXaxis().SetTitle(xtitle) sigdf.GetYaxis().SetTitle('Normalised Entries') #sigdf.GetXaxis().SetRangeUser(xlow,xhigh) #if sigdf.Integral() != 0.0: # sigdf.Scale(1./sigdf.Integral()) bkgdf = f.Get('Background '+distrib).Clone() bkgdf.SetDirectory(0) bkgdf.SetLineColor(rt.kBlue) bkgdf.SetTitle(xtitle) bkgdf.SetMarkerSize(1) bkgdf.GetXaxis().SetTitle(xtitle) bkgdf.GetYaxis().SetTitle('Normalised Entries') #bkgdf.GetXaxis().SetRangeUser(xlow,xhigh) #if bkgdf.Integral() != 0.0: # bkgdf.Scale(1./bkgdf.Integral()) f.Close() print type(sigdf) # now get the rest of them from the other files!!!! #tlistsig = rt.TList() #tlistbkg = rt.TList() #tlistsig.Add(sigdf) #tlistbkg.Add(bkgdf) # find extra files! if bdt: folds = 10 else: folds = 5 ef = [] for x in range(0,folds): ffff = fname[:cv_pos]+str(x)+fname[cv_pos+1:] ef.append(ffff) print ef print sigdf.Integral() for tf in ef: tmpfile = rt.TFile.Open(tf,mode) sigtmp = tmpfile.Get('Signal ' + distrib).Clone() sigtmp.SetDirectory(0) bkgtmp = tmpfile.Get('Background ' + distrib).Clone() bkgtmp.SetDirectory(0) sigdf.Add(sigtmp) bkgdf.Add(bkgtmp) tmpfile.Close() #ef.close() #sigdf2 = sigdf.Clone() #sigdf2.Reset() #sigdf2.Merge(tlistsig) #bkgdf2 = bkgdf.Clone() #bkgdf2.Reset() #bkgdf2.Merge(tlistbkg) print sigdf.Integral() if plotType != 'discriminant': sigdf.Rebin(4) bkgdf.Rebin(4) else: sigdf.Rebin(2) bkgdf.Rebin(2) if sigdf.Integral() != 0.0: sigdf.Scale(1./sigdf.Integral()) if bkgdf.Integral() != 0.0: bkgdf.Scale(1./bkgdf.Integral()) tc = rt.TCanvas(combined) tc.SetTitle(combined) legend = rt.TLegend(legendx1, legendy1, legendx2, legendy2);legend.SetFillColor(rt.kWhite) legend.SetBorderSize(0) legend.SetFillStyle(0) legend.AddEntry(sigdf, 'Signal') legend.AddEntry(bkgdf, 'Background') max_v = max(sigdf.GetMaximum(), bkgdf.GetMaximum()) sigdf.SetMaximum(max_v*1.4) bkgdf.SetMaximum(max_v*1.4) sigdf.Draw('hist') bkgdf.Draw('histsame') legend.Draw() label = rt.TLatex() label.SetTextSize(0.05) label.DrawLatexNDC(labelx,labely, '#sqrt{s}=13 TeV, '+str(pt_range[0])+'<p_{T}<'+str(pt_range[1])+' (GeV)') if fname.find('AGILE') != -1 or fname.find('DNN') != -1: param_label = gp.dnn(parameter_file) elif fname.find('BDT') != -1: param_label = gp.bdt(parameter_file) print param_label atlas.myText(paramx, paramy, 1, 0.04, param_label[taggerid]) if mode == 'UPDATE': tc.Write() else: tc.SaveAs(fname.replace('.root',file_flag+'.pdf'))
def make_plot_all_rods(error_dict, rod_dict, name): leg = TLegend(0,0,0,0) if 'lock' in name: leg = TLegend(0.18,0.85,0.50,0.55) else: leg = TLegend(0.18,0.85,0.40,0.55) leg.SetLineColor(0) leg.SetFillStyle(0) leg.SetShadowColor(0) leg.SetBorderSize(0) leg.SetNColumns(3) gStyle.SetLegendTextSize(0.045) v_hists = [] #for e,c in zip(error_bits, error_colors): for e in error_bits: h = TH1F('h'+e,'h'+e, len(rod_dict), 0, len(rod_dict)) h.SetFillStyle(1001) h.SetLineWidth(0) v_hists.append(h) v_hists[-1].SetDirectory(0) if bin(int('0x'+e, 16))[2:].zfill(4) == '0111': leg.AddEntry(v_hists[-1],'GOL 3',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1011': leg.AddEntry(v_hists[-1],'GOL 2',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1101': leg.AddEntry(v_hists[-1],'GOL 1',"f"); elif bin(int('0x'+e, 16))[2:].zfill(4) == '1110': leg.AddEntry(v_hists[-1],'GOL 0',"f"); else: leg.AddEntry(v_hists[-1],bin(int('0x'+e, 16))[2:].zfill(4),"f"); h = leg.GetY2()-leg.GetY1(); w = leg.GetX2()-leg.GetX1()*.6; leg.SetMargin(leg.GetNColumns()*h/(leg.GetNRows()*w)) for key,val in error_dict.items(): idx_rod = 0 for i, key2 in enumerate(rod_dict): if key2 == key[:8]: idx_rod = i v_hists[int(key[11:12], 16)].Fill(idx_rod, val) stack = THStack("stack","stack") for hist in v_hists: stack.Add(hist) if 'buff' in name: c1 = TCanvas( 'c1', 'c1', 2000, 500) else: c1 = TCanvas( 'c1', 'c1', 1000, 500) h1 = TH1F('h_1','h_1', len(rod_dict), 0, len(rod_dict)) for i, key in enumerate(rod_dict): h1.GetXaxis().SetBinLabel(i+1,key) h1.SetBinContent(i+1,rod_dict[key]) h1.GetXaxis().LabelsOption("v") h1.GetXaxis().SetTitle("ROD") h1.GetXaxis().SetTitleOffset(2.2) h1.GetYaxis().SetTitle("# of rocketio errors") h1.SetLineColor(kRed) h1.SetLineWidth(1) leg.AddEntry(h1,'total',"l"); c1.SetBottomMargin(0.23) h1.GetXaxis().SetTitle("ROD") h1.Draw("HIST") stack.Draw("PFC PLC SAME HIST") h1.Draw("SAMEHIST") AtlasStyle.ATLAS_LABEL(0.19,.88, 1, "Internal") leg.Draw() c1.Update() c1.Print("plots/"+name +".pdf") c1.Clear()
def getROC( signal, background, label, cut_start=None, cut_end=None, outputName="myROC.root", Rejection="l", omission = []): ROCList = [] markerlist = [21, 8 , 22, 23] print "get roc ", label for ivar in range(len(signal)): s_sort = np.sort( signal[ivar] ) b_sort = np.sort( background[ivar] ) #c_start=(0.0 if cut_start==None else cut_start) #c_end= (1.0 if cut_end==None else cut_end) print s_sort, b_sort for i in range(s_sort.shape[0]): if s_sort[i] == float("Inf"): s_sort[i] = 100000 if s_sort[i] == float("-Inf"): s_sort[i] = -1000000 for i in range(b_sort.shape[0]): if b_sort[i] == float("Inf"): b_sort[i] = 100000 if b_sort[i] == float("-Inf"): b_sort[i] = -1000000 c_start=np.min( (s_sort[0], b_sort[0]) ) c_end= np.max( (s_sort[len(s_sort)-1], b_sort[len(b_sort)-1]) ) if c_start==-float('inf'): c_start = -2*c_end print label[ivar], "min(", s_sort[0], b_sort[0], ")=", c_start print label[ivar], "max(", s_sort[-1], b_sort[-1], ")=", c_end s_eff=[] b_rej=[] n_points = 1000 c_delta = (1.0*c_end - 1.0*c_start) / (1.0*n_points) for i in range(1000): cut = c_start + i*1.0*c_delta s_eff.append( 1.0*np.count_nonzero( s_sort > cut ) / (1.0*len(s_sort)) ) b_count = np.count_nonzero( b_sort > cut ) b_rej.append( (1.0*len(b_sort)) / (1.0 if b_count==0 else (1.0*b_count)) ) ROC = ROOT.TGraph(n_points, array.array('d', s_eff), array.array('d', b_rej)) ROC.SetName("ROC_%i" % (ivar)) ROCList.append(ROC) f = ROOT.TFile(outputName, "update") canvas = ROOT.TCanvas("ROC_Overlay", "ROC_Overlay", 800, 600) canvas.cd() mg = ROOT.TMultiGraph() legend = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) for i in range(len(ROCList)): if i in omission: continue ROC = ROCList[i] ROC.SetLineWidth(3) ROC.SetLineColor(colorlist[i]) ROC.SetMarkerColor(colorlist[i]) ROC.SetMarkerSize(0) ROC.SetMarkerStyle(0) ROC.SetLineStyle(i+1) mg.Add(ROC) if omission == []: legend.AddEntry(ROC, label[i], "lp") f.WriteTObject(ROC, "ROC_"+label[i], "Overwrite") if omission!=[]: legend.AddEntry(ROCList[1], label[1], "lp") legend.AddEntry(ROCList[4], label[4], "lp") legend.AddEntry(ROCList[5], label[5], "lp") legend.AddEntry(ROCList[2], label[2], "lp") mg.Draw("AL") mg.GetXaxis().SetTitle("b-jet efficiency, #varepsilon_{b}") if Rejection == "l": mg.GetYaxis().SetTitle("light-jet rejection, 1/#varepsilon_{l}") if Rejection == "c": mg.GetYaxis().SetTitle("c-jet rejection, 1/#varepsilon_{c}") legend.Draw("same") Atlas.ATLASLabel(0.2, 0.88,0.13, "Simulation Internal",color=1) Atlas.myText(0.2, 0.81 ,color=1, size=0.04,text="#sqrt{s}=13 TeV, t#bar{t}") Atlas.myText(0.2, 0.75 ,color=1, size=0.04,text="p_{T}>20 GeV, |#eta|<2.5") #Atlas.myText(0.2, 0.69 ,color=1, size=0.04,text="Rel21") #canvas.Update() canvas.Draw() f.WriteTObject(canvas, canvas.GetName(), "Overwrite") f.Close() return (ROCList, canvas)
def DrawCorrelation(var, labels, model, varname): f = ROOT.TFile("corr_"+varname+".root", "recreate") #b_hist = ROOT.TH1D("corr_score_b_"+varname, "corr_score_b_"+varname, 15, 0.5, 15.5) #c_hist = ROOT.TH1D("corr_score_c_"+varname, "corr_score_c_"+varname, 15, 0.5, 15.5) #l_hist = ROOT.TH1D("corr_score_l_"+varname, "corr_score_l_"+varname, 15, 0.5, 15.5)# bjet_var = var[ labels[:,0]==5] cjet_var = var[ labels[:,0]==4] ljet_var = var[ labels[:,0]==0] bjet_score = model.pred[labels[:,0]==5] cjet_score = model.pred[labels[:,0]==4] ljet_score = model.pred[labels[:,0]==0] def loop(var, score): print var.shape print score.shape itrk_list = [] corr_list = [] for itrk in range(15): var_thistrk = [] score_thistrk = [] score_thistrk = score[ var[:, itrk] !=0 ] var_thistrk = var[ var[:, itrk] !=0, itrk] #hist.SetBinContent(itrk+1, pearsonr(var_thistrk, score_thistrk)[0] ) print pearsonr(var_thistrk, score_thistrk)[0] itrk_list.append(itrk+1) corr_list.append(pearsonr(var_thistrk, score_thistrk)[0] ) return ROOT.TGraph(15, array.array('d', itrk_list), array.array('d', corr_list)) b_hist= loop(bjet_var, bjet_score) c_hist = loop(cjet_var, cjet_score) l_hist = loop(ljet_var, ljet_score) f.cd() canvas = ROOT.TCanvas(varname, varname, 800, 600) canvas.cd() b_hist.SetLineColor( colorind[0]) b_hist.SetMarkerColor( colorind[0]) b_hist.SetMarkerStyle(20) b_hist.SetMarkerSize(1) b_hist.SetLineWidth( 3) c_hist.SetLineColor( colorind[1]) c_hist.SetMarkerColor( colorind[1]) c_hist.SetLineWidth( 3) c_hist.SetMarkerStyle(21) c_hist.SetMarkerSize(1) l_hist.SetLineColor( colorind[2]) l_hist.SetMarkerColor( colorind[2]) l_hist.SetLineWidth( 3) l_hist.SetMarkerStyle(22) l_hist.SetMarkerSize(1) legend = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) legend.AddEntry(b_hist, "b-jets", "lp") legend.AddEntry(c_hist, "c-jets", "lp") legend.AddEntry(l_hist, "light-jets", "lp") mg = ROOT.TMultiGraph() mg.Add(b_hist) mg.Add(c_hist) mg.Add(l_hist) mg.Draw("APL") mg.GetXaxis().SetTitle("i^{th} track in sequence") mg.GetYaxis().SetTitle("Correlation, #rho(D_{RNN}, "+varname+")") legend.Draw("same") Atlas.ATLASLabel(0.2, 0.88,0.13, "Simulation Internal",color=1) Atlas.myText(0.2, 0.81 ,color=1, size=0.04,text="#sqrt{s}=13 TeV, t#bar{t}") Atlas.myText(0.2, 0.75 ,color=1, size=0.04,text="p_{T}>20 GeV, |#eta|<2.5") #Atlas.myText(0.2, 0.69 ,color=1, size=0.04,text="Rel21") canvas.Draw() canvas.Write()
# # effPt = h_triggers[iT].GetXaxis().GetBinLowEdge( effBin ) leg = ROOT.TLegend(0.6, 0.4, 0.9, 0.9) leg.AddEntry( h_triggers[0], "Data "+trigger, "l") leg.AddEntry( h_triggers[2], "MC", "l") leg.AddEntry( h_triggers[4], "Biased MC", "l") h_triggers[0].Draw() h_triggers[2].Draw("same") h_triggers[4].Draw("same") AtlasStyle.ATLAS_LABEL(0.3,0.34, 1," Internal") AtlasStyle.myText(0.3,0.28,1, "#sqrt{s} = 13 TeV, 3.6 fb^{-1}") # AtlasStyle.myText(0.3,0.22,2, "99.5\% Efficient: %.0f GeV" %(effPt[0]) ) AtlasStyle.myText(0.3,0.22,2, "Data: %.0f, MC: %.0f, MCbiased: %.0f GeV" %(effPt[0], effPt[2], effPt[4]) ) leg.Draw("same") plotName = upDir+'/combinedPlots/'+histName[0]+'_Recoil_'+trigger+'.png' # plotName += h_data_triggers[iT].GetName()+'.png' c1.SaveAs(plotName); c1.Clear(); h_triggers[1].Draw() h_triggers[3].Draw("same") h_triggers[5].Draw("same")
def draw_breakdown(self, mode, use_multiple_models=True): print('***********************') print('draw_breakdown') print(mode) canvas3 = ROOT.TCanvas('canvas3_' + mode['mode'], 'canvas3', 1000, 800) canvas3.SetLeftMargin(0.15) #canvas3.SetLogy() #mat = MaterialVolume.MaterialVolume() #bins = array('f', [0] + mat.region_list) #h_factor = mode['h_factor'] #if mode['mode'] == 'cross_factor': # self.h_factor = ROOT.TH1F('h_value_' + mode['mode'], ';R [mm];' + mode['y_axis'], # len(mat.region_list), bins) #h_value = ROOT.TH1F('h_value_' + mode['mode'], ';R [mm];' + mode['y_axis'], # len(mat.region_list), bins) #h_est = ROOT.TH1F('h_est_' + mode['mode'], '', len(mat.region_list), bins) h_fill = ROOT.TH1F('h_fill_' + mode['mode'], '', len(mat.region_list), bins) for ii in range(len(mat.region_list)): if ii in self.vetoed_region: h_fill.SetBinContent(ii + 1, 99999.) h_fill.SetBinError(ii + 1, 0.) h_factor.SetBinContent(ii+1, 0) h_factor.SetBinError(ii+1, 0) h_factor_large.SetBinContent(ii+1, 0) h_factor_large.SetBinError(ii+1, 0) if not mode['mode'] == 'cross_factor': self.h_obs[mode['mode']].SetBinContent(ii+1, 0) self.h_obs[mode['mode']].SetBinError(ii+1, 0) self.h_est[mode['mode']].SetBinContent(ii+1, 0) self.h_est[mode['mode']].SetBinError(ii+1, 0) self.h_est_large[mode['mode']].SetBinContent(ii+1, 0) self.h_est_large[mode['mode']].SetBinError(ii+1, 0) # 8to10 self.h_obs_8to10[mode['mode']].SetBinContent(ii+1, 0) self.h_obs_8to10[mode['mode']].SetBinError(ii+1, 0) self.h_est_8to10[mode['mode']].SetBinContent(ii+1, 0) self.h_est_8to10[mode['mode']].SetBinError(ii+1, 0) self.h_est_large_8to10[mode['mode']].SetBinContent(ii+1, 0) self.h_est_large_8to10[mode['mode']].SetBinError(ii+1, 0) else: pass #h_fill.SetBinContent(ii + 1, -99999.) #h_fill.SetBinError(ii + 1, 0.) #if mode['mode'] == 'cross_factor': #pass #self.h_factor.SetBinContent(ii + 1, self.crossfactor_list[ii]) #self.h_factor.SetBinError(ii + 1, self.error_list[ii]) #self.h_factor.Sumw2() #elif mode['mode'] == '4': ##print(ii, self.obs_list[4][ii]) #h_value.SetBinContent(ii + 1, self.obs_list[4][ii]) #h_value.SetBinError(ii + 1, ROOT.TMath.Sqrt(self.obs_list[4][ii])) ##print(ii, self.est_list[4][ii]) #h_est.SetBinContent(ii + 1, self.est_list[4][ii]) #h_est.SetBinError(ii + 1, ROOT.TMath.Sqrt(self.est_list[4][ii])) #elif mode['mode'] == '5' or mode['mode'] == '6': # h_value.SetBinContent(ii + 1, self.obs_list[5][ii]) # h_value.SetBinError(ii + 1, ROOT.TMath.Sqrt(self.obs_list[5][ii])) #else: # print('invalid mode') h_fill.SetLineWidth(0) h_fill.SetFillColor(ROOT.kPink) h_fill.SetFillStyle(3001) if mode['mode'] == 'cross_factor': #h_factor.SetMaximum(0.0105) #h_factor.SetMaximum(0.0305) #h_factor.SetMaximum(0.905) #h_factor.SetMaximum(90.5) h_factor.SetMaximum(0.004) h_factor.GetYaxis().SetTitleOffset(1.80) h_factor.SetMinimum(0) #h_factor.SetMinimum(0.00002) #h_factor.SetMinimum(0.00002) h_factor.SetLineWidth(2) h_factor.SetLineColor(ROOT.kBlack) h_factor.SetMarkerColor(ROOT.kBlack) h_factor.SetMarkerStyle(20) h_factor.Draw('e') if use_multiple_models: h_factor_large.SetLineWidth(2) h_factor_large.SetLineColor(ROOT.kBlue+1) h_factor_large.SetMarkerColor(ROOT.kBlue+1) h_factor_large.SetMarkerStyle(20) h_factor_large.Draw('e,same') output_factor = ROOT.TFile('output_factor_{}.root'.format(self.m_cut), 'recreate') h_factor.Write() h_fill.Write() else: utils.decorate_histogram(self.h_obs[mode['mode']], ROOT.kGray+3) #self.h_obs[mode['mode']].SetMaximum(self.h_obs[mode['mode']].GetMaximum() * 3.1) #self.h_obs[mode['mode']].SetMaximum(self.h_obs[mode['mode']].GetMaximum() * 200) #self.h_obs[mode['mode']].SetMinimum(2e-2) self.h_obs[mode['mode']].SetMaximum(self.h_obs[mode['mode']].GetMaximum() * 3) self.h_obs[mode['mode']].SetMinimum(0) self.h_obs[mode['mode']].Draw('e') utils.decorate_histogram(self.h_est[mode['mode']], ROOT.kGreen+2, fill_style=3002) self.h_est[mode['mode']].Add(self.h_est_large[mode['mode']]) self.h_est[mode['mode']].SetLineWidth(0) self.h_est[mode['mode']].Draw('e2,same') ## 8to10 #utils.decorate_histogram(self.h_obs_8to10[mode['mode']], ROOT.kGray+3) #self.h_obs_8to10[mode['mode']].SetMaximum(self.h_obs_8to10[mode['mode']].GetMaximum() * 200) #self.h_obs_8to10[mode['mode']].Draw('e') #utils.decorate_histogram(self.h_est_8to10[mode['mode']], ROOT.kGreen+2, fill_style=3002) self.h_est_8to10[mode['mode']].Add(self.h_est_large_8to10[mode['mode']]) #self.h_est[mode['mode']].SetLineWidth(0) #self.h_est[mode['mode']].Draw('e2,same') h_fill.Draw('same') AtlasStyle.ATLASLabel(self.ax, self.ay, 'Work in Progress') AtlasStyle.myText(self.tx, self.ty, ROOT.kBlack, self.beam_condition, 0.038) leg3 = ROOT.TLegend(self.x_min, self.y_min, self.x_max, self.y_max) if mode['mode'] == 'cross_factor': if use_multiple_models: leg3.AddEntry(h_factor, 'Crossing Factor (NoLarge)', 'lep') leg3.AddEntry(h_factor_large, 'Crossing Factor (Large)', 'lep') else: leg3.AddEntry(h_factor, 'Crossing Factor', 'lep') elif mode['mode'] == '4': leg3.AddEntry(self.h_obs[mode['mode']], 'Observed', 'lep') leg3.AddEntry(self.h_est[mode['mode']], 'Predicted', 'epf') elif mode['mode'] == '5' or mode['mode'] == 6: leg3.AddEntry(self.h_obs[mode['mode']], 'Observed (Blinded)', 'lep') leg3.AddEntry(self.h_est[mode['mode']], 'Predicted', 'epf') utils.decorate_legend(leg3) leg3.Draw() directory = BasicConfig.plotdir + 'bg_est/' + str(date.today()) utils.save_as(canvas3, directory + '/dv_mass_fitter_summary_' + mode['mode']) canvas3.Close()
def draw_mass_distributions_and_ratio(self, ntrk, region, h_data, h_model, h_coll): #def draw_mass_distributions_and_ratio(self, ntrk, region, h_data, h_model): canvas2 = ROOT.TCanvas('canvas2', 'canvas2', 1000, 750) # mass distributions pad1 = ROOT.TPad('pad1', 'pad1', 0, 0.35, 1, 1.0) pad2 = ROOT.TPad('pad2', 'pad2', 0, 0.05, 1, 0.35) self.prepare_pads(canvas2, pad1, pad2) pad1.cd() if self.rebin > 1: # h_nocross.Rebin(rebin) #h_nolarge[region].Rebin(self.rebin) #h_large[region].Rebin(self.rebin) #h_data[region].Rebin(self.rebin) #h_data_collimated[region].Rebin(self.rebin) h_model = utils.rebin(h_model, bins_array) h_data = utils.rebin(h_data, bins_array) h_coll = utils.rebin(h_coll, bins_array) self.configure_data_histogram(h_data) h_data.SetMaximum(h_data.GetMaximum() * 8) self.configure_model_histogram(h_model) for bin in range(h_data.GetNbinsX()): if h_data.GetBinContent(bin+1) == 0: h_data.SetBinError(bin+1, 1.) h_data.Draw('e') if self.show_collimated: self.configure_model_histogram(h_coll) h_coll.SetFillStyle(3013) h_coll.SetLineWidth(0) h_coll.SetFillColor(ROOT.kAzure) h_coll.Draw('same,hist') h_model.SetLineWidth(2) h_model.Draw('same,hist') h_data.Draw('same,e') # list_diff3[ipad-1].SetLineWidth(2) AtlasStyle.ATLASLabel(self.ax, self.ay, 'Work in Progress') AtlasStyle.myText(self.tx, self.ty, ROOT.kBlack, self.beam_condition, 0.038) leg = ROOT.TLegend(self.x_min, self.y_min, self.x_max, self.y_max) leg.AddEntry(h_data, str(ntrk) + '-trk vertices Region' + str(region), 'lep') leg.AddEntry(h_model, str(ntrk-1) + '-trk vert + 1 random track', 'f') utils.decorate_legend(leg) leg.Draw() line = ROOT.TLine(self.m_cut, h_data.GetMinimum(), self.m_cut, h_data.GetMaximum() * 0.1) utils.decorate_line(line, ROOT.kGray+1, 5) line.Draw() # Ratio plot pad2.cd() h_ratio = h_data.Clone(str(ntrk) + 'trk_ratio' + str(region)) h_ratio.Sumw2() h_ratio.Divide(h_model) self.decorate_ratio_plot(h_ratio, 0.1, 1.9) h_ratio.Draw('e2p') line2 = ROOT.TLine(self.m_cut, h_ratio.GetMinimum(), self.m_cut, h_ratio.GetMaximum()) utils.decorate_line(line2, ROOT.kGray+1, 5) line2.Draw() directory = BasicConfig.plotdir + 'bg_est/' + str(date.today()) os.system('mkdir -p ' + directory) utils.save_as(canvas2, directory + '/dv_mass_fitter_' + str(ntrk) + 'Trk_Region' + str(region)) canvas2.Close()
def plotNominal(file, f_plotSys, f_addGagik): outDir = file[:-5]+'/' if not os.path.exists(outDir): os.mkdir(outDir) outDir += "plotNominal/" if not os.path.exists(outDir): os.mkdir(outDir) if not os.path.exists(outDir+'/eps'): os.mkdir(outDir+'/eps') AtlasStyle.SetAtlasStyle() gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info inFile = TFile.Open(file, "READ"); keyList = [key.GetName() for key in inFile.GetListOfKeys()] #List of top level objects dirList = [key for key in keyList if "Iteration" in key] #List of all directories #nomDir = [dir for dir in dirList if "NoCorr" in dir] nomDir = [dir for dir in dirList if "Nominal" in dir] if( not len(nomDir) == 1): print "Error, nominal directories are ", nomDir return else: nomDir = inFile.Get( nomDir[0] ) c1 = TCanvas() ##################### Plot All Nominal With Systematic Bands ################################# print "Plotting nominal hists " if(f_plotSys): sysDirNameList = [dir for dir in dirList if not "Nominal" in dir] sysDirList = [] for sysDirName in sysDirNameList: sysDirList.append( inFile.Get(sysDirName) ) ## Combine systematics in types ## #sysTypes = ["MJB_a", "MJB_b", "MJB_ptt", "MJB_pta", "Flavor", "EtaIntercalibration"] sysTypes = ["ZJ", "GJ", "MJB", "Flavor", "EtaIntercalibration", "All"] if "All" in sysTypes: colorOffset = 240./(len(sysTypes)-1) else: colorOffset = 240./len(sysTypes) histList = [key.GetName() for key in nomDir.GetListOfKeys()] for histName in histList: if "prof_" in histName or "ptSlice" in histName: continue nomHist = nomDir.Get( histName ) nomHist.SetName(nomHist.GetName()) if not type(nomHist) == TH1F and not type(nomHist) == TH1D and not type(nomHist) == TGraphErrors: #Can't draw bands if not 1D continue leg = TLegend(0.83, 0.15, 0.99, 0.95) pad1 = TPad("pad1", "", 0, 0, 0.83, 1) pad1.Draw() pad1.cd() leg.AddEntry( nomHist, "Nominal", "lp" ) #nomHist.SetMinimum(0.9) if( "Pt" in histName or "Energy" in histName): nomHist.GetXaxis().SetRange(nomHist.FindFirstBinAbove(0), nomHist.FindLastBinAbove(0)+1 ) nomHist.SetMaximum(1.5*nomHist.GetMaximum()) nomHist.SetMinimum(0.0001) if( "MJB" in histName) : nomHist.GetXaxis().SetRangeUser( 300, 2500 ) nomHist.SetMaximum(1.1) nomHist.SetMinimum(0.9) nomHist.GetXaxis().SetMoreLogLabels(True) # nomHist.SetMarkerSize(.75) if( "recoilPt" in histName): nomHist.GetYaxis().SetTitle("Entries / GeV") nomHist.GetXaxis().SetRangeUser( 500, 3100 ) if( "recoilPt" in histName): for iBin in range(1, nomHist.GetNbinsX()+1): nomHist.SetBinContent(iBin, nomHist.GetBinContent(iBin)/ nomHist.GetBinWidth(iBin)) nomHist.Draw() if not type(nomHist) == TGraphErrors: nomHist.Draw("p") else: nomHist.SetMarkerStyle(33) nomHist.SetMarkerSize(1.5) nomHist.SetLineWidth(4) nomHist.Draw("ap") if(f_plotSys): ## Get list of systematic histograms ## fullHistList = [] for thisSysDir in sysDirList: fullHistList.append( thisSysDir.Get(histName) ) fullHistList[-1].SetDirectory(0) ## Add systematic bands ## sysHistList = [] for iTopSys, topSysName in enumerate(sysTypes): if topSysName == 'All': subHistList = [thisHist for thisName, thisHist in zip(sysDirNameList, fullHistList) if any(otherTopName in thisName for otherTopName in sysTypes) ] else: subHistList = [thisHist for thisName, thisHist in zip(sysDirNameList, fullHistList) if topSysName in thisName] sysHistUp, sysHistDn = plotSysRatios.getCombinedSysHist(nomHist, subHistList, topSysName) for iBin in range(1, nomHist.GetNbinsX()+1): sysHistUp.SetBinContent( iBin, nomHist.GetBinContent(iBin)*(1.+sysHistUp.GetBinContent(iBin)) ) sysHistDn.SetBinContent( iBin, nomHist.GetBinContent(iBin)*(1.+sysHistDn.GetBinContent(iBin)) ) if topSysName == 'All': color = kBlack else: color = gStyle.GetColorPalette(int(colorOffset*(iTopSys+1))) sysHistUp.SetLineColor(color) sysHistUp.SetMarkerColor(color) sysHistUp.SetMarkerSize(1.5) sysHistUp.SetMarkerStyle(34) sysHistDn.SetLineColor(color) sysHistDn.SetMarkerColor(color) sysHistDn.SetMarkerSize(1.5) sysHistDn.SetMarkerStyle(34) if( "Pt" in histName or "Energy" in histName): sysHistUp.GetXaxis().SetRange(sysHistUp.FindFirstBinAbove(0), sysHistUp.FindLastBinAbove(0)+1) sysHistDn.GetXaxis().SetRange(sysHistDn.FindFirstBinAbove(0), sysHistDn.FindLastBinAbove(0)+1) if not type(nomHist) == TGraphErrors: sysHistUp.Draw("same hist l") sysHistDn.Draw("same hist l") else: sysHistUp.SetMarkerStyle(4) sysHistUp.SetMarkerSize(1.2) sysHistUp.Draw("same l") sysHistDn.SetMarkerStyle(4) sysHistDn.SetMarkerSize(1.2) sysHistDn.Draw("same l") if( topSysName == "EtaIntercalibration"): leg.AddEntry( sysHistUp, "EIC", "lp") else: leg.AddEntry( sysHistUp, topSysName, "lp") # Save them in a list sysHistList.append( sysHistUp ) sysHistList.append( sysHistDn ) # ### Add MC for Data MJB! ### # if( "data" in file and "MJB" in histName ): # filePath = os.path.dirname( file ) # if( "initial" in file): # mcFile = glob.glob( filePath+"/*mc14*MJB_initial.root") # elif( "final" in file): # mcFile = glob.glob( filePath+"/*mc14*MJB_final.root") # if len(mcFile) == 1: # mcFile = TFile.Open(mcFile[0], "READ") # mcKeyList = [key.GetName() for key in mcFile.GetListOfKeys()] #List of top level objects # mcDirList = [key for key in mcKeyList if "Iteration" in key] #List of all directories # #mcDir = [dir for dir in dirList if "NoCorr" in dir] # mcDir = [dir for dir in dirList if "Nominal" in dir] # if len(mcDir) == 1: # mcDir = mcFile.Get( mcDir[0] ) # mcHist = mcDir.Get(histName) # mcHist.SetMarkerColor(kRed) # mcHist.SetLineColor(kRed) # mcHist.SetMarkerStyle(33) # mcHist.SetMarkerSize(1.3) # mcHist.Draw("same p") # leg.AddEntry(mcHist, "MC", "lp") # # ### Add Gagik for DoubleMJB! ### # if( f_addGagik ): # if( "DoubleMJB" in histName ): # filePath = os.path.dirname( file ) # gagikFile = TFile.Open(filePath+"/ThirdCycle.EM4.sherpa.1.v11.root", "READ") # gagikHist = gagikFile.Get("g_DoMC_fmean_vs_recoil") # gagikHist.SetMarkerColor(kRed) # gagikHist.SetLineColor(kRed) # gagikHist.SetMarkerStyle(33) # gagikHist.SetMarkerSize(1.3) # gagikHist.Draw("same p") # leg.AddEntry(gagikHist, "8 TeV", "lp") # nomHist.Draw("same p") c1.cd() leg.Draw() AtlasStyle.ATLAS_LABEL(0.2,0.88, 1," Internal") AtlasStyle.myText(0.2,0.82,1, "#sqrt{s} = 13 TeV, 3.3 fb^{-1}") if "Pt" in histName or "alpha" in histName: pad1.SetLogy() c1.SaveAs(outDir+nomHist.GetName()+"_logy.png" ) c1.SaveAs(outDir+'/eps/'+nomHist.GetName()+"_logy.eps" ) pad1.SetLogy(0) if "MJB" in histName: pad1.SetLogx() c1.SaveAs((outDir+nomHist.GetName()+".png") ) c1.SaveAs((outDir+'/eps/'+nomHist.GetName()+".eps") ) c1.Clear() inFile.Close()
def combinedPlotNominal(files, normalize, ratio): files = files.split(',') dataFile = [file for file in files if ".data." in file][0] files.remove(dataFile) files.insert(0, dataFile) if any("Pythia" in file for file in files): pythiaFile = [file for file in files if "Pythia" in file][0] files.remove(pythiaFile) files.insert(1, pythiaFile) if any("Sherpa" in file for file in files): sherpaFile = [file for file in files if "Sherpa" in file][0] files.remove(sherpaFile) files.insert(2, sherpaFile) # Save output to directory of first file outDir = dataFile[:-5] + '/' if not os.path.exists(outDir): os.mkdir(outDir) if normalize: outDir += "combinedPlotNominal_normalized/" else: outDir += "combinedPlotNominal/" if not os.path.exists(outDir): os.mkdir(outDir) if not os.path.exists(outDir + '/eps'): os.mkdir(outDir + '/eps') AtlasStyle.SetAtlasStyle() gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info inFiles = [] nomDirs = [] for file in files: inFiles.append(TFile.Open(file, "READ")) keyList = [key.GetName() for key in inFiles[-1].GetListOfKeys() ] #List of top level objects dirList = [key for key in keyList if "Iteration" in key] #List of all directories nomDir = [dir for dir in dirList if "Nominal" in dir] if (not len(nomDir) == 1): print "Error, nominal directories are ", nomDir return else: nomDirs.append(inFiles[-1].Get(nomDir[0])) c1 = TCanvas() ################ Set Color Palate ####################3 # Data, Herwig, Pythia8, Sherpa colors = [kBlack, kRed, kBlue, kViolet, kCyan] markers = [20, 21, 23, 22, 33, 34] # colorMax = 240. # colorMin = 0. #20. # numInputs = len( nomDirs ) # colors = [] # if len(nomDirs) == 2: # colors = [kBlack, kRed] # else: # for iDir, nomDir in enumerate(nomDirs): # colorNum = int( colorMin+(colorMax-colorMin)*iDir/numInputs) # colors.append( gStyle.GetColorPalette( colorNum )) ##################### Plot All Nominal ################################# print "Plotting nominal hists " oneLine = TF1("zl1", "1", -10000, 10000) oneLine.SetTitle("") oneLine.SetLineWidth(1) oneLine.SetLineStyle(2) oneLine.SetLineColor(kBlack) histList = [key.GetName() for key in nomDirs[0].GetListOfKeys()] for histName in histList: if "prof_" in histName or "ptSlice" in histName: continue tmpHist = nomDirs[0].Get(histName) if not type(tmpHist) == TH1F and not type( tmpHist) == TH1D and not type( tmpHist) == TGraphErrors: #Can't draw bands if not 1D continue leg = TLegend(0.65, 0.72, 0.9, 0.93) leg.SetFillStyle(0) leg.SetTextFont(42) #! leg = TLegend(0.83, 0.15, 0.99, 0.95) if ratio: pad1 = TPad("pad1", "", 0, 0.32, 1, 1) pad2 = TPad("pad2", "", 0, 0, 1, 0.32) #! pad1 = TPad("pad1", "", 0, 0.3, 0.83, 1) #! pad2 = TPad("pad2", "", 0, 0, 0.83, 0.3) pad1.SetBottomMargin(0.01) pad2.SetTopMargin(0) pad2.SetBottomMargin(0.45) pad1.Draw() pad2.Draw() else: #! pad1 = TPad("pad1", "", 0, 0, 0.83, 1) pad1.Draw() pad1.cd() nomHists = [] drawString = "" maxVal = [] maxBinX = [] for iDir, nomDir in enumerate(nomDirs): thisFileStr = files[iDir].split('.')[2] if "all" in thisFileStr: thisFileStr = files[iDir].split('.')[1] nomHist = nomDir.Get(histName) nomHist.SetName(nomHist.GetName()) if "Beta" in histName: nomHist.Rebin(2) if "alpha" in histName: nomHist.Rebin(4) if "ptAsym" in histName: nomHist.Rebin(4) if "Eta" in histName and not type(nomHist) == TGraphErrors: nomHist.Rebin(4) if ("recoilPt" in histName): for iBin in range(1, nomHist.GetNbinsX() + 1): nomHist.SetBinContent( iBin, nomHist.GetBinContent(iBin) / nomHist.GetBinWidth(iBin)) if normalize and not type( nomHist) == TGraphErrors and nomHist.Integral() > 0.: nomHist.Scale(1. / nomHist.Integral()) if "Sherpa" in files[iDir] and "MJB" in histName: for iBin in range(31, nomHist.GetNbinsX() + 1): nomHist.SetBinContent(iBin, 0) nomHist.SetBinError(iBin, 0) nomHist.SetLineColor(colors[iDir]) nomHist.SetMarkerColor(colors[iDir]) nomHist.SetMarkerStyle(markers[iDir]) thisEntry = leg.AddEntry( nomHist, thisFileStr.replace('d', 'D').replace( 'Sherpa', 'Sherpa 2.1').replace('Herwig', 'Herwig++'), "lp") thisEntry.SetTextColor(colors[iDir]) #nomHist.SetMinimum(0.9) # if( "jetPt" in histName or "jetEnergy" in histName): # maxBinX.append(nomHist.FindLastBinAbove(0)+1) # nomHist.SetMaximum(1.5*nomHist.GetMaximum()) # nomHist.SetMinimum(0.000101) # else: nomHist.SetMaximum(1.5 * nomHist.GetMaximum()) #nomHist.SetMinimum(0.000101) if ("MJB" in histName): nomHist.SetMinimum(0.0000101) #nomHist.GetXaxis().SetRangeUser( 500, 2800 ) #!!public #nomHist.SetMaximum(1.06) #!!public #nomHist.SetMinimum(0.9701) #!!public nomHist.GetXaxis().SetRangeUser(300, 2800) nomHist.SetMaximum(1.2) nomHist.SetMinimum(0.8999) nomHist.GetXaxis().SetMoreLogLabels(True) nomHist.GetYaxis().SetTitle( "#LT p_{T}^{lead jet}/p_{T}^{recoil} #GT") nomHist.GetYaxis().SetTitleSize(0.09) nomHist.GetYaxis().SetTitleOffset(0.7) nomHist.GetYaxis().SetLabelSize(0.06) nomHist.GetYaxis().SetLabelOffset(0.01) nomHist.SetMarkerSize(.8) nomHist.SetLineWidth(1) elif ("Pt" in histName): nomHist.GetYaxis().SetTitle("AU") if ("jet0" in histName): nomHist.GetXaxis().SetRangeUser(200, 2000) else: nomHist.GetXaxis().SetRangeUser(0, 800) else: nomHist.GetYaxis().SetTitle("AU") if ("recoilPt" in histName): nomHist.GetYaxis().SetTitle("1/N dp_{T}^{recoil}/dN") nomHist.GetXaxis().SetRangeUser(300, 3000) if not type(nomHist) == TGraphErrors: #drawString = "histsamep" drawString = "psame" else: drawString = "apsame" nomHist.SetMarkerStyle(33) nomHist.SetMarkerSize(1.5) nomHist.SetLineWidth(4) nomHists.append(nomHist) maxVal.append(nomHist.GetMaximum()) maxDir = maxVal.index(max(maxVal)) nomHists[maxDir].Draw(drawString) if maxBinX: maxBinX = max(maxBinX) for iDir, nomDir in enumerate(nomDirs): nomHists[iDir].GetXaxis().SetRange(1, maxBinX) for iDir, nomDir in enumerate(nomDirs): nomHists[iDir].Draw(drawString) oneLine.Draw("same") for iDir, nomDir in enumerate(nomDirs): nomHists[iDir].Draw(drawString) nomHists[0].Draw(drawString) ## Draw data on top if ratio: pad2.cd() ratioHists = [] for iDir in range(1, len(nomDirs)): #ratioHists.append( nomHists[0].Clone() ) #ratioHists[iDir-1].Add(nomHists[iDir], -1.) #ratioHists[iDir-1].Divide(nomHists[iDir]) ratioHists.append(nomHists[iDir].Clone()) ratioHists[iDir - 1].Divide(nomHists[0]) ratioHists[iDir - 1].SetMarkerColor(colors[iDir]) ratioHists[iDir - 1].SetMarkerStyle(markers[iDir]) ratioHists[iDir - 1].SetLineColor(colors[iDir]) if iDir == 1: ratioHists[iDir - 1].SetMaximum(2) ratioHists[iDir - 1].SetMinimum(0.5) ratioHists[iDir - 1].GetXaxis().SetLabelOffset(.015) ratioHists[iDir - 1].GetXaxis().SetTitleOffset(1.3) ratioHists[iDir - 1].GetXaxis().SetLabelSize(0.13) ratioHists[iDir - 1].GetXaxis().SetTitleSize(0.16) # ratioHists[iDir-1].GetXaxis().SetTitle(nomHists[0].GetXaxis().GetTitle()); ratioHists[iDir - 1].GetXaxis().SetMoreLogLabels() ratioHists[iDir - 1].GetYaxis().SetLabelSize(0.13) ratioHists[iDir - 1].GetYaxis().SetTitleSize(0.16) ratioHists[iDir - 1].GetYaxis().SetLabelOffset(.01) ratioHists[iDir - 1].GetYaxis().SetTitleOffset(0.37) #ratioHists[iDir-1].GetYaxis().SetTitle("Significance") ratioHists[iDir - 1].GetYaxis().SetTitle(" MC / Data") ratioHists[iDir - 1].GetYaxis().SetNdivisions(7) if ("Pt" in histName): if ("jet0" in histName): ratioHists[iDir - 1].GetXaxis().SetRangeUser( 200, 2000) else: ratioHists[iDir - 1].GetXaxis().SetRangeUser( 0, 800) if ("MJB" in histName): #ratioHists[iDir-1].GetXaxis().SetRangeUser( 500, 2800 ) #!!public ratioHists[iDir - 1].GetXaxis().SetRangeUser(300, 2500) ratioHists[iDir - 1].SetMaximum(1.05) ratioHists[iDir - 1].SetMinimum(0.95) ratioHists[iDir - 1].GetXaxis().SetTitle( "p_{T}^{recoil} [GeV]") if ("recoilPt" in histName): ratioHists[iDir - 1].GetXaxis().SetRangeUser(300, 3000) ratioHists[iDir - 1].GetXaxis().SetTitle( "p_{T}^{recoil} [GeV]") # if( "jetBeta" in histName): # ratioHists[iDir-1].SetMaximum(1) # ratioHists[iDir-1].SetMinimum(-1) ratioHists[0].Draw("p") oneLine.Draw("same") for iDir in range(1, len(nomDirs)): ratioHists[iDir - 1].Draw("psame") c1.cd() leg.Draw() AtlasStyle.ATLAS_LABEL(0.2, 0.88, 1, " Preliminary") AtlasStyle.myText(0.2, 0.82, 1, "#sqrt{s} = 13 TeV, 3.3 fb^{-1}") AtlasStyle.myText(0.2, 0.76, 1, "Multijet Events") typeText = "anti-k_{t} R = 0.4" if "_LC_" in dataFile: typeText += ", LC+JES (in-situ)" else: typeText += ", EM+JES (in-situ)" AtlasStyle.myText(0.2, 0.7, 1, typeText) AtlasStyle.myText(0.2, 0.64, 1, "#left|#eta^{lead jet}#right| < 1.2") # AtlasStyle.myText(0.1,0.75,1, "m_{jj} Correction") if "MJB" in histName: pad1.SetLogx() if ratio: pad2.SetLogx() else: pad1.SetLogx(0) if ratio: pad2.SetLogx(0) #if "Pt" in histName or "alpha" in histName: pad1.SetLogy() c1.SaveAs(outDir + nomHist.GetName() + "_logy.png") c1.SaveAs(outDir + "/eps/" + nomHist.GetName() + "_logy.eps") pad1.SetLogy(0) c1.SaveAs((outDir + nomHist.GetName() + ".png")) c1.SaveAs((outDir + "/eps/" + nomHist.GetName() + ".eps")) c1.SaveAs((outDir + "/eps/" + nomHist.GetName() + ".pdf")) c1.Clear() for inFile in inFiles: inFile.Close()
def MultipleFlatEffCurve(outputName, approachList, bins, binslong, flav="L"): markerlist = [21, 8, 22, 23, 29, 34] fout = ROOT.TFile(outputName, "recreate") fout.cd() Canv = ROOT.TCanvas("EffComb", "EffComb", 0, 800, 0, 800) Canv.cd() EffCurves = [] for scoreList, varList, label, scoreCutList in approachList: heff = getFixEffCurve(scoreList, varList, label, binslong, fix_eff_target=0.7, scoreCutList=scoreCutList, onlyReturnCutList=False) EffCurves.append(heff) legend = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) legend_rel = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) ROCs = [] mg = ROOT.TMultiGraph() for i in range(len(EffCurves)): ROC = ConvertEffToGraph(EffCurves[i], bins, False) ROC.SetLineWidth(2) ROC.SetLineColor(colorlist[i]) ROC.SetMarkerColor(colorlist[i]) ROC.SetMarkerSize(1) ROC.SetMarkerStyle(markerlist[i]) ROC.SetLineStyle(i + 1) mg.Add(ROC) legend.AddEntry(ROC, approachList[i][2][1], "lp") ROCs.append(ROC) mg.Draw("AP") mg.GetXaxis().SetTitle("b-jet p_{T} [GeV]") if flav == "L": mg.GetYaxis().SetTitle("light-jet Rejection, 1/#varepsilon_{l}") if flav == "C": mg.GetYaxis().SetTitle("c-jet Rejection, 1/#varepsilon_{c}") legend.Draw("same") Atlas.ATLASLabel(0.2, 0.88, 0.13, "Simulation Internal", color=1) Atlas.myText(0.2, 0.81, color=1, size=0.04, text="#sqrt{s}=13 TeV, t#bar{t}") Atlas.myText(0.2, 0.75, color=1, size=0.04, text="p_{T}>20 GeV, |#eta|<2.5") Atlas.myText(0.2, 0.69, color=1, size=0.04, text="Flat 70% b-tagging WP") Canv.Write() fout.Close()
def create_cut_flow(): AtlasStyle.SetAtlasStyle() #input_tfile = utils.open_tfile(BasicConfig.workdir + 'DVTree_NTuple_data15_13TeV.root') #tree = input_tfile.Get('DVTree_NTuple') input_tfile = utils.open_tfile(args.inputFile) tree = input_tfile.Get('Nominal') cut_flow = [ 'Initial', 'Trigger', 'Filter', 'Cleaning', 'GRL', 'PV', 'NCB veto', 'MET', 'DV Selection' ] h_cut_flow = TH1F('cut_flow', ';;Number of Events', len(cut_flow), 0, len(cut_flow)) #h_cut_flow2 = TH1F('cut_flow2', ';;Number of Events', len(cut_flow), 0, len(cut_flow)) for bin, cut in enumerate(cut_flow): h_cut_flow.GetXaxis().SetBinLabel(bin + 1, cut) # entries = tree.GetEntries() for entry in range(entries): #if entry % 10000 == 0: # print('*** processed {0} out of {1}'.format(entry, entries)) utils.show_progress(entry, entries) #if entry == 100000: # break # get the next tree in the chain and verify ientry = tree.LoadTree(entry) if ientry < 0: break # copy next entry into memory and verify nb = tree.GetEntry(entry) if nb <= 0: continue event_weight = tree.McEventWeight * tree.PileupWeight * tree.ISRWeight for step, cut in enumerate(cut_flow): if step == 0: h_cut_flow.Fill(cut, event_weight) #h_cut_flow2.Fill(cut, event_weight) #elif step == 2: # if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 2): # h_cut_flow.Fill(cut, event_weight) # if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 2): # h_cut_flow2.Fill(cut, event_weight) #elif step == 6: # if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 6): # h_cut_flow.Fill(cut, event_weight) # if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 6): # h_cut_flow2.Fill(cut, event_weight) #elif step == 7: # #have_signal_like_dv = False # #for dv_index in range(len(tree.DV_passVtxCuts)): # # have_signal_like_dv = have_signal_like_dv or tree.DV_passVtxCuts[dv_index] # #if pass_event_cut(tree, 7) and tree.MET > 220 and have_signal_like_dv: # if tree.RandomRunNumber < 309311 and pass_event_cut(tree, 7): # h_cut_flow.Fill(cut, event_weight) # if tree.RandomRunNumber > 309311 and pass_event_cut(tree, 7): # h_cut_flow2.Fill(cut, event_weight) elif pass_event_cut(tree, step): h_cut_flow.Fill(cut, event_weight) #h_cut_flow2.Fill(cut, event_weight) output = TFile('cut_flow.root', 'recreate') h_cut_flow.Write() output.Close()
def make_systematic_table(): AtlasStyle.SetAtlasStyle() #directory = '/afs/cern.ch/work/k/kmotohas/DisplacedVertex/DV_xAODAnalysis/submitDir_LSF/mc/hist_DVPlusMETSys/' #directory = BasicConfig.workdir + 'hist_DVPlusMETSys/' directory = '/home/motohash/data/mc15_13TeV/DVPlusMETSys/' #tfile = TFile(BasicConfig.workdir + 'systTree.root') tfile = TFile(args.inputFile) key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()] print(len(key_list_all), key_list_all) regex = re.compile('Nominal|PRW|JET|MET.*') key_list = [key for key in key_list_all if re.match(regex, key)] print(len(key_list), key_list) tfile.Close() output_tfile = TFile('systematic_summary_SimpleMETFilter.root', 'recreate') #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402700, 402740)] tchains = [[dsid, [TChain(key, key + str(dsid)) for key in key_list]] for dsid in mc.parameters.keys()] #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402070, 402080)] #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in range(402070, 402080)] #trees = [gROOT.FindObject(key) for key in key_list] #entries_list = [tree.GetEntriesFast() for tree in trees] gStyle.SetHistMinimumZero() for dsid, each_tchain in tchains: print('') print(dsid) #print glob(directory + 'systTree_mc15_13TeV.' + str(dsid) + '*.root') for tchain in each_tchain: for input in glob(directory + 'systTree_mc15_13TeV.' + str(dsid) + '*.root'): #print(input) tchain.Add(input) #h_syst_diff = TH1F('syst_diff', ';Difference from Nominal', 101, -50.5, 50.5, 120, 0, 120) h_syst_diff = TH1F('syst_diff', ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list) + 1, 0, len(key_list) + 1) for ii, key in enumerate(key_list): h_syst_diff.GetXaxis().SetBinLabel(ii + 1, key) h_syst_diff.GetXaxis().SetBinLabel( len(key_list) + 1, 'ISR_Py2MG_SF_removed') h_syst_diff.SetMinimum(-0.3) h_syst_diff.SetMaximum(0.3) n_events_nominal = 0. for ii, tchain in enumerate(each_tchain): #print('') print('*** ' + key_list[ii]) n_events_weighted = 0. n_events_weighted_noISR = 0. entries = tchain.GetEntries() if entries == 0: continue for entry in xrange(entries): ## get the next tree in the chain and verify ientry = tchain.LoadTree(entry) if ientry < 0: break ## copy next entry into memory and verify nb = tchain.GetEntry(entry) if nb <= 0: continue #if pass_event_cut(tchain, 7): #if pass_event_cut(tchain, 5) and tchain.MET > 220 and tchain.PassCut7: # TODO if pass_event_cut(tchain, len(cut_flow) - 1): # TODO #if pass_event_cut(tchain, 5) and tchain.MET > 250 and tchain.PassCut7: # TODO #print(tree.McEventWeight, tree.PileupWeight) n_events_weighted += tchain.McEventWeight * tchain.PileupWeight * tchain.ISRWeight n_events_weighted_noISR += tchain.McEventWeight * tchain.PileupWeight if ii == 0: n_events_nominal = n_events_weighted if n_events_nominal < 1e-4: h_syst_diff.SetBinContent(len(key_list) + 1, 0) else: h_syst_diff.SetBinContent( len(key_list) + 1, float((n_events_weighted_noISR - n_events_nominal) / n_events_nominal)) diff = n_events_weighted - n_events_nominal #print(n_events_nominal, n_events_weighted, diff) if n_events_nominal < 1e-4: h_syst_diff.SetBinContent(ii + 1, 0) else: h_syst_diff.SetBinContent(ii + 1, float(diff / n_events_nominal)) #h_syst_diff.SetBinError(ii+1, n_event_weighted-n_events_nominal) utils.decorate_histogram(h_syst_diff, kPink, fill_style=3001) h_syst_diff.SetBarWidth(0.9) h_syst_diff.GetXaxis().SetLabelSize(0.035) output_tfile.cd() h_syst_diff.SetName('systematic_table_' + str(dsid)) h_syst_diff.Write()
def getROC(signal, background, label, cut_start=None, cut_end=None, outputName="myROC.root", Rejection="l", omission=[]): ROCList = [] markerlist = [21, 8, 22, 23] print "get roc ", label for ivar in range(len(signal)): s_sort = np.sort(signal[ivar]) b_sort = np.sort(background[ivar]) #c_start=(0.0 if cut_start==None else cut_start) #c_end= (1.0 if cut_end==None else cut_end) print s_sort, b_sort for i in range(s_sort.shape[0]): if s_sort[i] == float("Inf"): s_sort[i] = 100000 if s_sort[i] == float("-Inf"): s_sort[i] = -1000000 for i in range(b_sort.shape[0]): if b_sort[i] == float("Inf"): b_sort[i] = 100000 if b_sort[i] == float("-Inf"): b_sort[i] = -1000000 c_start = np.min((s_sort[0], b_sort[0])) c_end = np.max((s_sort[len(s_sort) - 1], b_sort[len(b_sort) - 1])) if c_start == -float('inf'): c_start = -2 * c_end print label[ivar], "min(", s_sort[0], b_sort[0], ")=", c_start print label[ivar], "max(", s_sort[-1], b_sort[-1], ")=", c_end s_eff = [] b_rej = [] n_points = 1000 c_delta = (1.0 * c_end - 1.0 * c_start) / (1.0 * n_points) for i in range(1000): cut = c_start + i * 1.0 * c_delta s_eff.append(1.0 * np.count_nonzero(s_sort > cut) / (1.0 * len(s_sort))) b_count = np.count_nonzero(b_sort > cut) b_rej.append((1.0 * len(b_sort)) / (1.0 if b_count == 0 else (1.0 * b_count))) ROC = ROOT.TGraph(n_points, array.array('d', s_eff), array.array('d', b_rej)) ROC.SetName("ROC_%i" % (ivar)) ROCList.append(ROC) f = ROOT.TFile(outputName, "update") canvas = ROOT.TCanvas("ROC_Overlay", "ROC_Overlay", 800, 600) canvas.cd() mg = ROOT.TMultiGraph() legend = ROOT.TLegend(0.5, 0.5, 0.75, 0.75) for i in range(len(ROCList)): if i in omission: continue ROC = ROCList[i] ROC.SetLineWidth(3) ROC.SetLineColor(colorlist[i]) ROC.SetMarkerColor(colorlist[i]) ROC.SetMarkerSize(0) ROC.SetMarkerStyle(0) ROC.SetLineStyle(i + 1) mg.Add(ROC) if omission == []: legend.AddEntry(ROC, label[i], "lp") f.WriteTObject(ROC, "ROC_" + label[i], "Overwrite") if omission != []: legend.AddEntry(ROCList[1], label[1], "lp") legend.AddEntry(ROCList[4], label[4], "lp") legend.AddEntry(ROCList[5], label[5], "lp") legend.AddEntry(ROCList[2], label[2], "lp") mg.Draw("AL") mg.GetXaxis().SetTitle("b-jet efficiency, #varepsilon_{b}") if Rejection == "l": mg.GetYaxis().SetTitle("light-jet rejection, 1/#varepsilon_{l}") if Rejection == "c": mg.GetYaxis().SetTitle("c-jet rejection, 1/#varepsilon_{c}") legend.Draw("same") Atlas.ATLASLabel(0.2, 0.88, 0.13, "Simulation Internal", color=1) Atlas.myText(0.2, 0.81, color=1, size=0.04, text="#sqrt{s}=13 TeV, t#bar{t}") Atlas.myText(0.2, 0.75, color=1, size=0.04, text="p_{T}>20 GeV, |#eta|<2.5") #Atlas.myText(0.2, 0.69 ,color=1, size=0.04,text="Rel21") #canvas.Update() canvas.Draw() f.WriteTObject(canvas, canvas.GetName(), "Overwrite") f.Close() return (ROCList, canvas)
pad2.Draw() pad1.cd() # drawing plots if (NPlots == 0): histo.Draw("HIST") else: histo.Draw("Same HIST") c.Update() # legend legend.AddEntry(histo,LegendEntryList[NPlots],"l") legend.Draw("Same") # ATLAS Style Setting AtlasStyle.ATLAS_LABEL(0.5, 0.85, internal = ATLAS_Internal, preliminary = ATLAS_Preliminary, color=1) AtlasStyle.myText(0.5, 0.8, 1, "#sqrt{s} = 13 TeV") # create ratio plots in pad2 if (EnableHistoCombination & EnableRatioPlots): ## save reference plot, i.e. NPlots == 0 if (NPlots == 0): refHisto = histo ## else plot ratio histos else: # change to pad2 pad2.cd() pad2.SetLogy(False) ## create and setup histoRatio histoRatio=histo.Clone()
def check_n_vertices_vs_met_threshold(): AtlasStyle.SetAtlasStyle() #input_tfile = utils.open_tfile(BasicConfig.workdir + 'DVTree_NTuple_data15_13TeV.root') input_tfile = utils.open_tfile(args.inputFile) #tree = input_tfile.Get('DVTree_NTuple') tree = input_tfile.Get('Nominal') #bin_name = ['Base', 'Trigger', 'Filter', 'MET200', 'MET220', 'MET250'] bin_name = ['Base', 'Trigger', 'Filter', 'MET250'] h_nevents_cut = TH1F('nevents_cut', ';;Double Ratio', len(bin_name), 0, len(bin_name)) h_nevents_all = TH1F('nevents_all', ';;Double Ratio', len(bin_name), 0, len(bin_name)) h_ndvs_cut = { ntracks: TH1F('ndvs_cut_' + str(ntracks), ';;Double Ratio', len(bin_name), 0, len(bin_name)) for ntracks in range(2, 6) } h_ndvs_all = { ntracks: TH1F('ndvs_all_' + str(ntracks), ';;Double Ratio', len(bin_name), 0, len(bin_name)) for ntracks in range(2, 6) } for bin, name in enumerate(bin_name): h_nevents_cut.GetXaxis().SetBinLabel(bin + 1, name) h_nevents_all.GetXaxis().SetBinLabel(bin + 1, name) for ntracks in range(2, 6): h_ndvs_cut[ntracks].GetXaxis().SetBinLabel(bin + 1, name) h_ndvs_all[ntracks].GetXaxis().SetBinLabel(bin + 1, name) entries = tree.GetEntries() for entry in range(entries): utils.show_progress(entry, entries) #if entry == 1000000: # break # get the next tree in the chain and verify ientry = tree.LoadTree(entry) if ientry < 0: break # copy next entry into memory and verify nb = tree.GetEntry(entry) if nb <= 0: continue if not utils.basic_event_selection(tree): continue # fill all for name in bin_name: h_nevents_all.Fill(name, 1.) for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): if utils.basic_dv_selection(tree, dv_index): if DV_nTracks < 6: h_ndvs_all[DV_nTracks].Fill(name, 1.) else: h_ndvs_all[5].Fill(name, 1.) # h_nevents_cut.Fill('Base', 1.) for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): if utils.basic_dv_selection(tree, dv_index): if DV_nTracks < 6: h_ndvs_cut[DV_nTracks].Fill('Base', 1.) else: h_ndvs_cut[5].Fill('Base', 1.) # Trigger if not tree.PassCut1: continue h_nevents_cut.Fill('Trigger', 1.) for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): if utils.basic_dv_selection(tree, dv_index): if DV_nTracks < 6: h_ndvs_cut[DV_nTracks].Fill('Trigger', 1.) else: h_ndvs_cut[5].Fill('Trigger', 1.) # Filter if not tree.PassCut2: continue h_nevents_cut.Fill('Filter', 1.) for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): if utils.basic_dv_selection(tree, dv_index): if DV_nTracks < 6: h_ndvs_cut[DV_nTracks].Fill('Filter', 1.) else: h_ndvs_cut[5].Fill('Filter', 1.) ## #if not tree.MET > 200: # continue #h_nevents_cut.Fill('MET200', 1.) #for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): # if pass_base_dv_selection(tree, dv_index): # if DV_nTracks < 6: # h_ndvs_cut[DV_nTracks].Fill(name, 1.) # else: # h_ndvs_cut[5].Fill(name, 1.) ## #if not tree.MET > 220: # continue #h_nevents_cut.Fill('MET220', 1.) #for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): # if pass_base_dv_selection(tree, dv_index): # if DV_nTracks < 6: # h_ndvs_cut[DV_nTracks].Fill(name, 1.) # else: # h_ndvs_cut[5].Fill(name, 1.) # if not tree.MET > 250: continue h_nevents_cut.Fill('MET250', 1.) for dv_index, DV_nTracks in enumerate(tree.DV_nTracks): if utils.basic_dv_selection(tree, dv_index): if DV_nTracks < 6: h_ndvs_cut[DV_nTracks].Fill('MET250', 1.) else: h_ndvs_cut[5].Fill('MET250', 1.) # output_tfile = TFile(args.outputFile, 'recreate') # #canvas = TCanvas('canvas', 'canvas', 1200, 800) #h_ndvs_all_clone = h_ndvs_all[2].Clone('unit') #h_ndvs_all_clone.Divide(h_ndvs_all[2]) #h_ndvs_all_clone.SetMaximum(3) #h_ndvs_all_clone.SetMinimum(0) #h_ndvs_all_clone.Draw() #legend = TLegend(0.5, 0.6, 0.85, 0.85) h_nevents_cut.Write() h_nevents_all.Write() for DV_nTracks in range(2, 6): h_ndvs_cut[DV_nTracks].Write() h_ndvs_all[DV_nTracks].Write() # # h_ndvs_cut[DV_nTracks].Sumw2() # h_ndvs_cut[DV_nTracks].Divide(h_ndvs_all[DV_nTracks]) # h_ndvs_cut[DV_nTracks].Divide(h_nevents_cut) # h_ndvs_cut[DV_nTracks].Multiply(h_nevents_all) # utils.decorate_histogram(h_ndvs_cut[DV_nTracks], BasicConfig.colors[DV_nTracks]) # h_ndvs_cut[DV_nTracks].Draw('same,hist') # legend.AddEntry(h_ndvs_cut[DV_nTracks], # '('+str(DV_nTracks)+'trk-DVs(cut)/2trk-DVs(all))/(Events(cut)/Events(all))', 'l') #utils.decorate_legend(legend) #legend.Draw() #utils.save_as(canvas, BasicConfig.plotdir + 'nVerts_met_dependency') #output = TFile('nVerts_met_dependency.root', 'recreate') #canvas.Write() output_tfile.Close()
def DrawHists(self, title, axisname=[], inplots=[], inlabel=[], instacks=[], instacklabel=[], sys=[]): maxval = 1 minval = 1000 secminval = 10000 legend = ROOT.TLegend(self.legend[0], self.legend[1], self.legend[2], self.legend[3]) legend.SetFillColor(0) doExist = True ## finding the right axises space for i in range(len(inplots)): if inplots[i] == None: continue doExist = False if ("TH2D" in inplots[i].ClassName()): continue if ("TH" not in inplots[i].ClassName()): legend.AddEntry(inplots[i], inlabel[i], "LPS") continue inplots[i] = CopyHist(inplots[i]) legend.AddEntry(inplots[i], inlabel[i], "LPS") thismax = inplots[i].GetMaximum() thismin = inplots[i].GetMinimum() if maxval < thismax: maxval = thismax if (minval >= thismin): minval = thismin inplots[i].GetYaxis().SetTitleOffset( inplots[i].GetYaxis().GetTitleOffset() * 1.1) for i in range(len(instacks)): ## scale the MC if instacks[i] == None: continue doExist = False instacks[i] = CopyHist(instacks[i]) if ((i != len(instacks) - 1) and self.CompareData): instacks[i].Scale(self.DrawScale) legend.AddEntry(instacks[i], instacklabel[i], 'f') if ((i == len(instacks) - 1) and self.CompareData): legend.AddEntry(instacks[i], instacklabel[i]) instacks[i].GetYaxis().SetTitleOffset( instacks[i].GetYaxis().GetTitleOffset() * 1.1) thismax = instacks[i].GetMaximum() thismin = instacks[i].GetMinimum() if maxval < thismax: maxval = thismax if (minval >= thismin and thismin != 0): minval = thismin if doExist: return if minval <= 1.0: minval = 1.0 ###### draw histogram Canv = ROOT.TCanvas('Canv_' + title, 'Canv_' + title, 0, 0, 800, 600) if (self.CompareData): Pad1 = ROOT.TPad('Pad1', 'Pad1', 0.0, 0.25, 1.0, 0.99, 0) Pad2 = ROOT.TPad('Pad2', 'Pad2', 0.0, 0.00, 1.0, 0.32, 0) Pad2.SetBottomMargin(0.4) Pad1.Draw() Pad2.Draw() Pad1.cd() ncolor = 0 for i in range(len(instacks)): if instacks[i] == None: ncolor += 1 continue instacks[i].SetMarkerColor(self.colorlist[ncolor]) instacks[i].SetFillColor(self.colorlist[ncolor]) instacks[i].SetFillStyle(self.FillStyle) instacks[i].SetLineColor(self.colorlist[ncolor]) instacks[i].SetLineColor(1) instacks[i].SetLineWidth(1) instacks[i].GetXaxis().SetTitle(axisname[0]) instacks[i].GetYaxis().SetTitle(axisname[1]) if (self.doRescale and not (self.doLogY)): instacks[i].GetYaxis().SetRangeUser(0, maxval * 4.5 / 3.) if (self.doRescale and self.doLogY): instacks[i].GetYaxis().SetRangeUser(minval / 100., maxval * 10.) ncolor += 1 for i in range(len(inplots)): if self.CompareData: XaxisTitle = inplots[i].GetXaxis().GetTitle() labelsize = inplots[i].GetXaxis().GetLabelSize() inplots[i].SetTitle("") inplots[i].GetXaxis().SetLabelSize(0) if i == len(inplots) - 1: if ("TH" not in inplots[i].ClassName()): inplots[i].Draw("") else: inplots[i].Draw("e") inplots[i].SetLineColor(1) inplots[i].SetMarkerColor(1) #inplots[i].SetFillColor(1) continue if i == len(inplots) - 2: if ("TH" not in inplots[i].ClassName()): inplots[i].Draw("same") else: inplots[i].Draw("e same") inplots[i].SetMarkerStyle(20) inplots[i].SetMarkerColor(2) inplots[i].SetLineColor(2) #### pay attention ## Pad2.cd() relsize = Pad2.GetAbsHNDC() / Pad1.GetAbsHNDC() size = Atlas.tsize / relsize Ratio = None if self.doDiff: Fit = None if ("TH" not in inplots[i].ClassName()): Ratio = deepcopy(inplots[len(inplots) - 1]) PreBinInt = Ratio.Integral() Fit = deepcopy(inplots[i]) else: Fit = deepcopy(inplots[len(inplots) - 1]) Ratio = deepcopy(inplots[i]) PreBinInt = Ratio.Integral() data = deepcopy(Ratio) Ratio.Add(Fit, -1) Ratio.Rebin(5) data.Rebin(5) Ratio.Divide(data) try: Fit.Rebin(5) except AttributeError: None else: Ratio = deepcopy(inplots[len(inplots) - 1]) Ratio.Divide(inplots[i]) Ratio.SetTitle("") Ratio.GetXaxis().SetLabelSize(size) Ratio.GetYaxis().SetLabelSize(size) Ratio.GetXaxis().SetTitleSize(size) Ratio.GetYaxis().SetTitleSize(size) Ratio.GetXaxis().SetTitleOffset( Ratio.GetXaxis().GetTitleOffset() * relsize * 2.9) Ratio.GetXaxis().SetLabelOffset(0.03) Ratio.GetYaxis().SetTitleOffset( Ratio.GetYaxis().GetTitleOffset() * relsize) Ratio.GetYaxis().SetTitle(self.DrawRatio) Ratio.GetXaxis().SetTitle(XaxisTitle) Ratio.GetYaxis().SetNdivisions(4) if self.doDiff: Ratio.GetYaxis().SetRangeUser(-0.2, 0.2) else: Ratio.GetYaxis().SetRangeUser(0.5, 1.5) Ratio.SetMarkerColor(1) Ratio.SetLineColor(1) Ratio.GetYaxis().SetNdivisions(5, ROOT.kFALSE) if sys == []: Ratio.Draw('e') else: Ratio.Draw('e') sys[0].SetFillStyle(3004) sys[0].SetFillColor(1) sys[0].SetMarkerStyle(10) sys[0].SetMarkerSize(0) if sys[0].GetBinError(1) != 0: sys[0].Draw("e2 same") if len(sys) > 1: sys[1].SetFillStyle(3004) sys[1].SetFillColor(2) sys[1].SetMarkerStyle(10) sys[1].SetMarkerSize(0) sys[1].SetLineColor(2) sys[1].SetLineWidth(2) sys[1].Draw("e2 same") if Ratio != None: line.Draw("same") Pad1.cd() continue if inplots[i] == None: continue inplots[i].SetMarkerColor(self.colorlist[ncolor]) #inplots[i].SetFillColor(self.colorlist[ncolor]) inplots[i].SetLineColor(self.colorlist[ncolor]) inplots[i].GetXaxis().SetTitle(axisname[0]) inplots[i].GetYaxis().SetTitle(axisname[1]) if (self.doRescale and not (self.doLogY)): inplots[i].GetYaxis().SetRangeUser(0, maxval * 4.5 / 3.) if (self.doRescale and self.doLogY): inplots[i].GetYaxis().SetRangeUser(minval / 100., maxval * 10.) ncolor += 1 count = 0 for i in range(len(inplots)): if inplots[i] == None: continue inplots[i].SetTitle("") if count != 0: self.drawOption = "same" if inplots[i].ClassName() != "TH2D": inplots[i].Draw(self.drawOption) else: inplots[i].Draw("colz") count += 1 count = 0 for i in range(len(instacks)): if instacks[i] == None: continue if "TH2" not in (instacks[i].ClassName()): #instacks[i].GetYaxis().SetMaxDigits(3) if count == 0: instacks[i].SetTitle("") if self.CompareData: instacks[i].GetXaxis().SetLabelSize(0) instacks[i].GetXaxis().SetTitle("") instacks[i].Draw("hist") count += 1 continue else: if self.CompareData: XaxisTitle = instacks[i].GetXaxis().GetTitle() labelsize = instacks[i].GetXaxis().GetLabelSize() instacks[i].SetTitle("") instacks[i].GetXaxis().SetLabelSize(0) if i == len(instacks) - 1: instacks[i].Draw("e same") instacks[i].SetLineColor(1) instacks[i].SetLineWidth(2) instacks[i].SetMarkerColor(1) instacks[i].SetFillColor(0) #### pay attention ## DataIntError = ROOT.Double(0) DataInt = instacks[i].IntegralAndError( 0, instacks[i].GetNbinsX() + 1, DataIntError) MCIntError = ROOT.Double(0) MCInt = instacks[i - 1].IntegralAndError( 0, instacks[i - 1].GetNbinsX() + 1, MCIntError) if MCInt == 0: MCInt = 1 print(" warning: no mc") if DataInt == 0: DataInt = 1 print(" warning: no data") rat = DataInt / MCInt rerr = sqrt((DataIntError / DataInt)**2 + (MCIntError / MCInt)**2) * rat #Atlas.myText(0.7, 0.45, 1, 0.04, "Data / MC = " + str(round(rat, 3)) +"#pm" + str(round(rerr,3))) count += 1 #continue if i == len(instacks) - 2: instacks[i].SetMarkerStyle(10) instacks[i].SetFillStyle(3004) instacks[i].SetMarkerSize(0.00001) instacks[i].SetFillColor(1) instacks[i].SetLineWidth(0) instacks[i].Draw("e2 same") #print instacklabel[i], len(instacklabel) Pad2.cd() relsize = Pad2.GetAbsHNDC() / Pad1.GetAbsHNDC() size = Atlas.tsize / relsize Ratio = CopyHist(instacks[len(instacks) - 1]) line = 0 if Ratio == None: continue if Ratio != None: line = ROOT.TLine( Ratio.GetXaxis().GetBinLowEdge(1), 1, Ratio.GetXaxis().GetBinUpEdge( Ratio.GetNbinsX()), 1) Ratio.Divide(instacks[i]) Ratio.SetTitle("") Ratio.GetXaxis().SetLabelSize(size) Ratio.GetYaxis().SetLabelSize(size) Ratio.GetXaxis().SetTitleSize(size) Ratio.GetYaxis().SetTitleSize(size) Ratio.GetXaxis().SetTitleOffset( Ratio.GetXaxis().GetTitleOffset() * relsize * 2.9) Ratio.GetXaxis().SetLabelOffset(0.03) Ratio.GetYaxis().SetTitleOffset( Ratio.GetYaxis().GetTitleOffset() * relsize) Ratio.GetYaxis().SetTitle(self.DrawRatio) Ratio.GetXaxis().SetTitle(XaxisTitle) Ratio.GetYaxis().SetRangeUser(0.5, 1.5) Ratio.GetYaxis().SetNdivisions(4) Ratio.SetMarkerColor(1) Ratio.SetLineColor(1) Ratio.SetLineWidth(2) Ratio.GetYaxis().SetNdivisions(5, ROOT.kFALSE) if sys == []: Ratio.Draw('e') elif len(sys) > 1: Ratio.Draw('e') sys[1].SetFillStyle(3001) sys[1].SetFillColor(30) sys[1].SetMarkerStyle(10) sys[1].SetMarkerSize(0) sys[1].SetLineColor(2) sys[1].SetLineWidth(2) sys[1].Draw("e2 same") sys[0].SetFillStyle(3004) sys[0].SetFillColor(1) sys[0].SetMarkerStyle(10) sys[0].SetLineWidth(2) sys[0].SetMarkerSize(0) if sys[0].GetBinError(1) != 0: sys[0].Draw("e2 same") Ratio.Draw('e same') else: Ratio.Draw('e') sys[0].SetFillStyle(3004) sys[0].SetFillColor(1) sys[0].SetMarkerStyle(10) sys[0].SetLineWidth(2) sys[0].SetMarkerSize(0) sys[0].Draw("e2 same") # if Ratio != None: # line.Draw("same") Pad1.cd() count += 1 continue instacks[i].Draw("hist same") else: if i == len(instacks) - 1: instacks[i].SetMarkerStyle(10) instacks[i].SetFillStyle(3020) instacks[i].Draw("e2 same") count += 1 continue instacks[i].Draw("hist same") ##### draw 2d else: instacks[i].SetTitle("") instacks[i].Draw('cont') count += 1 if ((instacks != [] and instacks[-1] != None and ("TH2" not in (instacks[-1].ClassName()))) or (inplots != [] and inplots[-1] != None and ("TH2" not in (inplots[-1].ClassName())))): legend.Draw("same") for text in self.texts: Atlas.myText(text[0], text[1], text[2], text[3], text[4]) if self.doAtlasLabel: Atlas.ATLASLabel(self.AtlasLabelPos, 0.88, self.shift, self.studytype, color=1) if self.doLabel and self.lumi != "0": Atlas.myText(self.AtlasLabelPos, 0.81, color=1, size=0.04, text="#sqrt{s}=" + self.sqrtS + " TeV " + "#intLdt=" + self.lumi + " fb^{-1}") if self.doLabel and self.lumi == "0": Atlas.myText(self.AtlasLabelPos, 0.81, color=1, size=0.04, text="#sqrt{s}=" + self.sqrtS + " TeV") if self.doLogY: Canv.SetLogy() if self.doLogX: Canv.SetLogx() if self.doLogZ: Canv.SetLogz() Canv.Write() if (self.doPrintPlots): Canv.SaveAs(self.OutPlotDir + Canv.GetName() + ".png") Canv.Close() return inplots
def fill_ntuple(): print('*** starting fill_ntuple() ') AtlasStyle.SetAtlasStyle() # # get key list # tfile = TFile(BasicConfig.workdir + 'systTree.root') # key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()] # regex = re.compile('PRW|JET|MET.*') # key_list = [key for key in key_list_all if re.match(regex, key)] # tfile.Close() # start making ttree #output_tfile = TFile('rhadron_v06-00-05.root', 'recreate') output_tfile = TFile(args.outputFile, 'recreate') # initialize TTree tree = TTree('rhadron', 'tree of rhadron properties for limit setting') # leaf variables from array import array mass_gluino = array('f', [0.]) delta_mass = array('f', [0.]) ctau = array('f', [0.]) eff = array('f', [0.]) eff_stat_error = array('f', [0.]) eff_syst_error = array('f', [0.]) eff_syst_error_ISR = array('f', [0.]) eff_syst_error_PRW = array('f', [0.]) eff_syst_error_JET = array('f', [0.]) eff_syst_error_MET = array('f', [0.]) # set branch tree.Branch("mGluino", mass_gluino, 'mGluino/F') tree.Branch("deltaM", delta_mass, 'deltaM/F') tree.Branch("ctau", ctau, 'ctau/F') tree.Branch("eff", eff, 'eff/F') tree.Branch("effRelStatErr", eff_stat_error, 'effRelStatErr/F') tree.Branch("effRelSystErr", eff_syst_error, 'effRelSystErr/F') tree.Branch("effRelSystErrISR", eff_syst_error_ISR, 'effRelSystErrISR/F') tree.Branch("effRelSystErrPRW", eff_syst_error_PRW, 'effRelSystErrPRW/F') tree.Branch("effRelSystErrJET", eff_syst_error_JET, 'effRelSystErrJET/F') tree.Branch("effRelSystErrMET", eff_syst_error_MET, 'effRelSystErrMET/F') #directory = '/afs/cern.ch/work/k/kmotohas/DisplacedVertex/DV_xAODAnalysis/submitDir_LSF/mc/hist_DVPlusMETSys/' #directory = BasicConfig.workdir + 'hist_DVPlusMETSys/' #directory = '/home/motohash/data/mc15_13TeV/DVPlusMETSys/v06-00-05/' #tfile = TFile(args.referenceFile) tfile = TFile(args.inputFile) key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()] print(len(key_list_all), key_list_all) regex = re.compile('Nominal|PRW|JET|MET.*') key_list = [key for key in key_list_all if re.match(regex, key)] print(len(key_list), key_list) tfile.Close() #c = 299792458. # [m/s] #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in range(402700, 402740)] #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in mc.parameters.keys()] #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in mc.parameters.keys()] dsids = [args.DSID] tchains = [[dsid, [TChain(key, key + str(dsid)) for key in key_list]] for dsid in dsids] cut_flow = [ 'Initial', 'Trigger', 'Filter', 'Cleaning', 'GRL', 'PV', 'NCB veto', 'MET', 'DV Selection' ] #systematic_tables = TFile('systematic_summary_SimpleMETFilter.root', 'open') #table = TH1F() m_MET_min = 250. # loop over dsid try: for dsid, each_tchain in tchains: print('') print(dsid) #index = 0 #for input in glob(directory + 'systTree_' + str(dsid) + '_*.root'): for tchain in each_tchain: #for input_file in glob(directory+'systTree_mc15_13TeV.' + str(dsid) + '*.root'): # print(input_file) # tchain.Add(input_file) tchain.Add(args.inputFile) mass_gluino[0] = mc.parameters[dsid]['g'] delta_mass[0] = mass_gluino[0] - mc.parameters[dsid]['chi0'] n_reweight_steps = 40 xmin = 1. xmax = 10000. ratio = xmax / xmin bins = [] for ii in range(n_reweight_steps): bins.append( xmax * 10**(ii * TMath.Log10(xmax / xmin) / n_reweight_steps - TMath.Log10(xmax / xmin))) #n_passed_w1 = [0. for _ in range(n_reweight_steps)] #n_passed = [0. for _ in range(n_reweight_steps)] from array import array limitsLifetime = array('d', bins) # tefficiency = [[ TEfficiency('tefficiency_{0}_{1}_{2}'.format(key, step, dsid), ';c#tau [mm]; Event-level efficiency', len(limitsLifetime) - 1, limitsLifetime) for step in range(n_reweight_steps) ] for key in key_list] #h_syst_diff = [[TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1) # for step in range(n_reweight_steps)] for key in key_list] h_syst_diff = [ TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list) + 1, 0, len(key_list) + 1) for step in range(n_reweight_steps) ] for step in range(n_reweight_steps): for jj, key in enumerate(key_list): h_syst_diff[step].GetXaxis().SetBinLabel(jj + 1, key) h_syst_diff[step].GetXaxis().SetBinLabel( len(key_list) + 1, 'ISR_Py2MG_SF_removed') n_events_weighted = [[0. for _ in range(n_reweight_steps)] for key in key_list] n_events_weighted_noISR = [[0. for _ in range(n_reweight_steps)] for key in key_list] # loop over tchain of each systematic for ii, tchain in enumerate(each_tchain): entries = tchain.GetEntries() print('*** processed systs: {0} / {1}'.format( ii, len(each_tchain))) #n_reweight_steps = 50 #for step in range(n_reweight_steps): # tefficiency.append(TEfficiency('tefficiency_'+str(step), ';c#tau [mm]; Event-level efficiency', # len(limitsLifetime)-1, limitsLifetime)) # h_syst_diff.append(TH1F('syst_diff_'+str(step), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1)) for step in range(n_reweight_steps): tefficiency[ii][step].SetUseWeightedEvents() #for jj, key in enumerate(key_list): # h_syst_diff[ii][step].GetXaxis().SetBinLabel(jj+1, key) #h_syst_diff[ii][step].GetXaxis().SetBinLabel(len(key_list)+1, 'ISR_Py2MG_SF_removed') # h_syst_diff[step].SetMinimum(-0.3) # h_syst_diff[step].SetMaximum(0.3) if entries == 0: continue for entry in range(entries): #if entry % 1000 == 0: # print('* processed events: {0} / {1}'.format(entry, entries)) utils.show_progress(entry, entries) #if entry == 605: # break # get the next tree in the chain and verify ientry = tchain.LoadTree(entry) if ientry < 0: break # copy next entry into memory and verify nb = tchain.GetEntry(entry) if nb <= 0: continue event_weight = tchain.McEventWeight * tchain.PileupWeight * tchain.ISRWeight ctau_MC = TMath.C( ) * mc.parameters[dsid]['t'] * 1e-9 # [nm]->[m] for step in range(n_reweight_steps): #print(tchain.GetListOfBranches()) pass_all = pass_event_cut(tchain, len(cut_flow) - 1) if pass_all: matched = False for idv in range(len(tchain.DV_x)): matched = matched or match( tchain, idv, cut=1.0) #print('pass_all is ', pass_all, ', matched is ', matched) pass_all = pass_all and matched target_ctau = xmax * 10**( step * TMath.Log10(xmax / xmin) / n_reweight_steps - TMath.Log10(xmax / xmin)) * 1e-3 # [mm]->[m] #print(target_ctau) lifetime_weight = get_lifetime_weight( tchain, target_ctau, ctau_MC) n_events_weighted[ii][ step] += event_weight * lifetime_weight n_events_weighted_noISR[ii][ step] += tchain.McEventWeight * tchain.PileupWeight * lifetime_weight #print(event_weight) #print(event_weight*lifetime_weight) #print(pass_all) tefficiency[ii][step].FillWeighted( pass_all, event_weight * lifetime_weight, target_ctau * 1e3) # end of loop over entries of each TChain # end loop over tchain of each systematic for step in range(n_reweight_steps): n_events_nominal = [0. for _ in range(n_reweight_steps)] for ii in range(len(each_tchain)): # if Nominal TTree, set syst diff of ISR as well if ii == 0: n_events_nominal[step] = n_events_weighted[ii][step] if n_events_nominal[step] < 1e-4: #h_syst_diff[ii][step].SetBinContent(len(key_list)+1, 0) h_syst_diff[step].SetBinContent( len(key_list) + 1, 0) else: #h_syst_diff[ii][step].SetBinContent(len(key_list)+1, h_syst_diff[step].SetBinContent( len(key_list) + 1, float((n_events_weighted_noISR[ii][step] - n_events_nominal[step]) / n_events_nominal[step])) #float((n_events_weighted[ii][step]-n_events_nominal[step])/n_events_nominal[step])) diff = n_events_weighted[ii][step] - n_events_nominal[step] #print(n_events_nominal, n_events_weighted, diff) if n_events_nominal[step] < 1e-4: #h_syst_diff[ii][step].SetBinContent(ii+1, 0) h_syst_diff[step].SetBinContent(ii + 1, 0) else: #h_syst_diff[ii][step].SetBinContent(ii+1, float(diff/n_events_nominal[step])) h_syst_diff[step].SetBinContent( ii + 1, float(diff / n_events_nominal[step])) #systematic_tables.GetObject('systematic_table_'+str(dsid), table) #syst_up, syst_down = root_sum_squares(table, 'x') #systs = root_sum_squares(h_syst_diff[ii][step], 'x') systs = root_sum_squares(h_syst_diff[step], 'x') #eff_syst_error[0] = max(syst_up, syst_down) # TODO #eff_syst_error[0] = (syst_up**2 + syst_down**2)**0.5 #### ############################ eff_syst_error[0] = (systs[0]**2 + systs[1]**2)**0.5 eff_syst_error_ISR[0] = systs[2] eff_syst_error_PRW[0] = systs[3] eff_syst_error_JET[0] = systs[4] eff_syst_error_MET[0] = systs[5] if eff_syst_error[0] > 1: print('eff_syst_error[0] = ' + str(eff_syst_error[0])) #eff_syst_error[0] = 1. #for step in range(n_reweight_steps): #for ct in bins: # print(len(bins), bins) #print(n_total_w1[step], n_total[step]) #sf = n_total_w1[step] / n_total[step] #n_passed[step] *= sf #n_total[step] *= sf #eff_no_weight, stat_error_no_weight = utils.division_error_propagation(n_passed_w1[step], n_total_w1[step]) #ctau[0] = TMath.Power(300, step/float(n_reweight_steps-1)) * 1e-3 # [mm]->[m] ct = bins[step] #print(ct) ctau[0] = ct * 1e-3 # [mm]->[m] #print(ctau[0]) bin_ctau = tefficiency[0][step].GetPassedHistogram().FindBin( ct) print(tefficiency[0][step].GetPassedHistogram().GetBinContent( bin_ctau)) print(tefficiency[0][step].GetTotalHistogram().GetBinContent( bin_ctau)) #print(bin_ctau) #print('ct', ct, 'bin_ctau', bin_ctau) eff[0] = tefficiency[0][step].GetEfficiency(bin_ctau) print(eff[0]) abs_stat_error = ( tefficiency[0][step].GetEfficiencyErrorLow(bin_ctau)**2 + tefficiency[0][step].GetEfficiencyErrorUp(bin_ctau)** 2)**0.5 #eff[0], abs_stat_error = utils.binomial_ratio_and_error(n_passed[step], n_total[step]) #if eff[0] < 1e-4: if eff[0] == 0: eff_stat_error[ 0] = 1. # avoid zero division error and divergence continue # not fill values in tree if efficiency is too small else: eff_stat_error[0] = abs_stat_error / eff[0] #if eff_stat_error[0] > 1: # print(n_passed[step], n_total[step], abs_stat_error, eff[0], eff_stat_error[0]) # eff_stat_error[0] = 1. tree.Fill() # end loop over n_reweight_steps except KeyboardInterrupt: pass output_tfile.Write() output_tfile.Close()
def plot_N_minus_one_distribution_in_SR(m12, varexp, lep_EEOS, lep_MMOS): # signal h_nuhm2 = get_N_minus_one_histogram( path + files['sigFile'], "MGPy8EG_A14N23LO_NUHM2_m12_" + str(m12) + "_weak_NoSys", varexp, lep_EEOS, lep_MMOS) h_nuhm2.SetLineColor(ROOT.kRed) h_nuhm2.SetLineWidth(2) # h_nuhm2.SetFillColor(ROOT.kRed) integral_nuhm2 = h_nuhm2.Integral() # data h_data = get_N_minus_one_histogram(path + files['dataFile'], "data", varexp, lep_EEOS, lep_MMOS) # bkg h_diboson = get_N_minus_one_histogram(path + files['dibosonFile'], "diboson_NoSys", varexp, lep_EEOS, lep_MMOS) h_fakes = get_N_minus_one_histogram(path + files['fakesFile'], "fakes_NoSys", varexp, lep_EEOS, lep_MMOS) h_other = get_N_minus_one_histogram(path + files['otherFile'], "other_NoSys", varexp, lep_EEOS, lep_MMOS) h_top = get_N_minus_one_histogram(path + files['topFile'], "top_NoSys", varexp, lep_EEOS, lep_MMOS) h_Zttjets = get_N_minus_one_histogram(path + files['ZttjetsFile'], "Zttjets_NoSys", varexp, lep_EEOS, lep_MMOS) h_data.SetLineColor(ROOT.kBlack) h_data.SetMarkerColor(ROOT.kBlack) h_data.SetMarkerStyle(ROOT.kFullCircle) h_data.SetMarkerSize(1.5) integral_data = h_data.Integral() h_diboson.SetLineColor(ROOT.kOrange + 1) h_diboson.SetFillColor(ROOT.kOrange + 1) h_diboson.SetFillStyle(1001) integral_diboson = h_diboson.Integral() h_fakes.SetLineColor(18) h_fakes.SetFillColor(18) h_fakes.SetFillStyle(1001) integral_fakes = h_fakes.Integral() h_other.SetLineColor(ROOT.kYellow - 9) h_other.SetFillColor(ROOT.kYellow - 9) h_other.SetFillStyle(1001) integral_other = h_other.Integral() h_top.SetLineColor(ROOT.kAzure + 7) h_top.SetFillColor(ROOT.kAzure + 7) h_top.SetFillStyle(1001) integral_top = h_top.Integral() h_Zttjets.SetLineColor(ROOT.kGreen + 2) h_Zttjets.SetFillColor(ROOT.kGreen + 2) h_Zttjets.SetFillStyle(1001) integral_Zttjets = h_Zttjets.Integral() hstack = ROOT.THStack() hstack.Add(h_other) hstack.Add(h_diboson) hstack.Add(h_Zttjets) hstack.Add(h_top) hstack.Add(h_fakes) h_SM = h_diboson.Clone() h_SM.Reset() h_SM.Add(h_diboson) h_SM.Add(h_fakes) h_SM.Add(h_other) h_SM.Add(h_top) h_SM.Add(h_Zttjets) h_SM.SetLineColor(ROOT.kGray + 3) h_SM.SetFillColor(ROOT.kGray + 3) h_SM.SetFillStyle(0) integral_SM = h_SM.Integral() h_SM_err = h_SM.Clone() h_SM_err.SetLineColor(ROOT.kGray + 3) h_SM_err.SetFillColor(ROOT.kGray + 3) h_SM_err.SetMarkerColor(ROOT.kGray + 3) h_SM_err.SetFillStyle(3004) # Set error of SM to stat + 20% syst for i in range(0, h_SM.GetXaxis().GetNbins() + 1): stat = h_SM.GetBinError(i) syst = h_SM.GetBinContent(i) error = math.sqrt(stat * stat + (syst * 0.2) * (syst * 0.2)) h_SM.SetBinError(i, error) # Making plot c1 = ROOT.TCanvas("c1", "", 800, 800) ROOT.gStyle.SetOptStat(0) # gStyle->SetOptFit(1111) # Upper plot will be in pad1 pad1 = ROOT.TPad("pad1", "pad1", 0, 0.35, 1, 1.0) pad1.SetBottomMargin(0.05) pad1.SetRightMargin(0.05) pad1.SetLeftMargin(0.16) # pad1->SetGridy() # grid lines pad1.Draw() # lower plot will be in pad pad2 = ROOT.TPad("pad2", "pad2", 0, 0.05, 1, 0.35) pad2.SetTopMargin(0.05) pad2.SetBottomMargin(0.3) pad2.SetRightMargin(0.05) pad2.SetLeftMargin(0.16) # pad2.SetGridy() # grid lines pad2.Draw() # # pad1: top pad # pad1.cd() # pad1 becomes the current pad # pad1.SetFrameLineWidth(2) hstack.Draw("hist") # need to draw first, otherwise cannot GetHistogram() hstack.GetHistogram().SetStats(0) # hstack.SetMinimum(0.) hstack.SetMaximum(h_SM.GetMaximum() * 2.6) hstack.GetHistogram().SetXTitle("") # suppress Xtitle if varexp == "met_Et": hstack.GetXaxis().SetRangeUser(0, 600) elif varexp == "lep1Pt" or varexp == "lep2Pt": hstack.GetXaxis().SetRangeUser(0, 60) hstack.GetXaxis().SetLabelSize(0) hstack.GetHistogram().SetYTitle(h_data.GetYaxis().GetTitle()) hstack.GetYaxis().SetLabelSize(0.04) hstack.GetYaxis().SetLabelOffset(0.015) hstack.GetYaxis().SetTitleSize(0.05) hstack.GetYaxis().SetTitleOffset(1.3) hstack.Draw("hist") # re-draw to make the changes working hstack.Draw("hist") h_SM.Draw("hist,same") h_SM_err.Draw("E2,same") h_data.Draw("E,same") h_nuhm2.Scale(10.) # multiply by 10 on the Nsig because Nsig is to small h_nuhm2.Draw("hist,same") x1, x2, dx = 0., 0., 0. if varexp == "met_Et": x1 = 200. dx = h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "nBJet20_MV2c10": x1 = 1. dx = -1 * h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "DPhiJ1Met": x1 = 2. dx = h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "minDPhiAllJetsMet": x1 = 0.5 dx = h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "mt_lep1": x1 = 70. dx = -1 * h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "MTauTau": x1 = 0. dx = -1 * h_SM.GetXaxis().GetXmax() * 0.1 x2 = 160. elif varexp == "Rll": x1 = 2. dx = -1 * h_SM.GetXaxis().GetXmax() * 0.1 elif varexp == "METOverHTLep": x1 = 5. dx = h_SM.GetXaxis().GetXmax() * 0.1 if (varexp != 'nJet30' and varexp != 'lep1Pt' and varexp != 'lep2Pt' and varexp != 'mll'): v_line_y = h_SM.GetMaximum() * 1.3 v_line1 = ROOT.TLine(x1, 0., x1, v_line_y) v_line1.SetLineWidth(3) v_line1.SetLineColor(ROOT.kBlack) v_line1.Draw() v_arrow1 = ROOT.TArrow(x1, v_line_y, x1 + dx, v_line_y, 0.03, "|>") v_arrow1.SetLineColor(ROOT.kBlack) v_arrow1.SetLineWidth(3) v_arrow1.SetFillColor(ROOT.kBlack) v_arrow1.SetFillStyle(1001) v_arrow1.Draw() v_line2, v_arror2 = None, None if varexp == "MTauTau": v_line2 = ROOT.TLine(x2, 0., x2, v_line_y) v_line2.SetLineWidth(3) v_line2.SetLineColor(ROOT.kBlack) v_line2.Draw() v_arrow2 = ROOT.TArrow(x2, v_line_y, x2 - dx, v_line_y, 0.03, "|>") v_arrow2.SetLineColor(ROOT.kBlack) v_arrow2.SetLineWidth(3) v_arrow2.SetFillColor(ROOT.kBlack) v_arrow2.SetFillStyle(1001) v_arrow2.Draw() AtlasStyle.ATLASLabel(0.2, 0.83, "Internal", ROOT.kBlack) AtlasStyle.myText(0.2, 0.78, "13 TeV, 36.1 fb^{-1}", ROOT.kBlack, 0.04) if lep_EEOS is True and lep_MMOS is False: AtlasStyle.myText(0.2, 0.75, "SRee-iMLLg ee", ROOT.kBlack, 0.02) if lep_MMOS is True and lep_EEOS is False: AtlasStyle.myText(0.2, 0.75, "SRmm-iMLLg #mu#mu", ROOT.kBlack, 0.02) if lep_EEOS is True and lep_MMOS is True: AtlasStyle.myText(0.2, 0.75, "SRee-iMLLg ee + SRmm-iMLLg #mu#mu", ROOT.kBlack, 0.02) AtlasStyle.myText(0.2, 0.72, "SusySkimHiggsino v1.9b", ROOT.kGray, 0.02) legend_nuhm2 = "NUHM2 m12={0} ({1:.2f})".format(m12, integral_nuhm2) legend_data = "Data ({0:.1f} Events)".format(integral_data) legend_SM = "SM stat #oplus 20% syst ({0:.1f})".format(integral_SM) legend_fakes = "Fake leptons ({0:.1f}%)".format(integral_fakes / integral_SM * 100) legend_top = 't#bar{t}& Single top ' + "({0:.1f}%)".format( integral_top / integral_SM * 100) legend_Zttjets = "Z(#rightarrow#tau#tau)+jets ({0:.1f}%)".format( integral_Zttjets / integral_SM * 100) legend_diboson = "Diboson ({0:.1f}%)".format(integral_diboson / integral_SM * 100) legend_other = "Others ({0:.1f}%)".format(integral_other / integral_SM * 100) legend = ROOT.TLegend(0.6, 0.5, 0.9, 0.87) legend.AddEntry(h_nuhm2, legend_nuhm2, "l") legend.AddEntry(h_data, legend_data, "pl") legend.AddEntry(h_SM_err, legend_SM, "fl") legend.AddEntry(h_fakes, legend_fakes, "f") legend.AddEntry(h_top, legend_top, "f") legend.AddEntry(h_Zttjets, legend_Zttjets, "f") legend.AddEntry(h_diboson, legend_diboson, "f") legend.AddEntry(h_other, legend_other, "f") legend.SetBorderSize(0) legend.SetTextFont(42) legend.SetTextSize(0.03) legend.SetFillColor(0) legend.SetFillStyle(0) legend.Draw() # # pad2: bottom pad # pad2.cd() # pad2 becomes the current pad pad2_X_min = h_data.GetXaxis().GetXmin() pad2_X_max = h_data.GetXaxis().GetXmax() if varexp == "met_Et": pad2_X_max = 600. elif varexp == "lep1Pt" or varexp == "lep2Pt": pad2_X_max = 60. pad2_Y_min = 0. pad2_Y_max = 2. pad2_X_title = h_data.GetXaxis().GetTitle() pad2_Y_title = "Data / SM" frame = pad2.DrawFrame(pad2_X_min, pad2_Y_min, pad2_X_max, pad2_Y_max) frame.GetXaxis().SetNdivisions(510) frame.GetYaxis().SetNdivisions(405) frame.SetLineWidth(1) frame.SetXTitle(pad2_X_title) frame.GetXaxis().SetTitleSize(25) frame.GetXaxis().SetTitleFont(47) frame.GetXaxis().SetTitleOffset(4.0) frame.GetXaxis().SetLabelFont( 43) # Absolute font size in pixel (precision 3) frame.GetXaxis().SetLabelSize(20) frame.GetXaxis().SetLabelOffset(0.05) frame.SetYTitle(pad2_Y_title) frame.GetYaxis().SetTitleSize(25) frame.GetYaxis().SetTitleFont(43) frame.GetYaxis().SetTitleOffset(2.0) frame.GetYaxis().SetLabelFont( 43) # Absolute font size in pixel (precision 3) frame.GetYaxis().SetLabelSize(20) frame.GetYaxis().SetLabelOffset(0.015) frame.Draw() line = ROOT.TLine(pad2_X_min, 1., pad2_X_max, 1.) line.SetLineColor(ROOT.kBlack) line.SetLineStyle(1) line.SetLineWidth(1) line.Draw("same") h_data_over_SM = h_data.Clone() h_data_over_SM.Divide(h_SM) h_data_over_SM.SetLineColor(ROOT.kBlack) h_data_over_SM.SetMarkerColor(ROOT.kBlack) h_data_over_SM.SetMarkerStyle(ROOT.kFullCircle) h_data_over_SM.SetMarkerSize(1.5) bin_center = [] for i in range(0, h_data_over_SM.GetXaxis().GetNbins() + 1): # remove the 0 bin content points by setting the central value to -99 if h_data_over_SM.GetBinContent(i) == 0: h_data_over_SM.SetBinContent(i, -99.) if h_data_over_SM.GetBinContent(i) > 2: bin_center.append(h_data_over_SM.GetBinCenter(i)) h_data_over_SM.Draw("E0,same") # Use E optoin, then no 0 bin content points are drawn but no error bars for those point > 2 # Use E0 option, the 0 bin content points are drawn and so do error bars # Add a small arrow for those bins with central value > 2 arrow_list = [] for x in bin_center: arrow = ROOT.TArrow(x, 1.7, x, 1.95, 0.01, "|>") # arrow.SetArrowSize(0.02) arrow.SetLineColor(ROOT.kOrange + 1) arrow.SetLineWidth(3) arrow.SetFillColor(ROOT.kOrange + 1) arrow.SetFillStyle(1001) # If draw arrow at here, then only the last arrow is drawn. # So put arrow object into a list and draw them later. if x < pad2_X_max: arrow_list.append(arrow) for arrow in arrow_list: arrow.Draw() h_SM_over_SM = h_SM.Clone() h_SM_over_SM.Divide(h_SM) h_SM_over_SM.SetLineColor(ROOT.kGray + 3) h_SM_over_SM.SetFillColor(ROOT.kGray + 3) h_SM_over_SM.SetMarkerColor(ROOT.kGray + 3) h_SM_over_SM.SetFillStyle(3004) h_SM_over_SM.Draw("E2,same") output = "NUHM2_m12_" + str(m12) + "_and_Bkg_" if varexp == 'mt_lep1+mt_lep2': output += "mt_lep1_plus_mt_lep2" else: output += varexp if lep_EEOS is True and lep_MMOS is False: output += "_EEOS" if lep_MMOS is True and lep_EEOS is False: output += "_MMOS" if lep_EEOS is True and lep_MMOS is True: output += "_SFOS" # output += "_N_minus_one_distribution_in_SR.pdf" output += "_N_minus_one_distribution_in_SR_times_10_on_Nsig.pdf" c1.SaveAs(output)
def draw_cross_section_limit_dM(tree, dM, flavor_of_sample='MET_TLJets'): AtlasStyle.SetAtlasStyle() entries = tree.GetEntries() current_mass = 0 upper_limits = [] mass_g = [] index = -1 point = 0 for entry in range(entries): tree.GetEntry(entry) if tree.deltaM == dM or (dM == 'large' and tree.mGluino - tree.deltaM == 100): if current_mass != tree.mGluino: print('*** {0}, {1}'.format(tree.mGluino, tree.deltaM)) upper_limits.append(TGraphErrors()) mass_g.append(int(tree.mGluino)) index += 1 point = 0 current_mass = tree.mGluino upper_limits[index].SetPoint(point, tree.ctau * 1e3, tree.xsUL) #upper_limits[index].SetPointError(point, 0, tree.xsUL*tree.effRelStatErr+tree.xsUL*tree.effRelSystErr) point += 1 print(tree.ctau, tree.xsUL) canvas = TCanvas('c', 'c', 1000, 800) canvas.SetLogx() canvas.SetLogy() h_xs = TH1F('xs', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310) h_xs.GetYaxis().SetRangeUser(0.1, 100) h_xs.Draw() #h_xs_line = TH1F('xs_line', ';c#tau [mm]; Cross Section [fb]', 1000, 0.9, 310) #print(mc.mass_xs_err[mass_g]['xs'] * 1e3) legend = TLegend(0.60, 0.75, 0.83, 0.90) for ii, upper_limit in enumerate(upper_limits): #upper_limit.RemovePoint(0) upper_limit.SetMarkerSize(0) upper_limit.SetFillStyle(3001) index = ii #if dM[ii] == 130: # index = 1 #elif dM[ii] == 80: # index = 2 # continue #elif dM[ii] == 50: # index = 3 #elif dM[ii] == 30: # index = 4 #print(upper_limit) upper_limit.SetFillColor(BasicConfig.colors[index+1]) upper_limit.SetLineColor(BasicConfig.colors[index+1]) upper_limit.Draw('lp,same') #upper_limit.Draw('c,same') #if dM[ii] > 100: # #legend.AddEntry(upper_limit, 'M_{#tilde{g}} = '+str(mass_g)+' GeV, #DeltaM = '+str(dM[ii])+' GeV', 'lf') # legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf') #legend.AddEntry(upper_limit, '#DeltaM = '+str(dM[ii])+' GeV', 'lf') legend.AddEntry(upper_limit, 'Mass G = ' + str(mass_g[ii]) + ' GeV', 'lf') utils.decorate_legend(legend) legend.Draw() AtlasStyle.ATLASLabel(0.19, 0.87, 'Work in Progress') AtlasStyle.myText(0.20, 0.79, kBlack, '#sqrt{s} = 13 TeV, #int L dt = 30 fb^{-1}', 0.035) #AtlasStyle.myText(0.20, 0.73, kBlack, 'Split-SUSY Model, M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.032) #AtlasStyle.myText(0.20, 0.67, kBlack, 'M_{#tilde{g}} = '+str(mass_g)+' GeV', 0.035) #if dM == 'large' and tree.mGluino - tree.deltaM == 100: # print('test') utils.save_as(canvas, BasicConfig.plotdir + 'xs_limit_large_dM_' + flavor_of_sample)
def plotMJBvEta(file, binnings): if not "MJB" in file: print "Error: plotMJBvEta.py must run on a file with \"MJB\", but was given", file print "Exiting..." return binnings = binnings.split(',') for iBinning in range(len(binnings)): if not binnings[iBinning][0] == '_': binnings[iBinning] = '_' + binnings[iBinning] print "Binnings are ", binnings outDir = file[:-5] + '/' if not os.path.exists(outDir): os.mkdir(outDir) outDir += "plotMJBvEta/" if not os.path.exists(outDir): os.mkdir(outDir) AtlasStyle.SetAtlasStyle() gROOT.ProcessLine("gErrorIgnoreLevel = 2000") #Ignore TCanvas::Print info inFile = TFile.Open(file, "READ") keyList = [key.GetName() for key in inFile.GetListOfKeys()] #List of top level objects dirList = [key for key in keyList if "Iteration" in key] #List of all directories #nomDir = [dir for dir in dirList if "NoCorr" in dir] nomDir = [dir for dir in dirList if "Nominal" in dir] if (not len(nomDir) == 1): print "Error, nominal directories are ", nomDir return else: nomDir = inFile.Get(nomDir[0]) c1 = TCanvas() histList = [ key.GetName() for key in nomDir.GetListOfKeys() if "MJB" in key.GetName() ] for binning in binnings: leg = TLegend(0.80, 0.15, 0.97, 0.95) pad1 = TPad("pad1", "", 0, 0, 0.85, 1) pad1.Draw() pad1.cd() hists = [] counter = 0 for histName in histList: if not binning in histName: continue counter += 1 hists.append(nomDir.Get(histName)) hists[-1].SetMarkerColor( gStyle.GetColorPalette(70 * (len(hists) - 1))) hists[-1].SetLineColor( gStyle.GetColorPalette(70 * (len(hists) - 1))) hists[-1].GetYaxis().SetRangeUser(0.9, 1.1) hists[-1].GetXaxis().SetMoreLogLabels(True) if ("DoubleMJB" in histName): hists[-1].GetXaxis().SetRangeUser(300, 2000) if (counter == 1): if type(hists[-1]) == TGraph: hists[-1].Draw("apXl") else: hists[-1].Draw() leg.AddEntry(hists[-1], "Nominal", "l") else: if type(hists[-1]) == TGraph: hists[-1].Draw("same pXl") else: hists[-1].Draw("same") leg.AddEntry(hists[-1], "Eta " + str(counter), "l") c1.cd() leg.Draw() pad1.SetLogx() c1.SaveAs(outDir + "/MJBvEta" + binning + ".png", "png") pad1.Clear() c1.Clear() inFile.Close()
import AtlasStyle import ROOT import BasicConfig import utils from datetime import date if __name__ == '__main__': AtlasStyle.SetAtlasStyle() mass_list = range(600, 1801, 200) tfiles = [ utils.open_tfile( BasicConfig.workdir + 'SysUnc_ISR/SysUnc_ISR_{:04d}_weight.root'.format(mass)) for mass in mass_list ] c = ROOT.TCanvas('c', 'c', 1000, 800) c.SetLogx() tf_out = ROOT.TFile( BasicConfig.workdir + 'SysUnc_ISR/SysUnc_ISR_weight.root', 'recreate') for ii, (tfile, mass) in enumerate(zip(tfiles, mass_list)): tfile.cd() c2 = ROOT.gROOT.FindObject('c2') hist = c2.GetPrimitive('ggSystem_SysUnc_ISR') hist.SetName('ggSystem_SysUnc_ISR_' + str(mass)) utils.decorate_histogram(hist, BasicConfig.colors[ii]) c.cd()
import ROOT, array, sys, os import math import time import glob sys.path.insert(0, '/home/jdandoy/Documents/Dijet/DijetFW/DijetHelpers/scripts/') import AtlasStyle AtlasStyle.SetAtlasStyle() import argparse parser = argparse.ArgumentParser( description="%prog [options]", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("-b", dest='batchMode', action='store_true', default=False, help="Batch mode for PyRoot") parser.add_argument("-run", dest='f_run', action='store_true', default=False, help="Running on data") parser.add_argument("-plot", dest='f_plot', action='store_true', default=False, help="Plot MC and Data curves together.") #parser.add_argument("-unbiased", dest='f_unbiased', action='store_true', default=False, help="Make MC denominator unbiased")