Filling a TNtuple in pyROOT

Posted on October 14, 2010. Filed under: Uncategorized | Tags: , , , |

I recently came across the problem that I wanted to create a TNtuple with 635 variables. I wanted to avoid using a TTree if possible (how to do this is discussed here).

I found a solution in a tutorial (page 17) how to use fill a TNtuple with few variables (TNTuple currently has two Fill(..) methods: One taking up to 15 values and another one taking an array).
So the example in the tutorial did not work for me as I have more than 15 values per row. I managed to do this however with code similar to the following one:

import ROOT, array

# the variable names for the output tuple
varnames = [ ... ]

# open the output file
fout = ROOT.TFile("output.root", "RECREATE")

# create the ntuple
output_tuple = ROOT.TNtuple("tuple","tuple",":".join(varnames))

# loop over events

    # calculate the values to be stored in the output tuple
    values = [ ... ]

    # convert the list of values to an float array and fill the output tuple

# end of loop over events

# write the tuple to the output file and close it

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

5 Responses to “Filling a TNtuple in pyROOT”

RSS Feed for Andre Holzner's Blog Comments RSS Feed

Thanks for the tip Andre!

My own two cents:
Curiously this crucial, undocumented incantation does not work if the ntuple is created as a TNtupleD.
(e.g. adding a “D” after output_tuple = ROOT.TNtuple will break it in the example above).
Tested in Root 5.28.


Hi Amnon,

thanks for the feedback !

Have you tried using a “d” instead of an “f” in the line where Fill(..) is called (when you use a TNtupleD instead of a TNtuple) ?

Hi Andre,

I happened to get back to this issue, and found our old thread via google 🙂

You’re right – using ‘d’ is all it take to work with TNtupleD.

thanks again,

I will be really glad if you kindly tell me how to loo[ the branches of TTree or TNtuple upto GetEntries() number of times and fill the data to Histograms like as in ROOT.

typically one can use tree.Draw(“expression”,”cut”,”goff”) for that (“goff” = graphics off), then use a loop for i in range(tree.GetSelectedRows()) and in the loop you can use tree.GetV1()[i] to get the value of the ith event.

Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: