How to return a TTree object?

Hi,

I would just like to know if it is possible to return a tree.

Just look at this code snippet:

import ROOT
def returnThisTree(FN):
  RootFile = ROOT.TFile(FN)
  tree = RootFile.Get("T")
  print tree
  return tree
tree = returnThisTree("file.root")
print tree

Will produce this output:
<ROOT.TTree object (“T”) at 0x40f1390>
None

So, somehow the tree, when returned from the function, becomes None.
I guess this is due to the TFile being closed inside the function that retrieves the tree.
Is there a way to do something like this?

Thanks for any help!

Hi,

[quote=“bjrnfrdnnd”]I would just like to know if it is possible to return a tree.

Just look at this code snippet:

import ROOT
def returnThisTree(FN):
  RootFile = ROOT.TFile(FN)
  tree = RootFile.Get("T")
  print tree
  return tree
tree = returnThisTree("file.root")
print tree

Will produce this output:
<ROOT.TTree object (“T”) at 0x40f1390>
None

So, somehow the tree, when returned from the function, becomes None.
I guess this is due to the TFile being closed inside the function that retrieves the tree.
Is there a way to do something like this?[/quote]

Yes it’s possible to return a tree and, yes, you figured out why this isn’t working (that the TFile is being closed). Open the TFile outside of the subroutine and pass it in, then this should work just fine.

Cheers,
Charles

p.s. Since you correctly diagnosed the problem yourself, you’ll only be charged 50% of the nominal rate for having questions answered. :smiley:

Hi,

You need to keep the TFile alive, this should do:import ROOT def returnThisTree(FN): RootFile = ROOT.TFile(FN) tree = RootFile.Get("T") print tree return (RootFile,tree) (RootFile,tree) = returnThisTree("file.root") print tree

Cheers,
Philippe.