Understanding Collections


A collection is a group of related objects. You will find it easier to manage a large number of items as a collection. For example, collections of points and lines might be managed by a graphics pad. A vertex will have a collection of tracks. A detector geometry contains collections of shapes, materials, rotation matrices and sub-detectors. Collections act as flexible alternatives to traditional data structures of computer science such as arrays, lists, and trees.

Collections can be thought of as polymorphic containers that can contain different types of elements. For this release of the ROOT system, elements to be placed in collections must be instances of classes. These may be classes defined by you or provided by ROOT. Collection elements must be instances of classes descending from TObject. The dependence of collections on TObject may disappear in the future when all C++ compilers used with the ROOT system fully support templates. In the mean time, knowing the role TObject plays in collections can be helpful.

In general you don't need to worry about TObject. Many ROOT classes have TObject as an ancestor. In fact, collections themselves are descendants of TObject. This makes it possible for collections to contain other collections (subcollections) in a tree structure. Such trees are used in the ROOT system to implement components of the graphics system (graphics pads containing pads), geometries (detectors in detectors), etc.

The basic protocol TObject defines for collection elements is shown below:

        IsEqual()
        Compare()
        IsSortable()
        Hash()

How to use and override these member functions is shown in the example program.

Types of Collections

The ROOT system implements the following type of collections: arrays, lists, sorted lists, B-trees, hashtables and maps. The figure below shows the inheritance hierarchy for the primary collection classes.

Ordered Collections (Sequences)

Sequences are collections that are externally ordered because they maintain internal elements according to the order in which they were added. The following sequence collections are available:

Both a TObjArray as well as a TOrdCollection can be sorted using their Sort() member function (assuming the stored items are sortable).

Sorted Collections

Sorted collections are ordered by an internal (automatic) sorting mechanism. The following sorted collections are available:

Unordered Collections

Unordered collections don't maintain the order in which the elements were added. I.e. when you iterate over an unordered collection, you are not likely to retrieve elements in the same order they were added to the collection. The following unordered collections are available:

Using Collections


Rene Brun, Fons Rademakers
Last update 24/11/95 by FR