Re: [ROOT] Writing array of histograms

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed May 14 2003 - 21:25:30 MEST


Hi Jason,

This is quite easy to do. Let me assume that you have created several
histograms TH1 *h1,*h2,*h3, etc in the current directory.
You can get a pointer to the list in memory with
   TList *list = gDirectory->GetList();

then write this list as one single key in a Root file with:
  TFile f("junk.root","new");
  list->Write("allhists",TObject::kSingleKey);

To read the list again in memory, you can do
  TFile f("junk.root");
  TList *list = (TList*)f.Get("allhists");
  list->ls();

Rene Brun

On 
Wed, 14 May 2003, jrieger wrote:

> Thanks for the quick response.  But what I would like to do is write the
> array of histograms as an array, rather than have each individual
> histogram appear separately in the root file.  I am trying to preserve the
> position in the array so that I can compare it to another array.  Is this
> something that can be done?  Thanks again
> 
> Jason
> 
> On Wed, 14 May 2003, Otto Schaile wrote:
> 
> > Hi Jason,
> > 
> > On Wed, 14 May 2003, jrieger wrote:
> > 
> > > Greetings,
> > > 
> > > I have generated an array of TH1F that I now want to write out to a root
> > > file.  However, I am having difficulty getting this to work.  Is it
> > > possible?  A portion of the code that I used to make this histogram is:
> > 
> > 
> > just put in front:
> > 
> > TFile * outf = new TFile("xxxx.root", "RECREATE");
> > // all new hist will be added to  directory of xxxx.root
> > 
> > > 
> > > TH1F *myhist[1000];
> > > char *histname = new char[10];
> > > sprintf(histname, "h_x_%d",xx);
> > > myhist[xx]=new TH1F(histname,"",100,-0.25,0.25);
> > > for (Int_t iii=0;iii<nentries; iii++) {
> > > 	Double_t binx=h_x->GetBinContent(iii);
> > > 	myhist[xx]->SetBinContent(iii,binx);
> > > }
> > 
> > // and at the end:
> > 
> > outf->Write();       // all objects in dir of outf (xxxx.root) 
> >                      //  are written
> > 
> > Cheers
> > Otto
> > 
> > -- 
> > Otto Schaile            Sektion Physik, LMU Muenchen
> > Phone: +49 89 289 14070     Am Coulombwall 1, D-85748 Garching, Germany
> > GSM:   +49 160 1553598
> > FAX:   +49 89 289 14072     EMail: Otto.Schaile@Physik.Uni-Muenchen.DE
> > 
> > 
> > 
> 
> 



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