TClonesArray vs. TObjArray

Hello Rooters,

I have a question concerning the ROOT container classes. Reading documentation currently does not clear my picture, therefore I ask the community.

I would like to store n objects of the same class-type (say class FooBar) in a container at application runtime. Is it recommended to use a TClonesArray or a TObjArray for the task?

If I understand the API-doc right, I should use in my case TClonesArray.

TObjArray seems to me as a general purpose container that stores any object. Even a mix of objects (= objects of different classes) in the same container instance.

Am I right or did I get something wrong?

Hello H.-Gerd,

TClonesArrays are intended for large repetitive data structures (like trees). Its items are instantiated via the new-by-placement operator, so that the array uses always the same memory region when processing the tree. This will speed up the program execution enormously.

The handling of TObjArray is easier, of course. For a small list, which is used a few times only in the application (and even not persistent), I would use an TObjArray.

Cheers,
Oliver

If you have a collection of N iobjects of the same class FooBar you can use
a TClonesArray of FooBar objects or a std::vector.
In both cases you will get as many branches in the Tree asyou have data members
in FooBar.

The advantage of TClonesArray compared to std::vector is that it minimizes the
number of new/delete operations.

In case you have a collection of N objects of class FooBar or objects deriving
from FooBar, you should use a TObjArray or a std::vector<FooBar*>.
In this case you will get only one branch in the Tree because FooBar and its derivatives
may have different number of data members per entry.

Note that the support for std::vector in TTrees was only introduced in 4.02.
Objects in TClonesArray or TObjArray must derive from TObject.
This is not required for objects stored in std::vector.

Rene