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):
tree.GetEntry(k)
histo.Fill(TauEta)
histo.Write()
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’
Hi,
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")
h.Draw()