Object Ownership?

Hello,

I was expecting these two scenarios to produce the same results. Am I wrong, or doing something wrong? Using 5.28 on a Snow Leopard. Thanks so much for any help.

1:

TFile f; if(condition) { f.Open(filename); TH1F *H = (TH1F*)f.Get(title); << apparently opens file but... f.ls(); << looks like file is empty H->GetBinContent(1); << crashes title->GetBinContent(1); << this works! But for "dynamic" titles I can't use this. }

2:

if(condition) { TFile f(filename); TH1F *H = (TH1F*)f.Get(title); << apparently opens file and... f.ls(); << file shows content now H->GetBinContent(1); << works }

Hi,

f.Open(filename);TFile::Open is a static function and the call f.Open does not affect the object ‘f’.

H is still zero since ‘f’ was not connected to file.

[/code] title->GetBinContent(1); << this works! But for “dynamic” titles I can’t use this.[/code]This works because CINT will look for an undefined variable inside the current ROOT file (which is in this case the ‘correct’ one thanks to the call to TFile::Open).

To use TFile::Open do:TFile *f = 0; if(condition) { f = TFile::Open(filename); TH1F *H = (TH1F*)f->Get(title); f->ls(); etc...

Cheers,
Philippe.