Hi everybody,
I’m sorry, but I’m stlli in trouble with my ROOT based reconstruction program.
I have the following user defined class structure:
[code]class MyEvent : public TObject {
public:
MyEvent() {}; // def ctor, never called
MyEvent(non_root_class* c) : se(c->SubClass()) {…}; // working (copy) ctor
…
private:
MySubEvent se;
…
};
class MySubEvent : public TObject {
public:
MySubEvent() { hits = new TClonesArray(“MyHit”, 0); } // def ctor, nevre called
MySubEvent(non_root_subclass* sc) { hits = new TClonesArray(“MyHit”, 10000); … /* loop over hits and fill TClonesArray / } // working (copy) ctor
~MySubEvent () {delete hits; }
private:
TClonesArray hits
};
class MyHit : public TObject {
private:
/* only basic types */
}
[/code]
and I want to write it into a tree with a single SPLITTED branch:
[code]MyEvent ev_ptr = 0;
TTree mytree = new TTree(“mytree”);
mytree->Branch(“mybranch”, “MyEvent”, &ev_ptr);
for (… /loop over events/) {
ev_ptr = new MvEvent(non_root_class* c);
mytree->Fill();
delete ev_ptr;
}
[/code]
The TTree is then written to a TFile.
As you can see I need to create and descruct the Event every time, because the ctor is the only way to fill it together with its sub elements. I assume this is legal since TTree->Branch() must recieve a ptr to a ptr to Event.
If this is not correct please tell me.
The problem is that when I read the file, TClonesArray is empty (has size 0). Commenting the “delete ev_ptr;” above would solve (I tried) the problem but is obviously not feasible because it can be a hard mem leak.
I tried to call the TTree::Branch after the first Event is constructed but nothing changed.
I guess the TTree::Fill() doesn’t actually write pointed TClonesArray to buffer but just saves the pointer, so if this is deleted the TClonesArray is not written. Am I correct?
I thought I had found a (ugly) work around by allocating the Event on the stack:
for (... /*loop over events*/) {
MyEvent ev(non_root_class* c);
ev_ptr = &ev;
mytree->Fill();
}
but this worked only before I introduced the “delete hits” in the ~MySubEvent() dtor; after that, it gives a segfault at Fill() time (not on the first event and not in 100% of cases).
Is there something wrong in what I’m attempting to?
One more question: I’m using ROOT v3.05/07 but when I read the with ROOT v4.00 STL containers are supported with splitting, I rushed to tried it out. Unfortunatly I wasn’t successful, I tried to replace my TClonesArray* with
[ul]
std:vector vec;
std:vector vec;
std:vector<MyHit> vec;
std:vector<MyHit*> *vec;
[/ul]
but with none of the 4 I get a browsable/drawable Branch as I can get with the TClonesArray. Why?
Thanks in advance for the help
Davide