Example #1
0
def make_slides_tex_file(slides_tex_file, slides_dict):
    """Generate TeX file for slides contents

    Parameters
    ----------
    slides_tex_file : str
        Filename for slides TeX file to be written
    slides_dict : dict
        Dict of slides contents
    """
    with open(slides_tex_file, "w") as slides:
        for slide in slides_dict:
            log.debug("Writing slide")
            template = None
            num_plots = len(slide.get('plots', ''))
            if num_plots == 1:
                template = bst.one_plot_slide
            elif num_plots == 2:
                template = bst.two_plot_slide
            elif num_plots == 3:
                template = bst.three_plot_slide
            elif num_plots <= 4:
                template = bst.four_plot_slide
            elif num_plots <= 6:
                template = bst.six_plot_slide
            else:
                raise RuntimeError("Cannot make a slide with %d plots" %
                                   num_plots)
            slides.write(
                bst.make_slide(slide_template=template,
                               slide_section=slide.get('title', ''),
                               slide_title=slide.get('title', ''),
                               plots=slide.get('plots', ''),
                               top_text=slide.get('toptext', ''),
                               bottom_text=slide.get('bottomtext', '')))
Example #2
0
def make_slides(config_filename):
    """Puts plots into one pdf.

    Parameters
    ----------
    config_filename : str
        Name of JSON config file

    Returns
    -------
    str
        Main TeX filename
    """
    print "Using configuration file", config_filename
    with open(config_filename, "r") as fp:
        config_dict = json.load(fp)

    out_stem = os.path.join(os.path.dirname(config_filename), "slides")
    main_file = out_stem + ".tex"
    slides_file = out_stem + "_input.tex"
    print "Writing to", main_file

    # Start beamer file - make main tex file
    # Use template - change title, subtitle, include file
    front_dict = config_dict['frontpage']
    make_main_tex_file(front_dict.get('title', ''),
                       front_dict.get('subtitle', ''),
                       front_dict.get('author', ''), main_file, slides_file)

    # Now make the slides file to be included in main file
    with open(slides_file, "w") as slides:
        slides_dict = config_dict['slides']
        for slide in slides_dict:
            print "Writing slide"
            template = None
            num_plots = len(slide.get('plots', ''))
            if num_plots == 1:
                template = bst.one_plot_slide
            elif num_plots == 2:
                template = bst.two_plot_slide
            elif num_plots <= 4:
                template = bst.four_plot_slide
            elif num_plots <= 6:
                template = bst.six_plot_slide
            else:
                raise RuntimeError("Cannot make a slide with %d plots" %
                                   num_plots)
            slides.write(
                bst.make_slide(slide_template=template,
                               slide_section=slide.get('title', ''),
                               slide_title=slide.get('title', ''),
                               plots=slide.get('plots', ''),
                               top_text=slide.get('toptext', ''),
                               bottom_text=slide.get('bottomtext', '')))
    return main_file
def plot_corr_results(in_name):
    """Puts correction plots from ROOT file in one pdf.

    Parameters
    ----------
    in_name : str
        Name of ROOT file to process (output from runCalibration.py)
    """
    print "Opening", in_name
    in_stem = os.path.basename(in_name).replace(".root", "")
    input_file = cu.open_root_file(in_name)

    # Setup output directory & filenames
    odir = os.path.join(os.path.dirname(os.path.abspath(in_name)), in_stem)
    cu.check_dir_exists_create(odir)

    out_name = os.path.join(odir, in_stem + ".pdf")
    out_stem = out_name.replace(".pdf", "")
    print "Writing to", out_name

    # Start beamer file - make main tex file
    # Use template - change title, subtitle, include file
    frontpage_title = "Correction value plots, binned by $|\eta|$"
    sub = in_stem.replace("output_", "").replace("_", "\_").replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub + "}"
    main_file = out_stem + ".tex"
    slides_file = out_stem + "_slides.tex"
    make_main_tex_file(frontpage_title, subtitle, AUTHOR, main_file, slides_file)

    # Now make the slides file to be included in main file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        etaBins = binning.eta_bins
        for i, (eta_min, eta_max) in enumerate(binning.pairwise(etaBins)):
            plotname = "l1corr_eta_%g_%g" % (eta_min, eta_max)
            bin_title = "%g <  |\eta^{L1}| < %g" % (eta_min, eta_max)
            xtitle = "<p_{T}^{L1}> [GeV]"
            ytitle = "Correction = 1/<p_{T}^{L1}/p_{T}^{Ref}>"
            output_plots = [os.path.join(odir, plotname + ext) for ext in ['.tex', '.pdf']]
            if plot_to_file(input_file, plotname, output_plots,
                            xtitle=xtitle, ytitle=ytitle, title="",
                            drawfit=True, extend_fit=True):
                titles.append("$%s$" % bin_title)
                plotnames.append(os.path.join(odir, plotname + ".tex"))
            # When we have 4 plots, or reached the end, write to a slide
            if (((i + 1) % 4 == 0) and (i != 0)) or (i == len(etaBins) - 2):
                print "Writing slide"
                slidetitle = "Correction value"
                slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))
                titles = []
                plotnames = []

    compile_pdf(main_file, out_name, odir, 1)
def plot_corr_results(in_name):
    """Puts correction plots from ROOT file in one pdf.

    Parameters
    ----------
    in_name : str
        Name of ROOT file to process (output from runCalibration.py)
    """
    print "Opening", in_name
    in_stem = os.path.basename(in_name).replace(".root", "")
    input_file = cu.open_root_file(in_name)

    # Setup output directory & filenames
    odir = os.path.join(os.path.dirname(os.path.abspath(in_name)), in_stem)
    cu.check_dir_exists_create(odir)

    out_name = os.path.join(odir, in_stem + ".pdf")
    out_stem = out_name.replace(".pdf", "")
    print "Writing to", out_name

    # Start beamer file - make main tex file
    # Use template - change title, subtitle, include file
    frontpage_title = "Correction value plots, binned by $|\eta|$"
    sub = in_stem.replace("output_", "").replace("_", "\_").replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub + "}"
    main_file = out_stem + ".tex"
    slides_file = out_stem + "_slides.tex"
    make_main_tex_file(frontpage_title, subtitle, AUTHOR, main_file, slides_file)

    # Now make the slides file to be included in main file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        etaBins = binning.eta_bins
        for i, (eta_min, eta_max) in enumerate(binning.pairwise(etaBins)):
            plotname = "l1corr_eta_%g_%g" % (eta_min, eta_max)
            bin_title = "%g <  |\eta^{L1}| < %g" % (eta_min, eta_max)
            xtitle = "<p_{T}^{L1}> [GeV]"
            ytitle = "Correction = 1/<p_{T}^{L1}/p_{T}^{Ref}>"
            output_plots = [os.path.join(odir, plotname + ext) for ext in ['.tex', '.pdf']]
            if plot_to_file(input_file, plotname, output_plots,
                            xtitle=xtitle, ytitle=ytitle, title="",
                            drawfit=True, extend_fit=True):
                titles.append("$%s$" % bin_title)
                plotnames.append(os.path.join(odir, plotname + ".tex"))
            # When we have 4 plots, or reached the end, write to a slide
            if (((i + 1) % 4 == 0) and (i != 0)) or (i == len(etaBins) - 2):
                print "Writing slide"
                slidetitle = "Correction value"
                slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))
                titles = []
                plotnames = []

    compile_pdf(main_file, out_name, odir, 1)
def plot_bin_results(in_name=""):
    """
    To plot X for each pt/eta bin.
    Yes that's a lot of plots.
    """
    # Setup input file
    print "Opening", in_name
    in_stem = os.path.basename(in_name).replace(".root", "")
    input_file = open_root_file(in_name)

    # Setup output directory & filenames
    odir = os.path.dirname(os.path.abspath(in_name))+"/"+in_stem+"/"
    check_dir_exists(odir)

    # only have to change output name here - reflected automatically in tex files etc
    out_name = odir+in_stem+"_bin.pdf"
    out_stem = out_name.replace(".pdf","")
    print "Writing to", out_name

    # Start beamer file
    # Use template - change title, subtitle, include file
    title = "Results for each bin"
    sub = in_stem.replace("output_", "")
    sub = sub.replace("_", "\_")
    sub = sub.replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub +"}"
    slides_file = out_stem+"_slides.tex"
    main_file = out_stem+".tex"
    with open("beamer_template.tex", "r") as t:
        with open(main_file, "w") as f:
            substitute = {"@TITLE": title, "@SUBTITLE": subtitle,
                          "@FILE": slides_file}
            for line in t:
                for k in substitute:
                    if k in line:
                        line = line.replace(k, substitute[k])
                f.write(line)

    # Now make the slides file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        for i, eta in enumerate(etaBins[0:-1]):
            emin = eta
            emax = etaBins[i+1]
            out_dir_eta = odir+"/eta_%g_%g/" % (emin, emax)
            check_dir_exists(out_dir_eta)
            if emin >= 3.:
                ptBins = binning.pt_bins_8_wide
            else:
                ptBins = binning.pt_bins_8
            print ptBins
            for j, pt in enumerate(ptBins[:-1]):
                ptmin = pt
                ptmax = ptBins[j+1]
                print ptmin, ptmax
                # for each pt bin we have a L1 pt plot, and a response plot w/fit
                name = "res_l1_%g_%g" % (ptmin, ptmax)
                plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{L1}", ytitle="", drawfit=True, xlim=[-2,5])
                plotnames.append(out_dir_eta+name+".tex")
                titles.append("$%g < |p_{T}^{L1}| < %g GeV$" % (ptmin, ptmax))

                # name = "res_ref_ref_%g_%g" % (ptmin, ptmax)
                # plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{Gen}", ytitle="", drawfit=True, xlim=[-2,5])
                # plotnames.append(out_dir_eta+name+".tex")

                # Plot pt diff
                name = "ptDiff_l1_%g_%g" % (ptmin, ptmax)
                plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="p_{T}^{L1} - p_{T}^{Gen}", ytitle="", drawfit=True, xlim=[-100,50])
                plotnames.append(out_dir_eta+name+".tex")

                titles.append("")

                if (len(plotnames) == 4):
                    print "Writing", emin, emax, ptmin, ptmax
                    slidetitle = "$%g <  |\eta| < %g$" % (emin, emax)
                    slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))
                    titles = []
                    plotnames = []
            slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))
        slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

        # the inclusive eta bin:
        # emin = eta
        # emax = etaBins[-1]
        # out_dir_eta = odir+"/eta_%g_%g/" % (emin, emax)
        # check_dir_exists(out_dir_eta)
        # for j, pt in enumerate(ptBins[0:-1]):
        #     ptmin = pt
        #     ptmax = ptBins[j+1]
        #     # for each pt bin we have a L1 pt plot, and a response plot w/fit
        #     name = "res_l1_%g_%g" % (ptmin, ptmax)
        #     plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{L1}", ytitle="", drawfit=True)
        #     plotnames.append(out_dir_eta+name+".tex")

        #     name = "res_ref_ref_%g_%g" % (ptmin, ptmax)
        #     plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{Gen}", ytitle="", drawfit=True)
        #     plotnames.append(out_dir_eta+name+".tex")

        #     titles.append("$%g < p_{T}^{L1} < %g GeV$" % (ptmin, ptmax))
        #     titles.append("")

        #     print "Writing", emin, emax, ptmin, ptmax
        #     slidetitle = "$%g <  |\eta| < %g$" % (emin, emax)
        #     slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

    compile_pdf(main_file, out_name, odir, n=1)
def plot_res_results(in_name_pre="", in_name_post=""):
    """
    Put resolution plot for each eta bin in one pdf.
    If in_name_post is specified, it will plot the graphs on the
    same canvas for ease of comparison.
    """

    # Setup input file (pre calib)
    print "Opening", in_name_pre
    in_stem_pre = os.path.basename(in_name_pre).replace(".root", "")
    input_file_pre = open_root_file(in_name_pre)

    # Setup input file (post calib)
    mode = ""
    if in_name_post:
        print "Opening", in_name_post
        in_stem_post = os.path.basename(in_name_post).replace(".root", "")
        input_file_post = open_root_file(in_name_post)
        mode = "_compare"
    else:
        print "Not opening post file"

    # Setup output directory & filenames
    odir = os.path.dirname(os.path.abspath(in_name_pre))+"/"+in_stem_pre+mode+"/"
    check_dir_exists(odir)

    out_name = odir+in_stem_pre+mode+".pdf"
    out_stem = out_name.replace(".pdf","")
    print "Writing to", out_name

    # Start beamer file
    # Use template - change title, subtitle, include file
    title = "Resolution plots, binned by $|\eta|$"
    sub = in_stem_pre.replace("res_", "").replace("_", "\_")
    sub += r"\\"
    if in_name_post:
        sub += "Comparing with: \\"
        sub += in_stem_post.replace("res_", "").replace("_", "\_")
    sub = sub.replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub +"}"
    slides_file = out_stem+"_slides.tex"
    main_file = out_stem+".tex"
    with open("beamer_template.tex", "r") as t:
        with open(main_file, "w") as f:
            substitute = {"@TITLE": title, "@SUBTITLE": subtitle,
                          "@FILE": slides_file}
            for line in t:
                for k in substitute:
                    if k in line:
                        line = line.replace(k, substitute[k])
                f.write(line)

    # some common bits for plots
    txt_pre = "Pre-calib" if in_name_post else "" # incase we only have 1 plot, not nec. pre calib
    txt_post = "Post-calib"

    lim_l1 = [0, 0.9]
    lim_ref = [0, 0.5]

    # Now make the slides file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        # Do the L1 resolution plot alongside its corresponding genjet resolution plot
        for i, eta in enumerate(etaBins[0:-1]):
            emin = eta
            emax = etaBins[i+1]

            # PLOT - L1 resolution (L1 - ref / L1) as binned by L1 jet pt
            name = "resL1_%g_%g" % (emin, emax)
            pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
            plots = [pre_dict]
            if in_name_post:
                post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
                plots.append(post_dict)

            multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
                xtitle="p_{T}^{L1} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{L1}", drawfit=True, drawopts="ALP", ylim=lim_l1)
            titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
            plotnames.append(odir+name+".tex")

            # PLOT - ref resolution (L1 - ref / ref) as binned by ref jet pt
            name = "resRefRef_%g_%g" % (emin, emax)
            pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
            plots = [pre_dict]
            if in_name_post:
                post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
                plots.append(post_dict)

            multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
                xtitle="p_{T}^{Gen} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{Gen}", drawfit=True, drawopts="ALP", ylim=lim_ref)
            titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
            plotnames.append(odir+name+".tex")

            print titles
            print plotnames
            if (len(plotnames) == 4):
                print "Writing", emin, emax
                slidetitle = "Resolution"
                slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))
                titles = []
                plotnames = []

        # # Do the inclusive eta plot
        # emin = etaBins[0]
        # emax = etaBins[-1]

        # # PLOT - L1 resolution (L1 - ref / L1) as binned by L1 jet pt
        # name = "resL1_%g_%g" % (emin, emax)
        # pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
        # plots = [pre_dict]
        # if in_name_post:
        #     post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
        #     plots.append(post_dict)

        # multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
        #     xtitle="p_{T}^{L1} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{L1}", drawfit=True, drawopts="ALP", ylim=lim_l1)
        # titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
        # plotnames.append(odir+name+".tex")

        # # PLOT - ref resolution binned by ref pt
        # name = "resRefRef_%g_%g" % (emin, emax)
        # pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
        # plots = [pre_dict]
        # if in_name_post:
        #     post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
        #     plots.append(post_dict)

        # multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
        #     xtitle="p_{T}^{L1} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{Gen}", drawfit=True, drawopts="ALP", ylim=lim_ref)
        # titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
        # plotnames.append(odir+name+".tex")

        # print "Writing", emin, emax
        # slidetitle = "Resolution comparison pre \\& post calibration (L1 \\& GenJet)"
        slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

    compile_pdf(main_file, out_name, odir)
