Merging Files

Hello,

I have 2 root files, both containing single trees with the same name/structure,
that I’d like to merge into 1 tree in 1 root file.

The tree in file X contains 75,427 events; the tree in file Y contains
28,918.

I’d like the merged tree to contain 28,918 events from X and 28,918 events
from Y.

The following lines successfully do the merge, but I have 75,427 X +
28,918 Y (as one would expect), rather than 28,918 X + 28,918 Y:

TChain* chain = new TChain(“fitTree”);
chain->Add(“X.root”);
chain->Add(“Y.root”);
TFile* rootFile = new TFile(“XPlusYMerged.root”,“RECREATE”);
chain->Merge(“XPlusYMerged.root”);
rootFile->Write();

Now, tab completing in ROOT gives:

root [1] chain->Add(
Int_t Add(TChain* chain)
Int_t Add(const char* name, Long64_t nentries = kBigNumber)

However, having taken a look at root.cern.ch/root/html//TChain.html#TChain%3AAdd,
I’m struggling to understand exactly how to use the second argument … I’ve tried a few possibilities, but with unexpected and unwanted results. I’m not even sure whether this is the correct method/argument to use …

Could anybody offer any help?

Many thanks,

Jim.

Int_t Add(const char* name, Long64_t nentries = kBigNumber) The 2nd argument is a helper argument that should be either equal to the actual number of entries in the TTree or kBigNumber (in which case the actual number of entries will be lookup by actually opening the file). This is simply a performance issue. To be clear nentries should never be something other than kBigNumber or the number of entries in the tree.

TFile* rootFile = new TFile("XPlusYMerged.root","RECREATE"); chain->Merge("XPlusYMerged.root"); rootFile->Write();
Should simply be written:chain->Merge("XPlusYMerged.root");
Merge will properly create and flush the output file.
Merge has no option to filter out some of the entries and thus can only copy the full trees.
For better control of the content of the output tree you will need to use CloneTree (see the copytree*.C example in the $ROOTSYS/tutorials directory).
Cheers,
Philippe