예제 #1
0
    def _plot1D_numpy(self, data, bins, weights=None, option='', **kwargs):
        """ ... """

        # Check(s)
        if bins is None:
            warning(
                "You need to specify 'bins' when plotting a numpy-type input.")
            return

        if len(data) != len(bins) and len(bins) < 2:
            warning("Number of bins {} is not accepted".format(len(bins)))
            return

        # Fill histogram
        if len(data) == len(bins):
            # Assuming 'data' and 'bins' are sets of (x,y)-points
            h = ROOT.TGraph(len(bins), np.array(bins, dtype=np.float),
                            np.array(data, dtype=np.float))
        else:
            h = ROOT.TH1F('h_{:d}'.format(int(time.time() * 1E+06)), "",
                          len(bins) - 1, np.array(bins, dtype=np.float))
            if len(data) == len(bins) - 1:
                # Assuming 'data' are bin values
                array2hist(data, h)
            else:
                # Assuming 'data' are values to be filled
                fill_hist(h, data, weights=weights)
                pass
            pass

        # Plot histogram
        return self._plot1D(h, option, **kwargs)
예제 #2
0
def main():
    args = get_args()
    urhfile = uproot.open(args.histogramfile)
    rrfile = ROOT.TFile.Open(args.outfile, "RECREATE")
    rrfile.cd()
    regions = args.regions
    if regions == ["_all"]:
        regions = DEFAULT_REGIONS
    for region in regions:
        keepers = get_keepers(urhfile, region, target=args.target)
        print(f"{region}:\t {keepers}")
        for k in urhfile.keys():
            key = k.decode('utf-8')
            if ";" in key:
                key = key.split(";")[0]
            if region in key:
                urhist = urhfile.get(key)
                count = urhist.values
                sumw2 = urhist.variances
                countkeep = np.zeros((keepers.shape[0] + 2))
                sumw2keep = np.zeros((keepers.shape[0] + 2))
                countkeep[1:-1] = count[keepers]
                sumw2keep[1:-1] = sumw2[keepers]
                rrhist = ROOT.TH1D(key, key, len(keepers), 0, 1)
                array2hist(countkeep, rrhist, np.sqrt(sumw2keep))
                rrhist.Write("", ROOT.TObject.kOverwrite)
                rrhist.SetDirectory(0)
    rrfile.Close()
예제 #3
0
  def toRoot(self, name, labels = "", xRescale = 1, yRescale = 1):

    # If there's a second dimension, create a TH2.
    if self.heights.ndim == 2:

      histogram = root.TH2F(
        name,
        labels,
        len(self.xCenters),
        self.xEdges[0] * xRescale,
        self.xEdges[-1] * xRescale,
        len(self.yCenters),
        self.yEdges[0] * yRescale,
        self.yEdges[-1] * yRescale
      )

    # If there's no second dimension, create a TH1.
    else:

      histogram = root.TH1F(
        name,
        labels,
        len(self.xCenters),
        self.xEdges[0] * xRescale,
        self.xEdges[-1] * xRescale
      )

    # Copy the bin contents and update the number of entries.
    rnp.array2hist(self.heights, histogram)
    histogram.ResetStats()

    return histogram
예제 #4
0
 def __init__(self, working_points, functions, efficiency_map):
     efficiency_array = hist2array(efficiency_map)
     working_points_indices = find_closest(working_points, efficiency_array)
     self.function_index_map = efficiency_map.empty_clone()
     array2hist(working_points_indices, self.function_index_map)
     self.indices = working_points_indices
     self.functions = functions
     self.dim = len(efficiency_array.shape)
예제 #5
0
def fold_hist(hist, fold):
    tmp = hist
    name = tmp.GetName()
    tmp.SetName('tmp')
    hist = ROOT.TH1D(name, '', len(fold), 0., float(len(fold)))
    hist.Sumw2()
    indices = np.array(fold)
    arr = np.sum(rnp.hist2array(tmp, copy=False)[indices], axis=1)
    tmp.Delete()
    rnp.array2hist(arr, hist)

    return hist
예제 #6
0
def numpy_to_TH2(np_arr,name,th3):
    '''
    converts 2D-numpy array with info of original TH3 into a TH2D histogram.
    '''
    np_arr_clean = np.where(np_arr<0,0,np_arr)
    from root_numpy import array2hist
    # The x bins here are hard coded just to make them comparable to annas old plots.
    # th2 = ROOT.TH2D(name,name,90,th3.xlow,-1,th3.ynumbins,th3.ylow,th3.yhigh)
    # array2hist(np_arr_clean[:-10,:],th2)
    
    th2 = ROOT.TH2D(name,name,th3.xnumbins,np.append(th3.bins[0][:-1,0],th3.bins[0][-1]),th3.ynumbins,np.append(th3.bins[1][:-1,0],th3.bins[1][-1]))
    array2hist(np_arr_clean,th2)
    return th2
예제 #7
0
def matrix_to_hist2d( value_matrix, x_edges, y_edges ):
    '''
    numpy matrix of values and bin edges -> Hist2D
    '''
    from root_numpy import array2hist

    rootpy_hist = Hist2D( x_edges, y_edges, type = 'D' )
    array2hist(value_matrix, rootpy_hist)

    # set_bin_value = rootpy_hist.SetBinContent
    # for x in range(0, len(x_edges)-1):
    #     for y in range(0, len(y_edges)-1):
    #         set_bin_value( x+1, y+1, value_matrix.item(y, x) )
    return rootpy_hist
예제 #8
0
def convert_to_th1d(in_map, errors=False):
    assert isinstance(in_map, Map)
    name = in_map.name
    assert len(in_map.shape) == 1
    n_bins = in_map.shape[0]
    edges = in_map.binning.bin_edges[0].m

    th1d = TH1D(name, name, n_bins, edges)
    array2hist(unp.nominal_values(in_map.hist), th1d)
    if errors:
        map_errors = unp.std_devs(in_map.hist)
        for idx in xrange(n_bins):
            th1d.SetBinError(idx+1, map_errors[idx])
    return th1d
예제 #9
0
def convert_to_th2d(in_map, errors=False):
    assert isinstance(in_map, Map)
    name = in_map.name
    n_bins = in_map.shape
    assert len(n_bins) == 2
    nbins_a, nbins_b = n_bins
    edges_a, edges_b = [b.m for b in in_map.binning.bin_edges]

    th2d = TH2D(name, name, nbins_a, edges_a, nbins_b, edges_b)
    array2hist(unp.nominal_values(in_map.hist), th2d)
    if errors:
        map_errors = unp.std_devs(in_map.hist)
        for x_idx, y_idx in product(*map(range, n_bins)):
            th2d.SetBinError(x_idx+1, y_idx+1, map_errors[x_idx][y_idx])
    return th2d