def plot_bin_results(in_name=""):
    """
    To plot X for each pt/eta bin.
    Yes that's a lot of plots.
    """
    # Setup input file
    print "Opening", in_name
    in_stem = os.path.basename(in_name).replace(".root", "")
    input_file = open_root_file(in_name)

    # Setup output directory & filenames
    odir = os.path.dirname(os.path.abspath(in_name)) + "/" + in_stem + "/"
    check_dir_exists(odir)

    # only have to change output name here - reflected automatically in tex files etc
    out_name = odir + in_stem + "_bin.pdf"
    out_stem = out_name.replace(".pdf", "")
    print "Writing to", out_name

    # Start beamer file
    # Use template - change title, subtitle, include file
    title = "Results for each bin"
    sub = in_stem.replace("output_", "")
    sub = sub.replace("_", "\_")
    sub = sub.replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub + "}"
    slides_file = out_stem + "_slides.tex"
    main_file = out_stem + ".tex"
    with open("beamer_template.tex", "r") as t:
        with open(main_file, "w") as f:
            substitute = {
                "@TITLE": title,
                "@SUBTITLE": subtitle,
                "@FILE": slides_file
            }
            for line in t:
                for k in substitute:
                    if k in line:
                        line = line.replace(k, substitute[k])
                f.write(line)

    # Now make the slides file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        for i, eta in enumerate(etaBins[0:-1]):
            emin = eta
            emax = etaBins[i + 1]
            out_dir_eta = odir + "/eta_%g_%g/" % (emin, emax)
            check_dir_exists(out_dir_eta)
            if emin >= 3.:
                ptBins = binning.pt_bins_8_wide
            else:
                ptBins = binning.pt_bins_8
            print ptBins
            for j, pt in enumerate(ptBins[:-1]):
                ptmin = pt
                ptmax = ptBins[j + 1]
                print ptmin, ptmax
                # for each pt bin we have a L1 pt plot, and a response plot w/fit
                name = "res_l1_%g_%g" % (ptmin, ptmax)
                plot_to_file(
                    input_file,
                    "eta_%g_%g/Histograms/%s" % (emin, emax, name),
                    [out_dir_eta + name + ".tex", out_dir_eta + name + ".pdf"],
                    xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{L1}",
                    ytitle="",
                    drawfit=True,
                    xlim=[-2, 5])
                plotnames.append(out_dir_eta + name + ".tex")
                titles.append("$%g < |p_{T}^{L1}| < %g GeV$" % (ptmin, ptmax))

                # name = "res_ref_ref_%g_%g" % (ptmin, ptmax)
                # plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{Gen}", ytitle="", drawfit=True, xlim=[-2,5])
                # plotnames.append(out_dir_eta+name+".tex")

                # Plot pt diff
                name = "ptDiff_l1_%g_%g" % (ptmin, ptmax)
                plot_to_file(
                    input_file,
                    "eta_%g_%g/Histograms/%s" % (emin, emax, name),
                    [out_dir_eta + name + ".tex", out_dir_eta + name + ".pdf"],
                    xtitle="p_{T}^{L1} - p_{T}^{Gen}",
                    ytitle="",
                    drawfit=True,
                    xlim=[-100, 50])
                plotnames.append(out_dir_eta + name + ".tex")

                titles.append("")

                if (len(plotnames) == 4):
                    print "Writing", emin, emax, ptmin, ptmax
                    slidetitle = "$%g <  |\eta| < %g$" % (emin, emax)
                    slides.write(
                        bst.make_slide(bst.four_plot_slide, titles, plotnames,
                                       slidetitle))
                    titles = []
                    plotnames = []
            slides.write(
                bst.make_slide(bst.four_plot_slide, titles, plotnames,
                               slidetitle))
        slides.write(
            bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

        # the inclusive eta bin:
        # emin = eta
        # emax = etaBins[-1]
        # out_dir_eta = odir+"/eta_%g_%g/" % (emin, emax)
        # check_dir_exists(out_dir_eta)
        # for j, pt in enumerate(ptBins[0:-1]):
        #     ptmin = pt
        #     ptmax = ptBins[j+1]
        #     # for each pt bin we have a L1 pt plot, and a response plot w/fit
        #     name = "res_l1_%g_%g" % (ptmin, ptmax)
        #     plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{L1}", ytitle="", drawfit=True)
        #     plotnames.append(out_dir_eta+name+".tex")

        #     name = "res_ref_ref_%g_%g" % (ptmin, ptmax)
        #     plot_to_file(input_file, "eta_%g_%g/Histograms/%s" % (emin, emax, name), [out_dir_eta+name+".tex", out_dir_eta+name+".pdf"], xtitle="(p_{T}^{L1} - p_{T}^{Gen}) / p_{T}^{Gen}", ytitle="", drawfit=True)
        #     plotnames.append(out_dir_eta+name+".tex")

        #     titles.append("$%g < p_{T}^{L1} < %g GeV$" % (ptmin, ptmax))
        #     titles.append("")

        #     print "Writing", emin, emax, ptmin, ptmax
        #     slidetitle = "$%g <  |\eta| < %g$" % (emin, emax)
        #     slides.write(bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

    compile_pdf(main_file, out_name, odir, n=1)
def plot_res_results(in_name_pre="", in_name_post=""):
    """
    Put resolution plot for each eta bin in one pdf.
    If in_name_post is specified, it will plot the graphs on the
    same canvas for ease of comparison.
    """

    # Setup input file (pre calib)
    print "Opening", in_name_pre
    in_stem_pre = os.path.basename(in_name_pre).replace(".root", "")
    input_file_pre = open_root_file(in_name_pre)

    # Setup input file (post calib)
    mode = ""
    if in_name_post:
        print "Opening", in_name_post
        in_stem_post = os.path.basename(in_name_post).replace(".root", "")
        input_file_post = open_root_file(in_name_post)
        mode = "_compare"
    else:
        print "Not opening post file"

    # Setup output directory & filenames
    odir = os.path.dirname(
        os.path.abspath(in_name_pre)) + "/" + in_stem_pre + mode + "/"
    check_dir_exists(odir)

    out_name = odir + in_stem_pre + mode + ".pdf"
    out_stem = out_name.replace(".pdf", "")
    print "Writing to", out_name

    # Start beamer file
    # Use template - change title, subtitle, include file
    title = "Resolution plots, binned by $|\eta|$"
    sub = in_stem_pre.replace("res_", "").replace("_", "\_")
    sub += r"\\"
    if in_name_post:
        sub += "Comparing with: \\"
        sub += in_stem_post.replace("res_", "").replace("_", "\_")
    sub = sub.replace("_ak", r"\\_ak")
    subtitle = "{\\tt " + sub + "}"
    slides_file = out_stem + "_slides.tex"
    main_file = out_stem + ".tex"
    with open("beamer_template.tex", "r") as t:
        with open(main_file, "w") as f:
            substitute = {
                "@TITLE": title,
                "@SUBTITLE": subtitle,
                "@FILE": slides_file
            }
            for line in t:
                for k in substitute:
                    if k in line:
                        line = line.replace(k, substitute[k])
                f.write(line)

    # some common bits for plots
    txt_pre = "Pre-calib" if in_name_post else ""  # incase we only have 1 plot, not nec. pre calib
    txt_post = "Post-calib"

    lim_l1 = [0, 0.9]
    lim_ref = [0, 0.5]

    # Now make the slides file
    with open(slides_file, "w") as slides:
        titles = []
        plotnames = []
        # Do the L1 resolution plot alongside its corresponding genjet resolution plot
        for i, eta in enumerate(etaBins[0:-1]):
            emin = eta
            emax = etaBins[i + 1]

            # PLOT - L1 resolution (L1 - ref / L1) as binned by L1 jet pt
            name = "resL1_%g_%g" % (emin, emax)
            pre_dict = dict(infile=input_file_pre,
                            plotname="eta_%g_%g/" % (emin, emax) + name,
                            color=r.kRed,
                            legend_text=txt_pre,
                            legend_style="LPE")
            plots = [pre_dict]
            if in_name_post:
                post_dict = dict(infile=input_file_post,
                                 plotname="eta_%g_%g/" % (emin, emax) + name,
                                 color=r.kBlue,
                                 legend_text=txt_post,
                                 legend_style="LPE")
                plots.append(post_dict)

            multiplot_to_file(
                plots,
                outfilename=[odir + name + ".tex", odir + name + ".pdf"],
                xtitle="p_{T}^{L1} [GeV]",
                ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{L1}",
                drawfit=True,
                drawopts="ALP",
                ylim=lim_l1)
            titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
            plotnames.append(odir + name + ".tex")

            # PLOT - ref resolution (L1 - ref / ref) as binned by ref jet pt
            name = "resRefRef_%g_%g" % (emin, emax)
            pre_dict = dict(infile=input_file_pre,
                            plotname="eta_%g_%g/" % (emin, emax) + name,
                            color=r.kRed,
                            legend_text=txt_pre,
                            legend_style="LPE")
            plots = [pre_dict]
            if in_name_post:
                post_dict = dict(infile=input_file_post,
                                 plotname="eta_%g_%g/" % (emin, emax) + name,
                                 color=r.kBlue,
                                 legend_text=txt_post,
                                 legend_style="LPE")
                plots.append(post_dict)

            multiplot_to_file(
                plots,
                outfilename=[odir + name + ".tex", odir + name + ".pdf"],
                xtitle="p_{T}^{Gen} [GeV]",
                ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{Gen}",
                drawfit=True,
                drawopts="ALP",
                ylim=lim_ref)
            titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
            plotnames.append(odir + name + ".tex")

            print titles
            print plotnames
            if (len(plotnames) == 4):
                print "Writing", emin, emax
                slidetitle = "Resolution"
                slides.write(
                    bst.make_slide(bst.four_plot_slide, titles, plotnames,
                                   slidetitle))
                titles = []
                plotnames = []

        # # Do the inclusive eta plot
        # emin = etaBins[0]
        # emax = etaBins[-1]

        # # PLOT - L1 resolution (L1 - ref / L1) as binned by L1 jet pt
        # name = "resL1_%g_%g" % (emin, emax)
        # pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
        # plots = [pre_dict]
        # if in_name_post:
        #     post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
        #     plots.append(post_dict)

        # multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
        #     xtitle="p_{T}^{L1} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{L1}", drawfit=True, drawopts="ALP", ylim=lim_l1)
        # titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
        # plotnames.append(odir+name+".tex")

        # # PLOT - ref resolution binned by ref pt
        # name = "resRefRef_%g_%g" % (emin, emax)
        # pre_dict = dict(infile=input_file_pre, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kRed, legend_text=txt_pre, legend_style="LPE")
        # plots = [pre_dict]
        # if in_name_post:
        #     post_dict = dict(infile=input_file_post, plotname="eta_%g_%g/" %(emin, emax)+name, color=r.kBlue, legend_text=txt_post, legend_style="LPE")
        #     plots.append(post_dict)

        # multiplot_to_file(plots, outfilename=[odir+name+".tex", odir+name+".pdf"],
        #     xtitle="p_{T}^{L1} [GeV]", ytitle="(p_{T}^{L1} - p_{T}^{Gen})/ p_{T}^{Gen}", drawfit=True, drawopts="ALP", ylim=lim_ref)
        # titles.append("$%g <  |\eta^{L1}| < %g$" % (emin, emax))
        # plotnames.append(odir+name+".tex")

        # print "Writing", emin, emax
        # slidetitle = "Resolution comparison pre \\& post calibration (L1 \\& GenJet)"
        slides.write(
            bst.make_slide(bst.four_plot_slide, titles, plotnames, slidetitle))

    compile_pdf(main_file, out_name, odir)