
import sys, os
from ROOT import TFile, TNtuple, TROOT


ifn = os.path.join(str(TROOT.GetTutorialDir()), 'io', 'tree', 'aptuple.txt')
ofn = 'aptuple.root'

print('opening file %s ...' % ifn)
infile = open( ifn, 'r' )
lines  = infile.readlines()
title  = lines[0]
labels = lines[1].split()

print('writing file %s ...' % ofn)
outfile = TFile( ofn, 'RECREATE', 'ROOT file with an NTuple' )
ntuple  = TNtuple( 'ntuple', title, ':'.join( labels ) )

for line in lines[2:]:
    words = line.split()
    row = map( float, words )
    ntuple.Fill(*row)

outfile.Write()

print('done')
