void example_toys() { auto file = TFile::Open("Validation_m1_iso_CR_exclude_Jpsi.root"); file->ls(); auto c = (TCanvas*) file->Get("c_control_Iso_offDiagonal_massC_no_Jpsi"); auto h1 = (TH1*) c->GetListOfPrimitives()->FindObject("ds_dimudimu_control_Iso_offDiagonal_2D_no_Jpsi__m1"); auto h2 = (TH1*) c->GetListOfPrimitives()->FindObject("h2D_template2D_exclude_Jpsi_px"); auto func = [&](double *x, double*) { int ibin = h2->FindBin(x[0]); return h2->GetBinContent(ibin);}; auto f1 = new TF1("f1",func, h1->GetXaxis()->GetXmin(), h1->GetXaxis()->GetXmax(), 0); double chi2BC = h1->Chisquare(f1,"L"); double pvalueBC = TMath::Prob(chi2BC, h1->GetNbinsX() ); double pvalueKS = h1->KolmogorovTest(h2); double distKS = h1->KolmogorovTest(h2,"M"); std::cout << " Baker-Cousins chi2 " << chi2BC << " pvalue " << pvalueBC << std::endl; std::cout << " Kolmogorov distance " << distKS << " pvalue " << pvalueKS << std::endl; new TCanvas(); h1->Draw(); h2->SetLineColor(kRed); h2->Draw("SAME"); // generate toys to calibrate p value auto ht = (TH1*) h1->Clone(); auto hbc = new TH1D("hbc","Baker-Cousins chi2",50,1,0); auto hks = new TH1D("hks","K-S distance",50,1,0); int ntoys = 1000; int nBC = 0; int nKS = 0; int nentries = h1->Integral(1,h1->GetNbinsX()); for (int i = 0; i < ntoys; ++i) { ht->Reset(); ht->FillRandom( "f1", nentries ); double chi2 = ht->Chisquare(f1,"L"); double dist = ht->KolmogorovTest(h2,"M"); hbc->Fill(chi2); hks->Fill(dist); // count number of times we get larger test statistic values if (chi2 > chi2BC) nBC++; if (dist > distKS) nKS++; } std::cout << "Corrected pvalue for chi2 test is " << nBC/double(ntoys) << std::endl; std::cout << "Corrected pvalue for KS test is " << nKS/double(ntoys) << std::endl; auto c1 = new TCanvas(); c1->Divide(1,2); c1->cd(1); hbc->Draw(); c1->cd(2); hks->Draw(); }