Reading TObjArrays/TClonesArrays from TTree: Re-using existing arrays


ROOT Version: 6.06.08
Platform: Linux (Debian 9)
Compiler: g++ 4.9


I’m trying to read some custom classes from TTrees and am a bit confused about what should be happening with custom classes that contain TObjArrays and TClonesArrays.

My top level class is an Event, stored in a TTree Branch. The Event class contains a TObjArray of Trigger objects. Each Trigger in turn contains TClonesArrays of various other objects.

Right now to prevent memory leaks I have to set TBranch::SetAutoDelete(kTrue), or else delete my event objects between reads. I’m wondering if it’s possible to re-use the existing objects when reading entries from the file. I’ve tried defining and calling Clear functions that deallocate heap objects and clear the TClonesArrays, but it seems the next GetEntry still default constructs a new TObjArray of Triggers, rather than using the existing one.

I have read through the guides, the class reference, tutorial scripts and past threads (i see TClonesArray use come up a lot!) but sadly I still don’t have a clear idea of whether this is possible, or if so, how to achieve it.

Unfortunately the choice to use the TObjArray imply the reallocation. The reason is that TObjArray is an heterogenous container and thus the I/O can not assume that it has the right type of objects when reading the next entry. To avoid allocation replace (at write time) the TObjArray by a TClonesArray or a series of TClonesArray.

Cheers,
Philippe.

I see, that makes sense. Thanks for clarifying.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.