Re: Saving histograms in a file AFTER book+fill

From: Martin Purschke (purschke@bnl.gov)
Date: Thu Jun 25 1998 - 15:27:40 MEST


Alberto Baldisseri wrote:

> Hi rooters,
>
> I have a macro that build some histograms and I would like to save it on
> a file. The standard
> way suggested on the tutorial is to open the file before creating the
> histograms, in that way
> all the histograms are located in the current directory of the open
> file. But, I would like to
> save the histograms AFTER the book+fill.
> When I open the file after the histogram book+fill, the file becomes the
> current directory
> which is empty. There is a way to copy all the histograms to the file
> directory ? Or another
> solution ?
>
> Regards,
>
> Alberto

Hi Alberto,

I had a similar problem a few weeks ago, and Rene pointed me to a good
solution. You can ask the TROOT object for a list of all objects, see
whether or not they inherit from  abstract class TH1 or whatever you want to
save, and then Write() them to the file. This is nice because you can
globally save all histograms in one go without doing it for all the
histograms individually in your code. I append a code snippet, where I save
all TH1's and derived classes to a file.

void saveHisto1d (const char * file)
{
  TFile *hfile = new TFile(file,"RECREATE","");

  TList *l = TROOT.GetList();
  //  l->ls();
  TIter next(l);
  TObject *obj;
  while((obj = (TObject*) next()))
      if (obj->InheritsFrom("TH1")) obj->Write();

  //  hfile->ls();
  delete hfile;
}

Enjoy,
    Martin

--
Martin L. Purschke               ;   purschke@bnl.gov
                                 ;   http://www.rhic.bnl.gov/~purschke
                                 ;
Brookhaven National Laboratory   ;   phone: +1-516-344-5244
Physics Department Bldg 510 C    ;   fax:   +1-516-344-3253
Upton, NY 11973-5000             ;
-----------------------------------------------------------------------



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:34 MET