예제 #10
0
파일: helpers.py 프로젝트: simonUU/b2plot
def hist2root(h, name=None, title=''):
    """ Convert your histograms to a root histogram.. if you really want to

    Args:
        h: array with the histogram h=(y_values, bin_edges, (patches=optional))
        name: Name for the root histogram, optional
        title: Title for the root histogram, optional

    Returns:
        TH1F or TH2F depending on the input array

    Examples:
        >>> data = [1,2,3,4,5,6,7]
        >>> h = b2plot.hist(data)
        >>> from b2plot.helpers import hist2root
        >>> root_hist = hist2root(h)

    """
    import root_numpy
    import random
    import ROOT

    y_values = h[0]
    name = "".join(random.choice("fck_root_wtf")
                   for _ in range(5)) if name is None else name
    if len(h) > 3:
        rh = ROOT.TH2F(name, title, len(h[1]) - 1, h[1], len(h[2]) - 1, h[2])
    else:
        rh = ROOT.TH1F(name, title, len(h[1]) - 1, h[1])
    return root_numpy.array2hist(y_values, rh)
def get_cdf (h):
    """
    Returns the cumulative distribution of `hist`.
    """

    # Prepare array(s)
    y = root_numpy.hist2array(h)

    # Compute CDF
    cdf = np.cumsum(y)

    # Convert to ROOT.TH1F
    hcdf = h.Clone("%s_CDF" % h.GetName())
    root_numpy.array2hist(cdf, hcdf)

    return hcdf
예제 #12
0
def main(args, options):
    if len(args) < 1:
        print('syntax: python3 his2root.py <his file> [options]')
        sys.exit(1)

    his_files = args[0:]
    print("Will convert HIS files: ", his_files)
    for his_file in his_files:
        print("\t===> Converting HIS file: ", his_file)
        stem, _ = os.path.splitext(his_file)
        his = openHIS(his_file)
        outname = stem + '.root' if not options.outfile else options.outfile
        rf = ROOT.TFile(outname, 'recreate')

        runN = stem.split('run')[-1]
        run = runN if len(runN) else 'XXXX'

        for idx, section in enumerate(his):
            if options.maxEntries > -1 and idx >= options.maxEntries:
                break
            print("transferring image ", idx)
            (nx, ny) = section.shape
            title = stem + ('_%04d' % idx)
            postfix = 'run{run}_ev{ev}'.format(run=run, ev=idx)
            title = 'pic_{pfx}'.format(pfx=postfix)
            h2 = ROOT.TH2S(title, title, nx, 0, nx, ny, 0, ny)
            h2.GetXaxis().SetTitle('x')
            h2.GetYaxis().SetTitle('y')
            _ = array2hist(np.fliplr(np.transpose(section)), h2)
            h2.Write()
        rf.Close()
예제 #13
0
def get_css_fns(shapeval, omega, originalHist, name):
    """
    ...
    """

    # Compute $F_{CSS}( x | \alpha, \Omega_{D} )$
    Fcss = originalHist.Clone("Fcss_%s_%.2f_%.2f_clone" %
                              (name, omega, shapeval))

    alpha = shapeval
    x = np.array(
        map(Fcss.GetXaxis().GetBinCenter, range(1,
                                                Fcss.GetNbinsX() + 1)))
    y = np.power(alpha / omega, alpha) / gamma(x) * np.power(
        x, alpha - 1) * np.exp(-alpha * x / omega)
    root_numpy.array2hist(y, Fcss)

    # Now, let's do some convolving!
    jssVar_conv = get_convolution(originalHist, Fcss)
    normalise(jssVar_conv)
    normalise(originalHist)

    # Make the CDFs
    jssVar_CDF = get_cdf(originalHist)
    jssVar_conv_CDF = get_cdf(jssVar_conv)

    # Let F = CDF of original function and G = CDF of convolution
    # Then, our map shoud be G^{-1}(F(X)) (http://math.arizona.edu/~jwatkins/f-transform.pdf page 3)
    fxvals = jssVar_CDF.GetXaxis().GetXbins()
    fyvals = []
    ginvxvals = []
    ginvyvals = jssVar_CDF.GetXaxis().GetXbins()

    for i in range(1, jssVar_CDF.GetNbinsX() + 1):
        fyvals += [jssVar_CDF.GetBinContent(i)]
        ginvxvals += [jssVar_conv_CDF.GetBinContent(i)]
        pass

    # Cumulative distribution of jssVar for low mass bin
    F = ROOT.TGraph(len(fxvals), np.array(fxvals), np.array(fyvals))
    Ginv = ROOT.TGraph(len(ginvxvals), np.array(ginvxvals),
                       np.array(ginvyvals))

    return F, Ginv
예제 #14
0
    def Interpolate(self, *args, **kwargs):
        r"""Replace zero valued data points by interpolating between all non-zero data
        point of the grid if no arguments are given. Otherwise the standard
        :func:`ROOT.TH2.Interpolate` functionality is used.

        In the former case the :py:mod:`scipy`'s :func:`interpolate.griddata` function
        is used.

        :param \*args: See :py:mod:`ROOT` documentation of :func:`ROOT.TH2.Interpolate`

        :param \**kwargs: see below

        :Keyword Arguments:
            * **method** (``str``) -- method of interpolation (default: 'cubic')

                * **nearest**: return the value at the data point closest to the point
                  of interpolation

                * **linear**: tessellate the input point set to n-dimensional simplices,
                  and interpolate linearly on each simplex

                * **cubic**: return the value determined from a piecewise cubic,
                  continuously differentiable and approximately curvature-minimizing
                  polynomial surface
        """
        if len(args) == 0:
            # https://stackoverflow.com/a/39596856/10986034
            method = kwargs.pop("method", "cubic")
            array = rnp.hist2array(self, include_overflow=True)
            array[array == 0] = np.nan
            x = np.arange(0, array.shape[1])
            y = np.arange(0, array.shape[0])
            array = np.ma.masked_invalid(array)
            xx, yy = np.meshgrid(x, y)
            x1 = xx[~array.mask]
            y1 = yy[~array.mask]
            newarr = array[~array.mask]
            GD1 = interpolate.griddata(
                (x1, y1), newarr.ravel(), (xx, yy), method=method, fill_value=0.0
            )
            rnp.array2hist(GD1, self)
        else:
            super(Histo2D, self).Interpolate(*args)
def get_convolution (h, Fcss):
    """
    Returns the convolution of `h` and `Fcss`.
    """

    # Check(s)
    N = Fcss.GetNbinsX()
    assert N == h.GetNbinsX()

    # Prepare array(s)
    f1 = root_numpy.hist2array(h)
    f2 = root_numpy.hist2array(Fcss)

    # Perform convolution
    conv = np.convolve(f1, f2)[:N]

    # Convert to ROOT.TH1F
    hconv = h.Clone("%s_conv" % h.GetName())
    root_numpy.array2hist(conv, hconv)

    return hconv
