# Write one output file from several input files

I would like to read in several chains, and then write out selected entries from these different chains to one output file. For example

``````vector<TChain*> vecChains;
//set up vecChains

TFile out("output.root", "recreate");

//loop over selected entries
{
vecChains[chain]->GetEntry(entry);
//write this entry to output.root
}
``````

I’ve tried several permutations of calling CloneTree and Fill trying to write out the selected events, but I either get only events from one chain, or multiple trees in my file, not one tree with entries from each chain.

Any ideas how to do this?

Thanks,
Alan

Hi,

The simpliest is to chain all the TChain objects.

```vector<TChain*> vecChains; TChain megaChain(treename); //loop over element of vecChains; { megaChain->Add(vecChain[i]); } // use megaChain for the selections/copy etc. ```
This is, of course, assuming that all the chain in vecChain contains the same type of trees.

Another alternative is to use TTree::CopyAddresses to set the branch on the chain to be copied.

Cheers,
Philippe.

The CopyAddresses method seems to do the trick.

Thanks a lot.

Alan