Re: [ROOT] Question about TClonesArray and copy

From: Thomas Bretz (tbretz@astro.uni-wuerzburg.de)
Date: Mon Sep 15 2003 - 14:38:48 MEST


Hi Rene,

thanks, but I can assume (simple because I know it) that the objects are 
consecutive. It is like I wrote it: The destination array is already 
allocated, but maybe with the wrong size. I want to transfer the 
contents of the source array into this array. Clone doesn't help here...

Thomas.

Rene Brun wrote:
> Hi Thomas,
> 
> You must make an implicit loop on the objects in the array. You should not
> assume
> that the objects are consecutive.
> Note that, like for any Root collection, you can do:
>   TClonesArray *a1; //your source array
>   TClonesArray *a2 = (TClonesArray*)a1->Clone();
> 
> Rene Brun
> 
> Thomas Bretz wrote:
> 
>>Dear all,
>>
>>I want to copy a TClonesArray from array1 to array2:
>>
>>class MyClass:public TObject { public: Double_t d; };
>>
>>TClonesArray a1("MyClass", 20);
>>TClonesArray a2("MyClass", 10);
>>
>>// call "new (a1[i]) MyClass;" for all entries in a1 and a2
>>
>>Is it enough to do:
>>a2.ExpandCreate(a1.GetEntriesFast());
>>memcpy(&a2, &a1, sizeof(a1));
>>
>>or must I do something like:
>>const Int_t n = a1.GetEntriesFast();
>>a2.InitSize(n);
>>for (int i=0; i<n; i++)
>>    a2.d = a1.d;
>>
>>BTW: MyClass contains plain values (basic data types) _only_.
>>
>>Thanks in advance,
>>Thomas.
> 
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:15 MET