Adding a branch to a tree

Hi everybody,

I have the following problem. I try to add a branch to a tree and to save the new tree (the one with one additional branch) in a new file.

here is the macro I use:
{
//Open the old file and get the tree

TFile f(“studentntuples.root_repos”);
f.cd(“StudentSampleAlg”);
TTree* nt1=gDirectory->Get(“1”);

//open the new file and create the new tree as a clone of the old one

TFile *g = new TFile(“modifiedntuples.root”, “RECREATE”);
TTree *newtree = nt1->CloneTree();

//delete the old tree and close the old file

nt1.Delete();
f.Close();

Float_t px, py, energy, transmom;

//add the branch to the new tree and try to fill it

newtree.Branch(“transmom”, &transmom, “transverse momentum/F”);

newtree->SetBranchAddress(“px”, &px);
newtree->SetBranchAddress(“py”, &py);
newtree->SetBranchAddress(“transmom”, &transmom);

int nentries=newtree->GetEntries();
cout<<nentries<<endl;

for( int i=0; i < nentries; i++){
newtree->GetEvent(i);
transmom = sqrt( pxpx + pypy);
newtree.Fill();
}

newtree.Write();

g->Write();
newtree.Delete();
g.Close();}

If I try to draw the new branch after a add it:

newtree->SetBranchAddress(“transmom”, &transmom);
newtree.Draw(“transmom”)

I get nentries in the one single bin (always the same but I do not know
what is actually giving that number)

If I draw the new branch after filling/writing the tree
newtree.Write();
newtree.Draw(“transmom”);

I get some strange histogram having double of the number of entries is supose to have and looking as the sum of the histogram descriebed above and the actual transmom histogram.

Could you please help me to understand what happen?

Thank you very much in advance,
Have a nice day,
Raluca

Hi,

nt1.Delete(); f.Close();
Here nt1.Delete(); is unnecessary since f.Close will take care of it. If it was usefull you would need to write it

will fill ALL the branch in the tree. you meant

TBranch * b = newtree.Branch("transmom", &transmom, "transverse momentum/F"); .... b->Fill(); ....

HOWEVER we strongly discorage adding a branch to already filled tree. Instead we recommend creating a new tree and making it a friend of the old tree.

Alternatively in your case you can save run-time by looping through the tree only once by adding the branch at the same time you are cloning.

Float_t px, py, energy, transmom;
nt1->SetBranchAddress("px", &px);
nt1->SetBranchAddress("py", &py);
nt1->SetBranchAddress("transmom", &transmom);

TFile *g = new TFile("modifiedntuples.root", "RECREATE");
TTree *newtree = nt1->CloneTree(0); // Do no copy the data yet

// DO NOT delete the old tree and close the old file

//add the branch to the new tree and try to fill it

newtree.Branch("transmom", &transmom, "transverse momentum/F");

int nentries=newtree->GetEntries();
cout<<nentries<<endl;

for( int i=0; i < nentries; i++){
   nt1->GetEntry(i);
   transmom = sqrt( px*px + py*py);
   newtree.Fill();
}

g.Write();

Still the cheapest option it to create a new tree with just the transmom branch and make it a friend of the first tree.

Cheers,
Philippe.

Hi again,

thank you very much for your answer, it helped, now I got what I want. Just a small correction for the sake of the people that will read these messages afterwards. The newtree will have 0 number of entries. I must then give up closing the old file f and modify the loop region as:

int nentries=nt1->GetEntries();
cout<<nentries<<endl;

for( int i=0; i < nentries; i++){
nt1->GetEvent(i);
transmom = sqrt( pxpx + pypy);
newtree.Fill();
}

have a nice day,
Raluca

I update my earlier message to fix this typo.

Thanks,
Philippe.