ROOT file format example

To run this example, you will need uproot, which is another SciKit-HEP library.

[1]:
import numpy as np
import boost_histogram as bh
import uproot
import os
import matplotlib.pyplot as plt

ROOT is a modular scientific software toolkit used in High Energy Physics. (HEP) The ROOT file format is used to store almost all HEP data. This notebook will illustrate one method for converting to/from the ROOT file format using uproot, a Python implementation of a ROOT file reader and writer.

For more complicated histograms, you may need Aghast and PyROOT, but that is a much heaver dependency, and is covered in a separate tutorial.

Start by making a 1D histogram:

[2]:
h = bh.Histogram(bh.axis.Regular(15, -3, 3))
h.fill(np.random.normal(size=1_000_000))
[2]:
Histogram(Regular(15, -3, 3), storage=Double()) # Sum: 997333.0 (1000000.0 with flow)
[3]:
root_file = uproot.recreate("demo_uproot_file.root")

# Uproot automatically converts NumPy style tuples
root_file["hist"] = h.to_numpy()

If you want to save and load the ROOT histogram, use uproot to read and write:

[4]:
root_file2 = uproot.open("demo_uproot_file.root")
uproot_hist = root_file2['hist']
uproot_hist.show()
                        0                                              1.667e+05
                        +------------------------------------------------------+
[-inf, -3)   0          |                                                      |
[-3, -2.6)   3271       |*                                                     |
[-2.6, -2.2) 9431       |***                                                   |
[-2.2, -1.8) 21794      |*******                                               |
[-1.8, -1.4) 45153      |***************                                       |
[-1.4, -1)   77616      |*************************                             |
[-1, -0.6)   1.1579e+05 |**************************************                |
[-0.6, -0.2) 1.4664e+05 |************************************************      |
[-0.2, 0.2)  1.5876e+05 |***************************************************   |
[0.2, 0.6)   1.4617e+05 |***********************************************       |
[0.6, 1)     1.1581e+05 |**************************************                |
[1, 1.4)     77953      |*************************                             |
[1.4, 1.8)   44486      |**************                                        |
[1.8, 2.2)   21976      |*******                                               |
[2.2, 2.6)   9231       |***                                                   |
[2.6, 3)     3246       |*                                                     |
[3, inf]     0          |                                                      |
                        +------------------------------------------------------+

This uproot histogram can be converted to NumPy with .numpy():

[5]:
h = bh.Histogram(bh.axis.Variable(uproot_hist.edges))
h[:] = uproot_hist.allvalues
plt.bar(h.axes[0].centers, h.view(), width=h.axes[0].widths);
../_images/notebooks_aghast_9_0.png

We could use a Weight() storage and read both allvalues and allvariances in, as well, since ROOT histograms can sometimes have this enabled.

Finally, let’s clean up after ourselves:

[6]:
if os.path.exists("demo_uproot_file.root"):
    os.remove("./demo_uproot_file.root")