Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist015_TH1_read_and_draw_uhi.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook -js
4## A Simple histogram drawing example.
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
14import ROOT
15from ROOT import TFile, gROOT
16
17mpl_fig = plt.figure(figsize=(14, 12))
18gs = mpl_fig.add_gridspec(3, 2, height_ratios=[1.5, 1.5, 1.5])
19
20# We connect the ROOT file generated in the hsimple.py tutorial
21
22example = TFile("py-hsimple.root")
24
25# Draw histogram hpx in first pad.
26hpx = gROOT.FindObject("hpx")
27
28# Get the statistics
29entries = int(hpx.GetEntries())
30mean = hpx.GetMean()
31std_dev = hpx.GetStdDev()
32info_text = f"Entries = {entries}\nMean = {mean:.5f}\nStd Dev = {std_dev:.3f}"
33
34ax1 = mpl_fig.add_subplot(gs[0:2, 0])
35ax1.set_facecolor("#FFFDD0C8")
36hep.histplot(hpx, ax=ax1, histtype="fill", color="#EEAC91", alpha=0.5, edgecolor="blue", linewidth=1.5)
38 0.65,
39 0.95,
40 info_text,
41 transform=ax1.transAxes,
42 verticalalignment="top",
43 fontsize=10,
44 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
45)
46
47# Draw hpx as an interactive lego.
48ax2 = mpl_fig.add_subplot(gs[0:2, 1], projection="3d")
49ax2.set_facecolor("#FFFDD0C8")
50x = np.array([hpx.GetBinCenter(i) for i in range(1, hpx.GetNbinsX() + 1)])
51y = np.zeros_like(x)
52z = np.zeros_like(x)
53dz = hpx.values()
54dx = np.full_like(x, hpx.GetBinWidth(1) * 0.9)
55dy = np.full_like(x, 0.2)
56ax2.bar3d(x, y, z, dx, dy, dz, color="#EEAC91", edgecolor="darkblue", alpha=0.85)
58 0.55,
59 0.85,
60 info_text,
61 transform=ax2.transAxes,
62 fontsize=10,
63 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
64)
65
66# Draw hpx with its errors and a marker.
67ax3 = mpl_fig.add_subplot(gs[2, :])
68ax3.set_facecolor("#FFFDD0C8")
69hep.histplot(hpx, ax=ax3, histtype="errorbar", color="darkblue")
70ax3.grid(True)
72 0.90,
73 0.95,
74 info_text,
75 transform=ax3.transAxes,
76 verticalalignment="top",
77 fontsize=10,
78 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
79)
80
81mpl_fig.suptitle("Drawing options for one dimensional histograms", fontsize=14, fontweight="bold")
83
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130