Re: [ROOT] Accessing TObjects in a TClonesArray

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Fri Aug 03 2001 - 10:51:58 MEST


Hi Tom, 

On Thu, 2 Aug 2001 16:41:17 +0000 (UTC)
Thomas Feser <tfeser@student.physik.uni-mainz.de> wrote
concerning ": [ROOT] Accessing TObjects in a TClonesArray":
>   I say:
> 
> 	om_values = new TClonesArray("cand", 500);
> 	for (Int_t i = 0; i < 500; i++) 
> 		new(om_values[i]) cand(0.,0.,0.,0,0.,0);

There's an error here, it should be 


  TClonesArray* om_values = new TClonesArray("cand", 500);
  for (Int_t i = 0; i < 500; i++) 
    new((*om_values)[i]) cand(0.,0.,0.,0,0.,0);
        ^^^^^^^^^^^^
        ||||||||||||

Note the explicit derefence. 
 
For the same job you can also use TClonesArray::ExpandAndCreate (I
think that's the name), whic will call the default constructor for
class cand, and so you can access them later on.  

But why initialise your array to a specific size? Why not create the
objects on demand, which is much more efficient anyway.

>   ("cand" is the name of a class derived from TObject.)
> 
>   Then comes my actual analysis. For every event, I want to define the
> members of om_values[i], so my idea was:
> 
> 	for (Int_t i = 0; om_values->After(current) != 0; 
> 			current = om_values->After(current)) {
> 		i++;
> 		...

It's much easier to loop over the TClonesArray, by doing 

  TIter next(om_values); 
  cand  *o = 0;
  while((o = (cand*)next())) { 
    // Do something to o
    ...
  }
 
Yours, 

Christian  -----------------------------------------------------------
Holm Christensen                             Phone:  (+45) 35 35 96 91 
  Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 305 
  DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm    
  Denmark                                    Email:       cholm@nbi.dk



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:54 MET