hist icon indicating copy to clipboard operation
hist copied to clipboard

Support ROOT serialization

Open LovelyBuggies opened this issue 4 years ago • 5 comments

We might need to design file media for cross-platform development. I think ROOT file is a good option. Maybe we can design functionality to serialize/deserialize our histograms or axis objects to/from ROOT files.

LovelyBuggies avatar Mar 09 '20 02:03 LovelyBuggies

This is handled by Aghast, but yes, we absolutely need to have Hist talk to Aghast so we can save and load ROOT Histograms, and help push Aghast forward as well.

henryiii avatar Mar 09 '20 13:03 henryiii

Duplicate with Serialization via Aghast in Hist plans.

LovelyBuggies avatar Mar 18 '20 10:03 LovelyBuggies

boost-histogram should also support this.

HDembinski avatar Mar 19 '20 08:03 HDembinski

Since we can convert our histograms to numpy.histogram objects (or nparray objects) and uproot could save and load numpy.histogram objects. Wouldn't that be an easy thing? Are we expected to convert them directly (bh/hist.histogram to ROOT without numpy)?

import hist
import uproot
import numpy as np
import matplotlib.pyplot as plt

file = uproot.recreate("tmp.root")
h = hist.Hist(hist.axis.Regular(15, -3, 3, name='x'))
h.fill(np.random.normal(size=1_000))

w, data = h.to_numpy()

file['title'] = 'A histogram'
file['h'] = np.histogram(data[:-1], bins=data, weights=w)
file2 = uproot.open("tmp.root")
print(file2['title'])
file2['h'].show()
# A histogram
#                  0                                                         159.6
#                  +-------------------------------------------------------------+
# [-inf, -3)   0   |                                                             |
# [-3, -2.6)   4   |**                                                           |
# [-2.6, -2.2) 9   |***                                                          |
# [-2.2, -1.8) 13  |*****                                                        |
# [-1.8, -1.4) 49  |*******************                                          |
# [-1.4, -1)   81  |*******************************                              |
# [-1, -0.6)   119 |*********************************************                |
# [-0.6, -0.2) 152 |**********************************************************   |
# [-0.2, 0.2)  147 |********************************************************     |
# [0.2, 0.6)   136 |****************************************************         |
# [0.6, 1)     107 |*****************************************                    |
# [1, 1.4)     87  |*********************************                            |
# [1.4, 1.8)   63  |************************                                     |
# [1.8, 2.2)   17  |******                                                       |
# [2.2, 2.6)   10  |****                                                         |
# [2.6, 3)     4   |**                                                           |
# [3, inf]     0   |                                                             |
#                  +-------------------------------------------------------------+

LovelyBuggies avatar Mar 24 '20 16:03 LovelyBuggies

If we can do it in this way, I think what we need to do is:

  • To see whether all bh/hist's histograms could be converted to numpy.histogram (for some special axes)
  • Encapsulate a function to write some params to the file like the axis names

LovelyBuggies avatar Mar 24 '20 16:03 LovelyBuggies