In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Plot function using ROOT


In [2]:
import ROOT
from rep.plotting import default_canvas
c1 = default_canvas()
fun1 = ROOT.TF1( 'fun1', 'abs(sin(x)/x)', 0, 10)
c1.SetGridx()
c1.SetGridy()
fun1.Draw()
c1


Out[2]:

Plot histogram using ROOT for branch in root file


In [3]:
F = ROOT.TFile("toy_datasets/toyMC_bck_mass.root")

In [4]:
T = F.Get("Tau23Mu")

In [5]:
T.Draw("min_DOCA")
c1


Out[5]:

use histogram settings


In [6]:
h1 = ROOT.TH1F("h1","hist from tree",50, -0.25, 0.25)

In [7]:
T.Draw("min_DOCA>>h1")
c1


Out[7]:

root_numpy way

There are two comfortable libraries to read and work with root files


In [8]:
import root_numpy
# read root data to numpy.array
data = root_numpy.root2array("toy_datasets/toyMC_bck_mass.root", 'Tau23Mu', branches=['min_DOCA', 'mass'])

In [9]:
import numpy
data2 = numpy.array(data[:10], dtype=[('1', numpy.float32), ('2', numpy.float32)])

convert to pandas


In [10]:
import pandas
pandas.DataFrame(data)


Out[10]:
min_DOCA mass
0 0.015175 1628.509784
1 0.000997 1679.370983
2 0.016812 2128.668736
3 0.006014 2089.925634
4 0.004157 1610.112183
5 0.056992 1397.421265
6 0.029978 1613.646622
7 0.050269 1918.156982
8 0.082375 1497.562059
9 0.037431 1914.717285
10 0.000503 1681.143799
11 0.037431 1914.717285
12 0.037431 1914.717285
13 0.003043 2056.731136
14 0.037431 1914.717285
15 0.034449 1844.825271
16 0.002454 1674.145226
17 0.028302 1578.553626
18 0.052585 1783.390566
19 0.039515 2130.677002
20 0.004157 1610.112183
21 0.051618 1968.904289
22 0.057749 1398.062700
23 0.056992 1397.421265
24 0.010571 2079.312230
25 0.046581 1481.461555
26 0.034090 1938.229590
27 0.039960 1941.380579
28 0.041143 1686.872633
29 0.050269 1918.156982
... ... ...
20431 0.006256 1442.303467
20432 0.000893 1960.582031
20433 0.012074 1684.759155
20434 0.001444 2159.516846
20435 0.084172 1805.777466
20436 0.004888 1837.179199
20437 0.041552 1938.861206
20438 0.012459 1876.531860
20439 0.035683 1764.710571
20440 0.022338 2019.067627
20441 0.001849 1866.240967
20442 0.040196 1411.917358
20443 0.057608 2063.528320
20444 0.009527 1492.762329
20445 0.102680 2062.338379
20446 0.027973 2074.640381
20447 0.027973 1765.936768
20448 0.002519 2144.978027
20449 0.000272 1376.617798
20450 0.019663 2172.513672
20451 0.016306 1402.403809
20452 0.001617 1668.644165
20453 0.133691 1703.245483
20454 0.001939 1854.070801
20455 0.011345 2170.454346
20456 0.030052 1802.026733
20457 0.039883 1943.062012
20458 0.061589 1861.949951
20459 0.016136 1615.272217
20460 0.070077 1541.635254

20461 rows × 2 columns

Histograms


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


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

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


Out[12]:
<matplotlib.text.Text at 0x10e5b9d90>

Read data using some preselections


In [13]:
data = root_numpy.root2array("toy_datasets/toyMC_bck_mass.root", 'Tau23Mu', branches=['mass'], selection='mass > 1700')

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


Out[14]:
<matplotlib.text.Text at 0x10e7e1510>

In [ ]: