Example #1
0
def get_uncertainty_graph(hnom, curve_uncert):
    """
    Convert an histogram and a RooCurve
    into a TGraphAsymmError

    Parameters
    ----------
    hnom: TH1F, TH1D, ...
        The histogram of nominal values
    curve_uncert: RooCurve
        The uncertainty band around the nominal value

    TODO: Improve the handling of the underflow and overflow bins
    """
    graph = Graph(hnom.GetNbinsX())
    for ibin in xrange(1, hnom.GetNbinsX() + 1):
        uncerts = []
        for ip in xrange(3, curve_uncert.GetN() - 3):
            x, y = ROOT.Double(0.), ROOT.Double(0.)
            curve_uncert.GetPoint(ip, x, y)
            if hnom.GetBinLowEdge(ibin) <= x < hnom.GetBinLowEdge(ibin + 1):
                uncerts.append(y)
        log.debug('{0}, bin {1}: {2}'.format(hnom.name, ibin, uncerts))
        low, high = min(uncerts), max(uncerts)
        bin_center = 0.5 * (hnom.GetBinLowEdge(ibin + 1) +
                            hnom.GetBinLowEdge(ibin))
        e_x_low = bin_center - hnom.GetBinLowEdge(ibin)
        e_x_high = hnom.GetBinLowEdge(ibin + 1) - bin_center
        bin_content = hnom.GetBinContent(ibin)
        e_y_low = hnom.GetBinContent(ibin) - low
        e_y_high = high - hnom.GetBinContent(ibin)
        graph.SetPoint(ibin - 1, bin_center, bin_content)
        graph.SetPointError(ibin - 1, e_x_low, e_x_high, e_y_low, e_y_high)
    return graph
Example #2
0
def UncertGraph(hnom, curve_uncert):
    """
    Convert an histogram and a RooCurve
    into a TGraphAsymmError

    Parameters
    ----------
    hnom: TH1F, TH1D,...
        The histogram of nominal values
    curve_uncert: RooCurve
        The uncertainty band around the nominal value
    curve_uncert: RooCurve
    TODO: Improve the handling of the underflow and overflow bins
    """

    graph = Graph(hnom.GetNbinsX())
    # ---------------------------------------------
    for ibin in xrange(1, hnom.GetNbinsX() + 1):
        uncerts = []
        for ip in xrange(3, curve_uncert.GetN() - 3):
            x, y = ROOT.Double(0.), ROOT.Double(0.)
            curve_uncert.GetPoint(ip, x, y)
            if int(x) == int(hnom.GetBinLowEdge(ibin)):
                uncerts.append(y)
        uncerts.sort()
        log.info('{0}: {1}'.format(hnom.name, uncerts))
        if len(uncerts) != 2:
            for val in uncerts:
                if val in uncerts:
                    uncerts.remove(val)
        if len(uncerts) != 2:
            raise RuntimeError(
                'Need exactly two error values and got {0}'.format(uncerts))

        bin_center = 0.5 * (hnom.GetBinLowEdge(ibin + 1) +
                            hnom.GetBinLowEdge(ibin))
        e_x_low = bin_center - hnom.GetBinLowEdge(ibin)
        e_x_high = hnom.GetBinLowEdge(ibin + 1) - bin_center
        bin_content = hnom.GetBinContent(ibin)
        e_y_low = hnom.GetBinContent(ibin) - uncerts[0]
        e_y_high = uncerts[1] - hnom.GetBinContent(ibin)
        graph.SetPoint(ibin - 1, bin_center, bin_content)
        graph.SetPointError(ibin - 1, e_x_low, e_x_high, e_y_low, e_y_high)
    # ---------------------------------------------
    return graph