def plot_hists(bg_hists,
               sig_hist,
               param,
               legend_labels,
               extra_output_name="",
               options=None):

    if not options:
        options = {
            "yaxis_log": True,
            "xaxis_range": [0, 500],
        }

    options["output_name"] = output_prefix + extra_output_name + param + ".pdf"
    options["lumi_value"] = 137.2
    options["legend_smart"] = False
    options["legend_percentageinbox"] = False
    options["cms_label"] = "Preliminary"
    options["xaxis_label"] = param

    ply.plot_hist(bgs=bg_hists,
                  sigs=sig_hist,
                  legend_labels=legend_labels,
                  sig_labels=["HH to #gamma#gamma#tau#tau signal"],
                  options=options)
def plot_occupancy(hist, prefix):
    filename_prefix = prefix.replace(" ", "_")
    filename_prefix = folder_prefix + filename_prefix
    #Fancy way to find xaxis range
    nonzero_flag = False
    ymax = 0
    xaxis_range = [0, 500]
    for i in range(1, hist.GetNbinsX() - 1):
        if hist.GetBinContent(i) != 0:
            if ymax < hist.GetBinContent(i):
                ymax = hist.GetBinContent(i)
    #Fancy way to find xaxis range
    nonzero_flag = False
    ymax = 0
    xaxis_range = [0, 100]
    summary_upper_limit = -1
    for i in range(1, hist.GetNbinsX() - 1):
        if hist.GetBinContent(i) != 0:
            if ymax < hist.GetBinContent(i):
                ymax = hist.GetBinContent(i)
            if i > 2 and nonzero_flag == False:
                xaxis_range[0] = hist.GetBinLowEdge(i - 2)
            nonzero_flag = True
        if hist.GetBinContent(i) != 0 and hist.GetBinContent(
                i + 1) == 0 and nonzero_flag == True:
            xaxis_range[1] = hist.GetBinLowEdge(i + 2)

        if nonzero_flag and hist.Integral(0, i) / hist.Integral(
                0, -1) > 0.99 and summary_upper_limit < 0:
            summary_upper_limit = hist.GetBinLowEdge(i + 1)

    url = (filename_prefix + ".pdf").replace(
        "/home/users/bsathian/public_html/",
        "http://uaf-10.t2.ucsd.edu/~bsathian/")

    if nonzero_flag == False:
        mdf.write("|{}|{}|{}|[plot]({})|\n".format(prefix, 0, 0, url))
    else:
        mdf.write("|{}|{}|{}|[plot]({})|\n".format(prefix, xaxis_range[0],
                                                   summary_upper_limit, url))

    ply.plot_hist(bgs=[hist],
                  legend_labels=[prefix],
                  options={
                      "output_name": filename_prefix + ".pdf",
                      "xaxis_range": xaxis_range,
                      "xaxis_label": prefix,
                      "title": prefix,
                      "yaxis_log": True,
                      "yaxis_range": [0.1, ymax],
                      "legend_percentageinbox": False,
                  })
Example #3
0
h2.Scale(1. / h2.Integral())

for i in xrange(10000, 100001, 10000):
    model = load_model("gen_%d.weights" % i)
    model.summary()
    noise = np.random.normal(0, 1, (50000, 8))
    #print(noise)
    gen_imgs = model.predict(noise)
    img = gen_imgs
    masses = M(img[:, 0], img[:, 1], img[:, 2], img[:, 3], img[:, 4],
               img[:, 5], img[:, 6], img[:, 7])

    print(masses)

    print("mean: %s, std: %s " % (masses.mean(), masses.std()))

    h1 = r.TH1F("h1", "masses", 60, 60, 120)
    plu.fast_fill(h1, masses)
    #for val in masses: h1.Fill(val)

    h1.Scale(1. / h1.Integral())

    ply.plot_hist(
        bgs=[h1, h2],
        legend_labels=["pred", "truth"],
        options={
            "do_stack": False,
            "yaxis_log": False,
            #"ratio_numden_indices": [1,0],
            "output_name": "masses_epoch_%d.pdf" % i,
        })
