Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Unified Histogram Interface (UHI)

ROOT histograms implement the Unified Histogram Interface (UHI), enhancing interoperability with other UHI-compatible libraries. This compliance standardizes histogram operations, making tasks like plotting, indexing, and slicing more intuitive and consistent.

Table of contents

Plotting

ROOT histograms implement the PlottableHistogram protocol. Any plotting library that accepts an object that follows the protocol can plot ROOT histogram objects.

You can read more about the protocol on the UHI plotting page.

Plotting with mplhep

import ROOT
import matplotlib.pyplot as plt
import mplhep as hep
# Create and fill a 1D histogram
h1 = ROOT.TH1D("h1", "MyHist", 10, -1, 1)
h1.FillRandom("gaus", 1000)
# Load a style sheet and plot the histogram
hep.style.use("LHCb2")
plt.title("MyHist")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.

Additional Notes

Indexing

ROOT histograms implement the UHI indexing specification. This introduces a unified syntax for accessing and setting bin values, as well as slicing histogram axes.

You can read more about the syntax on the UHI Indexing page.

Setup

The loc, undeflow, overflow, rebin and sum tags are imported from the ROOT.uhi module.

import ROOT
from ROOT.uhi import loc, underflow, overflow, rebin, sum
import numpy as np
h = ROOT.TH2D("h2", "h2", 10, 0, 1, 10, 0, 1)

Slicing

# Slicing over everything
h == h[:, :]
h == h[...]
# Slicing a range, picking the bins 1 to 5 along the x axis and 2 to 6 along the y axis
h1 = h[1:5, 2:6]
# Slicing leaving out endpoints
h2 = h[:5, 6:]
# Slicing using data coordinates, picking the bins from the one containing the value 0.5 onwards along both axes
h3 = h[loc(0.5):, loc(0.5):]
# Combining slicing with rebinning, rebinning the x axis by a factor of 2
h4 = h[1:9:rebin(2), :]

Setting

# Setting the bin contents
h[1, 2] = 5
# Setting the bin contents using data coordinates
h[loc(3), loc(1)] = 5
# Setting the flow bins
h[overflow, overflow] = 5
# Setting the bin contents using a numpy array
h[...] = np.ones((10, 10))
# Setting the bin contents using a scalar
h[...] = 5

Access

# Accessing the bin contents using the bin number
v = h[1, 2]
# Accessing the bin contents using data coordinates
v = h[loc(0.5), loc(0.5)]
v = h[loc(0.5) + 1, loc(0.5) + 1] # Returns the bin above the one containing the value 2 along both axes
# Accessing the flow bins
v = h[underflow, underflow]

Additional Notes