Filling a histogram with a ntuple data [PyROOT]

Dear experts,

I am trying to take the data from a ntuple and fill a histogram with it using pyroot. Everything that i found until now is on c++, and i wrote my script based on that. But i am having trouble with the .SetBranchAddres(), i dont know how to implement it in python. The code that i wrote is down below:

histFile = root.TFile.Open(histFileName, "READ")
tree = histFile.Get("T_s2thh_NOMINAL")
nLeafs = tree.GetListOfBranches()
histo = root.TH1F("histo", "My histogram", 100, -3, 3)
tree.SetBranchAddres("TauEta", &TauEta)
entries = tree.GetEntries()
for k in range(entries):

And i keep getting the error:

SyntaxError: invalid syntax

and we i dont use & before TauEta,i get:

AttributeError: ‘TTree’ object has no attribute ‘SetBranchAddres’

And here is how the .root file was configured:

I really would appreciate any tips or some indication of materials.

Best Regards,


1 Like

as @dastudillo points out there is a typo: SetBranchAddres instead of SetBranchAddress.

The next problem will be that &TauEta is not a valid Python construct (you need ROOT.addressof(TauEta)).

The final problem will be that for k in range(entries): tree.GetEntry(k) will be extremely slow (because the event loop will run at Python speed rather than C++ speed).

I can recommend to use RDataFrame instead which simplifies the task. Plotting a histogram of "TauEta" from tree "T_s2thh_NOMINAL" is very simple with RDataFrame:

df = ROOT.RDataFrame("T_s2thh_NOMINAL", histFileName)
h = df.Histo1D("TauEta")


1 Like

Thank you so much, it worked! Just another question, there is a way to loop over the branches in the df object?

df.GetColumnNames() gives you a list of available columns, if that’s what you are looking for.

1 Like

Yep, that is what i am looking for. Once more, thank you so much.

1 Like