Example #4
0
def plot_hist_1d(hdata=None,
                 hbkgs=[],
                 hsigs=[],
                 hsyst=None,
                 options={},
                 colors=[],
                 sig_labels=[],
                 legend_labels=[]):
    """
    Wrapper function to call Plottery.
    """

    # Sanity check. If no histograms exit
    if not hdata and len(hbkgs) == 0 and len(hsigs) == 0:
        print "[plottery_wrapper] >>> Nothing to do!"
        return

    # If a blind option is set, blind the data histogram to None
    # The later step will set the histogram of data to all zero
    if "blind" in options:
        if options["blind"]:
            hdata = None
        del options["blind"]

    # If hdata is none clone one hist and fill with 0
    if not hdata:
        if len(hbkgs) != 0:
            hdata = hbkgs[0].Clone("Data")
            hdata.Reset()
        elif len(hsigs) != 0:
            hdata = hsigs[0].Clone("Data")
            hdata.Reset()

    # Compute some arguments that are missing (viz. colors, sig_labels, legend_labels)
    hsig_labels = []
    if len(sig_labels) == 0:
        for hsig in hsigs:
            hsig_labels.append(hsig.GetName())
    hcolors = colors
    if len(colors) == 0:
        for index, hbg in enumerate(hbkgs):
            hcolors.append(2001 + index)
    hlegend_labels = []
    if len(legend_labels) == 0:
        for hbg in hbkgs:
            hlegend_labels.append(hbg.GetName())

    # Set maximum of the plot
    totalbkg = None
    if len(hbkgs) != 0:
        totalbkg = get_total_hist(hbkgs)
    yaxismax = get_max_yaxis_range([hdata, totalbkg]) * 1.8

    # Once maximum is computed, set the y-axis label location
    if yaxismax < 0.01:
        options["yaxis_title_offset"] = 1.8
    elif yaxismax < 0.1:
        options["yaxis_title_offset"] = 1.6
    elif yaxismax < 1.:
        options["yaxis_title_offset"] = 1.5
    elif yaxismax < 100:
        options["yaxis_title_offset"] = 1.2
    elif yaxismax < 1000:
        options["yaxis_title_offset"] = 1.45
    elif yaxismax < 10000:
        options["yaxis_title_offset"] = 1.6
    else:
        options["yaxis_title_offset"] = 1.8

    # Print histogram content for debugging
    #totalbkg.Print("all")
    #if len(hsigs) > 0:
    #    hsigs[0].Print("all")
    #for hbg in hbkgs:
    #    hbg.Print("all")

    # Print yield table if the option is turned on
    if "print_yield" in options:
        if options["print_yield"]:
            print_yield_table(hdata, hbkgs, hsigs, hsyst, options)
        del options["print_yield"]

    # Inject signal option
    if "inject_signal" in options:
        if options["inject_signal"]:
            if len(hsigs) > 0:
                hdata = hsigs[0].Clone("test")
                hdata.Reset()
                for hsig in hsigs:
                    hdata.Add(hsig)
                for hbkg in hbkgs:
                    hdata.Add(hbkg)
                for i in xrange(1, hdata.GetNbinsX() + 1):
                    hdata.SetBinError(i, 0)
                options["legend_datalabel"] = "Sig+Bkg"
        del options["inject_signal"]

    # If hsyst is not provided, compute one yourself from the bkg histograms
    if not hsyst:
        hsyst = get_total_err_hist(hbkgs)

    # The uncertainties are all accounted in the hsyst so remove all errors from bkgs
    remove_errors(hbkgs)

    # Here are my default options for plottery
    if not "canvas_width" in options: options["canvas_width"] = 604
    if not "canvas_height" in options: options["canvas_height"] = 728
    if not "yaxis_range" in options: options["yaxis_range"] = [0., yaxismax]
    if not "legend_ncolumns" in options: options["legend_ncolumns"] = 2
    if not "legend_alignment" in options:
        options["legend_alignment"] = "topleft"
    if not "legend_smart" in options: options["legend_smart"] = True
    if not "legend_scalex" in options: options["legend_scalex"] = 1.2
    if not "legend_scaley" in options: options["legend_scaley"] = 1.2
    if not "legend_border" in options: options["legend_border"] = False
    if not "legend_percentageinbox" in options:
        options["legend_percentageinbox"] = False
    if not "hist_line_none" in options: options["hist_line_none"] = True
    if not "show_bkg_errors" in options: options["show_bkg_errors"] = False
    if not "ratio_range" in options: options["ratio_range"] = [0.7, 1.3]
    if not "ratio_name_size" in options: options["ratio_name_size"] = 0.13
    if not "ratio_name_offset" in options: options["ratio_name_offset"] = 0.6
    if not "ratio_xaxis_label_offset" in options:
        options["ratio_xaxis_label_offset"] = 0.06
    if not "ratio_yaxis_label_offset" in options:
        options["ratio_yaxis_label_offset"] = 0.03
    if not "ratio_xaxis_title_offset" in options:
        options["ratio_xaxis_title_offset"] = 1.40
    if not "ratio_xaxis_title_size" in options:
        options["ratio_xaxis_title_size"] = 0.13
    if not "ratio_label_size" in options: options["ratio_label_size"] = 0.13
    if not "canvas_tick_one_side" in options:
        options["canvas_tick_one_side"] = True
    if not "canvas_main_y1" in options: options["canvas_main_y1"] = 0.2
    if not "canvas_main_topmargin" in options:
        options["canvas_main_topmargin"] = 0.2 / 0.7 - 0.2
    if not "canvas_main_rightmargin" in options:
        options["canvas_main_rightmargin"] = 50. / 600.
    if not "canvas_main_bottommargin" in options:
        options["canvas_main_bottommargin"] = 0.2
    if not "canvas_main_leftmargin" in options:
        options["canvas_main_leftmargin"] = 130. / 600.
    if not "canvas_ratio_y2" in options: options["canvas_ratio_y2"] = 0.342
    if not "canvas_ratio_topmargin" in options:
        options["canvas_ratio_topmargin"] = 0.05
    if not "canvas_ratio_rightmargin" in options:
        options["canvas_ratio_rightmargin"] = 50. / 600.
    if not "canvas_ratio_bottommargin" in options:
        options["canvas_ratio_bottommargin"] = 0.4
    if not "canvas_ratio_leftmargin" in options:
        options["canvas_ratio_leftmargin"] = 130. / 600.
    if not "xaxis_title_size" in options: options["xaxis_title_size"] = 0.06
    if not "yaxis_title_size" in options: options["yaxis_title_size"] = 0.06
    if not "xaxis_label_size_scale" in options:
        options["xaxis_label_size_scale"] = 1.4
    if not "yaxis_label_size_scale" in options:
        options["yaxis_label_size_scale"] = 1.4
    if not "xaxis_label_offset_scale" in options:
        options["xaxis_label_offset_scale"] = 4.0
    if not "yaxis_label_offset_scale" in options:
        options["yaxis_label_offset_scale"] = 4.0
    if not "xaxis_tick_length_scale" in options:
        options["xaxis_tick_length_scale"] = -0.8
    if not "yaxis_tick_length_scale" in options:
        options["yaxis_tick_length_scale"] = -0.8
    if not "ratio_tick_length_scale" in options:
        options["ratio_tick_length_scale"] = -1.0
    if not "output_name" in options: options["output_name"] = "plot.png"
    if not "cms_label" in options: options["cms_label"] = "Preliminary"
    if not "lumi_value" in options: options["lumi_value"] = "35.9"
    if not "bkg_err_fill_style" in options:
        options["bkg_err_fill_style"] = 3245
    if not "bkg_err_fill_color" in options: options["bkg_err_fill_color"] = 1

    # Call Plottery! I hope you win the Lottery!
    p.plot_hist(data=hdata,
                bgs=hbkgs,
                sigs=hsigs,
                syst=hsyst,
                sig_labels=hsig_labels,
                colors=hcolors,
                legend_labels=hlegend_labels,
                options=options)

    # Set permission
    os.system("chmod 644 {}".format(options["output_name"]))

    # Call nice plots
    copy_nice_plot_index_php(options)
