3The
TTree class has several additions
for its use from Python, which are also
6First,
TTree instances are iterable in Python. Therefore, assuming `t` is
14At each iteration,
a new entry of the
tree will be read. In the code above,
15`entry` allows to access the branch values
for the current entry. This can be
16done with the syntax `entry.branch_name` or,
if the branch
name is incompatible
17with Python naming rules, with
e.g.
"getattr(entry, '1_branch_name')".
19<em>Please note</em> that iterating in Python can be slow, so only
iterate over
20a tree as described above
if performance is not an issue or
when dealing with
21a small
dataset. To read and process the entries of
a tree in
a much faster
29The following example shows how we can create different types of branches of
a TTree.
30`Branch` links the
new branch with
a given Python
object. It is therefore possible to
34from array
import array
38# We create the file and the tree
39with
ROOT.TFile(
"outfile.root",
"RECREATE")
as ofile:
40 t =
ROOT.TTree(
"mytree",
"mytree")
42 # Basic type branch (float) - use array of length 1
43 n = array(
'f', [ 1.5 ])
44 t.Branch('floatb',
n, 'floatb/
F')
46 # Array branch - use array of length N
48 a = array(
'd',
N*[ 0. ])
49 t.Branch(
'arrayb',
a,
'arrayb[' + str(
N) +
']/D')
51 # Array branch - use NumPy array of length N
52 npa =
np.array(
N*[ 0. ])
53 t.Branch(
'nparrayb', npa,
'nparrayb[' + str(
N) +
']/D')
56 v =
ROOT.std.vector(
'double')(
N*[ 0. ])
57 t.Branch(
'vectorb0',
v)
59 # Class branch / struct in single branch
60 cb =
ROOT.TH1D(
"myHisto",
"myHisto", 64, -4, 4)
61 # This could have been any class known to ROOT, also custom
62 #cb = ROOT.MyCustomClass()
63 t.Branch(
'classb', cb)
65 # Struct as leaflist. This is interpreted on the fly,
66 # but could be known to ROOT by other means, such as
67 # header inclusion or dictionary load.
68 ROOT.gInterpreter.Declare(
'''
75 t.Branch(
'structll', ms,
'myint/I:myfloat/F')
77 # Store struct members individually
79 # Use the `addressof` function in the ROOT module
80 # to get the address of the struct members
81 t.Branch(
'myintb',
ROOT.addressof(ms,
'myint'),
'myint/I')
82 t.Branch(
'myfloatb',
ROOT.addressof(ms,
'myfloat'),
'myfloat/F')
84 # Let
's write one entry in our tree
86 # Finally flush the content of the tree to the file
90### Pythonization of TTree::SetBranchAddress
92This section is to be considered for advanced users. Simple event
93loops reading tree entries in Python can be performed as shown above.
95Below an example is shown of reading different types tree branches.
96Note that `SetBranchAddress` will just link a given branch with a
97certain Python object; after that, in order to read the content of such
98branch for a given TTree entry `x`, TTree::GetEntry(x) must be
102from array import array
106with ROOT.TFile('outfile.root
') as infile:
110 # Basic type branch (float) - use array of length 1
111 n = array('f', [ 0. ])
112 t.SetBranchAddress('floatb
', n)
114 # Array branch - use array of length N
116 a = array('d', N*[ 0. ])
117 t.SetBranchAddress('arrayb
', a)
119 # Array branch - use NumPy array of length N
120 npa = np.array(N*[ 0. ])
121 t.SetBranchAddress('nparrayb
', a)
124 v = ROOT.std.vector('double')()
125 t.SetBranchAddress('vectorb
', v)
129 # Any other class known to ROOT would have worked
131 t.SetBranchAddress('classb
', cb)
133 # Struct as leaflist. This is interpreted on the fly,
134 # but could be known to ROOT by other means, such as
135 # header inclusion or dictionary load.
136 ROOT.gInterpreter.Declare('''
143 t.SetBranchAddress(
'structll', ms)
static Roo_reg_AGKInteg1D instance
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void when
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
A chain is a collection of files containing TTree objects.
A simple TTree restricted to a list of float variables only.
A TTree represents a columnar dataset.
virtual Int_t Fill()
Fill all branches.
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
int iterate(rng_state_t *X)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...