Updating stats box not working

I’m trying to get my stats box to show the resolution of the fit on my histogram, but I can only get it to show by also showing the Std. Dev of the histogram instead of the Sigma of the fit.
This:

void MWE(){
  
  gStyle->SetOptStat(1110);
  gStyle->SetOptFit(0001);

  TH1F* h1 = new TH1F("h","h",500,200,700);
  TRandom3 *myRNG=new TRandom3();
  myRNG->SetSeed(1);
  
  for(int ii=0;ii<10000;ii++)
    {
      double rand = myRNG->Gaus(440,40);
      h1->Fill(rand);
    }

  TCanvas* c1 = new TCanvas("c1","c1",900,700);
  
  h1->Draw();

  c1->Modified();
  c1->Update();

  TF1* f11 = new TF1("f11","gaus",420,470);
  h1->Fit(f11,"QR");
  
  TPaveStats *s1 = (TPaveStats*)h1->FindObject("stats");
  s1->SetName("1");
  s1->SetOptFit(1);
  s1->SetOptStat(0);
  TList *l1      = s1->GetListOfLines();

  double MPV = f11->GetParameter("Mean");
  double Sigma = f11->GetParameter("Sigma");
  double Ratio = Sigma/MPV;
  TString Reso = Form("Resolution = %f%%",Ratio*100);
  TLatex *t1 = new TLatex(0,0,Reso);
  t1 ->SetTextFont(42);
  t1 ->SetTextSize(0);
  l1->Add(t1);

  c1->Modified();
  c1->Update();
  
}

Gives:

Which is almost what I want, but doesn’t have the Resolution at the end. Adding h1->SetStats(0); after l1->Add(t1); gives the following, which has Std Dev where I want Sigma to be and doesn’t show the results of the fit.

Can someone please help me combine the two things? Any advice would be appreciated!

{
   // Create and plot a test histogram with stats
   TCanvas *se = new TCanvas;
   TH1F *h = new TH1F("h","test",100,-3,3);
   h->FillRandom("gaus",3000);
   gStyle->SetOptFit();
   gStyle->SetOptStat(0);
   h->Fit("gaus");
   se->Update();

   // Retrieve the stat box
   TPaveStats *ps = (TPaveStats*)se->GetPrimitive("stats");
   ps->SetName("mystats");
   TList *listOfLines = ps->GetListOfLines();

   // Add a new line in the stat box.
   // Note that "=" is a control character
   TLatex *myt = new TLatex(0,0,"Test = 10");
   myt ->SetTextFont(42);
   myt ->SetTextSize(0.04);
   myt ->SetTextColor(kRed);
   listOfLines->Add(myt);
   // the following line is needed to avoid that the automatic redrawing of stats
   h->SetStats(0);
   se->Modified();
}

Yes that’s what I started from but as I say, either I can get the stats box drawn with the resolution at the end, or I can get the fit characteristics drawn, I can’t get the fit parameters with the resolution on the end

the example I sent you does that.

I don’t know what the difference is between what you sent and what I’m doing. What I’m doing isn’t giving me any stats at all

  gStyle->SetOptStat(1110);
  gStyle->SetOptFit(0001);

  TFile *_file6 = TFile::Open("/Users/bethlong/Downloads/Run660_AnalysisOutputFile.root");

  double x1=0.08;
  double x2=0.26;
  double y1=0.80;
  double y2=0.98;

  TCanvas *c53 = new TCanvas("c53","c53",900,700);
  TH1F* h1nsWindowEPVetoPlusESACRun660 = (TH1F*)_file6->Get("BremsstrahlungList/h1nsWindowEPVetoPlusESac");
  h1nsWindowEPVetoPlusESACRun660->Draw("HISTO");
  h1nsWindowEPVetoPlusESACRun660->SetTitle(";PositronEnergy+SACEnergy;#Events");
  h1nsWindowEPVetoPlusESACRun660->GetYaxis()->SetTitleOffset(1.1);
  h1nsWindowEPVetoPlusESACRun660->GetYaxis()->SetMaxDigits(3);

  gStyle->SetOptFit();
  gStyle->SetOptStat(0);
  
  TF1* f13 = new TF1("f13","gaus",420,470);
  h1nsWindowEPVetoPlusESACRun660->Fit(f13,"QR");
  f13->SetLineWidth(2);
  f13->Draw("sames");

  double Mean53 = f13->GetParameter("Mean");
  double Sigma53 = f13->GetParameter("Sigma");
  double Ratio53 = Sigma53/Mean53;
  TString Reso53 = Form("Resolution = %f%%",Ratio53*100);
  
  c53->SetRightMargin(0.015);
  c53->SetLeftMargin(0.073);
  c53->SetTopMargin(0.04);
  c53->SetBottomMargin(0.075);
  c53->SetBorderMode(1);
  c53->SetHighLightColor(0);
  c53->Modified();
  c53->Update();

  TPaveStats *s53 = (TPaveStats*)c53->GetPrimitive("stats");
  s53->SetName("53");

  TList *l53 = s53->GetListOfLines();
  TLatex *t53 = new TLatex(0,0,Reso53);
  t53 ->SetTextFont(42);
  t53 ->SetTextSize(0);
  l53->Add(t53);

  h1nsWindowEPVetoPlusESACRun660->SetStats(0); 
  //  s53->SetOptFit(1);  
  
  s53->SetX1NDC(x1+0.01);
  s53->SetX2NDC(x2+0.11);
  s53->SetY1NDC(y1-0.07);
  s53->SetY2NDC(y2-0.02);
  
  c53->Modified();
  c53->Update();
``
void mwe(){

   gStyle->SetOptStat(0);
   gStyle->SetOptFit();

   TH1F* h1 = new TH1F("h","h",500,200,700);
   TRandom3 *myRNG=new TRandom3();
   myRNG->SetSeed(1);

   for (int ii=0;ii<10000;ii++) {
      double rand = myRNG->Gaus(440,40);
      h1->Fill(rand);
   }

   TCanvas* c1 = new TCanvas("c1","c1",900,700);
   TF1* f11 = new TF1("f11","gaus",420,470);
   h1->Fit(f11);

   c1->Update();

   TPaveStats *s1 = (TPaveStats*)h1->FindObject("stats");
   s1->SetName("mystats");

   TList *l1      = s1->GetListOfLines();

   double MPV = f11->GetParameter("Mean");
   double Sigma = f11->GetParameter("Sigma");
   double Ratio = Sigma/MPV;
   TString Reso = Form("Resolution = %f%%",Ratio*100);
   TLatex *t1 = new TLatex(0,0,Reso);
   t1 ->SetTextFont(42);
   t1 ->SetTextSize(0);
   l1->Add(t1);

   // the following line is needed to avoid that the automatic redrawing of stats
   h1->SetStats(0);
   c1->Modified();
}

Ok so I needed this:

and this:

[quote=“couet, post:6, topic:55471”]

h1->SetStats(0);

Thank you

Yes simply do what this macro does. It is what can be found in the manual.

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