Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13import sys, os
14from ROOT import TFile, TNtuple, TROOT
15
16
17ifn = os.path.join(str(TROOT.GetTutorialDir()), 'pyroot', 'aptuple.txt')
18ofn = 'aptuple.root'
19
20print('opening file %s ...' % ifn)
21infile = open( ifn, 'r' )
22lines = infile.readlines()
23title = lines[0]
24labels = lines[1].split()
25
26print('writing file %s ...' % ofn)
27outfile = TFile( ofn, 'RECREATE', 'ROOT file with an NTuple' )
28ntuple = TNtuple( 'ntuple', title, ':'.join( labels ) )
29
30for line in lines[2:]:
31 words = line.split()
32 row = map( float, words )
33 ntuple.Fill(*row)
34
35outfile.Write()
36
37print('done')
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
Definition TFile.h:53
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
static const TString & GetTutorialDir()
Get the tutorials directory in the installation. Static utility function.
Definition TROOT.cxx:3068