Fit Statistics Box doesnot work

I am trying to plot the TF2D and fit the TH1D_ProjectionX histogram to a TF1 function . I have set the gStyleOpt(1111). The code work fine, but the fit statistics do not seem to appear. Below is my code,

#include <TH2F.h>
#include <fstream>
#include <iostream>

 
 //Define fit function
Double_t fitf(Double_t *x, Double_t *par)
  {
    Double_t re = 0.665*8*TMath::Pi()/3;
    Double_t theta0 = 0.003747414496;
    Double_t eenergy = 120*1E9;
    Double_t emass = 0.511*1E6;
    Double_t ga = eenergy/emass;
    
    Double_t dsig0 = re*par[0]/(par[1]*par[1]*pow((1+x[0]),3))*(par[1]*(1+pow((1+x[0]),2))-4*x[0]/par[1]*(1+x[0])*(par[1]-x[0]));
     
    return dsig0;
  }

void test(){
    gStyle->SetOptFit(1111);

    double a,b,w;
    double yup = 2*TMath::Pi();
    TH2D *h2 = new TH2D("h2","h2",50,0,2.28,50,0,yup);
	
    ifstream in1("result1e3.txt");
     if(in1.is_open())
    	 {
         int i=0;
         while(!in1.eof())
          {
             in1 >> a >> b >> w; 
             h2->Fill(a,b,w);	
	           i++;
	        }
	       in1.close(); 
       }


	//projection 
	TH1D *h2_px = h2->ProjectionX();

   TCanvas *c1 = new TCanvas("c1","c1",10,10,480,600);
   c1->Divide(1,2);
   c1->cd(1);
   h2->Draw("lego2 0");
    c1->cd(2);

//fit	
   TF1 *func = new TF1("func",fitf,0,2.2794,2);
   func->SetParameter(0,8000);
   func->SetParameter(1,2.2794);
   func->SetParLimits(1,2.27,2.31);
   func->SetParNames("Constant","#kappa");
   
   h2_px->Fit(func);
   func->Draw();
   h2_px->Draw("histsame");


//GET result 
	TF1 *fitre  = h2_px->GetFunction("func");
	Double_t chi2 = fitre->GetChisquare();
	Double_t par1 = fitre->GetParameter(1);
	Double_t err1 = fitre->GetParError(1);
        cout<< "the chi2 is equal "<< chi2 << endl; 
        cout<< "the value is equal "<< par1 << endl; 
        cout<< "the error is equal "<< err1 << endl; 
  
 }

result1e3.txt (264.8 KB)

ROOT Version: 6.20
Platform: win 10Preformatted text
Compiler: Not Provided


Try drawing func after the histogram, not before.

   h2_px->Fit(func);
   h2_px->Draw("hist");
   func->Draw("same");

Thank you very much!
It works well!

Best regards,
hurricane1127

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