RE: [ROOT] Writing array of histograms

From: Faine, Valeri (fine@bnl.gov)
Date: Wed May 14 2003 - 20:34:52 MEST


Hello Jason,

Did I understand properly you tried to mimic HBOOK histograms?
(If my guess is correct let me know I'll send you better example)

In general:

By default any object of the TH<n><t> (TH1F, TH2D etc) class are members
of the special ROOT container "current TDirectory" that the global
pointer gDirectory points to.

This means if you what your histogram belongs some container you may
have created a dedicated TDirectory first. Because the ctor of
TDirectory class re-assigns the gDirectory pointer  you may not have
made any special effort (call) to add your histogram to the container.
TH1 class ctor will do that for you.

 If you still want your own kind of container then you should

   1. disable the feature above
     ( see: http://root.cern.ch/root/htmldoc/TH1.html#TH1:AddDirectory )
   2. Make sure you the container is a C++ object and it is
ROOT-writable

In your example you create an array of the pointers (It doesn't matter
whether these pointers are for histograms or for any other class
objects).
In ROOT there is no direct method to write an array of "anything" out.
You can write the class object only. The array in your example is not an
object and it can not be written out.

You may to create TObjArray object instead and populate it with the
pointers. 
(see:
http://root.cern.ch/root/htmldoc/TObjArray.html#TObjArray:description )
Then you can use ROOT to write the ROOT class container, namely
TObjArray object out.
If this container is populated with the pointers to the ROOT histogram
then they must have been written out also.


> > TFile * outf = new TFile("xxxx.root", "RECREATE");
> > // all new hist will be added to  directory of xxxx.root
> >
> > >
      TObjArray *myhists = new TObjArray();
> > > char *histname = new char[10];
> > > sprintf(histname, "h_x_%d",xx);
> > > TH1F *myhist=new TH1F(histname,"",100,-0.25,0.25);
      myhists->AddLast(myhist);
> > > for (Int_t iii=0;iii<nentries; iii++) {
> > > 	Double_t binx=h_x->GetBinContent(iii);
> > > 	myhist[xx]->SetBinContent(iii,binx);
> > > }
> >
> > // and at the end:
> >
> > myhist->Write();     // all objects from myhists are written 
                         // out to the CURRENT 
                         // TFile. By chance it is outf (xxxx.root)
> >                      // 



                Hope this helps, Valeri

> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]
> On Behalf Of jrieger
> Sent: Wednesday, May 14, 2003 12:19 PM
> To: roottalk@pcroot.cern.ch
> Subject: Re: [ROOT] Writing array of histograms
> 
> 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