Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_ttree.pyzdoc
Go to the documentation of this file.
2
5
8\code{.py}
9for entry in t:
10 x = entry.branch_name
11 ...
13
14At above,
15`entry` allows to access the branch values for the current entry. This can be
17with Python naming rules, with e.g. "getattr(entry, '1_branch_name')".
18
20with
21faster
23
26
28
30`Branch` links the new branch with a given Python object. It is therefore possible to
31TTree::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
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
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
78 ms = ROOT.MyStruct()
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('''
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
#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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 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 winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void when
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 winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
#define gInterpreter
static TClass * Class()
Definition Class.C:29
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
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:925
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
A TTree represents a columnar dataset.
Definition TTree.h:84
virtual Int_t Fill()
Fill all branches.
Definition TTree.cxx:4610
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8486
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:365
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
int iterate(rng_state_t *X)
Definition mixmax.icc:34
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition RExports.h:167
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
const char * Array