ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
staff.py
Go to the documentation of this file.
1 # example of macro to read data from an ascii file and
2 # create a root file with a Tree.
3 #
4 # NOTE: comparing the results of this macro with those of staff.C, you'll
5 # notice that the resultant file is a couple of bytes smaller, because the
6 # code below strips all white-spaces, whereas the .C version does not.
7 
8 import re, array, os
9 from ROOT import *
10 
11 
12 ### a C/C++ structure is required, to allow memory based access
13 gROOT.ProcessLine(
14 "struct staff_t {\
15  Int_t Category;\
16  UInt_t Flag;\
17  Int_t Age;\
18  Int_t Service;\
19  Int_t Children;\
20  Int_t Grade;\
21  Int_t Step;\
22  Int_t Hrweek;\
23  Int_t Cost;\
24  Char_t Division[4];\
25  Char_t Nation[3];\
26 };" );
27 
28 ### function to read in data from ASCII file and fill the ROOT tree
29 def fillTree():
30 
31  staff = staff_t()
32 
33  # The input file cern.dat is a copy of the CERN staff data base
34  # from 1988
35 
36  f = TFile( 'staff.root', 'RECREATE' )
37  tree = TTree( 'T', 'staff data from ascii file' )
38  tree.Branch( 'staff', staff, 'Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:Cost' )
39  tree.Branch( 'Divisions', AddressOf( staff, 'Division' ), 'Division/C' )
40  tree.Branch( 'Nation', AddressOf( staff, 'Nation' ), 'Nation/C' )
41 
42  # note that the branches Division and Nation cannot be on the first branch
43  fname = os.path.join(os.path.dirname(__file__),'../tree/cernstaff.dat')
44  for line in open(fname).readlines():
45  t = filter( lambda x: x, re.split( '\s+', line ) )
46  staff.Category = int(t[0]) # assign as integers
47  staff.Flag = int(t[1])
48  staff.Age = int(t[2])
49  staff.Service = int(t[3])
50  staff.Children = int(t[4])
51  staff.Grade = int(t[5])
52  staff.Step = int(t[6])
53  staff.Hrweek = int(t[7])
54  staff.Cost = int(t[8])
55  staff.Division = t[9] # assign as strings
56  staff.Nation = t[10]
57 
58  tree.Fill()
59 
60  tree.Print()
61  tree.Write()
62 
63 #### run fill function if invoked on CLI
64 if __name__ == '__main__':
65  fillTree()
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
A TTree object has a header with a name and a title.
Definition: TTree.h:98
def fillTree
a C/C++ structure is required, to allow memory based access
Definition: staff.py:29