void muon_lifetime() { // Begin by loading the libraries gSystem->CompileMacro("Calibrate.C"); gSystem->CompileMacro("LikelihoodCurve.C"); // first we open the data file and fill an histogram TFile *_file0 = TFile::Open("data.root"); // the binning is chosen very fine TH1F* h = new TH1F("histo","histo",1000,0,4096); data->Draw("dt>>histo"); // we prepare the fit function TF1* f1=new TF1("user","[0]*exp(-(x)/[1])+[2]*0",0,4000); // with such a small binning, we must do a likelihood fit (we also set the verbose flag to get all details) user->SetParameters(40,100); user->FixParameter(2,1); histo->Fit("user","LV"); // fitting over the full range, we obtain a rather bad chi2 std::cout << "chi2/ndf for a fit over the full range: " << user->GetChisquare()/user->GetNDF() << std::endl; std::cout << "the corresponding probability is " << TMath::Prob(user->GetChisquare(),user->GetNDF())<< std::endl; // fitting over a limited range, the result still looks rather bad. // the normalized chi2 is ~1.2, but the NDOF is large and we would expect something // much better. TMath::Prob gives ~1E-4 histo->Fit("user","LV","",500,3500); float nchi2 = user->GetChisquare()/user->GetNDF(); std::cout << "chi2/ndf for a fit over [500,3500]: " << nchi2 << std::endl; std::cout << "the corresponding probability is " << TMath::Prob(user->GetChisquare(),user->GetNDF())<< std::endl; // to understand better if the chi2 probability makes sense, we can calibrate the cdf in our case. int nToyMc = 10000; TH1F* chisqu = Calibrate(histo,user,nToyMc,500,3500); // the distribution obtained is centered at ~1.2 ! // and the calibrated probability is ~70% ! The fit is actually excellent. std::cout << "calibrated fit probability is " << (1-(chisqu->Integral(0,chisqu->FindBin(nchi2))/float(nToyMc)))<< std::endl; // here, we plot the likelihood curves histo->Fit("user","LQ0","",500,3500); user->SetParLimits(2,1,2); // that parameter is just to let minuit compute something LikelihoodCurve(histo,user); // now, if we rebin, we can fit with a chi2. // We then see the fit is ok: // TMath::Prob(lastFitFunc->GetChisquare(),lastFitFunc->GetNDF()) ~0.5 // but note that we lost the details of the beginning of the curve. We don't see there is a problem there anymore. }