12import matplotlib.pyplot
as plt
15from ROOT
import TH1F, gBenchmark, gRandom
19fig, ax = plt.subplots(figsize=(8, 6), num=
"The HSUM Example")
21gBenchmark.Start(
"hsum")
25RANGE_MIN, RANGE_MAX = -4, 4
26total =
TH1F(
"total",
"This is the total distribution", BINS, RANGE_MIN, RANGE_MAX)
27main =
TH1F(
"main",
"Main contributor", BINS, RANGE_MIN, RANGE_MAX)
28s1 =
TH1F(
"s1",
"This is the first signal", BINS, RANGE_MIN, RANGE_MAX)
29s2 =
TH1F(
"s2",
"This is the second signal", BINS, RANGE_MIN, RANGE_MAX)
33counts = {
"total": np.zeros(BINS),
"main": np.zeros(BINS),
"s1": np.zeros(BINS),
"s2": np.zeros(BINS)}
37gauss, landau = gRandom.Gaus, gRandom.Landau
47def fill_hist(hist_name, x, weight=1.0):
48 if RANGE_MIN <= x < RANGE_MAX:
49 idx =
int((x - RANGE_MIN) / (RANGE_MAX - RANGE_MIN) * BINS)
50 counts[hist_name][idx] += weight
56for i
in range(1, N_EVENTS + 1):
58 xmain = gauss(-1, 1.5)
59 xs1 = gauss(-0.5, 0.5)
64 fill_hist(
"main", xmain)
65 fill_hist(
"s1", xs1, 0.3)
66 fill_hist(
"s2", xs2, 0.2)
67 fill_hist(
"total", xmain)
68 fill_hist(
"total", xs1, 0.3)
69 fill_hist(
"total", xs2, 0.2)
71 total[...] = counts[
"total"]
72 main[...] = counts[
"main"]
73 s1[...] = counts[
"s1"]
74 s2[...] = counts[
"s2"]
79 entries = total.GetEntries()
80 mean = total.GetMean()
81 stddev = total.GetStdDev()
82 stats_text = f
"Entries = {entries:.0f}\nMean = {mean:.2f}\nStd Dev = {stddev:.2f}"
83 hep.histplot(main, histtype=
"fill", color=
"gray", alpha=0.5, edgecolor=
"blue", linewidth=1.5, ax=ax)
84 hep.histplot(total, histtype=
"errorbar", color=
"black", ecolor=
"blue", linewidth=2, ax=ax)
85 hep.histplot(s1, histtype=
"errorbar", color=
"blue", alpha=0.7, ecolor=
"blue", linewidth=2, marker=
"+", ax=ax)
86 hep.histplot(s2, histtype=
"errorbar", color=
"blue", alpha=0.7, ecolor=
"blue", linewidth=2, marker=
"+", ax=ax)
87 ax.set_title(
"This is the total distribution", pad=20, fontsize=14, loc=
"center")
92 transform=ax.transAxes,
96 bbox=dict(facecolor=
"white", edgecolor=
"black", boxstyle=
"round,pad=0.2", alpha=0.9),
100 ax.set_xlim(RANGE_MIN, RANGE_MAX)
101 ax.set_ylim(0, max(counts[
"total"]) * 1.2)
1-D histogram with a float per channel (see TH1 documentation)