def __draw_stack(self, regions, th1d_dict, output_name): canvas = TCanvas("canvas", 'A basic canvas', 1000, 600) stack = THStack("stack", "stack") leg = Plotting.Create_Legend(0.60, 0.60, 0.95, 0.95) for region in regions: hist = th1d_dict[region] hist.SetLineColor(1) hist.SetLineWidth(1) hist.SetMarkerColor(self.__get_colour(region)) hist.SetMarkerStyle(0) hist.SetFillColor(self.__get_colour(region)) leg.AddEntry(hist, self.__get_label(region), "f") stack.Add(hist) del hist stack.Draw("HIST") stack.GetYaxis().SetTitle("Events") stack.GetXaxis().SetTitle("True #it{E_{T}^{miss}} [GeV]") leg.Draw() canvas.Print(output_name)
def main(): extension = "pdf" luminosity = 44307.4 ymin = 0 ymax = 0.024 data_set_names = [ (["SZee_all_susy_2l_0jets.root", "SZmumu_all_susy_2l_0jets.root"], "Z#rightarrowll", 860 + 0, 23), (["ZZ_1in3_susy_2l_0jets.root"], "ZZ#rightarrowll#nu#nu", 416 + 3, 43), (["WZ_1in3_susy_2l_0jets.root"], "WZ#rightarrowl#null", 416 - 8, 33), (["WW_1in3_susy_2l_0jets.root"], "WW#rightarrowl#nul#nu", 416 + 0, 20), ([ "ttbar_1in3_susy_2l_0jets.root", "top_1in3_susy_2l_0jets.root", "antitop_1in3_susy_2l_0jets.root" ], "Top", 800 + 4, 21), ] DATA_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Data", "SR_SUSY") ## Creating the plots gROOT.SetStyle("ATLAS") gROOT.ForceStyle() gStyle.SetErrorX(0.5) canvas = TCanvas("canvas", 'A basic canvas', 800, 600) # canvas.SetLogy() ## Adding in the legend leg = Plotting.Create_Legend(0.60, 0.60, 0.95, 0.95, ncols=1) ## Creating the stack stack = THStack("stack", "stack") stack.SetMinimum(ymin) stack.SetMaximum(ymax) ## Cycling through the different datasets for datasets, label, colour, style in data_set_names: ## Creating the total histogram which will be filled myhist = TH1D(label, label, 50, 0, 150) myhist.SetStats(True) myhist.StatOverflows(True) for dataset in datasets: ## The root file is opened rootfile_name = os.path.join(DATA_dir, dataset) print(rootfile_name) root_file = TFile.Open(rootfile_name, 'read') tree = root_file.Get("mt2_Truth") ## Creating the current histogram which will be filled thishist = TH1D(label, label, 50, 0, 150) thishist.SetStats(True) thishist.StatOverflows(True) ## Drawing the tree and saving the hist to the matrix execution = "mt2>>{}".format(label) tree.Draw(execution, "", "goff") thishist.SetDirectory(0) myhist.Add(thishist) ## We are dont with the file root_file.Close() del root_file ## Changing the properties of the histogram myhist.Scale(1 / myhist.Integral("width")) myhist.SetLineColor(colour) myhist.SetMarkerColor(colour) myhist.SetMarkerStyle(style) myhist.SetMarkerSize(1.5) ## Adding the legend entry leg.AddEntry(myhist, label, "p") ## Adding the object to the stack stack.Add(myhist) del myhist ## Drawing the stack on the currrent canvas stack.Draw("NOSTACK HIST P") leg.Draw() ## Setting axis labels stack.GetXaxis().SetTitle("m_{T2} (True #it{E}_{T}^{miss}) [GeV]") stack.GetYaxis().SetTitle("Normalised Distributions") ## Moving axis tick marks stack.GetYaxis().SetMaxDigits(3) stack.GetXaxis().SetLabelOffset(0.017) ## Drawing all text left = 0.2 size = 1.0 shift = 0.06 pos = 0.88 Plotting.Draw_ATLASLabel(left, pos, "Simulation", scale=1.0) pos -= shift Plotting.Draw_Text(left, pos, "work in progress", scale=size) pos -= shift Plotting.Draw_Lumi(left, pos, 0, scale=size) pos -= shift ## Updating the canvas canvas.Update() out_file = "true_strans.{}".format(extension) canvas.Print(out_file) del canvas return 0
signi_fpr, signi_tpr, _ = metrics.roc_curve(label, signi ) network_fpr, network_tpr, _ = metrics.roc_curve(label, network ) ## Correcting the fpr into a backround rejection rate tight_bck_rej = 1 - tight_fpr signi_bck_rej = 1 - signi_fpr network_bck_rej = 1 - network_fpr ## Converting the lines to tgraphs tight_gr = TGraph( len(tight_tpr), tight_tpr, tight_bck_rej ) signi_gr = TGraph( len(signi_tpr), signi_tpr, signi_bck_rej ) network_gr = TGraph( len(network_tpr), network_tpr, network_bck_rej ) ## Drawing the plots using ROOT canvas = TCanvas( "canvas", 'A basic canvas', 600, 600 ) leg = Plotting.Create_Legend( 0.2, 0.2, 0.7, 0.40, border_size=0, margin=0.1 ) tight_gr.Draw( 'AL' ) signi_gr.Draw( 'L' ) network_gr.Draw( 'L' ) leg.AddEntry( tight_gr, "Tight #it{E}_{T}^{miss}", "l") leg.AddEntry( signi_gr, "Object-based #it{E}_{T}^{miss} Significance", "l") leg.AddEntry( network_gr, "Network #it{E}_{T}^{miss}", "l") ## Changing the colours tight_gr.SetLineColor(4) signi_gr.SetLineColor(801) network_gr.SetLineColor(2) tight_gr.SetLineWidth(2) signi_gr.SetLineWidth(2)
def main(extension, network_name, data_set_name, process, luminosity, WP_list, H1D_hist_list, H1D_tail_list, profile_list, tgraph_list, H2D_hist_list): gROOT.SetStyle("ATLAS") gROOT.ForceStyle() gStyle.SetErrorX(0.5) OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output") hist_dir = os.path.join(OUTPUT_dir, network_name, data_set_name) rootfile_name = os.path.join(hist_dir, "histograms.root") ########## Loading and Plotting the TH1Ds ########## for (var, min, max) in H1D_hist_list: ## Creating the canvas object canvas = TCanvas("canvas_{}".format(var.name), 'A basic canvas', 800, 600) ## Adding a legend leg = Plotting.Create_Legend(0.72, 0.60, 0.95, 0.95, show_stats=True) ## Superimposing all of the graphs into a stack stack = Plotting.Draw_1D_Comp(rootfile_name, var, min, max, WP_list, leg) ## If the stack fails then we skip if stack == -1: continue ## Drawing all text draw_all_text(data_set_name, luminosity, process) ## Updating the canvas canvas.Update() ## Saving the file out_file = "{}/{}_{}.{}".format(hist_dir, data_set_name, var.plotname, extension) canvas.Print(out_file) del canvas ########## Loading and Plotting the Tails ########## for (var, min, max) in H1D_tail_list: ## Creating the canvas object canvas = TCanvas("canvas_tail_{}".format(var.name), 'A basic canvas', 800, 600) ## Adding in the legend leg = Plotting.Create_Legend(0.75, 0.65, 0.95, 0.95) ## Superimposing all of the graphs into a stack filled_stack = Plotting.Draw_1D_Comp(rootfile_name, var, min, max, WP_list, leg, isTail=True) ## If the stack fails then we skip if filled_stack == -1: continue ## Drawing all text draw_all_text(data_set_name, luminosity, process) ## Adding the legend leg.Draw() canvas.Update() canvas.SetLogy() out_file = "{}/{}_{}_tail.{}".format(hist_dir, data_set_name, var.plotname, extension) canvas.Print(out_file) del canvas ########## Profiles and TGraphs ########## for (var_x, var_y, min, max) in profile_list + tgraph_list: ## Are we looking at a TGraph or a TProfile isTgraph = True if (var_x, var_y, min, max) in tgraph_list else False ## Creating the canvas object canvas = TCanvas("canvas_{}_{}".format(var_x.name, var_y.name), 'A basic canvas', 800, 600) ## Adding in the legend leg = Plotting.Create_Legend(0.75, 0.65, 0.95, 0.95) ## Superimposing all of the graphs into a stack filled_stack = Plotting.Draw_2D_Comp(rootfile_name, var_x, var_y, min, max, WP_list, leg, isTgraph=isTgraph) ## If the stack fails then we skip if filled_stack == -1: continue ## Drawing all text draw_all_text(data_set_name, luminosity, process) ## Updating the canvas canvas.Update() out_file = "{}/{}_{}_{}.{}".format(hist_dir, data_set_name, var_x.plotname, var_y.plotname, extension) canvas.Print(out_file) del canvas ########## 2D histograms (each as their own) ########## for (vx, vy) in H2D_hist_list: for wp in WP_list: if wp.name not in ["Tight", "Network"]: continue canvas = TCanvas( "{}_{}_{}_2D".format(vx.plotname, vy.plotname, wp.name), 'A basic canvas', 800, 800) # gStyle.SetPalette(53) # TColor.InvertPalette() graph_name = "2D_{}_vs_{}_{}".format(vx.name, vy.name, wp.name) hist = Plotting.GetGraphFromFile(rootfile_name, graph_name) ylabel = wp.name + " " + vy.x_label + " " + vy.units if wp.name == "Network": if network_name == "flat_ann": ylabel = "(A) " + ylabel if network_name == "smpl_ann": ylabel = "(B) " + ylabel if network_name == "deep_ann": ylabel = "(Z) " + ylabel hist.GetXaxis().SetTitle(vx.x_label + " " + vx.units) hist.GetYaxis().SetTitle(ylabel) # canvas.SetLogz() hist.Draw("col") if vx.name == "Truth" or "Tghtx": xline = TLine(0, 0, 200, 200) xline.SetLineColor(1) xline.SetLineWidth(2) xline.Draw() # dline = TLine(0,0,80,-80) # dline.SetLineColor(1) # dline.SetLineWidth(2) # dline.Draw() else: hist.GetXaxis().SetTitle("E_{x}^{miss} - E_{x}^{miss, true} " + vx.units) hist.GetYaxis().SetTitle("E_{y}^{miss} - E_{y}^{miss, true} " + vy.units) xline = TLine(-50, 0, 50, 0) xline.SetLineColor(1) xline.SetLineWidth(2) xline.Draw() yline = TLine(0, -50, 0, 50) yline.SetLineColor(1) yline.SetLineWidth(2) yline.Draw() hist.GetYaxis().SetMaxDigits(3) hist.GetXaxis().SetLabelOffset(0.017) # Adding in some text and the ATLAS Labels leg = Plotting.Create_Legend(0.19, 0.78, 0.58, 0.94) leg.Draw() Plotting.Draw_ATLASLabel(0.2, 0.89, text="Simulation") Plotting.Draw_Text(0.2, 0.84, text="work in progress", scale=1) Plotting.Draw_Text(0.2, 0.79, text=process, scale=1) canvas.Update() out_file = "{}/{}_{}.{}".format(hist_dir, data_set_name, graph_name, extension) canvas.Print(out_file) return 0
def main(): extension = "pdf" ####### File names and locations ######## network_name = "deep_ann" data_set_name = "ttbar_1in3_tt_strict" #_NOJETS _SOMEJETS ###################################### process = "#it{t#bar{t}}" luminosity = 44307.4 ## Initialising plotting variables Var_Lin = Plotting.Variable( "Lin", "Linear_Response", "#Delta_{T}^{lin}", "", ) Var_Truth = Plotting.Variable("Truth", "TrueET", "True #it{E}_{T}^{miss}", "[GeV]") ymin = -0.2 ymax = 0.5 plot_list = [ ("main_ann", "Tight", "", 4, 21), ("main_ann", "Network", "", 2, 20), ("flat_ann", "Network", "(A) ", 3, 22), ("smpl_ann", "Network", "(B) ", 6, 23), ] ## Creating the plots gROOT.SetStyle("ATLAS") gROOT.ForceStyle() gStyle.SetErrorX(0.5) canvas = TCanvas("canvas_{}_{}".format(Var_Truth.name, Var_Lin.name), 'A basic canvas', 800, 600) ## Adding in the legend leg = Plotting.Create_Legend(0.65, 0.70, 0.95, 0.95) ## Creating the stack stack = THStack("stack", Var_Truth.name + Var_Lin.name) stack.SetMinimum(ymin) stack.SetMaximum(ymax) ## Cycling through the different network directories for network_name, wp_name, prefix, colour, marker in plot_list: OUTPUT_dir = os.path.join(os.environ["HOME_DIRECTORY"], "Output") hist_dir = os.path.join(OUTPUT_dir, network_name, data_set_name) rootfile_name = os.path.join(hist_dir, "histograms.root") ## Creating the working point variable wp = Plotting.WorkingPoint(wp_name) ## Generating the graph name graph_name = "{}_vs_{}_{}".format(Var_Truth.name, Var_Lin.name, wp.name) ## Loading the graph using its name and file location graph = Plotting.GetGraphFromFile(rootfile_name, graph_name) if graph == -1: continue ## Setting the colors specific to the working point graph.SetLineColor(colour) graph.SetMarkerColor(colour) graph.SetMarkerStyle(marker) ## Adding the legend entry leg.AddEntry(graph, prefix + wp.name, "p") ## Adding the object to the stack stack.Add(graph) del graph ## Drawing the stack on the currrent canvas stack.Draw("nostack") leg.Draw() ## Setting axis labels stack.GetXaxis().SetTitle(Var_Truth.x_label + " " + Var_Truth.units) stack.GetYaxis().SetTitle(Var_Lin.x_label + " " + Var_Lin.units) ## Moving axis tick marks stack.GetYaxis().SetMaxDigits(3) stack.GetXaxis().SetLabelOffset(0.017) ## Drawing all text left = 0.29 size = 1.0 shift = 0.06 pos = 0.88 Plotting.Draw_ATLASLabel(left, pos, "Simulation", scale=1.0) pos -= shift Plotting.Draw_Text(left, pos, "work in progress", scale=size) pos -= shift Plotting.Draw_Lumi(left, pos, 0, scale=size) pos -= shift Plotting.Draw_Text(left, pos, process, scale=size) pos -= shift ## Updating the canvas canvas.Update() out_file = "response_comparison_zoom.{}".format(extension) canvas.Print(out_file) del canvas return 0
def STACK(extension, OUTPUT_dir, network_name, luminosity, output_flag, output_mod, generator, extra_text, fudge, text, MC_list, Real_Data, var, wp=None): is_wp = False if wp is None else True ## The name of the histogram to be used hist_name = "{}_{}".format(var.name, wp.name) if is_wp else var.name ## Creating the canvas canvas = TCanvas("canvas" + hist_name, 'A basic canvas', 800, 800) ## Creating the first pad to contain the basic histogram plots histo_pad = TPad("histo_pad", "histo_pad", 0, 0.25, 1.0, 1.0) histo_pad.SetBottomMargin(0) histo_pad.Draw() histo_pad.cd() # if is_wp: histo_pad.SetLogy() ## Adding in the legend leg = Plotting.Create_Legend(0.70, 0.45, 0.95, 0.95, text_size=0.045) ## Creating a stack for the canvas stack = THStack("stack", var.name) hist_list = [] integral_list = [] for s, mc in enumerate(MC_list): rootfile_name = os.path.join(OUTPUT_dir, network_name, mc.file, "histograms.root") hist = Plotting.GetGraphFromFile(rootfile_name, hist_name) if hist == -1: continue hist.Scale(fudge * luminosity) hist.SetMarkerColor(mc.colour) hist.SetFillColor(mc.colour) hist.SetLineColor(1) hist.SetLineWidth(1) hist.SetMarkerStyle(0) leg.AddEntry(hist, mc.label, "f") integral_list.append(hist.Integral(0, hist.GetNbinsX() + 1)) hist.Scale(1, "width") hist_list.append(hist) if s == 0: MC_Total = hist.Clone() else: MC_Total.Add(hist.Clone()) del hist ## If the list is empty and no histograms were found then we skip the rest if not integral_list and is_wp: print("No MC histograms found for working point {}\n".format(wp.name)) return 0 ## Now we use the integrals to sort the histograms in descending order ord_idx = np.argsort(integral_list) ## Adding the histograms to the stack for i in ord_idx: # Draw the object on the stack stack.Add(hist_list[i]) stack.Draw("HIST") stack.GetYaxis().SetTitleOffset(1.2) ## Adding the MC Statistical, lumi and xsec uncertainty for i in range(1, MC_Total.GetNbinsX() + 1): if MC_Total.GetBinContent(i) > 0: e = MC_Total.GetBinError(i) h = MC_Total.GetBinContent(i) MC_Total.SetBinError(i, h * np.sqrt((e / h)**2 + 0.05**2 + 0.02**2)) MC_Total.SetMarkerSize(0.) MC_Total.SetFillStyle(3001) MC_Total.SetMarkerColor(1) MC_Total.SetFillColor(2) MC_Total.Draw("E2 SAME") ## Adding the data points rootfile_name = os.path.join(OUTPUT_dir, network_name, Real_Data.file, "histograms.root") real_hist = Plotting.GetGraphFromFile(rootfile_name, hist_name) real_hist.SetLineColor(1) real_hist.SetMarkerColor(Real_Data.colour) real_hist.SetMarkerStyle(20) real_hist.SetMarkerSize(1.2) real_int = real_hist.Integral(0, real_hist.GetNbinsX() + 1) leg.AddEntry(real_hist, Real_Data.label, "p") real_hist.Scale(1, "width") real_hist.Draw("e1p SAME") print("Real events = {:}".format(real_int)) print("MC events = {:}".format(sum(integral_list))) print("Fudge Factor = {:10.10}".format(real_int / sum(integral_list))) # print( "Z events = {:}".format( sum(integral_list[:3]) ) ) # print( "Non Z events = {:}".format( sum(integral_list[3:]) ) ) # print( "Z Factor = {:10.10}".format( ( real_int - sum(integral_list[3:]) ) / sum(integral_list[:3]) ) ) ## Adding in some text and the ATLAS Labels # if fudge != 1: text += " (#uparrow{:.2f}%)".format(fudge*100-100) s = 1.0 pos = 0.88 shift = 0.05 Plotting.Draw_ATLASLabel(0.20, pos, "work in progress", scale=1.0) pos -= shift + 0.01 Plotting.Draw_Lumi(0.20, pos, luminosity, scale=s) pos -= shift Plotting.Draw_Text(0.20, pos, text, scale=s) pos -= shift Plotting.Draw_Text(0.20, pos, generator, scale=s) Plotting.Draw_Text(0.55, 0.8, extra_text) leg.Draw() ## Plotting axis and limits stack.GetYaxis().SetTitle("Events per GeV") if var.ymin is not None: stack.SetMinimum(var.ymin) if var.ymax is not None: stack.SetMaximum(var.ymax) if var.xmin is not None and var.xmax is not None: stack.GetXaxis().SetLimits(var.xmin, var.xmax) ## Creating the ratio pad canvas.cd() ratio_pad = TPad("ratio_pad", "ratio_pad", 0, 0, 1.0, 0.25) ratio_pad.SetTopMargin(0.0) ratio_pad.SetBottomMargin(0.3) ratio_pad.Draw() ratio_pad.cd() ratio_pad.SetGrid(0, 1) ## Plotting the the MC error histogram MC_flat = MC_Total.Clone() for i in range(1, MC_Total.GetNbinsX() + 1): MC_flat.SetBinContent(i, 1) if MC_Total.GetBinContent(i) > 0: MC_flat.SetBinError( i, MC_Total.GetBinError(i) / MC_Total.GetBinContent(i)) MC_flat.SetMarkerSize(0.) MC_flat.SetFillStyle(3001) MC_flat.SetMarkerColor(1) MC_flat.SetFillColor(2) MC_flat.SetMaximum(1.6) MC_flat.SetMinimum(0.4) MC_flat.Draw("E2") leg.AddEntry(MC_flat, "MC stat unc", "f") ## Plotting the ratio of MC to Data ratio_hist = real_hist.Clone() mcratio_hist = MC_Total.Clone() mcratio_hist.SetError(arr.array('d', [0])) ratio_hist.Divide(mcratio_hist) ratio_hist.Draw("ep SAME") ## Plotting axis and limits if var.xmin is not None and var.xmax is not None: ratio_hist.GetXaxis().SetRange(var.xmin, var.xmax) x_title = wp.name + " " + var.x_label + " " + var.units if is_wp else var.x_label + " " + var.units MC_flat.GetXaxis().SetTitle(x_title) MC_flat.GetXaxis().SetTitleSize(25) MC_flat.GetXaxis().SetTitleFont(43) MC_flat.GetXaxis().SetTitleOffset(4) MC_flat.GetXaxis().SetLabelFont(43) MC_flat.GetXaxis().SetLabelSize(20) MC_flat.GetYaxis().SetTitle("Data/MC") MC_flat.GetYaxis().SetNdivisions(206, False) MC_flat.GetYaxis().ChangeLabel(-1, -1, -1, -1, -1, -1, " ") MC_flat.GetYaxis().ChangeLabel(1, -1, -1, -1, -1, -1, " ") MC_flat.GetYaxis().SetTitleSize(25) MC_flat.GetYaxis().SetTitleFont(43) MC_flat.GetYaxis().SetTitleOffset(1.55) MC_flat.GetYaxis().SetLabelFont(43) MC_flat.GetYaxis().SetLabelSize(20) ## Adding in a line on the x-axis line = TLine(ratio_hist.GetBinLowEdge(1), 1, ratio_hist.GetBinLowEdge(ratio_hist.GetNbinsX() + 1), 1) line.SetLineWidth(1) line.SetLineColor(1) line.SetLineStyle(9) line.Draw() ## Saving the canvas canvas.Update() out_folder = "{}/{}/Stack".format(OUTPUT_dir, network_name) if not os.path.exists(out_folder): os.system("mkdir -p {}".format(out_folder)) out_file = os.path.join(out_folder, "{}_{}".format(output_flag, var.name)) if is_wp: out_file += "_" + wp.name out_file += "_{}".format(generator) if "0 Jets" in extra_text: out_file += "_NOJETS" out_file += "{}.{}".format(output_mod, extension) canvas.Print(out_file)