I’ve created the smallest possible example of my problem. Thanks for your help in advance
First the event class used as the parent branch:
#include "TObject.h"
class eventClass : public TObject
{
public:
Float_t data1;
Float_t data2;
///Default Constructor.
eventClass() {};
virtual ~eventClass() {};
ClassDef(eventClass,1)
};
ClassImp(eventClass)
Second the code to create and read the data via the parent branch and via the sub branches:
[code]#include “TTree.h”
#include “TRandom3.h”
#include “eventClass.h”
TTree *CreateData() {
TRandom3 rand(1);
TTree *tree = new TTree(“tree”,“Tree”);
eventClass *data = new eventClass();
tree->Branch("event","eventClass",&data);
for (int i=0;i<1000;i++) {
data->data1 = rand.Uniform(0,1);
data->data2 = rand.Gaus(0,1);
tree->Fill();
}
tree->ResetBranchAddresses();
return tree;
}
void ReadTree(TTree *tree) {
//Read with parent branch
eventClass *data = new eventClass();
tree->SetBranchAddress(“event”,&data);
tree->GetEntry(0);
printf("Entry 0: Parent Branch: data1: %f data2: %f\n",data->data1,data->data2);
tree->GetEntry(1);
printf("Entry 1: Parent Branch: data1: %f data2: %f\n",data->data1,data->data2);
tree->ResetBranchAddresses();
delete data;
//Try with sub branches
Float_t data1;
Float_t data2;
tree->SetBranchAddress("data1",&data1);
tree->SetBranchAddress("data2",&data2);
tree->GetEntry(0);
printf("Entry 0: Sub Branch: data1: %f data2: %f\n",data1,data2);
tree->GetEntry(1);
printf("Entry 1: Sub Branch: data1: %f data2: %f\n",data1,data2);
tree->ResetBranchAddresses();
}
void subBranch() {
ReadTree(CreateData());
}
[/code]
The resulting output:
root [2] ReadTree(CreateData())
Entry 0: Parent Branch: data1: 0.417022 data2: 0.988366
Entry 1: Parent Branch: data1: 0.720325 data2: 0.721278
Entry 0: Sub Branch: data1: 0.000000 data2: 0.000000
Entry 1: Sub Branch: data1: 0.000000 data2: 0.000000
I would have liked to have seen the same values for both outputs. I expected the parent branch could get quite large and I may only be interested in a subset of it.