Re: Problem with TObjArray

From: Rene Brun (Rene.Brun@cern.ch)
Date: Sun Nov 01 1998 - 09:38:22 MET


Hi Alexander,
This is not a bug.
TObjArray is a TCollection. See documentation of TObject::Write
and TCollection::Write.
By default, all objects in the collection are written individually
(each object generates its own key). To write all objects using one key
specify a name and set option to kSingleKey (i.e 1).

Rene Brun



Alexander Zvyagin wrote:
> 
>                                            IHEP, Protvino, Russia,  1-NOV-1998
> 
>     Dear ROOTers and ROOT developers,
> 
> I have problem in storing TObjArray into ROOT file under ROOT 2.00/12. The
> macro is very simple, I try to save TObjArray in ROOT file, then read it back,
> and test what have I read. Macro and it output is attached below.
> 
> Thanks in advance,
> Alexander.
> 
> void bug(void)
> {
>   printf("Example 1:  Storing TObjArray with 0 entries.\n");
> 
>   f1 =  new TFile("f.root","RECREATE");
>   o1 =  new TObjArray;
>   f1 -> ls();
>   o1 -> Write("o1");
>   f1 -> ls();
>   f1 -> Close();
> 
>   printf("---------\n");
> 
>   // Reding back.
> 
>   f1 = new TFile("f.root");
>   f1 ->ls();
>   o1 = (TObjArray*)f1->Get("o1");
>   if( NULL==o1 )
>     printf("Can not find object\n");
>   else
>     printf("Entries = %d\n",o1 -> GetEntries());
> 
>   printf("===================================\n");
> 
>   printf("Example 2:  Storing TObjArray with four entries.\n");
> 
>   f1 =  new TFile("f.root","RECREATE");
>   o1 =  new TObjArray;
>   o1 -> Add(new TObject);
>   o1 -> Add(new TObject);
>   o1 -> Add(new TObject);
>   o1 -> Add(new TObject);
>   printf("Entries = %d\n",o1 -> GetEntries());
>   f1 -> ls();
>   o1 -> Write("o1");
>   f1 -> ls();
>   f1 -> Close();
> 
>   printf("---------\n");
> 
>   // Reading back.
> 
>   f1 = new TFile("f.root");
>   f1 ->ls();
>   o1 = (TObjArray*)f1->Get("o1");
>   if( NULL==o1 )
>     printf("Can not find object\n");
>   else
>     printf("Entries = %d\n",o1 -> GetEntries());
> 
>   printf("===================================\n");
> }
> 
> Output:
> root [0] .x bug.C
> TFile**         f.root
>  TFile*         f.root
> TFile**         f.root
>  TFile*         f.root
> ---------
> TFile**         f.root
>  TFile*         f.root
> Can not find object
> ===================================
> Entries = 4
> TFile**         f.root
>  TFile*         f.root
> TFile**         f.root
>  TFile*         f.root
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;1    Basic ROOT object
> ---------
> TFile**         f.root
>  TFile*         f.root
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;2    Basic ROOT object
>   KEY: TObject  o1;1    Basic ROOT object
> Entries = 0
> ===================================
> NULL
> root [1] .q



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