예제 #16
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
예제 #17
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
def generate_noise(options):
    t0 = time.time()

    Nclone = options.nimages
    run_count = str(options.run).zfill(5)

    infolder = ""
    infile = "histograms"
    outfolder = "/"

    ecdf_filename = options.filename

    outfile = ROOT.TFile(outfolder + '/' + infile + '_Run' + run_count +
                         '.root', 'RECREATE')  #OUTPUT NAME

    print('[Generating simulated images]')
    imgs = generateNoiseImage(ecdf_filename, Nclone)
    nX = np.shape(imgs)[1]
    nY = np.shape(imgs)[2]

    print('[Generating ROOT file]')
    for entry in range(0, Nclone):  #RUNNING ON ENTRIES

        final_imgs = ROOT.TH2F('pic_run' + str(run_count) + '_ev' + str(entry),
                               '', nX, 0, (nX - 1), nY, 0, (nY - 1))
        total = imgs[entry, :, :]

        array2hist(total, final_imgs)
        final_imgs.Write()

    print('[Run %d with %d images complete]' % (int(run_count), Nclone))
    #run_count+=1
    outfile.Close()

    t1 = time.time()
    print('Generation took %.2f seconds' % (t1 - t0))
    return
예제 #19
0
def check_array2hist(hist):
    shape = np.array([hist.GetNbinsX(), hist.GetNbinsY(), hist.GetNbinsZ()])
    shape = shape[:hist.GetDimension()]
    arr = RNG.randint(0, 10, size=shape)
    rnp.array2hist(arr, hist)
    arr_hist = rnp.hist2array(hist)
    assert_array_equal(arr_hist, arr)

    # Check behaviour if errors are supplied
    errors = arr * 0.1
    _hist = hist.Clone()
    _hist.Reset()
    rnp.array2hist(arr, _hist, errors=errors)
    arr_hist = rnp.hist2array(_hist)
    assert_array_equal(arr_hist, arr)
    if hist.GetDimension() == 1:
        errors_from_hist = np.array([_hist.GetBinError(ix)
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    if hist.GetDimension() == 2:
        errors_from_hist = np.array([[_hist.GetBinError(ix, iy)
                                      for iy in range(1, _hist.GetNbinsY() + 1)]
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    if hist.GetDimension() == 3:
        errors_from_hist = np.array([[[_hist.GetBinError(ix, iy, iz)
                                       for iz in range(1, _hist.GetNbinsZ() + 1)]
                                      for iy in range(1, _hist.GetNbinsY() + 1)]
                                     for ix in range(1, _hist.GetNbinsX() + 1)])
    assert_array_equal(errors, errors_from_hist)

    shape_overflow = shape + 2
    arr_overflow = RNG.randint(0, 10, size=shape_overflow)
    hist_overflow = hist.Clone()
    hist_overflow.Reset()
    rnp.array2hist(arr_overflow, hist_overflow)
    arr_hist_overflow = rnp.hist2array(hist_overflow, include_overflow=True)
    assert_array_equal(arr_hist_overflow, arr_overflow)

    if len(shape) == 1:
        return

    # overflow not specified on all axes
    arr_overflow2 = arr_overflow[1:-1]
    hist_overflow2 = hist.Clone()
    hist_overflow2.Reset()
    rnp.array2hist(arr_overflow2, hist_overflow2)
    arr_hist_overflow2 = rnp.hist2array(hist_overflow2, include_overflow=True)
    assert_array_equal(arr_hist_overflow2[1:-1], arr_overflow2)
예제 #20
0
    def save(self, output):

        # Save the transform and all axis units in NumPy format.
        np.savez(output,
                 transform=self.signal[self.physical],
                 **{
                     axis: self.axes[axis][self.physical]
                     for axis in self.axes.keys()
                 })

        # Create a ROOT histogram for the frequency distribution.
        histogram = root.TH1F("transform", ";Frequency (kHz);Arbitrary Units",
                              len(self.frequency),
                              self.frequency[0] - self.df / 2,
                              self.frequency[-1] + self.df / 2)

        # Copy the signal into the histogram.
        rnp.array2hist(self.signal, histogram)

        # Save the histogram.
        name = output.split(".")[0]
        outFile = root.TFile(f"{name}.root", "RECREATE")
        histogram.Write()
        outFile.Close()
예제 #21
0
def read_h5_write_root(fileH5, fileROOT, option='recreate', htype='i'):
    print("file h5 =", fileH5)
    image = read_image_h5(fileH5)
    tf = ROOT.TFile.Open(fileROOT, option)
    (nx, ny) = image.shape
    title = os.path.basename(fileH5).split('.')[0]
    title = title.replace('-', '_')
    h2 = ROOT.TH2I(title, title, nx, 0, nx, ny,
                   0, ny) if htype == 'i' else ROOT.TH2F(
                       title, title, nx, 0, nx, ny, 0, ny)
    h2.GetXaxis().SetTitle('x')
    h2.GetYaxis().SetTitle('y')
    _ = array2hist(image, h2)
    h2.Write()
    tf.Close()
    return
예제 #22
0
def ruttalo(his_file):
    stem, _ = os.path.splitext(his_file)
    his = openHIS(his_file)
    outname = stem + '.root'
    rf = ROOT.TFile(outname, 'recreate')

    runN = stem.split('run')[-1]
    run = runN if len(runN) else 'XXXX'

    for idx, section in enumerate(his):
        if idx % 5 == 0: print("transferring image ", idx)
        (nx, ny) = section.shape
        title = stem + ('_%04d' % idx)
        postfix = 'run{run}_ev{ev}'.format(run=run, ev=idx)
        title = 'pic_{pfx}'.format(pfx=postfix)
        h2 = ROOT.TH2S(title, title, nx, 0, nx, ny, 0, ny)
        h2.GetXaxis().SetTitle('x')
        h2.GetYaxis().SetTitle('y')
        _ = array2hist(np.fliplr(np.transpose(section)), h2)
        h2.Write()
    rf.Close()
    return outname, run
def plot_hist(prefit_list, fit_b_list, fit_s_list, hist_name):
    print("Processing {}".format(hist_name))
    print(prefit_list)
    print(fit_b_list)
    print(fit_s_list)
    canvas = pyr.TCanvas("canvas", "Histogram", 1800, 800)

    mountainrange_prefit = pyr.TH1F("mountainrange_prefit", hist_name, len(prefit_list), 0, len(prefit_list))
    mountainrange_fit_b  = pyr.TH1F("mountainrange_fit_b", hist_name, len(fit_b_list), 0, len(fit_b_list))
    mountainrange_fit_s  = pyr.TH1F("mountainrange_fit_s", hist_name, len(fit_s_list), 0, len(fit_s_list))

    root_numpy.array2hist(prefit_list, mountainrange_prefit)
    root_numpy.array2hist(fit_b_list, mountainrange_fit_b)
    root_numpy.array2hist(fit_s_list, mountainrange_fit_s)

    max_boundary = max([max(prefit_list), max(fit_b_list), max(fit_s_list)])*10.
    min_boundary = min([min(prefit_list), min(fit_b_list), min(fit_s_list)])/10.
    if min_boundary == 0: min_boundary = 1e-7

    mountainrange_prefit.Draw("HIST")
    mountainrange_fit_b.Draw("HIST SAME")
    mountainrange_fit_s.Draw("HIST SAME")

    mountainrange_prefit.SetLineColor(pyr.kBlack)
    mountainrange_fit_b.SetLineColor(pyr.kRed)
    mountainrange_fit_s.SetLineColor(pyr.kOrange)

    mountainrange_prefit.SetStats(0)
    mountainrange_fit_b.SetStats(0)
    mountainrange_fit_s.SetStats(0)

    mountainrange_prefit.GetXaxis().SetTitle("Histogram bins")
    pyr.gPad.SetLogy(True)
    mountainrange_prefit.GetYaxis().SetRangeUser(min_boundary, max_boundary)
    mountainrange_fit_b.GetYaxis().SetRangeUser(min_boundary, max_boundary)
    mountainrange_fit_s.GetYaxis().SetRangeUser(min_boundary, max_boundary)
    
    legend = pyr.TLegend(0.8, 0.8, 0.9, 0.9)
    legend.AddEntry(mountainrange_prefit, "prefit")
    legend.AddEntry(mountainrange_fit_b, "fit_b")
    legend.AddEntry(mountainrange_fit_s, "fit_s")
    legend.Draw()
    
    canvas.SaveAs("fitDiagnostics_plots_{}/{}.pdf".format(args.cardname, hist_name))
    canvas.SaveAs("fitDiagnostics_plots_{}/{}.png".format(args.cardname, hist_name))
예제 #24
0
_rhists = []
for _, title in productions:
    rhist = histograms[title].Clone('ratio_' + histograms[title].GetName())
    _rhists.append(rhist)
    
    rhist.Divide(htotal)

    rstack.Add(rhist)

rstack.Draw('SAME HIST')

uncert = htotal.Clone('uncert')
atotal = rnp.hist2array(htotal, copy=False)
err2s = rnp.array(uncert.GetSumw2(), copy=False)
err2s[1:-1] /= np.square(atotal)
rnp.array2hist(np.ones_like(atotal), uncert)

uncert.Draw('SAME E2')

ratiopad.SetTicky(1)

ratiopad.Update()

if args.config in ['fiducial', 'prefit']:
    canvas.raxes[0].SetTitle('fractions')
    rframe.SetMinimum(0.)
    rframe.SetMaximum(1.)

elif args.config == 'postfit':
    altrhist = althtotal.Clone('altrhist')
    altrhist.SetTitle('')
예제 #25
0
 def FileToHist(self, maxfilesize=100000, pt_cuts=[]):
     pt_cut_min = float(pt_cuts[0])
     pt_cut_max = float(pt_cuts[1])
     ptcut = str(int(pt_cut_min)) + "_" + str(int(pt_cut_max))
     self.LoadNormalization()
     os.system("mkdir -p " + self.pathPlots + "/histos/")
     for Sample in self.SamplesNames:
         SubSample = self.Samples_dict[Sample][0]
         for others in ["", "_others"]:
             f = ROOT.TFile.Open(
                 self.pathPlots + "/histos/" + Sample + others + "_" +
                 self.ptrange + "_ptcut_" + ptcut + ".root", "RECREATE")
             savename = self.NHBDict[Sample][SubSample].outdir.replace(
                 "/Vars/", "/Inputs/") + "TopJet" + others + self.NHBDict[
                     Sample][SubSample].FileExt.replace(
                         "index", self.ptrange + "_*").replace(
                             SubSample, Sample)
             filenameTemplate = sorted(glob(savename))
             savename = savename.replace("TopJet", "/histos/TopJet")
             nElements = 0
             Objs = {"Jet": [], "PF": [], "Image": []}
             for fn in filenameTemplate:
                 if nElements >= maxfilesize: break
                 TopJet = np.load(fn)
                 if len(TopJet) <= 0: continue
                 nElements += len(TopJet)
                 PT_mask = (TopJet[:, self.SubSetVars["Jet"]["jetpt"][1]] >
                            pt_cut_min) * (
                                TopJet[:,
                                       self.SubSetVars["Jet"]["jetpt"][1]] <
                                pt_cut_max)
                 Objs["Jet"].append(TopJet[PT_mask])
                 Objs["PF"].append(
                     np.load(fn.replace("TopJet", "PFParticles"))[PT_mask])
                 Objs["Image"].append(
                     np.load(fn.replace("TopJet", "Image"))[PT_mask])
             for Obj in self.ObjNames:
                 if len(Objs[Obj]) <= 0: continue
                 isImage = Obj == "Image"
                 Objs[Obj] = np.concatenate(Objs[Obj])
                 if len(Objs[Obj]) <= 0: continue
                 if Obj == "PF":
                     Objs[Obj] = np.concatenate(Objs[Obj], axis=0)
                 for VarName in self.SubSetVars[Obj]:
                     index = self.SubSetVars[Obj][VarName][1]
                     min, max, bin = self.SetRanges(VarName)
                     h_ = ROOT.TH1F(
                         VarName, "; " + VarName + "; A.U.", bin, min,
                         max) if not isImage else ROOT.TH2F(
                             "Image_" + VarName, "; #eta; #phi", self.n_eta,
                             -self.Radius, self.Radius, self.n_phi,
                             -self.Radius, self.Radius)
                     fill_hist(
                         h_,
                         Objs[Obj][:,
                                   index]) if not isImage else array2hist(
                                       np.sum(Objs[Obj][:, index, :, :],
                                              axis=0), h_)
                     h_.Write()
                     if Obj != "Jet": continue
                     min = self.ApplyNormalization2(min, VarName)
                     max = self.ApplyNormalization2(max, VarName)
                     self.ApplyNormalization(Objs[Obj][:, index], VarName)
                     h_ = ROOT.TH1F(
                         VarName + "_norm", "; " + VarName + "; A.U.", bin,
                         min, max) if not isImage else ROOT.TH2F(
                             "Image_" + VarName + "_norm", "; #eta; #phi",
                             self.n_eta, -self.Radius, self.Radius,
                             self.n_phi, -self.Radius, self.Radius)
                     fill_hist(
                         h_,
                         Objs[Obj][:,
                                   index]) if not isImage else array2hist(
                                       np.sum(Objs[Obj][:, index, :, :],
                                              axis=0), h_)
                     h_.Write()
             f.Close()
예제 #26
0
def plotstack(stacks, signals, uncerts, gobss, irow):
    ymax = 0.
    rmin = 0.
    rmax = 0.
    for icol in range(ncol):
        gobs = gobss[icol]
        uncert = uncerts[icol]
        signal = signals[icol]

        obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip)
                     for ip in range(gobs.GetN()))
        uncmax = max(
            uncert.GetBinContent(ix) + uncert.GetBinError(ix)
            for ix in range(1,
                            uncert.GetNbinsX() + 1))
        ymax = max(ymax, obsmax, uncmax)

        obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY()))
        bkg = rnp.hist2array(uncert)
        bkg -= rnp.hist2array(signal, copy=False)
        obsarray -= bkg

        mm = obsarray.min() - gobs.GetErrorYlow(np.argmin(obsarray))
        while rmin > mm * 1.1:
            rmin -= 2.

        mm = obsarray.max() + gobs.GetErrorYhigh(np.argmax(obsarray))
        while rmax < mm * 1.1:
            rmax += 2.

    if int(rmax) % 10 == 0:  # just to avoid axis label clashes
        rmax -= 1.

    canvas.yaxes[irow].SetNdivisions(405)
    canvas.yaxes[irow].SetTitle('events / GeV')
    canvas.raxes[irow].SetNdivisions(204)

    for icol in range(ncol):
        stack = stacks[icol]
        signal = signals[icol]
        uncert = uncerts[icol]
        gobs = gobss[icol]

        frame = uncert.Clone('frame')
        _temporaries.append(frame)
        frame.SetTitle('')
        frame.Reset()

        frame.SetTickLength(0.05, 'X')
        frame.SetTickLength(0.03, 'Y')
        frame.GetXaxis().SetNdivisions(canvas.xaxis.GetNdiv())
        frame.GetXaxis().SetLabelSize(0.)
        frame.GetXaxis().SetTitle('')
        frame.GetYaxis().SetNdivisions(canvas.yaxes[irow].GetNdiv())
        frame.GetYaxis().SetLabelSize(0.)
        frame.SetMinimum(0.)
        frame.SetMaximum(ymax * 1.4)

        distpad = canvas.cd((irow * ncol + icol) * 2 + 1)
        distpad.SetLogy(False)
        distpad.SetTicky(1)

        frame.Draw('HIST')
        stack.Draw('SAME HIST')
        uncert.Draw('SAME E2')
        gobs.Draw('PZ')

        distpad.Update()

        runcert = uncert.Clone('runcert')
        _temporaries.append(runcert)

        bkg = rnp.hist2array(runcert)
        bkg -= rnp.hist2array(signal, copy=False)

        obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY()))
        obsarray -= bkg

        robs = gobs.Clone('robs')
        _temporaries.append(robs)
        for ip in range(gobs.GetN()):
            robs.SetPoint(ip, gobs.GetX()[ip], obsarray[ip])

        rnp.array2hist(np.zeros_like(bkg), runcert)

        rframe = frame.Clone('rframe')
        _temporaries.append(rframe)
        rframe.GetXaxis().SetTickLength(0.15)
        rframe.GetYaxis().SetNdivisions(canvas.raxes[irow].GetNdiv())
        rframe.SetMinimum(rmin)
        rframe.SetMaximum(rmax)

        ratiopad = canvas.cd((irow * ncol + icol) * 2 + 2)
        ratiopad.SetLogy(False)
        ratiopad.SetGridy(False)
        ratiopad.SetTicky(1)

        rframe.Draw('HIST')
        runcert.Draw('SAME E2')
        zero.DrawLine(rframe.GetXaxis().GetXmin(), 0.,
                      rframe.GetXaxis().GetXmax(), 0.)
        signal.Draw('HIST SAME')
        robs.Draw('PZ')

        ratiopad.Update()
