Is there a better way to fill a TH1 histogram with numpy array than using a ‘for loop’ to loop through the the array.
PS: I have seen the answers which suggests to use root_numpy but the thing is that root_numpy is not compatible with any version of numpy >1.19.5, hence, the question.
ROOT Version: 6.24.6 Platform: Ubuntu 22 Compiler: Not Provided
Hi,
The simples and efficient way to fill now a TH1 histogram with a numpy array (1D) is as in this code example:
import numpy as np
import ROOT
# Create a numpy array with 1000 gaussian data with mu=0 and sigma=1
x = np.random.normal(0, 1, 1000)
# Create a ROOT histogram
hist = ROOT.TH1D("hist", "hist", 50, -3, 3)
# fill ROOT histogram with weights=1
hist.FillN(x.size, x, np.ones(x.size))