Esempio n. 1
0
def plot_dist(yvals_f, var, bin, label, outpath, sample):

    hists_gen = get_distribution(yvals_f, "gen", bin, var)
    # hists_cand = get_distribution(yvals_f, "cand", bin, var)
    hists_pred = get_distribution(yvals_f, "pred", bin, var)

    plt.figure()
    ax = plt.axes()
    v1 = mplhep.histplot(
        [h[bh.rebin(2)] for h in hists_gen],
        stack=True,
        label=[class_names[k] for k in [13, 11, 22, 1, 2, 130, 211]],
        lw=1)
    mplhep.histplot(
        [h[bh.rebin(2)] for h in hists_pred],
        stack=True,
        color=[x.stairs.get_edgecolor() for x in v1],
        lw=2,
        histtype="errorbar",
    )

    legend1 = plt.legend(v1, [x.legend_artist.get_label() for x in v1],
                         loc=(0.60, 0.44),
                         title="true")
    # legend2 = plt.legend(v2, [x.legend_artist.get_label() for x in v1], loc=(0.8, 0.44), title="pred")
    plt.gca().add_artist(legend1)
    plt.ylabel("Total number of particles / bin")
    cms_label(ax)
    sample_label(sample, ax)

    plt.yscale("log")
    plt.ylim(top=1e9)
    plt.xlabel(f"PFCandidate {label} [GeV]")
    plt.savefig(f"{outpath}/pfcand_{var}.pdf", bbox_inches="tight")
    plt.close()
Esempio n. 2
0
def test_rebin_1d():
    h = bh.Histogram(bh.axis.Regular(20, 1, 5))
    h.fill(1.1)

    hs = h[{0: slice(None, None, bh.rebin(4))}]
    assert_array_equal(hs.view(), [1, 0, 0, 0, 0])

    hs = h[{0: bh.rebin(4)}]
    assert_array_equal(hs.view(), [1, 0, 0, 0, 0])
Esempio n. 3
0
def test_rebin_nd():
    h = bh.Histogram(
        bh.axis.Regular(20, 1, 3), bh.axis.Regular(30, 1, 3), bh.axis.Regular(40, 1, 3)
    )

    s = bh.tag.Slicer()

    assert h[{0: s[:: bh.rebin(2)]}].axes.size == (10, 30, 40)
    assert h[{1: s[:: bh.rebin(2)]}].axes.size == (20, 15, 40)
    assert h[{2: s[:: bh.rebin(2)]}].axes.size == (20, 30, 20)

    assert h[{0: s[:: bh.rebin(2)], 2: s[:: bh.rebin(2)]}].axes.size == (10, 30, 20)

    assert h[{1: s[:: bh.sum]}].axes.size == (20, 40)
def test_mix_value_with_slice_2():
    h = bh.Histogram(bh.axis.Regular(10, 0, 10), bh.axis.Regular(10, 0, 10),
                     bh.axis.Integer(0, 2))

    vals = np.arange(100).reshape(10, 10)
    h[:, :, True] = vals

    assert h[0, 1, True] == 1
    assert h[1, 0, True] == 10
    assert h[1, 1, True] == 11
    assert h[3, 4, False] == 0

    assert_array_equal(h[:, :, True].view(), vals)
    assert_array_equal(h[:, :, False].view(), 0)

    h2 = h[bh.rebin(2), bh.rebin(5), :]
    assert_array_equal(h2.shape, (5, 2, 2))
Esempio n. 5
0
    def _step_shortcut(x: Any) -> Any:
        """
        Convert some specific indices to step.
        """

        if not isinstance(x, complex):
            return x

        if x.real != 0:
            raise ValueError("The step should not have real part")
        if x.imag % 1 != 0:
            raise ValueError("The imaginary part should be an integer")
        return bh.rebin(int(x.imag))
def test_get_1D_slice():
    h1 = bh.Histogram(bh.axis.Regular(10, 0, 1))
    h2 = bh.Histogram(bh.axis.Regular(5, 0, 0.5))
    h1.metadata = {"that": 3}

    h1.fill([0.25, 0.25, 0.25, 0.15])
    h2.fill([0.25, 0.25, 0.25, 0.15])

    assert h1 != h2
    assert h1[:5] == h2
    assert h1[:bh.loc(0.5)] == h2
    assert h1[2:4] == h2[2:4]
    assert h1[bh.loc(0.2):bh.loc(0.4)] == h2[bh.loc(0.2):bh.loc(0.4)]

    assert len(h1[2:4].view()) == 2
    assert len(h1[2:4:bh.rebin(2)].view()) == 1
    assert len(h1[::bh.rebin(2)].view()) == 5

    # Shortcut
    assert len(h1[bh.rebin(2)].view()) == 5

    assert h1[2:4].metadata == {"that": 3}
