Beispiel #1
0
def efficiency_graph(pass_function,
                     function_inputs,
                     xs,
                     bins=None,
                     error=0.005):
    pass_results = pass_function(function_inputs)
    if bins is None:  # Automatic binning
        # Compute the number of bins such that the error on the efficiency is equal to 'error' in each bin
        # The calculation is based on binomial errors and assumes that the efficiency is flat (that the distributions of all and selected events are the same)
        k = float(np.count_nonzero(pass_results))
        n = float(len(pass_results))
        percentiles = [0., 100.]
        if k > 0:
            nbins = (error * n)**2 / k / (1 - k / n)
            # Compute the bin bounaries with the same number of events in all bins
            percentiles = np.arange(0., 100., 100. / nbins)
            percentiles[-1] = 100.
        bins = np.unique(np.percentile(xs, percentiles))
    # Fill histograms of selected and all events and compute efficiency
    histo_pass = Hist(bins)
    histo_total = Hist(bins)
    fill_hist(histo_pass, xs, pass_results)
    fill_hist(histo_total, xs)
    efficiency = Graph()
    efficiency.Divide(histo_pass, histo_total)
    return efficiency
Beispiel #2
0
def rate(et, thresholds, total_events):
    pass_thresholds = pass_threshold(et, thresholds)
    # fill pass and total numbers of events
    bins = (pass_thresholds[:-1, 0] + pass_thresholds[1:, 0]) / 2.
    bins = np.append([-bins[0] + 2. * pass_thresholds[0, 0]], bins)
    bins = np.append(bins, [2. * pass_thresholds[-1, 0] - bins[-1]])
    histo_pass = Hist(bins)
    histo_total = Hist(bins)
    for b, n in zip(histo_pass[1:-1], pass_thresholds[:, 1]):
        b.value = n
        b.error = np.sqrt(n)
    for b in histo_total[1:-1]:
        b.value = total_events
        b.error = np.sqrt(total_events)
    rates = Graph()
    rates.Divide(histo_pass, histo_total)
    return rates
Beispiel #3
0
            tmplabels.append(b)
        # use them in the legend
        axes.legend(tmphandles, tmplabels, loc='best', numpoints=1)

    axislabel = ""
    for chunk in histogramName.split("_"):
        if "SR" in chunk or "minus" in chunk:
            continue
        axislabel += chunk
    # axislabel = " ".join(histogramName.split("_")[2:])
    axislabel = axislabel.split("<")[0].split(">")[0]

    if 'DataMain' in hists:
        if hists["DataMain"].Integral() and plotWithMPL:
            ratioplot = Graph()
            ratioplot.Divide(hists['DataMain'], stack.sum, 'pois')
            # ratioplot.color = "green"
            tmpyerror, tmpyerror2 = zip(*list(ratioplot.yerr()))
            tmpx = list(ratioplot.x())
            tmpy = list(ratioplot.y())
            tmpxy = zip(tmpx, tmpy, tmpyerror)
            # print tmpxy
            tmpxy = [tmp for tmp in tmpxy if tmp[1] != 0]
            # print tmpxy
            tmpx, tmpy, tmpyerror = zip(*tmpxy)
            # print tmpyerror
            # axes_ratio.errorbar(tmpx,tmpy, yerr = tmpyerror,xerr=False, emptybins=False, marker='o', lw=1, color="black")
            axes_ratio.errorbar(
                tmpx,
                tmpy,
                # list(ratioplot.y()),