Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist007_TH1_liveupdate_uhi.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook -js
4## Simple example illustrating how to use the C++ interpreter.
5##
6## \macro_code
7##
8## \date July 2025
9## \author Wim Lavrijsen, Nursena Bitirgen
10
11import matplotlib.pyplot as plt
12import mplhep as hep
13import numpy as np
14from ROOT import TH1F, gBenchmark, gRandom
15
16# Create a new canvas, and enable interactive mode.
17plt.ion()
18fig, ax = plt.subplots(figsize=(8, 6), num="The HSUM Example")
19
20gBenchmark.Start("hsum")
21
22# Create some histograms.
23BINS = 100
24RANGE_MIN, RANGE_MAX = -4, 4
25total = TH1F("total", "This is the total distribution", BINS, RANGE_MIN, RANGE_MAX)
26main = TH1F("main", "Main contributor", BINS, RANGE_MIN, RANGE_MAX)
27s1 = TH1F("s1", "This is the first signal", BINS, RANGE_MIN, RANGE_MAX)
28s2 = TH1F("s2", "This is the second signal", BINS, RANGE_MIN, RANGE_MAX)
29# total.Sumw2()
30
31# initialize a dictionary that holds the histogram counts as numpy arrays
32counts = {"total": np.zeros(BINS), "main": np.zeros(BINS), "s1": np.zeros(BINS), "s2": np.zeros(BINS)}
33
34# Initialize random number generator.
36gauss, landau = gRandom.Gaus, gRandom.Landau
37
38# def gauss(loc, scale):
39# return np.random.normal(loc, scale)
40
41# def landau(loc, scale):
42# return np.random.standard_cauchy() * scale + loc
43
44
45# initialize the histogram filling method
46def fill_hist(hist_name, x, weight=1.0):
47 if RANGE_MIN <= x < RANGE_MAX:
48 idx = int((x - RANGE_MIN) / (RANGE_MAX - RANGE_MIN) * BINS)
49 counts[hist_name][idx] += weight
50
51
52# Fill histograms randomly
53kUPDATE = 500
54N_EVENTS = 10000
55for i in range(1, N_EVENTS + 1):
56 # Generate random values.
57 xmain = gauss(-1, 1.5)
58 xs1 = gauss(-0.5, 0.5)
59 xs2 = landau(1, 0.15)
60
61 # Fill histograms
62 # Compute the counts
63 fill_hist("main", xmain)
64 fill_hist("s1", xs1, 0.3)
65 fill_hist("s2", xs2, 0.2)
66 fill_hist("total", xmain)
67 fill_hist("total", xs1, 0.3)
68 fill_hist("total", xs2, 0.2)
69 # Set the bin contents
70 total[...] = counts["total"]
71 main[...] = counts["main"]
72 s1[...] = counts["s1"]
73 s2[...] = counts["s2"]
74
75 # Update display every kUPDATE events.
76 if i % kUPDATE == 0:
77 ax.cla()
78 entries = total.GetEntries()
79 mean = total.GetMean()
80 stddev = total.GetStdDev()
81 stats_text = f"Entries = {entries:.0f}\nMean = {mean:.2f}\nStd Dev = {stddev:.2f}"
82 hep.histplot(main, histtype="fill", color="gray", alpha=0.5, edgecolor="blue", linewidth=1.5, ax=ax)
83 hep.histplot(total, histtype="errorbar", color="black", ecolor="blue", linewidth=2, ax=ax)
84 hep.histplot(s1, histtype="errorbar", color="blue", alpha=0.7, ecolor="blue", linewidth=2, marker="+", ax=ax)
85 hep.histplot(s2, histtype="errorbar", color="blue", alpha=0.7, ecolor="blue", linewidth=2, marker="+", ax=ax)
86 ax.set_title("This is the total distribution", pad=20, fontsize=14, loc="center")
87 ax.text(
88 0.95,
89 0.90,
90 stats_text,
91 transform=ax.transAxes,
92 ha="right",
93 va="top",
94 fontsize=12,
95 bbox=dict(facecolor="white", edgecolor="black", boxstyle="round,pad=0.2", alpha=0.9),
96 )
97
98 # Plot formatting
99 ax.set_xlim(RANGE_MIN, RANGE_MAX)
100 ax.set_ylim(0, max(counts["total"]) * 1.2)
101 plt.pause(0.001)
102
103# Done, show final plot.
104plt.grid(True)
105plt.ioff()
106plt.show()
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878