Write out a histogram to HDF5

FHist.jl provides an HDF5.jl extension to write and read histograms (Hist1D, Hist2D and Hist3D) to HDF5 files. Once the HDF5 package is loaded, the corresponding methods for h5writehist and h5readhist will become available.

The HDF5 format specification used in FHist.jl for histograms is language/library agnostic and easily portable to other libraries. Counts (weights, including sumw2), edges and other histogram parameters are stored as HDF5 datasets or attributes.

Let's create a Hist1D:

using FHist
using HDF5

h = Hist1D(randn(10_000); binedges = -3:0.1:3)
-3.0 3.0
  • edges: [-3.0, -2.9, -2.8, -2.7, -2.6, -2.5, -2.4, -2.3, -2.2, -2.1 … 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0]
  • bin counts: [4.0, 5.0, 11.0, 8.0, 21.0, 21.0, 18.0, 31.0, 36.0, 46.0 … 51.0, 34.0, 27.0, 27.0, 24.0, 14.0, 10.0, 6.0, 9.0, 5.0]
  • maximum count: 433.0
  • total count: 9975.0

Now write it to an HDF5 file:

h5writehist("foo.h5", "some/path/to/myhist", h)

The histogram is now saved in foo.h5 in the some/path/to/myhist group including all the meta information needed to be able to recreate it. This is how it looks like when opening it with HDF5.jl (any other HDF5 library will look similarly):

f = HDF5.File: (read-only) foo.h5
🗂️ HDF5.File: (read-only) foo.h5
└─ 📂 some
   └─ 📂 path
      └─ 📂 to
         └─ 📂 myhist
            ├─ 🏷️ _producer
            ├─ 🏷️ nentries
            ├─ 🏷️ overflow
            ├─ 🔢 edges_1
            ├─ 🔢 sumw2
            └─ 🔢 weights

Here is how to read it back to an actual FHist.Hist1D instance:

h5readhist("foo.h5", "some/path/to/myhist")
-3.0 3.0
  • edges: [-3.0, -2.9, -2.8, -2.7, -2.6, -2.5, -2.4, -2.3, -2.2, -2.1 … 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0]
  • bin counts: [4.0, 5.0, 11.0, 8.0, 21.0, 21.0, 18.0, 31.0, 36.0, 46.0 … 51.0, 34.0, 27.0, 27.0, 24.0, 14.0, 10.0, 6.0, 9.0, 5.0]
  • maximum count: 433.0
  • total count: 9975.0