You are here


How to use ROOT with Python (PyROOT)

PyROOT is a Python extension module that allows the user to interact with any ROOT class from the Python interpreter. This is done generically using the ROOT dictionary, therefore there is no need to generate any Python wrapper code to include new ROOT classes. At the same time PyROOT offers the possibility to execute and evaluate any Python command or start a Python shell from the ROOT/CLING prompt. Further details are available in the PyROOT section of the Users' Guide.

Enabling PyROOT

All the instructions relative to the flags to configure ROOT can be found here. In this particular case, PyROOT is enabled by default: just make sure you have the Python header files on your machine.

More pythonistic PyROOT: rootpy

The generality with which PyROOT creates bindings based on dictionaries makes that it is close to the original C++, which is great for e.g. sharing documentation. However, where it is possible to do so automatically, e.g. for handling of container classes, "pythonizations" are available. If that is not enough for your needs, then check out the rootpy project for a more pythonistic PyROOT.

High performance PyROOT: cppyy

A new approach is based on cppyy, a run-time bindings generator like PyROOT for the PyPy project. It has both a CINT and a pure-Reflex back-end and is, depending on use, 50x-100x faster than PyROOT. A pre-installed version based on the CINT backend is available on /afs as pypyroot.

Running PyROOT from Python interpreter

The user should make sure that the python command is the one of the installation that has been used to build the PyROOT extension module. The top level Python module is This module imports the extension module [.dll] and does a similar initialization as the ROOT application (i.e. loading common libraries, defining a number of globals, starting a thread to handle GUI events, etc.). This is the main entry point for any Python script using the ROOT classes. An example is as follows:

from ROOT import gROOT, TCanvas, TF1
c1 = TCanvas( 'c1', 'Example with Formula', 200, 10, 700, 500 )
# Create a one dimensional function and draw it
fun1 = TF1( 'fun1', 'abs(sin(x)/x)', 0, 10 )

The user can find a number of examples in $ROOTSYS/tutorials/pyroot. To run them, simply execute the command

 python $ROOTSYS/tutorials/pyroot/ 

Running Python from ROOT/CINT interpreter

A ROOT user can run any Python command and eventually switch to the python prompt from the ROOT/CINT prompt; the interpreter state will be preserved in between calls. Example commands to execute are:

root [0] TPython::Exec( "print 1 + 1" )
root [1] b = TPython::Eval( "TBrowser()" )
(class TObject*)0x8d1daa0
root [2] TPython::Prompt()
>>> i = 2; ^D
root [3] TPython::Prompt()
>>> print i

More detailed examples

See the ROOT ipython notebooks tutorials and the rootpy examples.