Re: writing a TObjArray

From: Rene Brun <Rene.Brun_at_cern.ch>
Date: Wed, 20 Jun 2007 17:06:15 +0200


Chiara,

I still do not understand your problem. Why do you need to delete your objects in the loop, simply modify them with a Setter function.

Rene Brun

Chiara.Zampolli_at_bo.infn.it wrote:
> Hi Rene,
> yes, sure, I'm still working on it, but my problem does not concern
> the use of the TTree (I am using it, of course, and it fulfills all of
> my requirements!), but simply how to fill a TObjArray without creating
> (with a new) all the objects without deleting them if not at the very
> end with Clear(). Probably this is the only way, in fact...
>
> Thank you for your help.
> Cheers,
> Chiara
>
>
>
> Quoting Rene Brun <Rene.Brun_at_cern.ch>:
>
>> Chiara,
>>
>> Your question is really unclear. Are you still working on the same
>> problem for which you posted
>> a few mails some months ago and where I recommended you twice to use a
>> TTree for this job?
>>
>> Rene Brun
>>
>> Chiara Zampolli wrote:
>>> Dear rooters,
>>>
>>> I would like to write a TObjArray in a file. The TObjArray is
>>> filled in a loop where every time the object I would like to store
>>> is created with a new. Since I set the array as the owner of its
>>> objects, calling the Clear method at the end would delete all the
>>> objects I have used to fill the array. This is the only way I have
>>> found to make everything work creating a new object in every
>>> iteration in the loop (where I CAN NOT delete them, otherwise the
>>> array is not writable!). If I don't do like this (i.e. don't call
>>> "new" at every iteration), but I just change the members of the
>>> object, all the objects at the end would be the same, and
>>> precisely, the same as the last object I have created.
>>> Calling "new" every time would be fine, if the number of objects
>>> I would like to store was not huge, as, on the contrary, it is in
>>> my case, which becomes memory consuming....
>>> What would be the best way to proceed, then?
>>> In the attached file you find an example (very simple), hopefully
>>> explaining the problem.
>>>
>>> Thanks in advance.
>>> Cheers,
>>> Chiara
>>> ------------------------------------------------------------------------
>>>
>>>
>>> void testWriteArray(){
>>> TObjArray *array = new TObjArray(10);
>>> array->SetOwner();
>>> // TH1F *h = new TH1F();
>>> for (Int_t i = 0;i<10;i++){
>>> TH1F *h = new TH1F();
>>> TString Name=Form("Histo%02i",i);
>>> h->Reset(); // useful if new is outside h->SetName(Name);
>>> h->SetTitle(Name);
>>> h->SetBins(10,10*i,10*(i+1));
>>> for (Int_t j=0;j<10;j++){
>>> h->Fill(10*i+j); // different histos to see what I get in the
>>> output
>>> }
>>> array->AddAt(h,i);
>>> //delete h;
>>> //h=0x0;
>>> }
>>>
>>> TH1F *h1 = (TH1F*)array->At(0);
>>> cout << " name h1 = " << h1->GetName() << endl;
>>> cout << " name h = " << h->GetName() << endl; // still there, sure
>>>
>>> TFile *file = new TFile("testWriteArray.root","RECREATE");
>>> array->Write("array",TObject::kSingleKey);
>>> file->Close();
>>> delete file;
>>> file = 0x0;
>>> array->Clear();
>>> //cout << " name h = " << h->GetName() << endl; // of course,
>>> does not // work
>>> if Clear is used
>>> return;
>>> }
>>>
>
>
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
Received on Wed Jun 20 2007 - 17:06:25 CEST

This archive was generated by hypermail 2.2.0 : Wed Jun 20 2007 - 17:50:02 CEST