TTree::SetBranchAddress and sub-branch

Hi,

I have created a class for my branch saved in a TTree. Here is the most condensed example.

class eventClass :TObject {
   public:
     int data1;
     eventClass();
};

I can read the data back with the following:

eventClass *evt = new eventClass(); tree->SetBranchAddress("evt",&evt); tree->GetEntry(0); printf("%d\n",evt->data1);

I would like to be able to set the branch address for the sub branch data1 as follows:

int data1;
tree->SetBranchAddress("data1",&data1);
tree->GetEntry(0);
printf("%d\n",data1);

The resulting value from the second read with the sub branch always returns 0. Am I missing something?

I’m using root version 5.34/09

Thanks.

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.

Any suggestions on how to deal with this?