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
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.
34from array
import array
38# We create the file and the tree
39with ROOT.TFile("outfile.root",
"RECREATE") as ofile:
42 # Basic
type branch (float) - use array of
length 1
43 n = array(
'f', [ 1.5 ])
44 t.Branch(
'floatb',
n,
'floatb/F')
48 a = array(
'd',
N*[ 0. ])
49 t.Branch(
'arrayb',
a,
'arrayb[' + str(
N) +
']/D')
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.
75 t.Branch(
'structll', ms,
'myint/I:myfloat/F')
77 # Store struct members individually
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 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
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.
1-D histogram with a double per channel (see TH1 documentation)
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, TClass *realClass, EDataType datatype, bool isptr, bool suppressMissingBranchError)
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)
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Namespace for new ROOT classes and functions.