[ROOT] TClonesArrays and TTrees

From: Greg Novak (novak@post.harvard.edu)
Date: Thu Sep 07 2000 - 17:24:08 MEST


I have an object that contains a TClonesArray, and I'd like to store this
object in a TTree object.  However, it seems that the demands memory
allocation and initializing pointers conflict with one another.  I'd like
to do this:

----- in Event.h
class Event : public TObject{
private: 
	TClonesArray *tracks;
public:
	Event(void);	
	Event(Int_T foo);
	~Event(void);
	ClassDef(Event, 1)
}
------ in Event.cc
ClassImpl(Event)

// default constructor
Event::Event(void) {
	tracks = new TClonesArray("Track", 10);
}
Event::Event(Int_t foo) {
	tracks = new TClonesArray("Track", 10);
	// then use foo for something
}
Event::~Event(void) {
	tracks->Clear();
	delete tracks;
}

However, set up my TFile and TTree objects, and read a bunch of these
objects from a Root file, my understanding is that the Root Streamer
function calls Event's default constructor.  This contains memory
allocation statements, and the destructor apparently never gets called
because this setup eventaully eats all the memory on my system.

Therefore, one solution would be to remove the memory allocation statement
from Event(void), and never use this constructor except in Root's Streamer
function (since all of my own code depends on tracks being initialized).

However, this doesn't work because the Streamer function generated by the
two macros ClassDef and ClassImpl contains code that depends on tracks
being initialized.  

I looked at the example code included with Root, Event.cxx, and it
sidesteps this problem by effectively making the tracks variable a static
class variable.  I don't want to do this because I could have more than
one Event kicking around and they need to have different track objects.  

So, is there a way to do what I want?

Thanks!
Greg



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:32 MET