Example #5
0
#!/bin/env python

from plottery import plottery as ply
import ROOT as r

tfile = r.TFile("output.root")

h_mmumu = tfile.Get("Mmumu")

import os
if not os.path.exists("plots"):
    os.makedirs("plots")

ply.plot_hist(bgs=[h_mmumu], options = { "output_name": "plots/Mmumu.pdf", "output_ic": True, })
Example #6
0
    truth = quantities_real[key]
    hpred = r.TH1F("hpred", "hpred", 100,
                   truth.mean() - 3. * truth.std(),
                   truth.mean() + 3. * truth.std())
    htruth = r.TH1F("htruth", "htruth", 100,
                    truth.mean() - 3. * truth.std(),
                    truth.mean() + 3. * truth.std())
    plu.fill_fast(hpred, pred)
    plu.fill_fast(htruth, truth)

    hpred.Scale(1. / hpred.Integral())
    htruth.Scale(1. / htruth.Integral())

    ply.plot_hist(bgs=[htruth, hpred],
                  colors=[r.kAzure + 2, r.kRed - 2],
                  legend_labels=["Real", "Fake"],
                  options={
                      "bkg_sort_method": "unsorted",
                      "do_stack": False,
                      "legend_percentageinbox": False,
                      "legend_alignment": "bottom right",
                      "legend_scalex": 0.7,
                      "legend_scaley": 0.7,
                      "legend_border": False,
                      "xaxis_label": label_map[key],
                      "yaxis_label": "Entries",
                      "output_name": "plots/compare_{}.pdf".format(key),
                      "ratio_numden_indices": [1, 0],
                      "output_ic": True,
                  })