example of macro to read data from an ascii file and create a root file with a Tree.
NOTE: comparing the results of this macro with those of staff.C, you'll notice that the resultant file is a couple of bytes smaller, because the code below strips all white-spaces, whereas the .C version does not.
4 from ROOT
import TFile, TTree, gROOT, AddressOf
25 staff = ROOT.staff_t()
30 f =
TFile(
'staff.root',
'RECREATE' )
31 tree =
TTree(
'T',
'staff data from ascii file' )
32 tree.Branch(
'staff', staff,
'Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:Cost' )
33 tree.Branch(
'Divisions', AddressOf( staff,
'Division' ),
'Division/C' )
34 tree.Branch(
'Nation', AddressOf( staff,
'Nation' ),
'Nation/C' )
37 fname = os.path.join(ROOT.gROOT.GetTutorialsDir(),
'tree',
'cernstaff.dat')
38 for line
in open(fname).readlines():
39 t = list(filter(
lambda x: x, re.split(
'\s+', line ) ) )
40 staff.Category = int(t[0])
41 staff.Flag = int(t[1])
43 staff.Service = int(t[3])
44 staff.Children = int(t[4])
45 staff.Grade = int(t[5])
46 staff.Step = int(t[6])
47 staff.Hrweek = int(t[7])
48 staff.Cost = int(t[8])
58 if __name__ ==
'__main__':
- Author
- Wim Lavrijsen
Definition in file staff.py.