Stream a class with complicated pointers

Dear rooters,
I want to stream this class:

Class MyEvent: Public TObject

int eventid;
TClonesArray* trks; //->
TClonesArray* pts; //->


The TClonesArray* pts contains these objects:
Class StEmcPoint:Public TObject

float energy;
vector<StTrack*> mTracks;

Each of a StEmcPoint object has zero or None-zero track(s) associated with it. the vector<StTrack*> is to do this. The vector doesn’t own the tracks.

The TClonesArray* trks contains pointers to StTracks: StTrack*. However, I never explictly add things into this container.

Before, I didn’t do the StEmcPoint-StTrack association, so the vector<StTrack*> might be always just empty. I didn’t have any problem to write and read the tree.

Then I did the association. I didn’t have problem to write the tree. But when I read the tree, for the first a few events when there is zero StTrack associated with the StEmcPoint, no problem. But when there is a StTrack associated with the StEmcPoint, the program crashed.

During the above process, I never explictly added StTrack* to the TClonesArray* trks! I only added StEmcPoint* to the TClonesArray* pts. I thought if there were any StTracks associated with one StEmcPoint, these StTracks should be automatically streamed and saved when the StEmcPoint was streamed, am I right?

The program crashed when GetEntry(i).

Any idea of what’s going on? Thanks!

To add a few lines.
The track may have a pointer pointing to vertex object. If the track is streamed, will the vertex object be automatically streamed too?

In this page
it says:
If a data member is a pointer to a TClonesArray object, one super branch of class TBranchClones is created. In our example, this is the case for the data member fTracks. This super branch will have a buffer where to store the number of objects in the array. This super branch has the name of the data member. ROOT will also automatically generate additional branches, one branch for each data member of the class referenced by the array (Track in the example). Note that the data members of this class must be basic types only or arrays of basic types (eg fErrors[9]). Data members of this class cannot be pointers.
In my case, I have a container of pointers as the data memeber of StEmcPoint, which is the class that TClonesArray contains. Is this the reason?


One possibility is that the I/O for vector<xx*> actually assumes it owns the memory.
In order to avoid problem you may want to consider using a vector and or TRefArray instead of straight pointer.