Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_ttree.pyzdoc
Go to the documentation of this file.
1\pythondoc TTree
2
3The TTree class has several additions for its use from Python, which are also
4available in its subclasses e.g. TChain and TNtuple.
5
6First, TTree instances are iterable in Python. Therefore, assuming `t` is
7a TTree instance, we can do:
8\code{.py}
9for entry in t:
10 x = entry.branch_name
11 ...
12\endcode
13
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')".
18
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
22way, please use ROOT::RDataFrame.
23
24Two methods of TTree have been pythonized to facilitate their: TTree::Branch and
26
27### Pythonization of TTree::Branch
28
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
31fill such object with the desired content before calling TTree::Fill.
32
33\code{.py}
34from array import array
35import numpy as np
36import ROOT
37
38# We create the file and the tree
39with ROOT.TFile("outfile.root", "RECREATE") as ofile:
40 t = ROOT.TTree("mytree", "mytree")
41
42 # Basic type branch (float) - use array of length 1
43 n = array('f', [ 1.5 ])
44 t.Branch('floatb', n, 'floatb/F')
45
46 # Array branch - use array of length N
47 N = 10
48 a = array('d', N*[ 0. ])
49 t.Branch('arrayb', a, 'arrayb[' + str(N) + ']/D')
50
51 # Array branch - use NumPy array of length N
52 npa = np.array(N*[ 0. ])
53 t.Branch('nparrayb', npa, 'nparrayb[' + str(N) + ']/D')
54
55 # std::vector branch
56 v = ROOT.std.vector('double')(N*[ 0. ])
57 t.Branch('vectorb0', v)
58
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)
64
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('''
69 struct MyStruct {
70 int myint;
71 float myfloat;
72 };
73 ''')
74 ms = ROOT.MyStruct()
75 t.Branch('structll', ms, 'myint/I:myfloat/F')
76
77 # Store struct members individually
78 ms = ROOT.MyStruct()
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')
83
84 # Let's write one entry in our tree
85 t.Fill()
86 # Finally flush the content of the tree to the file
87 t.Write()
88\endcode
89
90### Pythonization of TTree::SetBranchAddress
91
92This section is to be considered for advanced users. Simple event
93loops reading tree entries in Python can be performed as shown above.
94
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
99invoked.
100
101\code{.py}
102from array import array
103import numpy as np
104import ROOT
105
106with ROOT.TFile('outfile.root') as infile:
107
108 t = infile['mytree']
109
110 # Basic type branch (float) - use array of length 1
111 n = array('f', [ 0. ])
112 t.SetBranchAddress('floatb', n)
113
114 # Array branch - use array of length N
115 N = 10
116 a = array('d', N*[ 0. ])
117 t.SetBranchAddress('arrayb', a)
118
119 # Array branch - use NumPy array of length N
120 npa = np.array(N*[ 0. ])
121 t.SetBranchAddress('nparrayb', a)
122
123 # std::vector branch
124 v = ROOT.std.vector('double')()
125 t.SetBranchAddress('vectorb', v)
126
127 # Class branch
128 cb = ROOT.TH1D()
129 # Any other class known to ROOT would have worked
130 #cb = ROOT.MyClass()
131 t.SetBranchAddress('classb', cb)
132
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('''
137 struct MyStruct {
138 int myint;
139 float myfloat;
140 };
141 ''')
142 ms = ROOT.MyStruct()
143 t.SetBranchAddress('structll', ms)
144
145 t.GetEntry(0)
146\endcode
147
148\endpythondoc
#define d(i)
Definition RSha256.hxx:102
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
static Roo_reg_AGKInteg1D instance
#define N
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
char name[80]
Definition TGX11.cxx:110
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.
Definition TChain.h:33
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4593
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8375
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.
Definition TTree.h:353
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
for(Int_t i=0;i< n;i++)
Definition legend1.C:18
#define F(x, y, z)
int iterate(rng_state_t *X)
Definition mixmax.icc:34
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
TString as(SEXP s)
Definition RExports.h:86