Dump contents of a tree to an ASCII file

Hi all,

I’d like to be able dump all the entries from a TTree in a root file to an ASCII file. I’ve been using PyRoot for a while now, so I’d like to keep it in that format if possible.

However, I find that for a TTree with ~150 branches it takes about 1/3 second to read out. This seems long to me since on the same machine all of my other PyRoot activities run fast.

I fear I’m doing something silly in how I access the Leaf/Branches (I can never keep this straight) and maybe this is more a general ROOT question than a PyRoot question, but I figured I’d try here first.

Here’s the code.

[code]#!/usr/bin/env python

import some modules

import sys
from ROOT import *

outfile = open(“default.txt”, “w”)

##########################################

Create a chain based on the ntuple names

##########################################

Assuming the tree is named “ntp”

t = TChain(“ntp”)
print len(sys.argv)
for j in range(1, len(sys.argv)):
filename = sys.argv[j]
print("Adding file: " + filename)
t.Add(filename)

t.SetBranchStatus("*",1)

##########################################

Read and dump the tree entries

##########################################
nentries = t.GetEntries()
for nev in xrange (100):
t.GetEntry( nev )

output = ""
leaves = t.GetListOfLeaves()
for leaf in leaves:
output += "%f " % ( leaf.GetValue() )

output += "\n"
outfile.write(output)

outfile.close()
[/code]

Usage is “script.py infile.root”

Maybe this is just a slow process between the reading/writing of the 150 values/event. Any suggestions/help would be greatly appreciated. Much thanks in advance.

Matt

Hi,

Note that you can ‘almost’ get what you needed with maximum efficiency by doing:

########################################## 
# Read and dump the tree entries 
########################################## 
t.GetPlayer().SetScanRedirect(kTRUE) # You may have to cast t.GetPlayer() to a TTreePlayer*
t.GetPlayer().SetScanFileName("output.txt")
t.Scan("*")

Cheers,
Philippe.

Hi Philippe,

Whoa. Much faster than what I was doing. Thanks very much.

Not to sound ungrateful, but is there any way to format the output such that I don’t get the “*” as a field delimiter? If not, I can write a script to deal with it. Much appreciated for the response.

Matt

Hi Matt,

Short of modifying the code in TTreePlayer::Scan and recompiling ROOT, there is no option to change the delimiters.

Cheers,
Philippe.

Hi Philippe,

No worries. I can definitely work with this. Thanks again for the pointers.

Matt

That’s too bad you can’t change that as an option in the last argument. Like delimiter=space or whatever. It’d also be nice to get rid of the row as an option. Anyway, no big deal, but if making an ascii ntuple (like someone wants from me right now), it would make things really easy.

I don’t mean to complain. I could of course replace the delimiter with a script, but seems like a pretty useful feature.

Thanks again!

Elliott

Hi Elliot,

Indeed it would be useful :slight_smile:, the necessary changes are also pretty well localized (TTreePlayer::Scan) so maybe you could give a try to implementing these features and contributing them back to us :slight_smile:.

Thanks,
Philippe.

Oh wow. Okay, I’ll take a look. Especially if it’s something you guys would actually like.

I’m sorry I can’t make it a top priority right now. I know everyone is always super swamped, but I would love to contribute so I’ll see what I can do.

Elliott