예제 #27
0
def main(args):

    # Initialise
    args, cfg = initialise(args)

    # Load data
    data, _, _ = load_data(args.input + 'data.h5', train=True)
    msk_sig = data['signal'] == 1
    msk_bkg = ~msk_sig

    # -------------------------------------------------------------------------
    ####
    #### # Initialise Keras backend
    #### initialise_backend(args)
    ####
    #### # Neural network-specific initialisation of the configuration dict
    #### initialise_config(args, cfg)
    ####
    #### # Keras import(s)
    #### from keras.models import load_model
    ####
    #### # NN
    #### from run.adversarial.common import add_nn
    #### with Profile("NN"):
    ####     classifier = load_model('models/adversarial/classifier/full/classifier.h5')
    ####     add_nn(data, classifier, 'NN')
    ####     pass
    # -------------------------------------------------------------------------

    # Fill measured profile
    profile_meas, _ = fill_profile(data[msk_bkg])

    # Add k-NN variable
    knnfeat = 'knn'
    add_knn(data,
            newfeat=knnfeat,
            path='models/knn/knn_{}_{}.pkl.gz'.format(VAR, EFF))

    # Loading KNN classifier
    knn = loadclf('models/knn/knn_{:s}_{:.0f}.pkl.gz'.format(VAR, EFF))

    # Filling fitted profile
    with Profile("Filling fitted profile"):
        rebin = 8
        edges, centres = dict(), dict()
        for ax, var in zip(['x', 'y'], [VARX, VARY]):

            # Short-hands
            vbins, vmin, vmax = AXIS[var]

            # Re-binned bin edges  @TODO: Make standardised right away?
            edges[ax] = np.interp(
                np.linspace(0, vbins, vbins * rebin + 1, endpoint=True),
                range(vbins + 1),
                np.linspace(vmin, vmax, vbins + 1, endpoint=True))

            # Re-binned bin centres
            centres[ax] = edges[ax][:-1] + 0.5 * np.diff(edges[ax])
            pass

        # Get predictions evaluated at re-binned bin centres
        g = dict()
        g['x'], g['y'] = np.meshgrid(centres['x'], centres['y'])
        g['x'], g['y'] = standardise(g['x'], g['y'])

        X = np.vstack((g['x'].flatten(), g['y'].flatten())).T
        fit = knn.predict(X).reshape(g['x'].shape).T

        # Fill ROOT "profile"
        profile_fit = ROOT.TH2F('profile_fit', "",
                                len(edges['x']) - 1, edges['x'].flatten('C'),
                                len(edges['y']) - 1, edges['y'].flatten('C'))
        root_numpy.array2hist(fit, profile_fit)
        pass

    # Plotting
    with Profile("Plotting"):
        for fit in [False, True]:

            # Select correct profile
            profile = profile_fit if fit else profile_meas

            # Plot
            plot(profile, fit)
            pass
        pass

    # Plotting local selection efficiencies for D2-kNN < 0
    # -- Compute signal efficiency
    for sig, msk in zip([True, False], [msk_sig, msk_bkg]):

        if sig:
            rgbs = [(247 / 255., 251 / 255., 255 / 255.),
                    (222 / 255., 235 / 255., 247 / 255.),
                    (198 / 255., 219 / 255., 239 / 255.),
                    (158 / 255., 202 / 255., 225 / 255.),
                    (107 / 255., 174 / 255., 214 / 255.),
                    (66 / 255., 146 / 255., 198 / 255.),
                    (33 / 255., 113 / 255., 181 / 255.),
                    (8 / 255., 81 / 255., 156 / 255.),
                    (8 / 255., 48 / 255., 107 / 255.)]

            red, green, blue = map(np.array, zip(*rgbs))
            nb_cols = len(rgbs)
            stops = np.linspace(0, 1, nb_cols, endpoint=True)
        else:
            rgbs = [(255 / 255., 51 / 255., 4 / 255.),
                    (247 / 255., 251 / 255., 255 / 255.),
                    (222 / 255., 235 / 255., 247 / 255.),
                    (198 / 255., 219 / 255., 239 / 255.),
                    (158 / 255., 202 / 255., 225 / 255.),
                    (107 / 255., 174 / 255., 214 / 255.),
                    (66 / 255., 146 / 255., 198 / 255.),
                    (33 / 255., 113 / 255., 181 / 255.),
                    (8 / 255., 81 / 255., 156 / 255.),
                    (8 / 255., 48 / 255., 107 / 255.)]

            red, green, blue = map(np.array, zip(*rgbs))
            nb_cols = len(rgbs)
            stops = np.array([0] + list(
                np.linspace(0, 1, nb_cols - 1, endpoint=True) *
                (1. - EFF / 100.) + EFF / 100.))
            pass

        ROOT.TColor.CreateGradientColorTable(nb_cols, stops, red, green, blue,
                                             NB_CONTOUR)

        # Define arrays
        shape = (AXIS[VARX][0], AXIS[VARY][0])
        bins = [
            np.linspace(AXIS[var][1],
                        AXIS[var][2],
                        AXIS[var][0] + 1,
                        endpoint=True) for var in VARS
        ]
        x, y, z = (np.zeros(shape) for _ in range(3))

        # Create `profile` histogram
        profile = ROOT.TH2F('profile', "",
                            len(bins[0]) - 1, bins[0].flatten('C'),
                            len(bins[1]) - 1, bins[1].flatten('C'))

        # Compute inclusive efficiency in bins of `VARY`
        effs = list()
        for edges in zip(bins[1][:-1], bins[1][1:]):
            msk_bin = (data[VARY] > edges[0]) & (data[VARY] < edges[1])
            msk_pass = data[knnfeat] < 0
            num = data.loc[msk & msk_bin & msk_pass,
                           'weight_test'].values.sum()
            den = data.loc[msk & msk_bin, 'weight_test'].values.sum()
            effs.append(num / den)
            pass

        # Fill profile
        for i, j in itertools.product(*map(range, shape)):

            # Bin edges in x and y
            edges = [bin[idx:idx + 2] for idx, bin in zip([i, j], bins)]

            # Masks
            msks = [(data[var] > edges[dim][0]) & (data[var] <= edges[dim][1])
                    for dim, var in enumerate(VARS)]
            msk_bin = reduce(lambda x, y: x & y, msks)
            data_ = data[msk & msk_bin]

            # Set non-zero bin content
            if np.sum(msk & msk_bin):
                msk_pass = data_[knnfeat] < 0
                num = data.loc[msk & msk_bin & msk_pass,
                               'weight_test'].values.sum()
                den = data.loc[msk & msk_bin, 'weight_test'].values.sum()
                eff = num / den
                profile.SetBinContent(i + 1, j + 1, eff)
                pass
            pass

        c = rp.canvas(batch=True)
        pad = c.pads()[0]._bare()
        pad.cd()
        pad.SetRightMargin(0.20)
        pad.SetLeftMargin(0.15)
        pad.SetTopMargin(0.10)

        # Styling
        profile.GetXaxis().SetTitle("Large-#it{R} jet " +
                                    latex(VARX, ROOT=True) +
                                    " = log(m^{2}/p_{T}^{2})")
        profile.GetYaxis().SetTitle("Large-#it{R} jet " +
                                    latex(VARY, ROOT=True) + " [GeV]")
        profile.GetZaxis().SetTitle("Selection efficiency for %s^{(%s%%)}" %
                                    (latex(VAR, ROOT=True), EFF))

        profile.GetYaxis().SetNdivisions(505)
        profile.GetZaxis().SetNdivisions(505)
        profile.GetXaxis().SetTitleOffset(1.4)
        profile.GetYaxis().SetTitleOffset(1.8)
        profile.GetZaxis().SetTitleOffset(1.3)
        zrange = (0., 1.)
        if zrange:
            profile.GetZaxis().SetRangeUser(*zrange)
            pass
        profile.SetContour(NB_CONTOUR)

        # Draw
        profile.Draw('COLZ')

        # Decorations
        c.text(qualifier=QUALIFIER, ymax=0.92, xmin=0.15)
        c.text(["#sqrt{s} = 13 TeV", "#it{W} jets" if sig else "Multijets"],
               ATLAS=False)

        # -- Efficiencies
        xaxis = profile.GetXaxis()
        yaxis = profile.GetYaxis()
        tlatex = ROOT.TLatex()
        tlatex.SetTextColor(ROOT.kGray + 2)
        tlatex.SetTextSize(0.023)
        tlatex.SetTextFont(42)
        tlatex.SetTextAlign(32)
        xt = xaxis.GetBinLowEdge(xaxis.GetNbins())
        for eff, ibin in zip(effs, range(1, yaxis.GetNbins() + 1)):
            yt = yaxis.GetBinCenter(ibin)
            tlatex.DrawLatex(
                xt, yt, "%s%.1f%%" %
                ("#bar{#varepsilon}^{rel}_{%s} = " %
                 ('sig' if sig else 'bkg') if ibin == 1 else '', eff * 100.))
            pass

        # -- Bounds
        BOUNDS[0].DrawCopy("SAME")
        BOUNDS[1].DrawCopy("SAME")
        c.latex("m > 50 GeV",
                -4.5,
                BOUNDS[0].Eval(-4.5) + 30,
                align=21,
                angle=-37,
                textsize=13,
                textcolor=ROOT.kGray + 3)
        c.latex("m < 300 GeV",
                -2.5,
                BOUNDS[1].Eval(-2.5) - 30,
                align=23,
                angle=-57,
                textsize=13,
                textcolor=ROOT.kGray + 3)

        # Save
        mkdir('figures/knn/')
        c.save('figures/knn/knn_eff_{}_{:s}_{:.0f}.pdf'.format(
            'sig' if sig else 'bkg', VAR, EFF))
        pass

    return
