{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Access TTree in Python using PyROOT\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['notebook'],\n", " function() {\n", " IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-c++src'] = {'reg':[/^%%cpp/]};\n", " console.log(\"JupyROOT - %%cpp magic configured\");\n", " }\n", ");\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.07/07\n" ] } ], "source": [ "import ROOT" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open a file which is located on the web. No type is to be specified for \"f\"." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "f = ROOT.TFile.Open(\"https://root.cern.ch/files/summer_student_tutorial_tracks.root\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Loop over the TTree called \"events\" in the file. It is accessed with the dot operator.\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing event number 0\n", "Max pt is 37.588238\n", "Processing event number 100\n", "Max pt is 32.405018\n", "Processing event number 200\n", "Max pt is 34.624805\n", "Processing event number 300\n", "Max pt is 37.022694\n", "Processing event number 400\n", "Max pt is 35.201751\n", "Processing event number 500\n", "Max pt is 35.257848\n", "Processing event number 600\n", "Max pt is 43.020298\n", "Processing event number 700\n", "Max pt is 36.731349\n", "Processing event number 800\n", "Max pt is 34.082705\n", "Processing event number 900\n", "Max pt is 34.757829\n" ] } ], "source": [ "maxPt=-1\n", "for event in f.events:\n", " maxPt=-1\n", " for track in event.tracks:\n", " pt = track.Pt()\n", " if pt > maxPt: maxPt = pt\n", " if event.evtNum % 100 == 0:\n", " print \"Processing event number %i\" %event.evtNum\n", " print \"Max pt is %f\" %maxPt" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }