Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
mrt.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_pyroot
3 ## \notebook -nodraw
4 ## Build ROOT Ntuple from other source.
5 ## This program reads the `aptuple.txt' file row by row, then creates
6 ## the Ntuple by adding row by row.
7 ##
8 ## \macro_output
9 ## \macro_code
10 ##
11 ## \author Wim Lavrijsen
12 
13 import sys, os
14 from ROOT import TFile, TNtuple, TROOT
15 
16 
17 ifn = os.path.join(str(TROOT.GetTutorialDir()), 'pyroot', 'aptuple.txt')
18 ofn = 'aptuple.root'
19 
20 print('opening file %s ...' % ifn)
21 infile = open( ifn, 'r' )
22 lines = infile.readlines()
23 title = lines[0]
24 labels = lines[1].split()
25 
26 print('writing file %s ...' % ofn)
27 outfile = TFile( ofn, 'RECREATE', 'ROOT file with an NTuple' )
28 ntuple = TNtuple( 'ntuple', title, ':'.join( labels ) )
29 
30 for line in lines[2:]:
31  words = line.split()
32  row = map( float, words )
33  ntuple.Fill(*row)
34 
35 outfile.Write()
36 
37 print('done')