Copy a TTree from a TFile to another TFile

In ROOT 6, what is the canonical way to read a TTree from a TFile and write it entirely to another TFile? Using TTree() or TTreeReader() functionality?

Hi,

The simplest way is to use the rootcp utility.

Cheers,
Danilo

Hi,

From C++, you would use the TTree::CloneTree method.

Cheers,
Philippe.

Thanks for the help.

I don’t know what rootcp is and don’t know how to use/find it.

I tried this in C++:

TFile* rootfile_in = new TFile("in.root", "READ"); TFile* rootfile_out = new TFile("out.root", "RECREATE"); TTree* roottree_in = (TTree*)rootfile_in->Get("tree"); TTree* roottree_out = roottree_in->CloneTree(0); roottree_out->SetNameTitle("name", "title"); roottree_out->CopyEntries(roottree_in); roottree_out->Write(); rootfile_out->Close(); rootfile_in->Close();
Works fine. When I try it in Python:

rootfile_in = TFile('in.root', 'READ') rootfile_out = TFile('out.root', 'RECREATE') roottree_in = rootfile_in.Get('tree') roottree_out = roottree_in.CloneTree(0) roottree_out.SetNameTitle('name', 'title') roottree_out.CopyEntries(roottree_in) roottree_out.Write() rootfile_out.Close() rootfile_in.Close()
I get two TTrees in the output file, one properly filled (cycle 3), the other one only half filled (cycle 2). It works with small trees, but for large TTrees (~ 40000 events), I see this behavior. What am I doing wrong?

Hi,

rootcp is a utility that we provide as part of the latest releases (v6.06).

Nothing. This is the expected behavior (See the User’s guide for more details), the ‘cycle 2’ is a backup snapshot of the TTree meta-data to reduce the risk of data loss (in case the program filling the tree unexpectedly terminate) [It does not take much of space on the file]

Cheers,
Philippe.

Hi,

just to put a reference to the new root commandline utils (which include rootcp): root.cern.ch/how/how-quickly-in … ntent-file
In any case, it is just enough to type “rootcp” once ROOT is installed, at least version 6.06.

Cheers,
Danilo

Thank you very much. All this information is very helpful.

I tried just typing rootcp, but my ROOT installation was apparently not fresh enough.