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