TObjArray
The following may be needed if not using
CINT Shortcuts
#include "TObjArray.h"
Description
TObjArray 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 TObjArray 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
TClonesArray
which
inherits
from TObjArray, TObjArray can hold objects that belong to a mixture
of classes.
To create a TObjArray that can, initially, hold 500 objects:-
TObjArray *MyObjArray = new TObjArray(500);
To retrieve the ith MyClass object of the array (remember that
C++ arrays
start at element 0) use the expression:-
(MyClass*) MyObjArray->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(MyObjArray);
MyClass *MyObject;
while( MyObject=(MyClass *)next() {
Process MyObject object.
}
TObjArray 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.
Example Use in MINFast
The
MINFDetector
object
HasA
TObjArray. The elements of the array are themselves TObjArrays of MINFHits
ordered by hit number - a nice example of nesting containers in containers.
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