ROOT
CLING_PROFILE
Reference Guide
Loading...
Searching...
No Matches
staff.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_tree
3
## \notebook -nodraw
4
## example of macro to read data from an ascii file and
5
## create a root file with a Tree.
6
##
7
## NOTE: comparing the results of this macro with those of staff.C, you'll
8
## notice that the resultant file is a couple of bytes smaller, because the
9
## code below strips all white-spaces, whereas the .C version does not.
10
##
11
## \macro_code
12
##
13
## \author Wim Lavrijsen
14
15
import
os
16
17
import
ROOT
18
from
ROOT
import
TFile, TTree, addressof, gROOT
19
20
## A C/C++ structure is required, to allow memory based access
21
gROOT.ProcessLine(
22
"""struct staff_t {
23
Int_t Category;
24
UInt_t Flag;
25
Int_t Age;
26
Int_t Service;
27
Int_t Children;
28
Int_t Grade;
29
Int_t Step;
30
Int_t Hrweek;
31
Int_t Cost;
32
Char_t Division[4];
33
Char_t Nation[3];
34
};"""
35
)
36
37
38
## Function to read in data from ASCII file and fill the ROOT tree
39
def
staff():
40
41
staff = ROOT.staff_t()
42
43
# The input file cern.dat is a copy of the CERN staff data base
44
# from 1988
45
46
f =
TFile
(
"staff.root"
,
"RECREATE"
)
47
tree =
TTree
(
"T"
,
"staff data from ascii file"
)
48
tree.Branch(
"staff"
, staff,
"Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:Cost"
)
49
tree.Branch(
"Divisions"
, addressof(staff,
"Division"
),
"Division/C"
)
50
tree.Branch(
"Nation"
, addressof(staff,
"Nation"
),
"Nation/C"
)
51
52
# note that the branches Division and Nation cannot be on the first branch
53
fname = os.path.join(str(ROOT.gROOT.GetTutorialDir()),
"io"
,
"tree"
,
"cernstaff.dat"
)
54
with
open(fname)
as
file:
55
for
line
in
file.readlines():
56
t = line.split()
57
staff.Category =
int
(t[0])
# assign as integers
58
staff.Flag =
int
(t[1])
59
staff.Age =
int
(t[2])
60
staff.Service =
int
(t[3])
61
staff.Children =
int
(t[4])
62
staff.Grade =
int
(t[5])
63
staff.Step =
int
(t[6])
64
staff.Hrweek =
int
(t[7])
65
staff.Cost =
int
(t[8])
66
staff.Division = t[9]
# assign as strings
67
staff.Nation = t[10]
68
69
tree.Fill()
70
71
tree.Print()
72
tree.Write()
73
74
f.Close()
75
76
77
#### run fill function if invoked on CLI
78
if
__name__ ==
"__main__"
:
79
staff()
TFile
Definition
TFile.h:130
TTree
A TTree represents a columnar dataset.
Definition
TTree.h:89
int
staff
Definition
staff.py:1
tutorials
io
tree
staff.py
ROOTCLING_PROFILE - Reference Guide Generated on
(GVA Time) using Doxygen 1.16.1