A question of TChain

I need to mix 2 tree from 2 different file.For example, I want to get ,say ,5000 event from the first tree and 10000 event from the second then mix them to research the background and signal.
Can I do it by TChain directly, or I must fill a new tree event by event?

TTree *T1 the 1st Tree “T” in file1 built with class Event
TTree *T2 the 2nd Tree “T” in file2 built with class Event
TTree *T3 the output Tree in file3 built with the merge of T1 and T2

TRandom3 r; Event *event1=0, *event2=0; TFile *file1 = TFile::Open("file1.root"); TTree *T1 = (TTree*)file1->Get("T"); TFile *file2 = TFile::Open("file2.root"); TTree *T2 = (TTree*)file1->Get("T"); T1->SetBranchAddress("Event",&event1); T2->SetBranchAddress("Event",&event2); TFile *file3 = TFile::Open("file3.root","recreate"); TTree *T3 = T2->CloneTree(0); //load all branch baskets of T1 in memory T1->LoadBaskets(); Long64_t N1 = T1->GetEntries(); Long64_t N2 = T2->GetEntries(); //loop on entries in T2 and for each entry get a random entry from T1 for (Long64_t i=0;i<N2;i++) { Long64_t i1 = (Long64_t)r.Uniform(0,N1-1); T2->GetEntry(i); T1->GetEntry(i1); //add your code to mix event1 and event2 into event2 T3->Fill(); } T3->AutoSave();