#!/usr/bin/env python # coding: utf-8 # # Access TTree in Python using PyROOT and fill a histogram #
# First import the ROOT Python module. # In[1]: import ROOT # Optional: activate the JavaScript visualisation to produce interactive plots. # In[2]: get_ipython().run_line_magic('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("https://root.cern.ch/files/summer_student_tutorial_tracks.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()