Issue using TRefArray

Hi,

I have a class that I am using in my event which has a TRefArray on each object of a TClonesArray. The referenced objects are being added using the TRefArray::Add() method.

But the code is not behaving as I would expect. For the first event, everything looks fine. I see the right number of objects from the ref array in my analysis script.

But for subsequent events after the first, something gets very screwy, and not all the object references are being saved. So I get error messages like …

Error in <TRefArray::At>: index 3 out of bounds (size: 3, this: 0x364bd00)

Even though I add 30 objects to this array in my sim program, when I go to read the events back, ROOT says the size of the array is only 3!

I think I am doing the right thing in my class for clearing this data between events …

SimCalorimeterHit::~SimCalorimeterHit() {
    TObject::Clear();
    delete simParticleContribs_;
    simParticleContribs_ = 0;
}

void SimCalorimeterHit::Clear(Option_t *option) {
    TObject::Clear();
    simParticleContribs_->Delete();
}

Any idea what might be going on here? Is there some “gotcha” when using TRefArray in conjunction with TClonesArray that I might be encountering here?

I can provide more complete code snippets if it might help.

Thanks.

–Jeremy

Hi Jeremy,

Is the simParticleContribs array the TClonesArray or the TRefArray. Is the TRefArray also clearer? In Clear why call Delete on the array?

Cheers,
Philippe.

[quote=“pcanal”]Hi Jeremy,

Is the simParticleContribs array the TClonesArray or the TRefArray. Is the TRefArray also clearer? In Clear why call Delete on the array?

Cheers,
Philippe.[/quote]

simParticleContribs is the TRefArray (I can provide the header file if needed). It lives in an object that is within a TClonesArray in my event model class.

My understanding was that I need to call Delete() on the TRefArray pointer to avoid a memory leak and possible IO corruption/problems; I ran into problems before when I didn’t do this. I have another class that uses a TRefArray and calls Delete() and it seems to work okay. But here I have something a bit more complicated (the TRefArray can have 1-30+ references in it), and it is not behaving at all how I would expect.

Fair enough.

Interesting. Does the failing TRefArray have the right size just before TTree::Fill (in the writer)?

Cheers,
Philippe.

Fair enough.

Interesting. Does the failing TRefArray have the right size just before TTree::Fill (in the writer)?

Cheers,
Philippe.[/quote]

Thanks for the suggestion.

It looks like the output TRefArray does not have the correct size just before Fill.

Let me investigate this further to see if I can figure out why that would be…