예제 #28
0
def plotstack(stack, signal, uncert, gobs, ivert):
    uncert.SetTickLength(0., 'X')
    uncert.SetTickLength(0., 'Y')
    uncert.SetFillColor(ROOT.kBlack)
    uncert.SetFillStyle(3003)
    uncert.SetLineWidth(0)
    uncert.GetXaxis().SetNdivisions(120)
    uncert.GetYaxis().SetNdivisions(110)

    distpad = canvas.cd(ivert * 2 + 1)

    distpad.SetLogy(False)

    frame = uncert.Clone('frame')
    _temporaries.append(frame)
    frame.SetTitle('')
    frame.Reset()

    frame.GetXaxis().SetLabelSize(0.)
    frame.GetXaxis().SetTitle('')
    obsmax = max(gobs.GetY()[ip] + gobs.GetErrorYhigh(ip)
                 for ip in range(gobs.GetN()))
    frame.SetMinimum(0.)
    frame.SetMaximum(max(obsmax, uncert.GetMaximum()) * 1.4)

    frame.Draw('HIST')
    stack.Draw('SAME HIST')
    uncert.Draw('SAME E2')
    gobs.Draw('PZ')

    distpad.Update()

    ratiopad = canvas.cd(ivert * 2 + 2)

    ratiopad.SetGridy(False)

    runcert = uncert.Clone('runcert')
    _temporaries.append(runcert)
    runcert.SetTitle('')

    bkg = rnp.hist2array(runcert)
    bkg -= rnp.hist2array(signal, copy=False)

    obsarray = rnp.array(ROOT.TArrayD(gobs.GetN(), gobs.GetY()))
    obsarray -= bkg

    robs = gobs.Clone('robs')
    _temporaries.append(robs)
    for ip in range(gobs.GetN()):
        robs.SetPoint(ip, gobs.GetX()[ip], obsarray[ip])

    rnp.array2hist(np.zeros_like(bkg), runcert)

    runcert.SetTickLength(0., 'X')
    runcert.SetTickLength(0., 'Y')
    runcert.GetXaxis().SetLabelSize(0.)
    runcert.GetXaxis().SetTitle('')
    runcert.GetYaxis().SetLabelSize(0.)
    runcert.GetYaxis().SetTitle('')
    runcert.GetYaxis().SetNdivisions(502)

    runcert.Draw('E2')

    zero = ROOT.TLine(runcert.GetXaxis().GetXmin(), 0.,
                      runcert.GetXaxis().GetXmax(), 0.)
    _temporaries.append(zero)
    zero.SetLineColor(ROOT.kBlack)
    zero.SetLineWidth(1)
    zero.SetLineStyle(ROOT.kSolid)
    zero.Draw()

    signal.Draw('HIST SAME')

    robs.Draw('PZ')

    rmin = 0.
    while rmin > obsarray.min() * 1.1:
        rmin -= 10.
    rmax = 0.
    while rmax < obsarray.max() * 1.1:
        rmax += 10.

    runcert.SetMaximum(rmax)
    runcert.SetMinimum(rmin)

    ratiopad.Update()
