In [1]:
%pylab inline
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
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]:
In [5]:
File = ROOT.TFile("toy_datasets/random.root")
Tree = File.Get("tree")
Tree.Draw("first")
canvas
Out[5]:
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]:
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]
In [9]:
import pandas
dataframe = pandas.DataFrame(data)
# looking ar first elements
dataframe.head()
Out[9]:
In [10]:
figure(figsize=(9, 7))
hist(data['first'], bins=50)
xlabel('first')
Out[10]:
In [11]:
figure(figsize=(9, 7))
hist(data['second'], bins=50)
xlabel('second')
Out[11]: