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_image
7## \macro_output
8## \macro_code
9##
10## \date July 2025
11## \author Wim Lavrijsen, Nursena Bitirgen
12
13import matplotlib.pyplot as plt
14import mplhep as hep
15import numpy as np
16import ROOT
17from ROOT import TFile, gROOT
18
19mpl_fig = plt.figure(figsize=(14, 12))
20gs = mpl_fig.add_gridspec(3, 2, height_ratios=[1.5, 1.5, 1.5])
21
22# We connect the ROOT file generated in a previous tutorial
23File = "py-hsimple.root"
25 ROOT.Info("hist015_TH1_read_and_draw.py", File + " does not exist")
26 exit()
27
28example = TFile(File)
30
31# Draw histogram hpx in first pad.
32hpx = gROOT.FindObject("hpx")
33
34# Get the statistics
35entries = int(hpx.GetEntries())
36mean = hpx.GetMean()
37std_dev = hpx.GetStdDev()
38info_text = f"Entries = {entries}\nMean = {mean:.5f}\nStd Dev = {std_dev:.3f}"
39
40ax1 = mpl_fig.add_subplot(gs[0:2, 0])
41ax1.set_facecolor("#FFFDD0C8")
42hep.histplot(hpx, ax=ax1, histtype="fill", color="#EEAC91", alpha=0.5, edgecolor="blue", linewidth=1.5)
44 0.65,
45 0.95,
46 info_text,
47 transform=ax1.transAxes,
48 verticalalignment="top",
49 fontsize=10,
50 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
51)
52
53# Draw hpx as an interactive lego.
54ax2 = mpl_fig.add_subplot(gs[0:2, 1], projection="3d")
55ax2.set_facecolor("#FFFDD0C8")
56x = np.array([hpx.GetBinCenter(i) for i in range(1, hpx.GetNbinsX() + 1)])
57y = np.zeros_like(x)
58z = np.zeros_like(x)
59dz = hpx.values()
60dx = np.full_like(x, hpx.GetBinWidth(1) * 0.9)
61dy = np.full_like(x, 0.2)
62ax2.bar3d(x, y, z, dx, dy, dz, color="#EEAC91", edgecolor="darkblue", alpha=0.85)
64 0.55,
65 0.85,
66 info_text,
67 transform=ax2.transAxes,
68 fontsize=10,
69 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
70)
71
72# Draw hpx with its errors and a marker.
73ax3 = mpl_fig.add_subplot(gs[2, :])
74ax3.set_facecolor("#FFFDD0C8")
75hep.histplot(hpx, ax=ax3, histtype="errorbar", color="darkblue")
76ax3.grid(True)
78 0.90,
79 0.95,
80 info_text,
81 transform=ax3.transAxes,
82 verticalalignment="top",
83 fontsize=10,
84 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
85)
86
87mpl_fig.suptitle("Drawing options for one dimensional histograms", fontsize=14, fontweight="bold")
89
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131