void exampleFitHistogram() { auto h1 = new TH1D("h1","h1",100,-5,5); // generate data (gaussian with m=0, s=1) h1->FillRandom("gaus"); // add an extra gaussian contribution (m=0, s= 3) for (int i = 0; i < 200; ++i) h1->Fill(gRandom->Gaus(0,3)); auto c1 = new TCanvas(); c1->Divide(2,1); c1->cd(1); // fit the data TF1 *fitgaus = new TF1("fitgaus", "gaus", -5,5); // least square fit auto result1 = h1->Fit("fitgaus","S"); std::cout << " chi2 / " << "ndf = " << result1->Chi2() << " / " << result1->Ndf() << std::endl; auto rp1 = new TRatioPlot(h1); rp1->Draw(); h1->SetTitle("Least Square Fit"); // Maximum likelihood fit // copy histogram c1->cd(2); auto h2 = new TH1D(*h1); h2->SetTitle("Maximum Likelihood Fit"); auto result2 = h2->Fit("fitgaus","S L"); // compute chi2 from likelihood ratio (2 * minLL) // use all bins (also empty ones) when computing ndf std::cout << " chi2 / " << "ndf = " << 2* result2->MinFcnValue() << " / " << result2->Ndf() << std::endl; auto rp2 = new TRatioPlot(h2); rp2->Draw(); }