Re: writing a TObjArray

From: Chiara <Chiara.Zampolli_at_bo.infn.it>
Date: Thu, 21 Jun 2007 08:42:09 +0200


Thank you for your suggestions, Leo. For the time being, I think I have found a solution...
cheers,
chiara

Leandro Franco ha scritto:
> Hi,
>
>
> I don't know if what you want to do is possible.
>
> You want to have an array of "something" without being forced to
> create that "something"...
>
>
> The data you write on disk has to be somewhere... if you delete every
> element of the array how would you remember what you had there?...
>
>
> I think the only way to avoid the allocations would be to have just
> one temporal object that is written to the file at each step of the
> loop... but I don't know if that fits your purpose.
>
>
> Cheers,
> Leo
>
>
>
> On Jun 20, 2007, at 5:26 PM, Chiara.Zampolli_at_bo.infn.it wrote:
>
>> Hi Rene,
>> if I simply use the setters, then all the objects will become
>> identical to the first since I am using a pointer, am I wrong? I have
>> sent to roottalk my example with histograms, using also the Reset
>> method, but at the end all the objects in the array had (for example)
>> the same name...
>> thanks again,
>> ch
>>
>>
>> Quoting Rene Brun <Rene.Brun_at_cern.ch>:
>>
>>> 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.
>>>>
>>
>>
>>
>> ----------------------------------------------------------------
>> This message was sent using IMP, the Internet Messaging Program.
>>
>>
>
Received on Thu Jun 21 2007 - 08:44:48 CEST

This archive was generated by hypermail 2.2.0 : Thu Jun 21 2007 - 17:50:02 CEST