//%jsroot on
TCanvas c;
double mu=100;//average count per second
double trange=10000;//time interval - seconds
int n=mu*trange;//number of events;
auto h1=new TH1D("h1","Time distribution of random events",200,0,trange);
gRandom=new TRandom3(0);
vector<double> vt;
// generate n uniform random numbers
for(int i=0;i<n;i++) {
double t=gRandom->Rndm()*trange;
vt.push_back(t);
h1->Fill(t,1);
}
cout<<"unsorted random number:";
for(int i=0;i<5;i++) cout<<vt[i]<<" ";
cout<<endl;
//sort random numbers in ascending order
sort(vt.begin(),vt.end());//sort vector in ascending order
cout<<"sorted random number:";
for(int i=0;i<5;i++) cout<<vt[i]<<" ";
cout<<endl;
h1->SetMinimum(0);
h1->Draw();
c.Draw();
double xmax=3.0*7/mu;
double dx=xmax/1000.;
TH1D *h[5];
for(int i=0;i<5;i++) {
h[i]=new TH1D(Form("hn%d",i+1),Form("Scaled interval distribution of %d-events.",i+1),1000,0,xmax);
}
for(int i=0; i<vt.size();i++) {
for(int j=1;j<=5;j++) {
if(i>=j) h[j-1]->Fill(vt[i]-vt[i-j],1./n/dx);
}
}
gPad->SetLogy();
h[0]->Draw();
h[0]->Fit("expo");//fit with function of f(x) = exp(p0+p1*x)
h[1]->SetLineColor(kGreen);
h[1]->Draw("same");
h[2]->SetLineColor(kBlue);
h[2]->Draw("same");
h[3]->SetLineColor(kBlack);
h[3]->Draw("same");
h[4]->SetLineColor(kPink);
h[4]->Draw("same");
c.Draw();
exp(4.60630)
!jupyter nbconvert distribution_of_time_intervals.ipynb --to html