void exampleChisquare() { RooWorkspace w; w.factory("Gaussian::g(x[-3,3],m[0,-3,3],s[1,0,10])"); auto g = w.pdf("g"); auto x = w.var("x"); x->setBins(50); RooArgSet pars(*w.var("m"), *w.var("s")); RooArgSet obs(*x); auto data = g->generate(obs, 5000); g->fitTo(*data); auto pl = x->frame(); data->plotOn(pl); g->plotOn(pl); auto c = new TCanvas(); c->Divide(2,1); c->cd(1); pl->Draw("SAME"); auto h1 = data->createHistogram("x"); auto f1 = g->asTF(obs, RooArgSet(), obs); auto fcor = new TF1("f2",[&](double* x, double *p){return f1->EvalPar(x,p)*h1->GetSumOfWeights()*h1->GetBinWidth(1); },-3,3,0); // clone the function to keep it alive in the plot auto f2 = (TF1*) fcor->Clone(); c->cd(2); h1->Draw(); f2->Draw("SAME"); double chi2 = h1->Chisquare(fcor,"L"); cout << "chi2 = " << chi2 << " chi2/ndf = " << chi2/(h1->GetNbinsX()-2) << std::endl; cout << "chi2/ndf from RooPlot = " << pl->chiSquare() << std::endl; }