Sorry, my last version of the code was not correct.
The correct one
{
TObjArray hSPD(2);
TString baseName="hist_";
for (Int_t i=0;i<2;i++){
TString name=baseName + i; TH1F *h = new TH1F(name,name,10*(i+1),10*i,10*(i+1)); h->SetDirectory(0); hSPD[i] = h;
or (better)
{
TObjArray *hSPD = new TObjArray(2);
TString baseName="hist_";
for (Int_t i=0;i<2;i++){
TString name=baseName + i; TH1F *h = new TH1F(name,name,10*(i+1),10*i,10*(i+1)); h->SetDirectory(0); hSPD->Add(h);
Hope this helps,
Valeri ________________________________
From: Fine, Valeri
Sent: Tue 2/12/2008 1:04 PM
To: 'Chiara Zampolli'; 'roottalk'
Subject: RE: [ROOT] writing pointers to pointers to a file
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 referenceWhen 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,> > > This, as I said, trying to write an instance of the class. Moreover,
> > > 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
> > >
> that.
> > 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
This archive was generated by hypermail 2.2.0 : Thu Feb 14 2008 - 17:50:02 CET