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 the hsimple.py tutorial
23
24example = TFile("py-hsimple.root")
26
27# Draw histogram hpx in first pad.
28hpx = gROOT.FindObject("hpx")
29
30# Get the statistics
31entries = int(hpx.GetEntries())
32mean = hpx.GetMean()
33std_dev = hpx.GetStdDev()
34info_text = f"Entries = {entries}\nMean = {mean:.5f}\nStd Dev = {std_dev:.3f}"
35
36ax1 = mpl_fig.add_subplot(gs[0:2, 0])
37ax1.set_facecolor("#FFFDD0C8")
38hep.histplot(hpx, ax=ax1, histtype="fill", color="#EEAC91", alpha=0.5, edgecolor="blue", linewidth=1.5)
40 0.65,
41 0.95,
42 info_text,
43 transform=ax1.transAxes,
44 verticalalignment="top",
45 fontsize=10,
46 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
47)
48
49# Draw hpx as an interactive lego.
50ax2 = mpl_fig.add_subplot(gs[0:2, 1], projection="3d")
51ax2.set_facecolor("#FFFDD0C8")
52x = np.array([hpx.GetBinCenter(i) for i in range(1, hpx.GetNbinsX() + 1)])
53y = np.zeros_like(x)
54z = np.zeros_like(x)
55dz = hpx.values()
56dx = np.full_like(x, hpx.GetBinWidth(1) * 0.9)
57dy = np.full_like(x, 0.2)
58ax2.bar3d(x, y, z, dx, dy, dz, color="#EEAC91", edgecolor="darkblue", alpha=0.85)
60 0.55,
61 0.85,
62 info_text,
63 transform=ax2.transAxes,
64 fontsize=10,
65 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
66)
67
68# Draw hpx with its errors and a marker.
69ax3 = mpl_fig.add_subplot(gs[2, :])
70ax3.set_facecolor("#FFFDD0C8")
71hep.histplot(hpx, ax=ax3, histtype="errorbar", color="darkblue")
72ax3.grid(True)
74 0.90,
75 0.95,
76 info_text,
77 transform=ax3.transAxes,
78 verticalalignment="top",
79 fontsize=10,
80 bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.7),
81)
82
83mpl_fig.suptitle("Drawing options for one dimensional histograms", fontsize=14, fontweight="bold")
85
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