Make histogram with data stored in branch using PyROOT

Hello guys!
First of all, sorry if the question is very obvious, I am a newbie using PyROOT.
I am trying to make a histogram with data stored in a branch (see code)
Turns out it doesn’t read the values. Read the number of entries ok, but the value is always the same.
What could it be? Do you know another way to do it?
If you can help me, I would be grateful. Thank you very much in advance.

Best, Dayron.

Code

histFile = ROOT.TFile.Open( histFileName ,“READ”)

tree = histFile.Get(“charge_gap;1”) ##get tree
branch = tree.GetBranch(“KINETIC_ENERGY”) ##get branch named “KINETIC_ENERGY”

histo8 = ROOT.TH1F(“charged_distro_gap”,“charged part distro on gap” ,512 ,0 ,150)

nEntries = branch.GetEntries()

for i in range (0,nEntries):
data = branch.GetEntry(i)
histo8.Fill(data)

Hello,

You can iterate over the tree like this:

histFile = ROOT.TFile.Open( histFileName ,“READ”)
tree = histFile.Get(“charge_gap”)

for entry in tree:
     branch = entry.KINETIC_ENERGY
     ...

This should give you a different value for the branch at every iteration.

Please note that running a loop in Python is slow, so I would recommend to only use this option if your dataset is not big or if you don’t care about performance. If you do, I would recommend you have a look at RDataFrame:

https://root.cern/doc/master/classROOT_1_1RDataFrame.html

where the loop happens in C++.