Home | News | Documentation | Download

How to select events randomly?

Dear Rooters,
I have lot of events in my root file. But I want to choose few events only. So I used TRandom function in the following way. After choosing few events I want to save them in another root file. I ran the code but it is not saving any root file(veto.root). Could you please suggest me how to resolve the problems.

Thanks in Advance.

#include <TRandom1.h>
#include <TRandom2.h>
#include <TRandom3.h>
#include <TFile.h>

void makeinc(){
  TChain chain("track");
  chain.Add("/home/souvik/tracking/root/root/mixed/recon_mixed*.root");
  Double_t xMbc;
  chain.SetBranchAddress("Mbc",&xMbc);

  Int_t ntries = chain.GetEntries();
  float mass_ary[300000000];
  for(int i=0; i<ntries; i++){
     chain.GetEntry(i);
     mass_ary[i] = xMbc;
  }

  TNtuple *track = new TNtuple("track","","mmass");
  TBits *bits = new TBits(ntries);

  Int_t n = 21120;  //21122;//19009//21877//20207                                                                                                                                                           
  Int_t i=0;
  Int_t seed = i*10000;
  TRandom *rand = new TRandom3(seed);
  while(i<n) {
    Int_t j = rand->Integer(ntries);
    if (bits->TestBitNumber(j)) continue;  //this entry exist                                                                                                                                               
    bits->SetBitNumber(j);
    track->Fill(mass_ary[j]);
     i++;
  }
  TFile *f = new TFile("veto.root", "recreate");
  track->Write();
  f->Close();
}

Regards


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


At least … move “TFile *f = new TFile(...);” to right before “TNtuple *track = new TNtuple(...);

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.