RE: [ROOT] troubles renaming object several times

From: Philippe Canal (pcanal@fnal.gov)
Date: Thu Sep 04 2003 - 19:34:08 MEST


Hi Stephane,

What happens is as follow:

   TH1F* h1=new TH1F("h1",...);
   TH1F* h2=new TH1F("h2",...);
   myfunc(h1,...)
// at this point an histogram named 'hr' has been created and 
// is kept in the current directory's list of object
   hr->SetName("h1r");
// a 'variable' whose name is also 'hr' and is a 'TH1F*' is 
// automatically created by CINT and it values is set to the
// address of the object whose ROOT-name is 'hr'.
   myfunc(h2,...) 
// another histogram named 'hr'  has been created and 
// is kept in the current directory's list of object
   hr->SetName("h2r");
// there is variable whose name is 'hr' already in scope,
// CINT does NOT recreate it and does NOT change its value
// hence is changes the name of the first histo (from h1r to h2r).

To avoid this issue do:
  TH1F * myfunc(TH1F* h1,...){
     hr=new TH1F("hr",...);
     return hr;
  }
and
  TH1F* h1=new TH1F("h1",...);
  TH1F* h2=new TH1F("h2",...);
  TH1F* hrp = myfunc(h1,...)
  hrp->Setname("h1r");
  hrp = myfunc(h2,...)
  hrp->Setname("h2r");

Cheers,
Philippe

-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Stephane Plaszczynski
Sent: Thursday, September 04, 2003 6:46 AM
To: roottalk@pcroot.cern.ch
Subject: [ROOT] troubles renaming object several times


hello,

I encounter a problem when creating histograms inside an external script.
I have a function which takes as argument an histogram and create
another one in the code called "hr".

myfunc(TH1F* h1,...){

hr=new TH1F("hr",...)
}

now I call it (from another script) and change the "hr" name.
TH1F* h1=new TH1F("h1",...);
myfunc(h1,...)
hr->Setname("h1r");

this works and I got "h1" and "h1r".

But when I do it twice:
TH1F* h1=new TH1F("h1",...);
TH1F* h2=new TH1F("h2",...);
myfunc(h1,...)
hr->Setname("h1r");
myfunc(h2,...)
hr->Setname("h2r");

here I should have h1,h1r,h2,h2r but I actually got:
.ls

root [2] .ls
TROOT*          Rint    The ROOT of EVERYTHING
 OBJ: TH1F      h1       : 0 at: 0x8a1f490
 OBJ: TH1F      h2       : 0 at: 0x8a1f9a8
 OBJ: TH1F      hr       : 0 at: 0x8ab5260
 OBJ: TH1F      h2r      : 0 at: 0x8a9dce8


does any one knows what happens? (h1r actually exists but is now the
same than h2r!)

Stephane


PS: I attach the entire script (use test() function to reproduce the
problem).



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:15 MET