Re: Copying TTrees

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Sat, 21 May 2011 10:34:10 -0500


Hi Dan,

> outputFile->WriteObject(t1,"newTreeName1");

This only copy the meta data of the TTree and not the actual data. You need to use CloneTree to copy both. For example:

   TFile* outputFile = TFile::Open("allData.root", "RECREATE" );    TFile* inputFile1 = TFile::Open("file1.root","READ");    TTree *t1 = (TTree*)inputFile1->Get("results");    TFile* inputFile2 = TFile::Open("file2.root","READ");    TTree *t2 = (TTree*)inputFile2->Get("results");    t1->SetName("newTreeName1");
   t2->SetName("newTreeName2");
   outputFile->cd();
   t1->CloneTree(-1,"fast");
   t2->CloneTree(-1,"fast");
   outputFile->Write();

Cheers,
Philippe.

On 5/19/11 9:04 PM, Dan Pomeroy wrote:
> Hello,
>
> I am trying to do the following, but can't seem to figure it out. I
> have two files (file1 and file2) and each has a tree of the same name
> ("results"). I would like to make a new file (file3) which has two
> trees in it, each with different names, but which are just copies of
> the trees in file1 and file2. Essentially I want to merge file1 and
> file2 into a new file with two trees. Here's what I tried:
>
> TFile* outputFile = TFile::Open("allData.root", "RECREATE" );
> TFile* inputFile1 = TFile::Open("file1.root","READ");
> TTree *t1 = (TTree*)inputFile1->Get("results");
> TFile* inputFile2 = TFile::Open("file2.root","READ");
> TTree *t2 = (TTree*)inputFile2->Get("results");
> outputFile->WriteObject(t1,"newTreeName1");
> outputFile->WriteObject(t2,"newTreeName2");
>
>
> When I do this and look at the output file in a TBrowser it appears to
> have worked, however when I then try to draw any of the variables in
> either tree I get errors like these:
> Error in<TFile::ReadBuffer>: error reading all requested bytes from
> file allData.root, got 0 of 10716
>
>
> Can anyone explain the proper way to merge these trees into one file? Thanks,
>
>
> -Dan
>
Received on Sat May 21 2011 - 17:34:20 CEST

This archive was generated by hypermail 2.2.0 : Sun May 22 2011 - 05:50:01 CEST