Re: use of globals in test examples
Rene Brun (Rene.Brun@cern.ch)
Tue, 24 Mar 1998 17:39:54 +0100
Rutger van der Eijk wrote:
>
> Hi all,
>
> I was taking a close look at the event/track example in $ROOTSYS/test and
> started to wonder why a global *gTracks is defined. The constructor of the
> event class creates the gTracks global (a TClonesArray) if it does not
> exist yet and puts the fTracks datamember of the instantiated Event object
> to this global. In the MainEvent program only one instantiation of Event
> is made. In a loop the datamembers are set to different values and
> everytime a tree is filled with the event.
> I wonder now what would happen if we would create 2 Event
> objects. In that case both Event objects will use the same Track array.
> This doesn't seem object-oriented to me, and I don't see the advantage of
> using the global gTracks in this case. I'm a new user of ROOT so I might
> make a mistake here, and miss the reason why a global object should be
> used in this case. If this is the case I would appriciate anybody
> explaining this to me, so that i can use this in my own programs. (I can
> imagine that it has something to do with the addresspointer used by the
> tree fill function...)
>
> Rutger van der Eijk
Apologies for the long list of emails in roottalk/rootdev/private
not yet answered. I hope to recover in the coming few days.
You are in theory perfectly right. One should avoid the use
of globals as much as possible.
In the case of the Event test program, I made a compromise
between a simple minded class from which you do not learn much
and a more sophisticated example too complicated for a novice user.
The fTracks container should in fact be part of a RUN class, since
by definition the corresponding TClonesArray survives between events.
The test program was designed to make one single instance of Event
when generating the output file and possibly one instance of Event
for each event when reading the file.
A more useful example is given in the ATLFast program.
Rene Brun