How to copy a TTree from one file to another?


I’m trying to copy a TTree from a TFile to another.
First I extract the TTree “TTreeTest” from the original TFile (with Get()), then I write it in a new TFile:

[size=85]root [0] GFile = new TFile("./test.root",“read”)
(class TFile*)0x8637a70
root [1] TTree tt_tmp;
root [2] tt_tmp=(TTree
(class TTree*)0x860f1f0
root [3] TFile *tfile = new TFile("./", “recreate”);
root [4] tfile->cd();
root [5] tt_tmp->Write();
root [6] tfile->Close();[/size]

This seems to work but when I read the TTree in the new TFile I get errors like:
[size=85]root [2] Error in TFile::ReadBuffer: error reading all requested bytes from file, got 0 of 162[/size]

I did the same procedure with histograms and it worked. What did I do wrong?
Thanks for your help.


This techique only copy the TTree meta data and not the data.

Instead use TTree::CloneTree (see $ROOTSYS/tutorials/tree/copytree* for some examples).