Extract data from histogram as numpy array

I have a histogram (from a TTree) holding an oscilloscope waveform. I would like to apply a low-pass filter to the data, but don’t see easy ways to do this in ROOT, so I plan to pull out the x/y data to numpy arrays, and then use scipy for signal processing (e.g. scipy.signal.savgol_filter).

But how do I get the y-values from the histogram?

I’ve tried:

>>> y = hist.GetArray()
<read-write buffer ptr 0x7fdc10f35400, size 2147483647 at 0x111a06b70>
>>> print y[1]

which looks promising, but

>>> np.array(hist.GetArray())
 *** Break *** segmentation violation
[<unknown binary>]
[<unknown binary>]
[<unknown binary>]

I could loop and do GetBinContent() but figured that there must be a more efficient way.


‘y’ will have a method SetSize(), into which you can pass the number of bins from the histo, then the np iteration will stop appropriately

That worked wonderfully:

>>> y = hist.GetArray()
>>> y.SetSize(hist.GetNbinsX())
>>> y = np.array(y)

Also, this apparently works as well:

yvals = np.array(hist)[1:-1]   # cut off over/underflow bins

(where np.array(hist) returns an array whose length is hist.GetNbinsX()+2)

How about for the x-values (the bins)?

In particular, I try:

>>> hist.GetXaxis().GetBinLowEdge(100)
>>> x = hist.GetXaxis().GetXbins()
>>> x
<ROOT.TArrayD object at 0x7fd415027df8>
>>> np.array(x)
array([], dtype=float64)
### try comparable approach to the y-values...
>>> x.Set(hist.GetNbinsX())
>>> np.array(x)
array([ 0.,  0.,  0., ...,  0.,  0.,  0.])

So I can convert to a numpy array, but the resulting array is all zeros…

1 Like


That’s not a python issue, the TAxis returns nullptr for equidistant bins. And I agree that the documentation is too minimalistic…

I.e. If GetXbins() is null you could use xrange for the “array”.

Cheers, Axel

I see. So since I know that my bins are equally spaced, this works for me:

>>> np.linspace(h.GetXaxis().GetXmin(), h.GetXaxis().GetXmax()-h.GetXaxis().GetBinWidth(1), num=h.GetNbinsX())

(where h is the histogram).

Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.