def test_repr():
    assert repr(bh.loc(2)) == "loc(2)"
    assert repr(bh.loc(3) + 1) == "loc(3) + 1"
    assert repr(bh.loc(1) - 2) == "loc(1) - 2"

    assert repr(bh.underflow) == "underflow"
    assert repr(bh.underflow + 1) == "underflow + 1"
    assert repr(bh.underflow - 1) == "underflow - 1"

    assert repr(bh.overflow) == "overflow"
    assert repr(bh.overflow + 1) == "overflow + 1"
    assert repr(bh.overflow - 1) == "overflow - 1"

    assert repr(bh.rebin(2)) == "rebin(2)"
Esempio n. 8
0
def test_shrink_rebin_1d():
    h = bh.Histogram(bh.axis.Regular(20, 0, 4))
    h.fill(1.1)
    hs = h[{0: slice(bh.loc(1), bh.loc(3), bh.rebin(2))}]
    assert_array_equal(hs.view(), [1, 0, 0, 0, 0])
Esempio n. 9
0
    def __init__(self,
                 files,
                 observable="nCleaned_Cands",
                 name="QCD",
                 channel="",
                 kfactor=1.0,
                 ptype="background",
                 luminosity=1.0,
                 rebin=1,
                 rebin_piecewise=[],
                 normalise=True,
                 xsections=None,
                 mergecat=True,
                 binrange=None):
        self._files = files
        self.observable = observable
        self.name = name
        self.ptype = ptype
        self.lumi = luminosity
        self.xsec = xsections
        self.outfile = None
        self.channel = channel
        self.nominal = {}
        self.systvar = set()
        self.rebin = rebin
        self.rebin_piecewise = np.array(rebin_piecewise).astype(np.float)
        self.binrange = binrange  # dropping bins the same way as droping elements in numpy arrays a[1:3]

        for fn in self._files:
            _proc = os.path.basename(fn).replace(".root", "")
            _file = uproot.open(fn)

            if not _file:
                raise ValueError("%s is not a valid rootfile" % self.name)

            histograms = None

            _scale = 1
            if ptype.lower() != "data":
                _scale = self.lumi * 1000.0  #

            for name in _file.keys():
                name = name.replace(";1", "")
                if name != self.observable: continue
                roothist = _file[name]
                newhist = roothist.to_boost() * _scale

                #### merge bins
                if self.rebin >= 1 and newhist.values(
                ).ndim == 1:  #written only for 1D right now
                    newhist = newhist[::bh.rebin(self.rebin)]

                ####merge bins to specified array
                if len(self.rebin_piecewise) != 0 and newhist.values(
                ).ndim == 1:  #written only for 1D right now
                    current_bins, current_edges = newhist.to_numpy()
                    #current_bins = newhist.axes.edges #Confused Chad. This returns a 'boost_histogram.axis.ArrayTuple' which cannot be cast into np,array?
                    new_freq = self.rebin_piecewise_constant(
                        current_edges, newhist.values(), self.rebin_piecewise)
                    new_variances = self.rebin_piecewise_constant(
                        current_bins, newhist.variances(),
                        self.rebin_piecewise)
                    newhist = bh.Histogram(bh.axis.Variable(
                        self.rebin_piecewise),
                                           storage=bh.storage.Weight())
                    newhist[:] = np.stack([new_freq, new_variances], axis=-1)

                newhist.name = name
                if name in self.nominal.keys():
                    self.nominal[name] += newhist
                else:
                    self.nominal[name] = newhist

                try:
                    self.systvar.add(re.search("sys_[\w.]+", name).group())
                except:
                    pass

        self.merged = {}
        self.merged = {i: (i, c) for i, c in self.nominal.items()}
Esempio n. 10
0

histname='h1_phoPt'
Entries = hist_dict[histname].sum(flow=True)
Lumi=150000


## --Normalize histogram
print("Number of events: ",Entries)
weight = Lumi / Entries # In this case, the hist already weighted by xsec 
#weight = 1


## --Rebining  or Slicing
rebin=10
print(hist_dict[histname][::bh.rebin(rebin)])
hist_obj = hist_dict[histname][::bh.rebin(rebin)]
bin_width = hist_obj.axes[0].widths[0]

## --Draw hist
mplhep.histplot(hist_obj*weight,label='%s %d' % ("Entries: ",Entries))
plt.title(histname)

# Log scale
plt.yscale('log')

# Label name
xlabel_name = histname.split('_')[1] + '[GeV]'
ylabel_name = "Number of events / %3.1f GeV" %(bin_width)
plt.xlabel(xlabel_name)
plt.ylabel(ylabel_name)