A confusion of tfile

Hi,
I find a confused problem:

[code]>>> ROOT.TFile.Open(‘data.root’)
<ROOT.TFile object (“data.root”) at 0x2339db0>

ROOT.TFile.Open(‘data.root’).Get(‘data’)
None

file=ROOT.TFile.Open(‘data.root’)
file.Get(‘data’)
<ROOT.TNtuple object (“data”) at 0x2339770>
[/code]

I think the result of

and

file=ROOT.TFile.Open('data.root') file.Get('data')

should be same. But one of it the result is none. It is wired . Here data.root can be any root file and ‘data’ is a tree of the file.

Also, I find that,

[quote]>>> root_data = ROOT.TFile.Open(‘data.root’)

root_data = root_data.Get(‘data’)
root_data
None

data = ROOT.TFile.Open(‘data.root’)
root_data = data.Get(‘data’)
root_data
<ROOT.TNtuple object (“data”) at 0x21a0f00>
[/quote]

I am totally confused…Since I never meet such problem. In python, I used to use code like this:

xxx= a_class
xxx = xxx.a_member

Why can’t I use it here?

Hi,

that second example is not the same as the first. Is a simple matter of reference counting. If you do:>>> ROOT.TFile.Open('data.root')then the only thing keeping the file alive, is the implicit variable underscore (’_’). Once that dies, the file’s refcount goes to zero, and it closes. Most importantly, it takes the data with it.

This one:>>> ROOT.TFile.Open('data.root').Get('data')is none, b/c ref-counting of the TFile goes to zero with only the result of Get() assigned to the implicit variable, which then promptly gets erased.

Either load the data and reset its directory, or copy it over, or simply keep the file alive for the duration you need the data. Your final example takes the last approach.

Cheers,
Wim