Converting numpy array into ROOT hist

Hi there!

I have a numpy array - for example:

[0.2 0.4 0. 0.4 1.2 1. 1.2 0.6 0.8 1. 0.8 0.8 1.4 1.2 1. 0.4 0.6 1.2

  1. 1.6 0.6 0.8 1.2 0.4 0.6 1. 0.6 1.2 0.4 0.8 1.6 1. ]

which represents the frequencies of an histogram which x-bins are between 0 and 1. i need to save this histogram into a root file, but I don’t know if it is even possible.

I have searched and the function array2hist from root_numpy seems to do this, but this library is not updated and I can not use it. Do you know any alternative ways to do so?

Thanks in advance.

hist = TH1D("name","title",len(yourArray),0,1)
for i,freq in enumerate(yourArray):

and then save hist to a TFile.

If you need a speedup, you might want to use the function FillN() to avoid the loop.

Or even just storing the array as a plain TArrayD instead of a histogram.

FillN is a very good idea. @etejedor what’s the way to pass a numpy array as C-style array?


A numpy array of doubles can be passed to a method that expects a C-style array of doubles such as FillN.

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