Allowing inline plots


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Creating ROOT file using root_numpy


In [2]:
import numpy
import root_numpy
# generating random data
data = numpy.random.normal(size=[10000, 2])
# adding names of columns
data = data.view([('first', float), ('second', float)])
# 
root_numpy.array2root(data, filename='./toy_datasets/random.root', treename='tree', mode='recreate')

In [3]:
!cd ./toy_datasets/ ; ls


README.md    magic04.data random.root

Plot function using ROOT


In [4]:
import ROOT
from rep.plotting import default_canvas
canvas = default_canvas()
fun1 = ROOT.TF1( 'fun1', 'abs(sin(x)/x)', 0, 10)
canvas.SetGridx()
canvas.SetGridy()
fun1.Draw()
# Drawing output (last line is considered as output of cell)
canvas


Out[4]:

Plot histogram using ROOT for branch in root file


In [5]:
File = ROOT.TFile("toy_datasets/random.root")
Tree = File.Get("tree")
Tree.Draw("first")
canvas


Out[5]:

use histogram settings


In [6]:
# we need to keep histogram in any variable, otherwise it will be deleted automatically
h1 = ROOT.TH1F("h1","hist from tree",50, -0.25, 0.25)
Tree.Draw("min_DOCA>>h1")
canvas


Out[6]:

root_numpy way

There are two libraries to work with ROOT files

Let's show how to use the second library.


In [7]:
data = root_numpy.root2array("toy_datasets/random.root", 
                             treename='tree', 
                             branches=['first', 'second', 'sin(first) * exp(second)'], 
                             selection='first > 0')

in example above we selected three branches (one of which is an expression and will be computed on-the-fly) and selections


In [8]:
# taking, i.e. first 10 elements:
data2 = data[:10]

convert to pandas


In [9]:
import pandas
dataframe = pandas.DataFrame(data)
# looking ar first elements
dataframe.head()


Out[9]:
first second sin(first) * exp(second)
0 0.527168 0.744906 1.059625
1 0.916685 -0.052861 0.752729
2 0.790873 -0.355231 0.498396
3 0.955387 0.013831 0.827909
4 0.853532 0.217922 0.937102

Histograms in python


In [10]:
figure(figsize=(9, 7))
hist(data['first'], bins=50)
xlabel('first')


Out[10]:
<matplotlib.text.Text at 0x110f821d0>

In [11]:
figure(figsize=(9, 7))
hist(data['second'], bins=50)
xlabel('second')


Out[11]:
<matplotlib.text.Text at 0x1110adbd0>