Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist003_TH1_draw_uhi.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook
4## Draw a 1D histogram to a canvas.
5##
6## \note When using graphics inside a ROOT macro the objects must be created with `new`.
7##
8## \macro_code
9##
10## \date July 2025
11## \author Rene Brun, Giacomo Parolini, Nursena Bitirgen
12
13import matplotlib.pyplot as plt
14import mplhep as hep
15import numpy as np
16import ROOT
17
18# Create and fill the histogram.
19# See hist002_TH1_fillrandom_userfunc.C for more information about this section.
20form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")
21rangeMin = 0.0
22rangeMax = 10.0
23sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
24sqroot.SetParameters(10.0, 4.0, 1.0, 20.0)
25
26nBins = 200
27h1d = ROOT.TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax)
28
29random_numbers = np.array([sqroot.GetRandom() for _ in range(10000)])
30h1d[...] = np.histogram(np.array([sqroot.GetRandom() for _ in range(10000)]), bins=nBins, range=(rangeMin, rangeMax))[0]
31
32# Create a canvas and draw the histogram
33plt.figure(figsize=(7, 9))
34
35# Split the canvas into two sections to plot both the function and the histogram
36plt.axes([0.05, 0.55, 0.90, 0.40])
37x = np.linspace(rangeMin, rangeMax, 500)
38plt.plot(x, [sqroot.Eval(xi) for xi in x], "b-", lw=5)
40plt.title("x*gaus(0) + [3]*form1")
41plt.text(5, 40, "The sqroot function", fontsize=18, weight="bold", bbox=dict(facecolor="white", edgecolor="black"))
42plt.xlim(rangeMin, rangeMax)
43plt.ylim(bottom=0)
44
45plt.axes([0.05, 0.05, 0.90, 0.40])
46hep.histplot(h1d, yerr=False, histtype="fill", color="brown", alpha=0.7, edgecolor="blue", linewidth=1)
47
48plt.title("Test random numbers")
49plt.xlabel("x")
50plt.ylabel("Entries")
51plt.xlim(rangeMin, rangeMax)
52plt.ylim(bottom=0)
53
54stats_text = (
55 f"Entries = {len(random_numbers)}\nMean = {(np.mean(random_numbers)):.3f}\nStd Dev = {(np.std(random_numbers)):.2f}"
56)
57plt.text(0.90, 0.90, stats_text, transform=plt.gca().transAxes, ha="right", va="top", bbox=dict(facecolor="white"))
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.