Random event reading from Ntuple files

Hi there ,
I am beginner with root so sorry if my question may sound easy, here is the problem ;
I have a two set of files of Ntuple root (D3PDs) , for ttbar and w+js , assume with these names , ttbar_1.root , ttbar_2.root , and w.j1.root w.j2.root ,…
as well they have the same tree structure named reco , truth , trigger .
I want to read 500 events randomly from each three trees (reco, truth , trigger). one time only from ttbar files next time from both.
is there any way to change input file after meeting some conditions at event loop from ttbar to w+jet ? I mean for example code start reading event from ttbar file than after 500 good events it will switch to read events w+jets file for another 500 good events .
I appreciate if you give me a start code or some tips how should I do that . I have tried to find some tips from ROOT tutorials but I am totally lost now . :frowning:
Thanks :smiley:

see this example below


[code]void grandom()
TFile *f1 = TFile::Open(“hsimple1.root”);
TTree T1 = (TTree)f1->Get(“ntuple”);
//set T1 branch addresses
Long64_t N1 = T1->GetEntries();
TFile *f2 = TFile::Open(“hsimple2.root”);
TTree T2 = (TTree)f2->Get(“ntuple”);
//set T2 branch addresses
Long64_t N2 = T2->GetEntries();
const Int_t nbunch = 500;
TRandom3 r;

//loop on first file, nbunch entries at the time, reading randomly
//within the nbunch entries
for (Long64_t entry1 = 0; entry1<N1;entry1 += nbunch) {
printf(“processing events from %lld to %lld\n”,entry1,entry1+nbunch);
for (Long64_t i1=0;i1<nbunch;i1++) {
Long64_t e1 = entry1 + Long64_t(r.Uniform(0,nbunch));
//now you can process e1
//read randomly entries from next bunch of file2
//assuming that N2>=N1
Long64_t e2 = entry1 + Long64_t(r.Uniform(0,nbunch));
//now you can process e2, merge with e1, etc