예제 #29
0
parameters.variables.ieta = 'abs(ieta)'
parameters.variables.et = 'et_raw'
parameters.variables.ntt = 'ntt'
parameters.variables.rho = 'rho'
parameters.variables.iso = 'iso'
## Steps
parameters.steps.train_workingpoints = True
parameters.steps.fit_ntt_vs_rho = True
parameters.steps.test_workingpoints = True
parameters.steps.do_compression = True
## eta-pt efficiency shape
parameters.eta_pt_optimization.eta_optimization = 'none'
efficiencies_low_array = np.array([0.80,0.80,0.80,0.80,0.80,0.75,0.80, 0.85])
efficiencies_high_array = np.array([0.92,0.95,0.95,0.95,0.95,0.95,0.95, 0.95])
eta_binning = [0.5, 3.5, 6.5, 9.5, 13.5, 18.5, 22.5, 25.5, 28.5]
efficiencies_low = Hist(eta_binning)
efficiencies_high = Hist(eta_binning)
array2hist(efficiencies_low_array, efficiencies_low)
array2hist(efficiencies_high_array, efficiencies_high)
parameters.eta_pt_optimization.eta_pt_efficiency_shapes = \
        double_slope_relaxation_vs_pt(efficiencies_low,\
                                      efficiencies_high,\
                                      threshold_low=56.,\
                                      threshold_high=80.,\
                                      eff_min=0.5,\
                                      max_et=120.)
