TCanvas *c1=new TCanvas;
float tau=1.0;
TRandom3 *r=new TRandom3();
TH1F *h1=new TH1F("h1","h1",10000,0,10);//unbinned
TH1F *h2=new TH1F("h2","h2",20,0,10);//binnded
float tsum=0;
int ntot=50;
for(int i=0;i<ntot;i++) {
float t1=r->Exp(tau);
tsum +=t1;
h1->Fill(t1);
h2->Fill(t1);
}
h1->Draw();
h1->SetMaximum(2);
c1->Draw();
TGraph *gLogML=new TGraph();
for(int i=0;i<120;i++)
{
float tau=0.7+0.01*i;
float LogML=ntot*log(1./tau)-tsum/tau;
gLogML->SetPoint(i,tau,LogML);
}
c1->Clear();
gLogML->Draw();
c1->Draw();
float tau_exp=tsum/ntot;
cout<<"Expected tau for Maximum likelihood estimation is "<<tau_exp<<endl;
cout<<"Sigma is "<<tau_exp*tau_exp/ntot<<endl;
TF1 *f1=new TF1("f1","[0]*TMath::Exp(-x/[1])",0,8);
f1->SetParNames("const","lambda");
f1->SetParameter(1,1);
gPad->SetLogy();
h2->Draw();
h2->Fit(f1,"LRI");//L-binned Maximum likelihood , I- integration
c1->Draw();
h2->Draw();
h2->Fit(f1,"RI");//LS is defalut fitting method in ROOT
c1->Draw();
TH1F *h3=new TH1F("h3","h3",20,0,10);//binnded
for(int i=0;i<10000;i++) {
float t1=r->Exp(tau);
h3->Fill(t1);
}
h3->Fit(f1,"LRI");//ML
c1->Draw();
h3->Fit(f1,"RI");//LS
c1->Draw();