Copy a part of 2 tree from 2 root files in a new file


I have 2 root files with the same TTree inside (same branch, same name, etc), but with different number of entries.
I want another root file with the same structure, but with a part of entries of first file and a part of second file.
Is there a way to do it?
Thank you very much

do, eg:

{ TFile *f1 = TFile::Open("file1.root"); TTree *T1 = (TTree*)f1->Get("treename"); TFile *f2 = TFile::Open("file2.root"); TTree *T2 = (TTree*)f2->Get("treename"); TFile *f = new TFile("new.root","recreate"); TTree *T = (TTree*)T1->CloneTree(0); T->CopyEntries(T1,1000); T2->CopyAddresses(T); //thanks Philippe (see doc) T->CopyEntries(T2,2000,""); T->Write(); }


Hi Rene,

thank you very much for the answer, but I obtain this error:

Error: Can't call TTree::CopyEntries(T2,2000,"") in current scope test1.c:47:
Possible candidates are...
(in TTree)
/usr/share/root/lib/  -1:-1   0 public: virtual Long64_t TTree::CopyEntries(TTree* tree,Long64_t nentries=-1);
*** Interpreter error recovered ***

what can I do?
Thank you


You have an older version of ROOT. Remove the last parameter to CopyEntries.


Hi Philippe,

thank you very much, it’s perfect.


sorry but is it possible to extend this method to more files?
I tried to extend to 8 files but I had some problems.
Until 3 files everything is ok, over I don’t have errors but I have an overlap of the histograms.
For example I have samples of higgs mass with different value, 100, 105, 115, 120, etc, but in the final file I have only 3 bins (100, 105, 115), and the last one is the overlap of the next samples (115 + 120 + 125 + etc).
Do you have any idea?

Hi Ciccio,

There is no inherent limitation on the number of files that can be written this way. In older version of ROOT, once a TTree reach 2Gb (by default) it switches over to a new file (closing the previous output TFile and rendering any pointer to it invalid). If this is the limit you are reaching your would see in your current file system directory file name ‘new_1.root’,‘new_2.root’,etc… and you can solve the problem by either increasing the limit:TTree::SetMaxTreeSize(200*1024*1024); // 200Mbor make sure to use the current TFile pointer when storing the TTree:T->GetCurrentFile()->Write(); delete T->GetCurrentFile(); // rather than delete f;