TTree: accessing the value of an event for one branch

Hi there,

first of all sorry for this basic question, but I after 2 hours of searching with Google and the Root User Guide I still didn’t find an answer. I’m using v5.34.

I have a TTree (named tree, contains all events) with some branches (e.g. momentum) and I want to access the value (float) for one event and one branch.

In a loop, I would do this:

datafile = ROOT.TFile.Open("filename.root", "read")
Tree = datafile.Get('tree')


for event in tree:
	Par1 = event.branch1

But how do I access it without a loop?
Tree.GetEntry(i) only returns the number of bytes read, so that’s not useful. I’d like something in the sense of Tree.EventNr10.branch1.

In another thread I read this (Looping through TTree):

for i in range(myTree.GetEntriesFast()):
    val = myTree.myBranchName

How can this work? For me it’s always an attribute error, because Tree.GetEntry(i) is an int (number of bytes read), so ‘Tree.GetEntry(10).branch1’ does not work.

Thanks a lot!

So I do something like

Tree.Draw("momentum:energy:x", "goff")
mom = Tree.GetVal(0)
en = Tree.GetVal(1)

(I can’t test right now, only tomorrow)? This also works if the parameter (e.g. position) is an array, right?

Feels a little bit (no offense, just from my limited Point of View) like a workaround for something as simple as “Tree.Event1.Property1”.

Thank you! :slight_smile:

As shown in the example this technique allows to access not only raw variables but also combinaison or transformation of them.