RE: writing pointers to pointers to a file

From: Fine, Valeri <fine_at_bnl.gov>
Date: Tue, 12 Feb 2008 13:04:47 -0500


I am sorry replying your question "incrementally". To be able to write the very histogram objects properly one extra C++ statement has to be added to your code:

       TObjArray * hSPD = new TObjArray(2);  

       for (Int_t i=0;i<2;i++){
           sprintf(name,"hist_%i",i);
           hSPD[i]  = new TH1F(name,name,10*(i+1),10*i,10*(i+1)));
           hSPD[i]->SetDirectory(0);  // one needs that
       }
 
       hSPD->Write();

See http://root.cern.ch/root/html/TH1.html for the further explanations.

It says,

" . . .
When an histogram is created, a reference to it is automatically added to the list of in-memory objects for the current file or directory. This default behaviour can be changed by:

       h->SetDirectory(0);          for the current histogram h
       TH1::AddDirectory(kFALSE);   sets a global switch disabling the
reference
When the histogram is deleted, the reference to it is removed from the list of objects in memory. When a file is closed, all histograms in memory associated with this file are automatically deleted.

. . . "

> >
> > >
> > > Dear rooters,
> > >
> > > would it be possible to write pointers to pointers into a
file?
> > The
> > > problem arises from the need to write an instance of a class which
> > > contains pointers to pointers data members into a file. In

> particular,

> > > the pointers to pointers are TH1F**, and the error message reads:
> > >
> > > Program received signal SIGSEGV, Segmentation fault.
> > > [Switching to Thread -1433986848 (LWP 24923)]
> > > 0x048e1245 in vtable for TAxis ()
> > > from /home/zampolli/SOFT/ROOT/root/lib/libHist.so
> > >
> > > This, as I said, trying to write an instance of the class.
Moreover,
> > > trying to write directly on a file a pointer to pointer - such as
> > with:
> > >
> > > TH1F **hSPD = new TH1F*[2];
> > > char name[100];
> > > for (Int_t i=0;i<2;i++){
> > > sprintf(name,"hist_%i",i);
> > > hSPD[i]=new TH1F(name,name,10*(i+1),10*i,10*(i+1));
> > > }
> > >
> > > hSPD->Write();
> > >
> > > does not work either...
> >
> > I believe there is the straight forward ROOT ( and OO) solution for
> that.
> > You should create the ROOT container (for example TObjArray). Add
all
> > your obiect pointers there and write the container out.
> >
> > What about:
> >
> > TObjArray *h_array = new TObjArray;
> >
> > for (Int_t i=0;i<2;i++){
> > sprintf(name,"hist_%i",i);
> > h_array->Add(new TH1F(name,name,10*(i+1),10*i,10*(i+1)));
> > }
> >
> > h_array->Write();
> >
> > Of course, you can create the nested container also, if you need.
> 
> Here you are the same solution as above. I have changed the variable
> names, and added the container size explicitly. (The container is to
> grow as needed with each Add method applied anyway)
> See: http://root.cern.ch/root/html/TObjArray.html
> 
> 
>       TObjArray * hSPD = new TObjArray(2);
> 
>       for (Int_t i=0;i<2;i++){
>           sprintf(name,"hist_%i",i);
>           hSPD[i]  = new TH1F(name,name,10*(i+1),10*i,10*(i+1)));
>       }
> 
>       hSPD->Write();
> 

> > Hope this helps,
> > Valeri
> >
> >
> > > So, I'm wondering whether writing pointers to pointers to a file
is
> > not
> > > possible at all...
> > > Thanks for your help.
> > > Cheers,
> > > chiara
Received on Tue Feb 12 2008 - 19:05:31 CET

This archive was generated by hypermail 2.2.0 : Tue Feb 12 2008 - 23:50:01 CET