def PlotEfficiency(htest_s, htest_b, saveDir, saveName, saveFormats): ROOT.gStyle.SetOptStat(0) canvas = plot.CreateCanvas() canvas.cd() canvas.SetLeftMargin(0.145) canvas.SetRightMargin(0.11) # Calculate signal and background efficiency vs output xvalue, eff_s, eff_b, error = CalcEfficiency(htest_s, htest_b) graph_s = plot.GetGraph(xvalue, eff_s, error, error, error, error) graph_b = plot.GetGraph(xvalue, eff_b, error, error, error, error) plot.ApplyStyle(graph_s, ROOT.kBlue) plot.ApplyStyle(graph_b, ROOT.kRed) # Calculate significance vs output h_signif0, h_signif1 = CalcSignificance(htest_s, htest_b) plot.ApplyStyle(h_signif0, ROOT.kGreen) plot.ApplyStyle(h_signif1, ROOT.kGreen + 3) #=== Get maximum of significance maxSignif0 = h_signif0.GetMaximum() maxSignif1 = h_signif1.GetMaximum() maxSignif = max(maxSignif0, maxSignif1) # Normalize significance h_signifScaled0 = h_signif0.Clone("signif0") h_signifScaled0.Scale(1. / float(maxSignif)) h_signifScaled1 = h_signif1.Clone("signif1") h_signifScaled1.Scale(1. / float(maxSignif)) #Significance: Get new maximum ymax = max(h_signifScaled0.GetMaximum(), h_signifScaled1.GetMaximum()) for obj in [graph_s, graph_b, h_signifScaled0, h_signifScaled1]: obj.GetXaxis().SetTitle("Output") obj.GetYaxis().SetTitle("Efficiency") obj.SetMaximum(ymax * 1.1) obj.SetMinimum(0) #Draw h_signifScaled0.Draw("HIST") h_signifScaled1.Draw("HIST SAME") graph_s.Draw("PL SAME") graph_b.Draw("PL SAME") graph = plot.CreateGraph([0.5, 0.5], [0, ymax * 1.1]) graph.Draw("same") #Legend leg = plot.CreateLegend(0.50, 0.25, 0.85, 0.45) leg.AddEntry(graph_s, "Signal Efficiency", "l") leg.AddEntry(graph_b, "Bkg Efficiency", "l") leg.AddEntry(h_signifScaled0, "S/#sqrt{S+B}", "l") leg.AddEntry(h_signifScaled1, "2#times(#sqrt{S+B} - #sqrt{B})", "l") leg.Draw() # Define Right Axis (Significance) signifColor = ROOT.kGreen + 2 rightAxis = ROOT.TGaxis(1, 0, 1, 1.1, 0, 1.1 * maxSignif, 510, "+L") rightAxis.SetLineColor(signifColor) rightAxis.SetLabelColor(signifColor) rightAxis.SetTitleColor(signifColor) rightAxis.SetTitleOffset(1.25) rightAxis.SetLabelOffset(0.005) rightAxis.SetLabelSize(0.04) rightAxis.SetTitleSize(0.045) rightAxis.SetTitle("Significance") rightAxis.Draw() plot.SavePlot(canvas, saveDir, saveName, saveFormats) canvas.Close() return
def main(): ROOT.gStyle.SetOptStat(0) # Apply tdr style style = tdrstyle.TDRStyle() style.setOptStat(False) style.setGridX(False) style.setGridY(False) # Definitions filename = opts.filename tfile = ROOT.TFile.Open(filename) dName = "" if ("TT" in filename): dName = "TT" elif ("QCD" in filename): dName = "QCD" #Signal and background branches signal = uproot.open(filename)["treeS"] background = uproot.open(filename)["treeB"] # Input list inputList = [] inputList.append("TrijetPtDR") inputList.append("TrijetDijetPtDR") inputList.append("TrijetBjetMass") inputList.append("TrijetLdgJetBDisc") inputList.append("TrijetSubldgJetBDisc") inputList.append("TrijetBJetLdgJetMass") inputList.append("TrijetBJetSubldgJetMass") inputList.append("TrijetMass") inputList.append("TrijetDijetMass") inputList.append("TrijetBJetBDisc") inputList.append("TrijetSoftDrop_n2") inputList.append("TrijetLdgJetCvsL") inputList.append("TrijetSubldgJetCvsL") inputList.append("TrijetLdgJetPtD") inputList.append("TrijetSubldgJetPtD") inputList.append("TrijetLdgJetAxis2") inputList.append("TrijetSubldgJetAxis2") inputList.append("TrijetLdgJetMult") inputList.append("TrijetSubldgJetMult") nInputs = len(inputList) #Signal and background dataframes df_signal = signal.pandas.df(inputList) df_background = background.pandas.df(inputList) # Number of events to predict the output and plot the top-quark mass if (opts.plotSignal): nEvts = len(df_signal.index) else: nEvts = len(df_background.index) # User few events for testing if (opts.test): nEvts = 1000 print "=== Number of events: ", nEvts #Signal and background datasets dset_signal = df_signal.values dset_background = df_background.values # Concat signal, background datasets df_list = [df_signal, df_background] df_all = pandas.concat(df_list) dataset = df_all.values # Target (top-quark mass) datasets dset_target_all = pandas.concat([ signal.pandas.df(["TrijetMass"]), background.pandas.df(["TrijetMass"]) ]).values dset_target_bkg = background.pandas.df(["TrijetMass"]).values dset_target_signal = signal.pandas.df(["TrijetMass"]).values # Signal and background inputs X_signal = dset_signal[:nEvts, 0:nInputs] X_background = dset_background[:nEvts, 0:nInputs] if (opts.plotSignal): X = dset_signal[:nEvts, 0:nInputs] target = dset_target_signal[:nEvts, :] else: X = dset_background[:nEvts, 0:nInputs] target = dset_target_bkg[:nEvts, :] #Load models lamValues = [0, 1, 5, 10, 20] #, 100, 500] # fixme! should be given as option colors = [ ROOT.kBlack, ROOT.kBlue, ROOT.kMagenta, ROOT.kOrange, ROOT.kRed, ROOT.kGreen, ROOT.kOrange + 7 ] canvas = plot.CreateCanvas() canvas.cd() ymaxFactor = 1.1 ymax = 0 histoList = [] if (opts.setLogY): canvas.SetLogy() ymaxFactor = 2 # load the models with different lambda for lam in lamValues: print "Lambda = ", lam if (lam == 0): # For labda = 0 load the simple classification (sequential) model loaded_model = load_model( 'models_16Nov/Model_relu_relu_relu_sigmoid.h5') else: loaded_model = load_model( 'models_16Nov/modelANN_32_Adam_0p0008_500_tanh_relu_msle_lam%s.h5' % (lam)) # Compile the model loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc']) Y = loaded_model.predict(X, verbose=1) # Concatenate Y (predicted output) and target (top-quark mass) # Ymass 0 column: output # Ymass 1st column: top-quark mass Ymass = numpy.concatenate((Y, target), axis=1) # Get selected top candidates (pass the output working point) Ymass_sel = Ymass[Ymass[:, 0] >= opts.wp] # Select samples with y > WP (col 0) massSel = Ymass_sel[:, 1] # Get the top-quark mass (col 1) for the selected samples. #Plot resutls nbins = 100 xmin = 0 xmax = 1000 # Change x-axis range when plotting the signal if (opts.plotSignal): nbins = 45 xmax = 450 width = float(xmax) / nbins histo = ROOT.TH1F('histo_lam%s' % (lam), '', nbins, xmin, xmax) mass_sel = [] print "selected entries:", len(massSel) for mass in massSel: histo.Fill(mass) histoList.append(histo.Clone("histoClone_lam%s" % lam)) del loaded_model # Create legend leg = plot.CreateLegend(0.6, 0.67, 0.9, 0.85) #Legend text dText = dName dText = dText.replace("TT", "t#bar{t}") # Loop over the histograms in the histoList for i in range(len(histoList)): leg.AddEntry(histoList[i], "%s (#lambda = %.0f)" % (dText, lamValues[i]), "f") # Normalize histograms to unity histoList[i].Scale(1. / (histoList[i].Integral())) ymax = max(ymax, histoList[i].GetMaximum()) histoList[i].GetXaxis().SetTitle("m_{top} (GeV)") histoList[i].GetYaxis().SetTitle("Arbitrary Units / %.0f GeV" % (width)) plot.ApplyStyle(histoList[i], colors[i]) histoList[i].Draw("HIST same") for i in range(len(histoList)): histoList[i].SetMaximum(ymax * ymaxFactor) leg.Draw("same") if (opts.plotSignal): sbText = "truth-matched" else: sbText = "unmatched" # Additional text tex1 = plot.Text(" %s top candidates" % sbText, 0.85, 0.5) tex2 = plot.Text(" with output value > %s" % (opts.wp), 0.82, 0.45) #Draw text tex1.Draw() tex2.Draw() # Draw line to indicate the real value of the top-quark mass graph = plot.CreateGraph([173., 173.], [0, ymax * ymaxFactor]) graph.Draw("same") # CMS extra text and lumi text #plot.CMSText("CMS Preliminary") #Fixme! cmsExtra doesn't work #cmsText.Draw() # Output directory dirName = plot.getDirName(opts.saveDir) # Save the plot saveName = opts.saveName if (opts.saveName == "TopMassANN"): saveName = "TopMassANN_%s_%s" % (dName, sbText.replace("-", "_")) # Save the histogram plot.SavePlot(canvas, dirName, saveName)
def PlotAndWriteJSON(signal, bkg, saveDir, saveName, jsonWr, saveFormats, **kwargs): resultsDict = {} resultsDict["signal"] = signal resultsDict["background"] = bkg normalizeToOne = False if "normalizeToOne" in kwargs: normalizeToOne = kwargs["normalizeToOne"] # Create canvas ROOT.gStyle.SetOptStat(0) canvas = plot.CreateCanvas() canvas.cd() hList = [] gList = [] yMin = 100000 yMax = -1 xMin = 0.0 xMax = 1.0 nBins = 50 xTitle = "DNN output" yTitle = "Entries" log = True if "log" in kwargs: log = kwargs["log"] if "xTitle" in kwargs: xTitle = kwargs["xTitle"] if "yTitle" in kwargs: yTitle = kwargs["yTitle"] elif "output" in saveName.lower(): xTitle = "Entries" elif "efficiency" in saveName.lower(): xTitle = "Efficiency" elif "significance" in saveName.lower(): xTitle = "Significance" else: pass if "xMin" in kwargs: xMin = kwargs['xMin'] if "xMax" in kwargs: xMax = kwargs['xMax'] if "nBins" in kwargs: xBins = kwargs['nBins'] # For-loop: for i, key in enumerate(resultsDict.keys(), 0): h = ROOT.TH1F(key, '', nBins, xMin, xMax) for j, x in enumerate(resultsDict[key], 0): h.Fill(x) try: yMin = min(x[0], yMin) except: pass # Save maximum yMax = max(h.GetMaximum(), yMax) # Customise & append to list plot.ApplyStyle(h, i + 1) if normalizeToOne: if "ymax" in kwargs: yMax = kwargs["yMax"] else: yMax = 1.0 if "yMin" in kwargs: yMin = kwargs["yMin"] else: yMin = 1e-4 if h.Integral() > 0.0: h.Scale(1. / h.Integral()) hList.append(h) if yMin <= 0.0: yMin = 100 if log: canvas.SetLogy() # For-loop: All histograms for i, h in enumerate(hList, 0): h.SetMinimum(yMin * 0.85) h.SetMaximum(yMax * 1.15) h.GetXaxis().SetTitle(xTitle) h.GetYaxis().SetTitle(yTitle) if i == 0: h.Draw("HIST") else: h.Draw("HIST SAME") # Create legend leg = plot.CreateLegend(0.6, 0.75, 0.9, 0.85) for h in hList: leg.AddEntry(h, h.GetName(), "l") leg.Draw() plot.SavePlot(canvas, saveDir, saveName, saveFormats) canvas.Close() # Create TGraph for h in hList: gList.append(convertHistoToGaph(h)) # Write the Tgraph into the JSON file for gr in gList: gName = "%s_%s" % (saveName, gr.GetName()) jsonWr.addGraph(gName, gr) return
def PlotAndWriteJSON(signal, bkg, saveDir, saveName, jsonWr, saveFormats): resultsDict = {} resultsDict["signal"] = signal resultsDict["background"] = bkg # Create canvas ROOT.gStyle.SetOptStat(0) canvas = plot.CreateCanvas() canvas.cd() hList = [] gList = [] yMin = 100000 yMax = -1 # For-loop: for i, key in enumerate(resultsDict.keys(), 0): h = ROOT.TH1F(key, '', 50, 0.0, 1.0) for j, x in enumerate(resultsDict[key], 0): h.Fill(x) try: yMin = min(x[0], yMin) except: pass # Save maximum yMax = max(h.GetMaximum(), yMax) # Customise & append to list plot.ApplyStyle(h, i + 1) hList.append(h) if yMin <= 0.0: yMin = 100 canvas.SetLogy() # For-loop: All histograms for i, h in enumerate(hList, 0): h.SetMinimum(yMin * 0.85) h.SetMaximum(yMax * 1.15) h.GetXaxis().SetTitle("DNN output") if "output" in saveName.lower(): h.GetYaxis().SetTitle("Entries") elif "efficiency" in saveName.lower(): h.GetYaxis().SetTitle("Efficiency") elif "significance" in saveName.lower(): h.GetYaxis().SetTitle("Significance") else: pass if i == 0: h.Draw("HIST") else: h.Draw("HIST SAME") # Create legend leg = plot.CreateLegend(0.6, 0.75, 0.9, 0.85) for h in hList: leg.AddEntry(h, h.GetName(), "l") leg.Draw() plot.SavePlot(canvas, saveDir, saveName, saveFormats) canvas.Close() # Create TGraph for h in hList: gList.append(convertHistoToGaph(h)) # Write the Tgraph into the JSON file for gr in gList: gName = "%s_%s" % (saveName, gr.GetName()) jsonWr.addGraph(gName, gr) return