Beispiel #1
0
def plot_hists(hists, name="canv", **kwargs):
    canv = ROOT.TCanvas(name, name)
    draw_cmd = kwargs["draw_cmd"] if "draw_cmd" in kwargs.keys() else "E1"
    #    title = kwargs["title"] if "title" in kwargs.keys() else "NOTITLE"
    line_width = kwargs["line_width"] if "line_width" in kwargs.keys() else 2
    x_label = kwargs.get("x_label", "XLABEL")
    y_label = kwargs.get("y_label", "events/bin")
    do_log_y = kwargs["do_log_y"] if "do_log_y" in kwargs.keys() else False
    min_bin = kwargs.get("min_bin", 0)
    max_bin_mult = kwargs.get("max_bin_mult", 1.5)
    styles = kwargs.get("styles", {})

    max_bin = get_max_bin([hist for hist in hists])

    first = False
    for hist in hists:
        hist.Draw(draw_cmd + (" SAME" if first else ""))
        first = True

#    hists[0].SetTitle(title)
    hists[0].SetStats(False)
    hists[0].SetMaximum(max_bin_mult * max_bin)
    hists[0].SetMinimum(min_bin)
    hists[0].GetXaxis().SetTitle(x_label)
    hists[0].GetYaxis().SetTitle(y_label)

    if do_log_y:
        canv.SetLogy()

    return canv
Beispiel #2
0
def plot_hists(hists, name="canv", **kwargs):
    canv = ROOT.TCanvas(name, name)
    draw_cmd = kwargs["draw_cmd"] if "draw_cmd" in kwargs.keys() else "E1"
#    title = kwargs["title"] if "title" in kwargs.keys() else "NOTITLE"
    line_width = kwargs["line_width"] if "line_width" in kwargs.keys() else 2
    x_label = kwargs.get("x_label", "XLABEL")
    y_label = kwargs.get("y_label", "events/bin")
    do_log_y = kwargs["do_log_y"] if "do_log_y" in kwargs.keys() else False
    min_bin = kwargs.get("min_bin", 0)
    max_bin_mult = kwargs.get("max_bin_mult", 1.5)
    styles = kwargs.get("styles", {})

    max_bin = get_max_bin([hist for hist in hists])

    first = False
    for hist in hists:
        hist.Draw(draw_cmd + (" SAME" if first else ""))
        first = True

#    hists[0].SetTitle(title)
    hists[0].SetStats(False)
    hists[0].SetMaximum(max_bin_mult*max_bin)
    hists[0].SetMinimum(min_bin)
    hists[0].GetXaxis().SetTitle(x_label)
    hists[0].GetYaxis().SetTitle(y_label)

    if do_log_y:
        canv.SetLogy()

    return canv
Beispiel #3
0
def plot_hists(hists, **kwargs):
    """
    Draws a list of histograms side-by-side on a new canvas.

    Args:
        hists - a list of Hist instances to draw

    Keywords:
        name: the name of the canvas to create.
        draw_cmd: the ROOT draw command. Can be a single command
            (use the same for all) or a list with the length of hists
        x_label: a string with the title for the x axis.
        y_label: ditto for the y axis.
        do_log_y: a boolean to specify if the y axis should be logarithmic.
        min_bin: a numerical value for the lower range of the y axis.
        max_bin: ditto for the upper range. Overrides the max_bin_mult variable.
        max_bin_mult: a multiplier for the y axis, applied on the maximal bin
            height.
        do_chi2: a boolean to specify whether to do the Chi2 test between
            the first and subsequent histograms.
        do_ks: ditto for the KS test.

    Returns:
        a handle to the new canvas.
    Raises:
        ValueError: when the number of draw commands did not equal the number
            of hists
    """

    canv = kwargs.get("canvas", ROOT.TCanvas())
    canv.cd()
    draw_cmd = kwargs["draw_cmd"] if "draw_cmd" in kwargs.keys() else "E1"
    x_label = kwargs.get("x_label", "XLABEL")
    y_label = kwargs.get("y_label", "")
    do_log_y = kwargs["do_log_y"] if "do_log_y" in kwargs.keys() else False
    min_bin = kwargs.get("min_bin", 0)
    max_bin = kwargs.get("max_bin", None)
    max_bin_mult = kwargs.get("max_bin_mult", 1.5)
    do_chi2 = kwargs.get("do_chi2", False)
    do_ks = kwargs.get("do_ks", False)

    max_bin_val = get_max_bin([hist for hist in hists])

    if isinstance(draw_cmd, basestring):
        draw_cmd = [draw_cmd]*len(hists)
    if len(draw_cmd) != len(hists):
        raise ValueError("Must have the same number of draw commands as hists: %s vs %s" % (str(draw_cmd), str(hists)))
    first = False
    for dc, hist in zip(draw_cmd, hists):
        hist.Draw(dc + (" SAME" if first else ""))
        first = True

    hists[0].SetStats(False)
    if not max_bin:
        hists[0].SetMaximum(max_bin_mult*max_bin_val)
    else:
        hists[0].SetMaximum(max_bin)

    hists[0].SetMinimum(min_bin)
    hists[0].GetXaxis().SetTitle(x_label)
    hists[0].GetYaxis().SetTitle(y_label)

    if do_chi2:
        for h in hists[1:]:
            chi2 = hists[0].Chi2Test(h, "WW CHI2/NDF")
            h.SetTitle(h.GetTitle() + " #chi^{2}/ndf=%.1f" % chi2)

    if do_ks:
        for h in hists[1:]:
            ks = hists[0].KolmogorovTest(h, "")
            h.SetTitle(h.GetTitle() + " log p=%.1f" % math.log(ks))

    if do_log_y:
        canv.SetLogy()

    return canv