In [1]:
import ROOT
from PyTreeReader import PyTreeReader
This is to create an histogram and read a tree.
In [2]:
h = ROOT.TH1F("h","h",1024,-256,256)
fill = h.Fill
f = ROOT.TFile(ROOT.gROOT.GetTutorialsDir()+"/hsimple.root")
tree = f.ntuple
In [3]:
%%timeit -n 1 -r 1
for event in tree:
fill(event.px*event.py*event.pz*event.random)
This is how we use for the first time the PyTreeReader and how we benchmark it. The only difference within the loop body consists in invoking functions called as the branches rather than data members called as the branches themselves. Even for this trivial tree, the difference is impressive.
Actually, having little read, decompression and deserialisation is better: the difference in time is really due to the superior performance of the PyTreeReader.
In [4]:
%%timeit -n 1 -r 1
for event in PyTreeReader(tree):
fill(event.px()*event.py()*event.pz()*event.random())
In [5]:
%%timeit -n 1 -r 1
ptr = PyTreeReader(tree, cache=True)
In [6]:
ptr = PyTreeReader(tree, cache=True)
In [7]:
%%timeit -n 1 -r 1
for event in ptr:
fill(event.px()*event.py()*event.pz()*event.random())
It can be inconvenient to construct methods to access all the branches, even more to cache all of their contents by default. This is the reason why PyTreeReader provides the pattern constructor argument. For example, in order to consider only branches whose names start with "p", one can do:
In [8]:
%%timeit -n 1 -r 1
ptr = PyTreeReader(tree, cache=True, pattern="p[*]")
Pattern is indeed the pattern to match the desired branches by name (fnmatch is used). Its default value is simple, "*";
In [9]:
for py in ptr.py_array()[:10]: print py # Better stopping after a few of them :)
Wondering about the type returned by py_array? A widely adopted C++ data structure contiguous in memory:
In [10]:
ptr.py_array()
Out[10]:
This is the first implementation of the class. A lot can be achieved with seizable performance improvements with respect to the traditional way of looping. The usability is preserved.
However, some interesting features could be added: