Re: [ROOT] TObjArray of TClonesArrays

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jan 30 2002 - 19:19:09 MET


Hi Ruben,

Two solutions to your problem
 - solution1 (recommended)  in testwrite
   Set the ownership of your top level TObjArray.
     detectors = new TObjArray(nsens);
     detectors->SetOwner(kTRUE); //<======add this line

 - solution 2 (if you do not want to rewrite your data set) in testread

  for (int i=0;i<nev;i++) {
    // Read the branch for this detector
    branch->GetEntry(i);
    if (!(i%100)) {
      printf("Ev. %5d : %7s %7s %s\n",i,"Sensor","NDigits","&TClonesArray");
      int nsens = detectors->GetLast()+1;
      for (int j=0;j<nsens;j++) {
	TClonesArray *arr = (TClonesArray*)detectors->At(j);
	printf("            %7d  %7d  %p |\n",j,arr->GetLast()+1,arr);
      }
    }
    detectors->Delete(); //<======add this line
  }

Rene Brun

 Ruben Shahoian wrote:
> 
> Hello,
> I need to write and read the branc of the tree, which constists of the
> TObjArray of TClonesArray (TObjArray being collector of data
> (TClonesArray) of different sensor).
> So, I do
> 
>   detectors = new TObjArray(nsens);
>   for (int i=0;i<nsens;i++) {
>     TClonesArray *arr = new TClonesArray("TObject",3000);
>     detectors->AddAt(arr,i);
>   }
>   file = new TFile("tree.root","RECREATE");
>   tree = new TTree("DataTree","Tree");
>   branch = tree->Branch("Det1","TObjArray",&detectors,32000,0);
> 
> It works fine for writing.
> But then, in the reading method, I do
>  TObjArray *detectors=0;
>   branch->SetAddress(&detectors);
>   ...
>   branch->GetEntry(i);
> 
> It reads back everything correctly but it appears that for each event
> it creates new TClonesArrays and attaches it to TObjArray (without even
> deleting the previous TClonesArrays, although, I would expect that they
> simply should be reused).
> I've tried both splitting the tree and setting SetAutoDelete(kTRUE),
> very soon it swallows up all RAM and starts swapping ...
> Is this normal behavoiur?
> I attach short macros (run .x testrw.C, demonstrating the problem)
> 
> Best regards,
>         Ruben
> 
>   --------------------------------------------------------------------------------
> 
>                   Name: testrw.C
>    testrw.C       Type: Plain Text (TEXT/PLAIN)
>               Encoding: BASE64



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:40 MET