RE: TFile::Get problem

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Fri, 03 Aug 2007 11:39:02 -0500


Hi,

By default new Histogram and TTree are attached to the current ROOT directory. There can only be one Histogram or TTree with the __same__ name in a ROOT directory.

When you do:

   TFile f("somefile.root")
   TFile g("someotherfile.root")
   TH1F h1=*(<dynamic_cast TH1F *>(f.Get("hisname")))

The following happens:

   f.Get("hisname")
reads from the file 'f' an object named 'hisname'

   TH1F h1=*(....*)...
This creates a __new__ histogram with the same name and the same content as 'hisnam' __AND__ associate this __new__ object with the current ROOT directory which is 'g' (last opened file).

  g.Get("hisname")
reads from the file 'g' an object name 'hisname', It first looks in the list of in-memory objects and find the object just created (aka h1) and returns this object ...

hence the content of h1 and h2 is thje same.

You can change the default behavior:

   TH1::SetDirectory(0);
which will prevent the auto-association of the histogram with the directory.

Or instead of copying the object, just use the returned pointer:

TFile f("somefile.root")
TFile g("someotherfile.root")
TH1F *h1=(<dynamic_cast TH1F *>(f.Get("hisname"))) TH1F *h2=(<dynamic_cast TH1F *>(g.Get("hisname")))

Cheers,
Philippe.

Ps. You may want to re-read the User's Guide chapter on object ownership.

-----Original Message-----
From: owner-roottalk_at_pcroot.cern.ch [mailto:owner-roottalk_at_pcroot.cern.ch] On Behalf Of ???
Sent: Friday, August 03, 2007 3:56 AM
To: roottalk_at_pcroot.cern.ch
Subject: [ROOT] TFile::Get problem

When I use the following code:
TFile f("somefile.root")
TFile g("someotherfile.root")
//open two different root file which contain different histograms with
the same name

TH1F h1=*(<dynamic_cast TH1F *>(f.Get("hisname"))) TH1F h2=*(<dynamic_cast TH1F *>(g.Get("hisname")))
//get two different histograms

the I meet some problem:some data member of h2 has been overfilled with h1's data

but if I do
TH1F h2=*(<dynamic_cast TH1F *>(g.Get("hisname"))) TH1F h1=*(<dynamic_cast TH1F *>(f.Get("hisname"))) then it's OK

who can tell me why? Received on Fri Aug 03 2007 - 18:42:35 CEST

This archive was generated by hypermail 2.2.0 : Mon Aug 06 2007 - 05:50:02 CEST