Access TTree in Python using PyROOT and fill a histogram


First import the ROOT Python module.


In [1]:
import ROOT


Welcome to JupyROOT 6.07/07

Optional: activate the JavaScript visualisation to produce interactive plots.


In [2]:
%jsroot on

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


In [3]:
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 [4]:
h = ROOT.TH1F("TracksPt","Tracks;Pt [GeV/c];#",128,0,64)
for event in f.events:
    for track in event.tracks:
        h.Fill(track.Pt())
c = ROOT.TCanvas()
h.Draw()
c.Draw()