Hallo all,
I have just discovered something with TTree->GetEntry and (multiple virtual) inheritance.
I have a class that inherits like this:
Base->(virtual)->DeriveA
Base->(virtual)->DeriveB
myClass<-DeriveA
myClass<-DeriveB
I sketch the code here:
class Base {
public:
Int_t common_variables;
TTree *tree;
[...]
}
Base::loop {
loadTree();
for (int i=0; i < last; i++) {
getEntries(i);
do_analysis();
}
}
class DeriveA: public Base {
Double_t HT; // tree variable
[...]
}
class DeriveB: public Base {
[...]
}
class myClass public DeriveA, public DeriveB {
[...]
}
DeriveA::loadTree() {
## load some file ##
tree = (TTree*)gDirectory->Get("EV0");
tree->SetBranchAddress("HT",&HT);
tree->SetMakeClass(1); // copy & paste from tree->MakeClass output
## HT ok!
[...]
## tried this:
## tree->GetEntry(0)
## HT ok!
}
DeriveA::getEntries(i){
tree->LoadTree(i); // copy&paste from tree->MakeClass output
tree->GetEntry(i);
common_variables = some_function(HT);
[...]
## HT not OK if tree->SetBranchAddress("HT",&HT); is called earlier
}
DeriveB::do_analysis(){
some_otherfunction (common_variables);
[...]
}
I observe now this:
I have an instance of myClass and I call myClass::loop which is Base::loop, then DerivedA::loadTree is called which loads the tree and sets the branch addresses to variables that are defined in DerivedA, e.g. Double_t HT. In the loop DerivedA::getEntries is called and the value of HT is printed out. Now the value of HT is completely wrong.
I tested it with getting the entry (only one) right after I loaded the tree with exactly the same commands (SetBranchAddress(“HT”,&HT); GetEntry(0) HT has the right value. If I then follow with a debugger the value of HT it suddenly is wrong after DerivedA::getEntries (same commands!) is called.
Testing it with a fixed value of HT and never calling SetBranchAddress(“HT”,&HT), the value of HT is always the same, so no flaw in my classes (and HT is only defined in DerivedA, never overwritten by any child classes).
I also tried not to use multiple virtual inheritance like this:
Base->DeriveA->DeriveB->myClass
I still get the same problem that after the second GetEntry call the value of HT is wrong.
So is this a ROOT problem?
Thank you
Duc