Posts Tagged ‘ROOT’


Loop over histograms in ROOT file

In python on Jul 29, 2016 by theoryl Tagged: , ,

Looping over all histograms in a ROOT file using pyROOT

from ROOT import TFile
tfile = TFile.Open("histos.root")
for h in tfile.GetListOfKeys():
    h = h.ReadObj()
    print h.ClassName(), h.GetName()


Loop over all histograms in pyROOT

In howto on Jun 19, 2016 by theoryl Tagged: , , ,

Here’s a code snippet to loop over all histograms (and get their names) in pyROOT:

from ROOT import TFile

fname = 'histos.root'
hnames = []

tfile = TFile.Open(fname)
for key in tfile.GetListOfKeys():
    h = key.ReadObj()
    if h.ClassName() == 'TH1F' or h.ClassName() == 'TH2F':


Build ROOT 6.06 on Linux Mint 17.3

In howto on Dec 16, 2015 by theoryl Tagged: , , , , ,

To build ROOT 6.06/00 (released on 2015-12-09) on Linux Mint 17.3 “Rosa”, follow these instructions:

sudo apt-get install git dpkg-dev make g++ gcc binutils libx11-dev libxpm-dev \
libxft-dev libxext-dev cmake libpng12-dev libjpeg-dev libgif-dev libxml2-dev python-dev

tar -xzf root_v6.06.00.source.tar.gz

mkdir root-build
cd root-build
cmake ../root-6.06.00/

# Edit CMakeCache.txt to enable roofit, minuit2, fortran, xrootd

cmake --build .
cmake -DCMAKE_INSTALL_PREFIX=/tmp/root -P cmake_install.cmake

(based on

The instructions should also work for Ubuntu 14.04 “Trusty Tahr” and other distributions derived from it.


TGraph in pyROOT

In howto on Nov 22, 2015 by theoryl Tagged: , ,

pyROOT is a python binding of ROOT, which allows you to do a lot of pythonic things. The example below shows how to make a TGraph in pyROOT:

from ROOT import TGraph, gPad
from array import array
from math import exp

def func(x):
    y = 100*exp(-x)
    return y

# Method 1
if True:
    n = 100
    gr1 = TGraph(n)

    for i in range(n):
        x = 0.01*i
        gr1.SetPoint(i, x, func(x))


# Method 2
if False:
    n = 100
    xarr = []
    yarr = []

    for i in range(n):
        x = 0.01*i
        y = func(x)

    gr2 = TGraph(len(xarr), array('d', xarr), array('d', yarr))



RooFit tutorial by Wouter

In shared on Aug 24, 2013 by theoryl Tagged: , ,

RooFit tutorial by Wouter Verkerke at School of Statistics (SOS ’08):

(in PDF)


TCanvas and ownership

In howto on Sep 21, 2012 by theoryl Tagged: , ,

I’m getting random segmentation faults making plots on pyROOT. It turned out that there is a little secret:

from ROOT import SetOwnership
canvas = ROOT.TCanvas()
SetOwnership(canvas, False)

Not entirely sure what it does, but it makes the canvas behave as expected as in plain old ROOT.


PyROOT installation on Ubuntu 10.04

In howto on Jul 13, 2012 by theoryl Tagged: , , , , , , , ,

Last time I installed ROOT, I did not have PyROOT enabled. So I have to recompile. The following assumes Python 2.6 has been installed.

Install ROOT v5.27/06b on Ubuntu 10.04:

# Download ROOT via svn checkout. For simplicity, download to the home area
svn co ~/root
cd ~/root
svn switch

# Get all the dependencies for Ubuntu
sudo apt-get install build-essential libx11-dev libxpm-dev libxft-dev libxext-dev

# Configure. Enable PyROOT along with many useful features
# (to change the installation directory, add `--prefix=/path/to/installation/directory/`)
./configure linux --enable-python --with-python-incdir=/usr/include/python2.6/ --with-python-libdir=/usr/lib/ --enable-mathcore --enable-mathmore --enable-roofit --enable-minuit2 --enable-gdml --enable-table

# Make
# (to use n number of cores, do `make -j n`)

# Install
# (to gain superuser privilege, do `sudo make install`)
make install

To run ROOT, the environment variables need to be set. For bash shell:

export ROOTSYS=$HOME/root
export PATH=$ROOTSYS/bin:$PATH

For c-shell:

setenv ROOTSYS $HOME/root
setenv PATH $ROOTSYS/bin:$PATH

Equivalently, they can be set by doing

source $HOME/root/bin/
#< for c-shell, do `source $HOME/root/bin/thisroot.csh`

To avoid setting the above variables every time, you can put the commands into ~/.bashrc (or ~/.cshrc).

Now try running ROOT:


If the ROOT splash screen shows up, quit and try running PyROOT:

python ~/root/tutorials/pyroot/

Please report any inaccuracy.