Access TTree in Python using PyROOT



In [1]:
import ROOT


Welcome to JupyROOT 6.07/07

Open a file which is located on the web. No type is to be specified for "f".


In [2]:
f = ROOT.TFile.Open("http://indico.cern.ch/event/395198/material/0/0.root")

Loop over the TTree called "events" in the file. It is accessed with the dot operator. Same holds for the access to the branches: no need to set them up - they are just accessed by name, again with the dot operator.


In [3]:
maxPt=-1
for event in f.events:
    maxPt=-1
    for track in event.tracks:
        pt = track.Pt()
        if pt > maxPt: maxPt = pt
    if event.evtNum % 100 == 0:
        print "Processing event number %i" %event.evtNum
        print "Max pt is %f" %maxPt


Processing event number 0
Max pt is 37.588238
Processing event number 100
Max pt is 32.405018
Processing event number 200
Max pt is 34.624805
Processing event number 300
Max pt is 37.022694
Processing event number 400
Max pt is 35.201751
Processing event number 500
Max pt is 35.257848
Processing event number 600
Max pt is 43.020298
Processing event number 700
Max pt is 36.731349
Processing event number 800
Max pt is 34.082705
Processing event number 900
Max pt is 34.757829