Create root file with TTrees, reading from another file (but changing TTree name)

I am sure this might have been answered, but I cannot find the thread.

I have a root file (lets say file1.root) that contains some TTrees:


each of those with several branches.

I want to create another root file (lets say file2.root) with the same trees, but with different names, like:


I have tried several things, but i finally got here…

sorry if this is trivial, but this is really taking me now a lot of time.
Any Help?

Dear ElChavo,

What did you try exactly?
The tutorial ‘tutorials/tree/copytree.C’ shows an example how to do it. By default all branches are copied.
You can change the name of the new tree with newtree->SetName(“newname”) before writing it out to the new file. also, CloneTree accepts an option fast to avoid uncompressing the tree during copy.

Something like this should do it:

TFile *file = TFile::Open("file.root");
TTree *tree = (TTree*)file->Get("name");

// Create a new file, clone tree in new file, change name
TFile *newfile = TFile::Open("newfile.root","recreate");
TTree *newtree = tree->CloneTree(-1, "fast");

delete file;
delete newfile;

G Ganis

Dear ganis

thanks for the reply.
The step that I was missing was:


I actually managed to do it. Thanks for your answer ayway.