How to make stat box appear

Hello Forum Colleagues,
I believe the usual way to make the stat box with the fit parameters and stats is to use the command “gStyle->SetOptFit(1)”. However, it seems to have no effect on the section of the code below:

gROOT->SetStyle("Plain");   //set plain TStyle
gStyle->SetOptStat(0); //draw statistics on plots, 0 for no output
gStyle->SetOptStat(111111); //draw statistics on plots, 0 for no output
gStyle->SetOptFit(1);    //draw fit results on plot, 0 for no output
gStyle->SetPalette(57);     //set color map
gStyle->SetOptTitle(0);    //suppress title box


TCanvas c;


// draw fit including the error band
c.Divide(2, 2);
for (unsigned i = 0; i < nmodels; ++i) {
    c.cd(i + 1);
    models[i]->DrawFit();
}
c.Print("/home/riclambo/Desktop/bat_27_06_345/C6_data/data-all-band.pdf");

// draw data and all fits in the same plot (w/o error bands)
c.Clear();
c.cd(1);

gr.SetMarkerStyle(20);
gr.SetMarkerSize(.5);
gr.GetXaxis()->SetTitle("cos(#theta)");
gr.GetYaxis()->SetTitle("C6 (meV #AA^{6})");
gr.Draw("ap");

for (unsigned i = 0; i < nmodels; ++i) {
    models[i]->GetFitFunction().SetLineColor(i + 1);
    models[i]->GetFitFunction().SetLineWidth(2); 
    models[i]->GetFitFunction().GetXaxis()->SetTitle("cos(#theta)");
    models[i]->GetFitFunction().GetYaxis()->SetTitle("C6 (meV #AA^{6})");
    models[i]->GetFitFunction().Draw("l same");

    double chi2_models = models[i]->CalculateChi2(models[i]->GetBestFitParameters());
    double pvalue_models   = models[i]->CalculatePValue(models[i]->GetBestFitParameters());

    cout << "model_chi2" << "\t" << i << "\t" << chi2_models << endl; 
    cout << "pvalue" << "\t" << i << "\t" <<pvalue_models << "\n" << endl ;

      }
    c.Print("/home/riclambo/Desktop/bat_27_06_345/C6_data/data-all.pdf");

Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.24
_Platform: Ubuntu
Compiler: Not Provided


It seems you draw a TGraph, a TGraph has no stats.

Is there a way that I can make the stats appear on the graph? using something like:

  TPaveStats *ps = (TPaveStats* ) g->GetListOfFunctions()->FindObject(“stats”);

As I said a TGraph has no stats, you cannot make appear something which does not exist. Only histograms have stats.

The poor man workaround would be something like this and Draw the TH1D.
See Convert TGraph to TH1 histogram
However I suspect you will need tweaks to do on the conversion routine if you need to make some sort of error setting or configure weights on th1D.

@couet Maybe an example of how to manually create (and draw) a TPaveStats, with fit statistics for a (fitted) TF1, would be helpful.

Yes for the TGraph fit results it works, the fit box is created:

void graphfit()
{
   gStyle->SetOptFit(1);
   Double_t x[] = {1., 2., 3.};
   Double_t y[] = {2., 3., 2.};
   auto g = new TGraph(3, x, y);
   g->Fit("gaus");
   g->Draw("A*");
}

So, we’re back to the original problem … why can the “stats” be missing?

@Ricardo_Lambo I guess one would need to inspect the “DrawFit” method.
Just to make sure … remember that the “stats” box is “attached” to the graph and NOT to the “fit function”. Moreover, you need to really draw the graph to create its “stats” box. I do not have any example of how to manually create (and draw) a TPaveStats (with fit statistics) when drawing a “fit function” alone.

with this example they are not missing:

void graphfit()
{
   gStyle->SetOptFit(1);
   Double_t x[] = {1., 2., 3.};
   Double_t y[] = {2., 3., 2.};
   auto g = new TGraph(3, x, y);
   g->Fit("gaus");
   g->Draw("A*");
   gPad->Update();
   TPaveStats *st = (TPaveStats*)g->GetListOfFunctions()->FindObject("stats");
   st->ls();
}
Processing graphfit.C...

****************************************
Minimizer is Minuit / Migrad
Chi2                      =  3.19935e-07
NDf                       =            0
Edm                       =  6.39931e-07
NCalls                    =           71
Constant                  =      3.00056   +/-   1.00004     
Mean                      =            2   +/-   0.435822    
Sigma                     =      1.11018   +/-   0.658012     	 (limited)
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

OBJ: TPaveStats	stats  	X1= 2.360000 Y1=2.852500 X2=3.440000 Y2=3.152500

Thanks @Wile_E_Coyote @RENATO_QUAGLIANI @couet. I was able ot use the last solution to make the stats I wanted appear on the graph.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.