Filling TH1F histogram without using Fill()

Hi,

I am using PyROOT to make a histogram using data from a text file. The text file just has values that can be passed into a NumPy array. At the moment, I am making the histogram by iterating through the array and filling the histogram one value at a time. This works well enough for a small file, but for bigger ones, this seems rather inefficient. Since I already have the array, is there a way to pass in the whole array instead of one value at a time?

Here’s an example of what I’m doing:

import ROOT

# create a new ROOT file
f = ROOT.TFile("histo.root", "RECREATE")

# create an array of data
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# create a histogram with 10 bins and a range from 0 to 11
histo = ROOT.TH1F("histo", "Histogram from Array", 10, 0, 11)

# fill the histogram with the data from the array
for x in data:
    histo.Fill(x)

# write the histogram to the ROOT file
histo.Write()

# open the ROOT file and draw the histogram
f = ROOT.TFile("histo.root")
histo = f.Get("histo")
histo.Draw()

# show the plot
ROOT.gPad.Update()
ROOT.gPad.WaitPrimitive()

Create an “array.array('d', [...])” or a “nupmy.array([...], numpy.double)” and then use: TH1::FillN

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