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), -3:0.1:3)
-3.0 3.0
  • edges: -3.0:0.1:3.0
  • bin counts: [5, 9, 17, 11, 13, 24, 24, 33, 32, 49 … 47, 42, 32, 29, 25, 11, 11, 14, 10, 4]
  • maximum count: 427
  • total count: 9977

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: [5, 9, 17, 11, 13, 24, 24, 33, 32, 49 … 47, 42, 32, 29, 25, 11, 11, 14, 10, 4]
  • maximum count: 427
  • total count: 9977