TClonesArray

Header Files

The following may be needed if not using CINT Shortcuts #include "TClonesArray.h"

Description

TClonesArray is a container class that is to say it is a class whose objects hold other objects. For background information, including the way objects are held and how to iterate over all contained objects see ROOT Containers. There it explains that the TClonesArray container is dynamic, increasing in size as required and that the array holds pointers to TObject objects. Objects of any class that inherits from TObject can also be stored in the array, but the pointer must be converted using the syntax:- (Class * ) objptr to get access to the object. Unlike TObjArray TClonesArray, which inherits from TObjArray can only hold objects that all belong to the same class. When a TClonesArray object is created, its constructor must be passed the class name of the objects it will contain. For example, to create a TClonesArray that can, initially, hold 500 REROOT_FLSDigit objects:-
TClonesArray *flsdigitsArray = new TClonesArray("REROOT_FLSDigit",500);

To retrieve the ith object of the array (remember that C++ arrays start at element 0) use the expression:-

(REROOT_FLSDigit*) flsdigitsArray->At(i);
The At function checks that i is within the array bounds and returns 0 (i.e. the null pointer) if not, or if there is no object in this position. If certain that i is valid, the member function UncheckedAt is quicker, as it does not bound check. To loop over all non-zero entries in the array a TIter object can be used, for example:- TIter next(flsdigitsArray); REROOT_FLSDigit *myflsdigit; while( myflsdigit=(REROOT_FlSDigit *)next() { Process FLSDigit object. }

TClonesArray objects have a wide range of member functions, mostly by inheritance from base classes. These include:- Add, BinarySearch, Capacity, Clear, Delete, FindObject, First, GetEntries, GrowBy, and Remove. Hopefully the names are sufficiently meaningful to suggest their function.

Advantages of TClonesArray over TObjArray

It may seem odd that TClonesArray inherits from TObjArray but is restricted to holding a single type of object, but the class was designed to optimise performance and to support automated processing.

Performance Optimistation

Objects that are required for an indefinite period have to be created on the heap memory using the new operator, and eventually destroyed using the delete operator. To keep the heap from fragmenting unnecessarily it has to be careful how it allocates memory, so these operations are quite expensive in cpu. Events often contain many small identical objects, for example in MINFast FLSDigit objects, and then the creation and destruction each event can present a serious processing overhead. TClonesArray objects avoid this overhead by providing the space for the object as well as a pointer to it. Using the array operator [], instead of the At member function, for example:- (*flsdigitsArray)[i]; returns the address where an object can be stored, regardless of whether an object is present at this slot or not. It does this by checking the array slot and, if set to 0, using the new operator to reserve the space needed to hold an object (it knows the class of the object that will occupy the slot). So once all the slots in the TClonesArray have been allocated, they can be used again and again, event after event with no further heap allocation.

In principle I/O could present a problem: if a event contained a TClonesArray then, when reading in the event, the TClonesArray would be recreated and all the good work wasted. The solution is to have the event only contain a pointer to TClonesArray, and to create the TClonesArray before entering the event I/O loop. Then the TClonesArray object is told to input itself but does not loose all its allocated space.

Automated Processing

Normally event I/O uses the TTree class that offers a number of advantages, such as ensuring that all component objects of an event get read or written together. As the TClonesArray only contains a single type of object it is possible for branch splitting to break out all the data members of the objects it holds. For example, if an event has a pointer to a TClonesArray holding FLSDigit objects, then histograms of its data can be drawn with a single command. This is described in TTree.

Example Use in MINFast

The REROOT_Event object has pointers to a number of TClonesArray objects that are effectively the OO equivalent of the ADAMO Entity Sets.

For more information see Reading ROOT Class Info.


Go Back to the The ROOT Crib Top Page


If you have any comments about this page please send them to Nick West