How to share a *tree with two derived classes


I have two derived classes with a TChain* in the parent class. The parent class is constructed using MakeClass() and reads in a pre-existing TChain. I’d like my derived classes to have access to particular branches in the parent class so that I can use the ParentClass objects in the methods of each derived class. I tried doing this with:

Class ParentClass {
      TChain* fChain;
ParentClass::ParentClass(TTree* tree) : fChain(0){
void ParentClass::Init(TTree *tree){
   fChain = tree->CloneTree();
   fChain->SetBranchAddress("BranchA", &BranchA, &b_BranchA);
   fChain->SetBranchAddress("BranchB", &BranchB, &b_BranchB);

void ParentClass::ActivateClassA(int entry){

void ParentClass::ActivateClassB(int entry){

and then in the derived classes:

Class DerivedClassA:public ParentClass{};
DerivedClassA::DerivedClassA(TTree *tree) : ParentClass(tree){};
Class DerivedClassB:public ParentClass{};
DerivedClassB::DerivedClassB(TTree *tree) : ParentClass(tree){};

and then in the

DerivedClassA *A = new DerivedClassA(inputTree);
DerivedClassB *B = new DerivedClassB(inputTree);
for(int iev=0;iev<inputTree->GetEntries(); ++iev){
  Long64_t entry = A->LoadTree(iev);

However, the code crashes when calling the method ActivateA(int entry) and I can’t completely understand why this is. If I comment out the DerivedClassB stuff, it works fine. I know I can just use one derived class and pass this derived object to Class B (e.g. A->BranchB), but why doesn’t activating the fChain in DerivedClassA and DerivedClassB allow me access to BranchA and BranchB within their respective classes?