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/CINT prompt. Further details are available in the PyROOT manual.

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.

Building and installing PyROOT

The user should follow the standard ROOT installation instructions and, if necessary, enable the build of the PyROOT module. A Python version ≥ 2.2 is required, including the developer's package of python if you want to build ROOT and PyROOT yourself. On Debian-like systems that developer's package is called python-x.y-dev. The location of python needs to be specified if it is not installed in any of the standard places:

./configure <arch> --enable-python [--with-python-incdir=<dir>] [--with-python-libdir=<dir>]
where the Python include directory should point to the directory containing the Python.h file, and the library directory should contain libpython.x.y.[so][.dll], where 'x' and 'y' are the major and minor version number of Python, respectively. Note that at the end of the configuration, the list of enabled features is printed: make sure that 'python' is listed among them.

If you do not specify the inc and lib directories configure will try to use the environment variable $PYTHONDIR  or alternatively will look in the standard locations.

A library called [libPyROOT.dll] will be created in the $ROOTSYS/lib [$ROOTSYS/bin] and a top Python module will be copied to the same place.

Setting up the environment

To work with PyROOT, the LD_LIBRARY_PATH [PATH] and PYTHONPATH need to be set in addition to the standard ROOTSYS:

for Unix platforms


for Windows


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] gSystem->Load("libPyROOT")
root [1] TPython::Exec( "print 1 + 1" )
root [2] b = TPython::Eval( "TBrowser()" )
(class TObject*)0x8d1daa0
root [3] TPython::Prompt()
>>> i = 2; ^D
root [4] TPython::Prompt()
>>> print i

More detailed examples

See these ipython notebooks and the rootpy examples.


Pere Mato, Wim Lavrijsen. Last update 2014-01-24