## LUT Compression
parameters.compression.eta = [0,5,6,9,10,12,13,14,17,18,19,20,23,24,25,26,32]
parameters.compression.et = [0,18,20,22,28,32,37,42,52,63,73,81,87,91,111,151,256]
parameters.compression.ntt = [0,6,11,16,21,26,31,36,41,46,51,56,61,66,71,76,81,86,91,96,101,106,111,116,121,126,131,136,141,146,151,156,256]
예제 #30
0
def optimize_background_rejection_vs_ieta(effs, isolations, signalfile, signaltree, backgroundfile, backgroundtree, inputnames=['abs(ieta)','ntt'], targetname='iso', cut='et>10'):
    ieta_binning = [0.5, 3.5, 6.5, 9.5, 13.5, 18.5, 24.5, 27.5]
    # Compute signal efficiencies
    ninputs = len(inputnames)
    branches = copy.deepcopy(inputnames)
    branches.append(targetname)
    data = root2array(signalfile, treename=signaltree, branches=branches, selection=cut)
    data = data.view((np.float64, len(data.dtype.names)))
    inputs = data[:, range(ninputs)].astype(np.float32)
    targets  = data[:, [ninputs]].astype(np.float32).ravel()
    xs  = data[:, [0]].astype(np.float32).ravel()
    # fill signal ieta histogram and normalize to 1
    histo_signal = Hist(ieta_binning)
    fill_hist(histo_signal, xs)
    #histo_signal.Scale(1./histo_signal.integral(overflow=True))
    # signal_efficiencies is a 2D array
    # The first dimension corresponds to different ieta values
    # The second dimension corresponds to different working points
    signal_efficiencies = [graph2array(efficiency.efficiency_graph(pass_function=(lambda x:np.less(x[1],iso.predict(x[0]))), function_inputs=(inputs,targets), xs=xs, bins=ieta_binning))[:,[1]].ravel() for iso in isolations]
    signal_efficiencies = np.column_stack(signal_efficiencies)
    # Compute background efficiencies
    ninputs = len(inputnames)
    branches = copy.deepcopy(inputnames)
    branches.append(targetname)
    data = root2array(backgroundfile, treename=backgroundtree, branches=branches, selection=cut)
    data = data.view((np.float64, len(data.dtype.names)))
    inputs = data[:, range(ninputs)].astype(np.float32)
    targets  = data[:, [ninputs]].astype(np.float32).ravel()
    xs  = data[:, [0]].astype(np.float32).ravel()
    # fill background ieta histogram and normalize to 1
    histo_background = Hist(ieta_binning)
    fill_hist(histo_background, xs)
    #histo_background.Scale(1./histo_background.integral(overflow=True))
    # background_efficiencies is a 2D array
    # The first dimension corresponds to different ieta values
    # The second dimension corresponds to different working points
    background_efficiencies = [graph2array(efficiency.efficiency_graph(pass_function=(lambda x:np.less(x[1],iso.predict(x[0]))), function_inputs=(inputs,targets), xs=xs, bins=ieta_binning))[:,[1]].ravel() for iso in isolations]
    background_efficiencies = np.column_stack(background_efficiencies)
    signal_efficiencies_diff_graphs = []
    background_efficiencies_diff_graphs = []
    optimal_points_graphs = []
    optimal_points = []
    # compute best working point for each ieta (loop on ieta)
    for i,(signal_effs,background_effs) in enumerate(zip(signal_efficiencies, background_efficiencies)):
        # Compute the probability of signal in this ieta bin for the different efficiency points
        # It is assumed that the cut is applied only in this bin, all the other bins keep the same number of entries
        n_i = histo_signal[i+1].value
        n_tot = histo_signal.integral(overflow=True)
        proba_signal = np.array([n_i*eff/(n_tot-n_i*(1.-eff)) for eff in signal_effs])
        # Same as above, but for background
        n_i = histo_background[i+1].value 
        n_tot = histo_background.integral(overflow=True)
        proba_background = np.array([n_i*eff/(n_tot-n_i*(1.-eff)) for eff in background_effs])
        signal_efficiencies_diff_graph, background_efficiencies_diff_graph, optimal_points_graph, optimal_point = find_best_working_point(effs, signal_effs, background_effs, proba_signal, proba_background)
        signal_efficiencies_diff_graph.SetName('efficiencies_signal_ieta_{}'.format(i))
        background_efficiencies_diff_graph.SetName('efficiencies_background_ieta_{}'.format(i))
        optimal_points_graph.SetName('signal_background_optimal_points_ieta_{}'.format(i))
        signal_efficiencies_diff_graphs.append(signal_efficiencies_diff_graph)
        background_efficiencies_diff_graphs.append(background_efficiencies_diff_graph)
        optimal_points_graphs.append(optimal_points_graph)
        optimal_points.append(optimal_point)
    optimal_points_histo = Hist(ieta_binning)
    array2hist(optimal_points, optimal_points_histo)
    return signal_efficiencies_diff_graphs, background_efficiencies_diff_graphs, optimal_points_graphs, optimal_points_histo
                        .format(args.slot, oh, args.mapping))
                else:
                    chanOrStripLabel = "Readout Strip"
                    (histArray, edges) = rp.hist2array(
                        vfatLatHists2D[args.slot][oh][vfat], return_edges=True)
                    remappedArray = np.zeros(histArray.shape)
                    xMax = histArray.shape[0]
                    yMax = histArray.shape[1]
                    for lat in range(0, xMax):
                        for vfatCH in range(0, yMax):
                            strip = dictMapping[
                                args.slot][oh][vfat]['Strip'][vfatCH]
                            remappedArray[lat][strip] = histArray[lat][vfatCH]
                            pass
                        pass
                    vfatLatHists2D[args.slot][oh][vfat] = rp.array2hist(
                        remappedArray, vfatLatHists2D[args.slot][oh][vfat])
                    pass
                pass

            # Set Style
            vfatHitMulti[args.slot][oh][vfat].SetTitle("VFAT{0}".format(vfat))
            vfatHitMulti[args.slot][oh][vfat].GetXaxis().SetTitle(
                "Hit Multiplicity per Event")
            vfatHitMulti[args.slot][oh][vfat].GetXaxis().SetRangeUser(
                1e-1, 129)
            vfatHitMulti[args.slot][oh][vfat].GetYaxis().SetTitle("N")
            vfatHitMulti[args.slot][oh][vfat].GetYaxis().SetRangeUser(
                1e-1, 1e8)

            # Set Style
            vfatLatHists[args.slot][oh][vfat].SetTitle("VFAT{0}".format(vfat))
    if args.ratio:
        max_boundary = 10.
        min_boundary = 0.00001
    else:
        max_boundary = max([
            max(hist_central_array[np.nonzero(hist_central_array)]),
            max(hist_upper_array[np.nonzero(hist_upper_array)]),
            max(hist_lower_array[np.nonzero(hist_lower_array)])
        ]) * 10.
        min_boundary = min([
            min(hist_central_array[np.nonzero(hist_central_array)]),
            min(hist_upper_array[np.nonzero(hist_upper_array)]),
            min(hist_lower_array[np.nonzero(hist_lower_array)])
        ]) / 10.

    root_numpy.array2hist(hist_central_array, mountainrange_central)
    root_numpy.array2hist(hist_upper_array, mountainrange_upper)
    root_numpy.array2hist(hist_lower_array, mountainrange_lower)
    #mountainrange_central = root_numpy.array2hist(hist_central_array)
    #mountainrange_upper = root_numpy.array2hist(hist_upper_array)
    #mountainrange_lower = root_numpy.array2hist(hist_lower_array)
    #root_numpy.array2hist(hist_central_array, mountainrange_central_ratio)
    #root_numpy.array2hist(hist_upper_array, mountainrange_upper_ratio)
    #root_numpy.array2hist(hist_lower_array, mountainrange_lower_ratio)

    if args.ratio:
        mountainrange_upper.Divide(mountainrange_central)
        mountainrange_lower.Divide(mountainrange_central)
        mountainrange_central.Divide(mountainrange_central)

    if debug: print("Drawing mountainrange")
예제 #33
0
parameters.variables.iso = 'iso'
## Steps
parameters.steps.train_workingpoints = True
parameters.steps.fit_ntt_vs_rho = True
parameters.steps.test_workingpoints = True
parameters.steps.do_compression = True
## eta-pt efficiency shape
parameters.eta_pt_optimization.eta_optimization = 'none'
efficiencies_low_array = np.array(
    [0.80, 0.80, 0.80, 0.80, 0.80, 0.75, 0.80, 0.85])
efficiencies_high_array = np.array(
    [0.92, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95, 0.95])
eta_binning = [0.5, 3.5, 6.5, 9.5, 13.5, 18.5, 22.5, 25.5, 28.5]
efficiencies_low = Hist(eta_binning)
efficiencies_high = Hist(eta_binning)
array2hist(efficiencies_low_array, efficiencies_low)
array2hist(efficiencies_high_array, efficiencies_high)
parameters.eta_pt_optimization.eta_pt_efficiency_shapes = \
        double_slope_relaxation_vs_pt(efficiencies_low,\
                                      efficiencies_high,\
                                      threshold_low=56.,\
                                      threshold_high=80.,\
                                      eff_min=0.5,\
                                      max_et=120.)
## LUT Compression
parameters.compression.eta = [
    0, 5, 6, 9, 10, 12, 13, 14, 17, 18, 19, 20, 23, 24, 25, 26, 32
]
parameters.compression.et = [
    0, 18, 20, 22, 28, 32, 37, 42, 52, 63, 73, 81, 87, 91, 111, 151, 256
]
예제 #34
0

# ### Plot jet images

# In[ ]:

from ROOT import gStyle, kLightTemperature, kBlackBody, kCool, kColorPrintableOnGrey , kPastel , kViridis
from IPython.display import Image
print("Number of jet images: ", len(jet_images))
    
sum_images = np.sum(jet_images, axis=0)

h_title = "Jet Image, "+str(pt_low)+" < p_{T} < "+str(pt_high)+" GeV"
h = TH2D( "h_jet_image", h_title, n_bins, ji_x_range[0], ji_x_range[1], n_bins, ji_y_range[0], ji_y_range[1])

rtnp.array2hist(sum_images, h)

#Plot using root
gStyle.SetOptStat(0)
# gStyle.SetPalette(kPastel)
# gStyle.SetPalette(kCool)
gStyle.SetPalette(kColorPrintableOnGrey)

c = TCanvas("c", "c", 800, 600)
c.SetLogz()

#h.SetAxisRange(1, 100000, "Z")
h.GetXaxis().SetTitle("#eta")
h.GetYaxis().SetTitle("#phi")
h.Draw("colz")