Re: [ROOT] Is there a "true" clone function for TH1F ?

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Thu Jul 25 2002 - 18:21:35 MEST


Hi Pierre-Luc,

  the Clone() method clones correctly the complete histogram including
the fDirectory member which links it to the original directory. What you
should do to have your clone survive the file closure is to call:

  if(th1fbuf){
    fPDF=dynamic_cast<TH1F*>(th1fbuf->Clone());
    fPDF->SetDirectory(0);   // add this line
  }

which dis-associates the histogram from the file. When a file is closed
it closes all associated histograms.

Cheers, Fons.



On Thu, 2002-07-25 at 16:56, Pierre-Luc Drouin wrote:
> 
> Hello,
> 
> I'm looking for a "true" Clone function for TH1F. To illustrate what I
> mean, here is a part of my code (fPDF is a TH1F* member variable of QDis):
> 
> int QDis::LoadPDF(const char *aPDFFileName, const char *aPDFHistogramName)
> {
>   // Loads a PDF histogram from a file with the proper attributes.
>   ClearPDF();
>   TFile *f = new TFile(aPDFFileName, "READ");
>   TH1F* th1fbuf = dynamic_cast<TH1F*>(f->Get(aPDFHistogramName));
>   if(th1fbuf){
>     fPDF=dynamic_cast<TH1F*>(th1fbuf->Clone());
>   }
>   f->Close();
>   delete f;
>   return (th1fbuf && fPDF);
> }
> 
> void QDis::ClearPDF(){
> if(fPDF){
>         delete fPDF;
>         fPDF=NULL;
> }
> }
> 
> Here is a simple main() function:
> int main();
> 
> int main()
> {
>   QDis mydis;
>   mydis.LoadPDF("observed.root","observed");
>   mydis.ClearPDF();
> }
> 
> Since the instance of fPDF seems to share some pointers with the
> instance of th1fbuf (and of TFile *f), I get an error
> message when I try to call ClearPDF() in
> main(), because f has been deleted in LoadPDF. So, is there a
> "true" Clone() member function for TH1F? I've tried the Copy() member
> function, but I get the same error:
> 
>  *** Break *** segmentation violation
> 
> Thank you!
> Pierre-Luc Drouin
> SNO, Carleton University
-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland
E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7679480



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:01 MET