Failed to read custom class from TTree

ROOT Version: 6.22/00 Built for linuxx8664gcc
Platform: 20.04 5.4.0-42-generic #46-Ubuntu
Compiler: g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0

Hello! I am trying to read an object of a custom class from a TTree.
I can write it without problems but I cannot read it. I have tried to find a solution in the documentation (TTree chapter, example 4) and in the forum, and it seems that what I do should in principle work.

For the sake of simplicity let us assume that my class has the following structure

class Event : public TObject {

  Int_t num_hits_;
  TClonesArray hits_;


Here is how I write (working OK):

Event event;
tree_.Branch("event.", "Event", &event, 64000, 99);

Here are many failed attempts to read the event:

  1. Event object is not filled
Event * event = new Event;
tree->SetBranchAddress("event", &event);
  1. Event object is not filled
Event event;
tree->SetBranchAddress("event", &event);
  1. Segmentation fault
Event event;
tree->SetBranchAddress("event.num_hits_", &event.num_hits_); // OK can be read
tree->SetBranchAddress("event.hits_", &event.hits_); // segmentation fault here

Other useful info

  • I produce a dictionary for all custom classes
  • I load the dictionary library and check it with TClassTable::GetDict(“Event”)
  • If I open the ROOT file in the command line I can check that all variables are correctly set

The only real difference between my case and the examples is that I define the TClonesArray on the stack and initialize it during the Event object construction like this:

Event::Event() : hits_("Hit", 1000) {}

following the C++ good practice of not using the heap if not necessary. Is this a case where using the heap is a requirement to get things working?

I solved the problem by allocating the TClonesArray on the heap like this:

class Event : public TObject {


  Int_t num_hits_;
  TClonesArray * hits_;

  Event() {
  hits_ = new TClonesArray("Hit", 1000);


So it was indeed a heap-stack problem or some mistake of mine of how I was passing pointers around.

Oh boy, do I dream about the day when ROOT7 is a reality and all of this new/delete nonsense is over.
Because ROOT7 is about that, right?

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