Add trees

I am using the root 3.10/02 pro.

I have 100 trees and want to select some events from each of them and add them to one big tree. I wrote two functions

TTree smalltree;
TEventList evlist;

GetEvent(file, selection) {
TFile *f;
if (smalltree) delete smalltree;
if (f) delete f;
f=new TFile(file);
smalltree=(TTree*)f->Get("T");
smalltree->Draw(">>evlist", selection);
evlist=gDirectory->Get("evlist");
}

CreateBigTree() {
TTree *bigtree=new TTree("bigtree", "Big Tree");
TEvent *evsmall, *evbig;
bigtree->SetBranchAddress("Events", "TEvent", &evbig, 32000, 0);
while (inputfile>>file) {
GetEvent(file, selection);
smalltree->SetBranchAddress("Events", &evsmall);
for (i=0; i<evlist->GetN(); i++) {
smalltree->GetEntry(evlist->GetEntry(i));
evbig=evsmall;
bigtree->Fill();
}
}

Everything is ok for the first file, but segmetation error happens as soon as it start to read the second file. I tried to delete evsmall and set it to 0 in CreateBigTree(). It doesn’t help. Could you tell me how to fullfill my goal?

Thanks

Make a TChain with all the files that you want to merge and use
TTree::CopyTree. Example:

{
//creates a TChain to be used by the h1analysis.C class
//the symbol H1 must point to a directory where the H1 data sets
//have been installed

TChain chain(“h42”);
chain.Add("$H1/dstarmb.root");
chain.Add("$H1/dstarp1a.root");
chain.Add("$H1/dstarp1b.root");
chain.Add("$H1/dstarp2.root");

TFile *f = new TFile(“cpt.root”,“recreate”);
TTree *T = chain.CopyTree(“nelec>4”);
T->Print();
T->Write();
delete f;
}

see also the copytree2,3 tutorials.
You do not indicate which version you use. I assume it is the current PRO release 3.10/02.
see:http://root.cern.ch/root/roottalk/RoottalkRules.html

Rene