Copy some branches into different trees and manipulate some other and then write into different trees

Hello guys,
I have a problem that probably you could solve easily.

I have an existing tree with a certain number of branches.
I would like to get some of these branches and dump them, with a DIFFERENT name, into another tree (which will be saved into another file).

First question: is there a faster way than looping over the entries of the tree? I mean also a function that avoids somehow to loop…

In addition, some other branches I would like to manipulate them and then dump , with a DIFFERENT name, into another tree.

-----------Example of My code--------------
TChain *ch = new TChain(“oldtree”);

ch->SetBranchStatus(“run”,1); // branch that I would like to copy with a new name into the new file

//-------------------opening output file-----------------------
TFile *output_file = new TFile(“newfile.oot”, “RECREATE”);

//-----------------------------------------new Tree definition
TTree* newtree = new TTree(“newTree”, “New Output Tree”);

newtree = oldtree->CloneTree();


ch->SetBranchStatus(“event”,1); // branch that I want to manipulate first

newtree->Branch(“bs_Event”, &bs_Event, “bs_Event/I”);

for (Int_t iEntry = 0; iEntry < nentries; ++iEntry){


bs_Event = event*1000;//dummy manipulation


}//end of loop over the entries


Second question: how do I avoid to write also the old tree into the new file?
In the way reported above I get also “oldtree” into my newfile.

Thank you in advance

Hi Matteo,

you could try TDataFrame, see for example
So, borrowing some of your variable names:

ROOT::Experimental::TDataFrame t("oldtree",input_file);
t.Snapshot("newTree", "newfile.root", {"run","bs_Event"});


thanks for the suggestion.

Is this TDataFrame something present only in root 6? Cause I’m using root 5.34 to do what I’m doing…


Hi Matteo,

TDF is part of ROOT starting from 6.10.


Probably not due the change in name and the needed data update.

Rather than

TTree* newtree = new TTree(“newTree”, “New Output Tree”);
newtree = oldtree->CloneTree(); // Lose and leak the TTree allocated above.

you probably meant:

TTree* newtree = oldtree->CloneTree();
newTree->SetTitle("New Output Tree”);

and you likely meant

TTree* newtree = oldtree->CloneTree(0); // Copy just the structure and none of the data.


Thanks Philippe.

Ah ok, so basically I can do a loop to dump the old variables into the new ones without the need of cloning the old tree.
Too bad…

Actually I’ve done a few small tests and it seems that it’s faster (at least for a limited subset of variables) to do the loop rather than do CloneTree().

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.