Posts Tagged ‘pyROOT’

Articles

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")
tfile.cd()
for h in tfile.GetListOfKeys():
    h = h.ReadObj()
    print h.ClassName(), h.GetName()
Advertisements

Articles

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':
        hnames.append(h.GetName())

Articles

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))

    gr1.SetMarkerStyle(20)
    gr1.SetMarkerSize(1)
    gr1.Draw("AP")


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

    for i in range(n):
        x = 0.01*i
        y = func(x)
        xarr.append(x)
        yarr.append(y)

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

    gr2.SetMarkerStyle(20)
    gr2.SetMarkerSize(1)
    gr2.Draw("AP")

Articles

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.

Articles

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 http://root.cern.ch/svn/root/tags/v5-27-06d ~/root
cd ~/root
svn switch http://root.cern.ch/svn/root/branches/v5-27-06-patches

# 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`)
make

# 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
export LD_LIBRARY_PATH=$ROOTSYS/lib:$PYTHONDIR/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$ROOTSYS/lib:$PYTHONPATH

For c-shell:

setenv ROOTSYS $HOME/root
setenv PATH $ROOTSYS/bin:$PATH
setenv LD_LIBRARY_PATH $ROOTSYS/lib:$PYTHONDIR/lib:$LD_LIBRARY_PATH
setenv PYTHONPATH $ROOTSYS/lib:$PYTHONPATH

Equivalently, they can be set by doing

source $HOME/root/bin/thisroot.sh
#< 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:

root

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

python ~/root/tutorials/pyroot/demo.py

Please report any inaccuracy.