Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_ttree.pyzdoc
Go to the documentation of this file.
1/**
2\class TTree
3\brief \parblock \endparblock
4\htmlonly
5<div class="pyrootbox">
6\endhtmlonly
7## PyROOT
8
9The TTree class has several additions for its use from Python, which are also
10available in its subclasses e.g. TChain and TNtuple.
11
12First, TTree instances are iterable in Python. Therefore, assuming `t` is
13a TTree instance, we can do:
14\code{.py}
15for entry in t:
16 x = entry.branch_name
17 ...
18\endcode
19
20At each iteration, a new entry of the tree will be read. In the code above,
21`entry` allows to access the branch values for the current entry. This can be
22done with the syntax `entry.branch_name` or, if the branch name is incompatible
23with Python naming rules, with e.g. "getattr(entry, '1_branch_name')".
24
25<em>Please note</em> that iterating in Python can be slow, so only iterate over
26a tree as described above if performance is not an issue or when dealing with
27a small dataset. To read and process the entries of a tree in a much faster
28way, please use ROOT::RDataFrame.
29
30Second, a couple of TTree methods have been modified to facilitate their use
31from Python: TTree::Branch and TTree::SetBranchAddress.
32
33Regarding TTree::Branch, the following example shows how we can create
34different types of branches of a TTree. Note that `Branch` will just link
35the new branch with a given Python object, so it is still necessary to fill
36such object with the desired content before calling TTree::Fill.
37\code{.py}
38from array import array
39import numpy as np
40import ROOT
41from ROOT import addressof
42
43# Basic type branch (float) - use array of length 1
44n = array('f', [ 1.5 ])
45t.Branch('floatb', n, 'floatb/F')
46
47# Array branch - use array of length N
48N = 10
49a = array('d', N*[ 0. ])
50t.Branch('arrayb', a, 'arrayb[' + str(N) + ']/D')
51
52# Array branch - use NumPy array of length N
53npa = np.array(N*[ 0. ])
54t.Branch('nparrayb', npa, 'nparrayb[' + str(N) + ']/D')
55
56# std::vector branch
57v = ROOT.std.vector('double')(N*[ 0. ])
58t.Branch('vectorb0', v)
59
60# Class branch / struct in single branch
61cb = ROOT.MyClass()
62t.Branch('classb', cb)
63
64# Struct as leaflist
65# Assuming:
66# struct MyStruct {
67# int myint;
68# float myfloat;
69# };
70ms = ROOT.MyStruct()
71t.Branch('structll', ms, 'myint/I:myfloat/F')
72
73# Store struct members individually
74ms = ROOT.MyStruct()
75# Use `addressof` to get the address of the struct members
76t.Branch('myintb', addressof(ms, 'myint'), 'myint/I')
77t.Branch('myfloatb', addressof(ms, 'myfloat'), 'myfloat/F')
78\endcode
79
80Concerning TTree::SetBranchAddress, below is an example of prepare
81the reading of different types of branches of a TTree. Note that
82`SetBranchAddress` will just link a given branch with a certain
83Python object; after that, in order to read the content of such
84branch for a given TTree entry `x`, TTree::GetEntry(x) must be
85invoked.
86\code{.py}
87from array import array
88import numpy as np
89import ROOT
90
91# Basic type branch (float) - use array of length 1
92n = array('f', [ 0. ])
93t.SetBranchAddress('floatb', n)
94
95# Array branch - use array of length N
96N = 10
97a = array('d', N*[ 0. ])
98t.SetBranchAddress('arrayb', a)
99
100# Array branch - use NumPy array of length N
101npa = np.array(N*[ 0. ])
102t.SetBranchAddress('nparrayb', a)
103
104# std::vector branch
105v = ROOT.std.vector('double')()
106t.SetBranchAddress('vectorb', v)
107
108# Class branch
109cb = ROOT.MyClass()
110t.SetBranchAddress('classb', cb)
111
112# Struct branch (both single-branch and leaf list)
113ms = ROOT.MyStruct()
114ds.SetBranchAddress('structb', ms)
115\endcode
116\htmlonly
117</div>
118\endhtmlonly
119*/