Looping over Tree branches

Hi All,

First of all let me state that this a newbie question regarding usage of ROOT Trees.

I have a class called Neuron that I want to store in a ROOT tree. For the time being I am not concerned with the spilting details and leave it to the ROOT scheme.

So I want to make one tree called “NeuronTree” and store the Neuron objects as independent BRANCHES in this tree. I am using following code to acheive this;

[code]

{
TFile f = new TFile (“neuron.root”,“CREATE”);
TTree t(“NeuronTree”, " A Tree with Neurons");
// create 100 branches
for (Int_t i=0;i<100;i++){
TString name=“Neuron”;
Neuron
neuron = new Neuron();
neuron->SetCellName(name+i);
neuron->SetLayer(i%5);
neuron->SetGeneticType(“ABS”);
t.Branch(name+i,“Neuron”,&neuron,32000,99);
t.Fill();
}
f->Write();
f->Close();
}[/code]

This results in stroring these objects into the tree in independent branches.
So the tree root is “NeuronTree” and it has 100 branches named Neuron0-99 which contain the data for a Neuron.

Now I have to read these objects and display their contents. However I dont want to provide name of these individual branches, rather loop over them based on the branch index . So after trying few examples here and there I could come up with following code;

Neuron *n = new Neuron();
TBranch *br = tree->Branch("Test","Neuron",&n,32000,99);
Int_t count =tree->GetNbranches();

for (Int_t i=0;i<count;i++){
   tree->GetEntry(i);
   n->Print()
}

I used statement
TBranch *br = tree->Branch(“Test”,“Neuron”,&n,32000,99);

only when I was not able to find out a way to refer to tree branches with index numbers and in my application I will not know the name of these individual branches in advance.

I will appreciate if someone can indicate me a possibly better way to read objects stored in tree branches.

Regards,
Asif

Hi Asif,

I suggest the following code when reading:

TFile *f = new TFile(“neuron.root”);
TTree t = (TTree)f->Get(“NeuronTree”);
Int_t count =tree->GetNbranches();
Neuron *neurons = new Neuron[count];
TString name = “Neuron”;
for (i=0;i<count;i++) {
neurons[i]=0;
t->SetBranchAddress(name+i,neurons[i]);
}
for (entry=0;entryGetEntries();entry++){
tree->GetEntry(entry);
for (i=0;i<count;i++) {
neurons[i]->Print();
}
}

Rene

Hi Rene,

Thanks a lot for your reply.

But the code here suggests that I know the names of tree branches in advance

TString name = “Neuron”;
for (i=0;i<count;i++) {
neurons[i]=0;
t->SetBranchAddress(name+i,neurons[i]);
}

How to do it without providing a branch name paremeter i.e. assume that I find out that there are 100 branches (all containing Neuron class objects) , and i want to read all of them and display their contents. But i do not know under which name these branches are stored in the tree.

Thanks a lot in advance,

Asif

Asif,

It is not clear to me what you want to achieve.
I assume that somewhere you want to read in your Neuron class.
To do this, you must specify the association between a branch name
and the object where you want to read (SetBranchAddress).
You have the possibility to loop on all branches of a Tree via
tree.GetListOf Branches();
get the pointer to thye branch and find its name.

Rene