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:
- Event object is not filled
Event * event = new Event;
tree->SetBranchAddress("event", &event);
- Event object is not filled
Event event;
tree->SetBranchAddress("event", &event);
- 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?