Add Branches to Tree

Hello all,

I need some assistance in writing a couple of variables calculated in my code to a previously written tree. However the tree I am trying to write to is being copied from the source file I am running on (the “physics” tree), so I am cloning the tree and then trying to write my new variables to that tree. If anyone has any suggestions as to how to successfully do this they would be appreciated. Thanks so much.

You can do something line in the piece of code below. See also TTree::AddFriend


void addBranches() { TFile *fold = TFile::Open("oldfile.root"); TTree *Told = (TTree*)fold->Get("mytreename"); TFile *fnew = TFile::Open("newfile.root","recreate"); TTree *Tnew = Told->CloneTree(0); TBranch *b1 = Tnew->Branch("bnew1",...); TBranch *b2 = Tnew->Branch("bnew2",...); Long64_t nentries = Told->GetEntries(); for (Long64_t i=0;i<nentries;i++) { Told->GetEntry(i); //fill variables/classes to go in branches b1 and b2 //then fill the new Tree with the branch variables b1->Fill(); b2->Fill(); } Tnew->AutoSave(); delete fnew; delete fold; }