Get the values for an event from a tree

So I have a .root file, and in it is a TTree. The setup looks like;

I can open this tree with pyroot, and myTree.Show() seems to print the values associated with a particular event;

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> myFile = ROOT.TFile("TTTo2L2Nu_ntp.root")
>>> myTree = myFile.Get("btagana/ttree")
>>> myTree.Show(0)
======> EVENT:0
 nBitTrigger     = 2
 BitTrigger      = 2049, 
 Run             = -1
 Evt             = 15294515
 LumiBlock       = 16420
 pthat           = -1
 mcweight        = 72.6983
 BX              = -1
 nPV             = 3

Those are the numbers that I want to access. I want to be able to get some or all of the variables for a given event, so that I can work on one event at a time. What do I need to call?

Many thanks,

You can iterate over a tree like this:

for event in myTree:
    print( event.mcweight )
    # or do anything else with any other variable
1 Like

Thanks that’s great;

>>> i=0
>>> for event in myTree:
...     print(event.nDaughters)
...     i +=1
...     if(i > 4): break

It there a way that I can start this somewhere in the middle of my tree? (Short of a really long loop)

I can’t test it right now, but it might work to call myTree.GetEvent(starteventnumber) beforehand

No, sorry, that returns a plain old int, and it dosn’t change where the iterator starts either.

This should work

start  = 1234
i = start
while myTree.GetEntry(i):
    i +=1
    if i - start > 4:
1 Like

Works perfectly, many thanks.

1 Like

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