gStyle->SetOptFit(1111) couldn't display all the fit parameter

#include <TStyle.h>
#include <TH2.h>
#include <TSystem.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TCut.h>
#include <TString.h>
#include <TGraph.h>
#include <TF1.h>
#include <TMath.h>
#include <iostream>
#include <TFile.h>
#include <TH1.h>
# include<TPaveStats.h>

double function1(double *x, double *p){ 
return p[0]*TMath::Gaus(x[0],p[1],p[2])+p[3]*TMath::Gaus(x[0],p[4],p[5])+x[0]*p[6];
}



void fit_script(){
Int_t nbins=110;
Double_t xmin=0;
Double_t xmax=20;
Double_t mean1=7.5;
Double_t sigma1=1.2;
Double_t mean2=11;
Double_t sigma2=1.5;

TCanvas *c1 = new TCanvas("canvas", "Histograms", 800, 600);
gStyle->SetOptStat(1);
gStyle->SetOptFit(1111);
gStyle->SetStatY(0.9);
gStyle->SetStatX(0.35);
gStyle->SetStatW(0.13);
gStyle->SetStatH(0.11);


char formula1[100];
sprintf(formula1, "TMath::Gaus(x,%f,%f)", mean1, sigma1);

char formula2[100];
sprintf(formula2, "TMath::Gaus(x,%f,%f)", mean2, sigma2);

// 创建TF1对象
TF1 *gaus1 = new TF1("gaus1", formula1, xmin, xmax);
TF1 *gaus2 = new TF1("gaus2", formula2, xmin, xmax);
TF1 *bg1 = new TF1("bg1","8*x",xmin,xmax);

TF1* g1 = new TF1("g1","gaus",0,10);
TF1* g2 = new TF1("g2","gaus",9,14);
TF1 *b3 = new TF1("b3","x*[0]",16,20);



TF1* total = new TF1("mstotal",function1,0,20,7);

TH1D* gauss1=new TH1D("gauss1","gauss1",nbins,xmin,xmax);
TH1D* gauss2=new TH1D("gauss2","gauss2",nbins,xmin,xmax);
TH1D* bg=new TH1D("bg","bg",nbins,xmin,xmax);
TH1D* hs=new TH1D("hs","hs",nbins,xmin,xmax);

gauss1->FillRandom("gaus1", 3500);
gauss2->FillRandom("gaus2", 3300);
bg->FillRandom("bg1", 3200);

TList *list = new TList;
list->Add(gauss1);
list->Add(gauss2);
list->Add(bg);
hs->Merge(list);

hs->SetLineColor(kRed);
gauss1->SetLineColor(kBlue);
gauss2->SetLineColor(kGreen);
bg->SetLineColor(kBlack);


hs->Draw();
gauss1->Draw("same");
gauss2->Draw("same");
bg->Draw("same");
Double_t par[7];
// Fit each function
hs->Fit(g1,"R0");
hs->Fit(g2,"R0+");
hs->Fit(b3,"R0+");
g1->GetParameters(&par[0]);
g2->GetParameters(&par[3]);
b3->GetParameters(&par[6]);

// Use the parameters on the sum
total->SetParameters(par);
total-> SetParName(0, "amplitude1"); 
total-> SetParName(1, "mean1"); 
total-> SetParName(2, "sigma1"); 
total-> SetParName(3, "amplitude2"); 
total-> SetParName(4, "mean2"); 
total-> SetParName(5, "sigma2"); 
total-> SetParName(6, "line_par"); 

hs->Fit(total,"R+");
c1->Update();

}

which couldn’t display all the fit parameter,but some gaussian parameters?

Welcome to the ROOT forum

#include <TStyle.h>
#include <TH2.h>
#include <TSystem.h>
#include <TChain.h>
#include <TCanvas.h>
#include <TCut.h>
#include <TString.h>
#include <TGraph.h>
#include <TF1.h>
#include <TMath.h>
#include <iostream>
#include <TFile.h>
#include <TH1.h>
# include<TPaveStats.h>

double function1(double *x, double *p) {
   return p[0]*TMath::Gaus(x[0],p[1],p[2])+p[3]*TMath::Gaus(x[0],p[4],p[5])+x[0]*p[6];
}

void fit_script(){
   Int_t nbins=110;
   Double_t xmin=0;
   Double_t xmax=20;
   Double_t mean1=7.5;
   Double_t sigma1=1.2;
   Double_t mean2=11;
   Double_t sigma2=1.5;

   auto canvas = new TCanvas("canvas", "Histograms", 800, 600);
   gStyle->SetOptFit(11111111);
   gStyle->SetStatY(0.9);
   gStyle->SetStatX(0.35);
   gStyle->SetStatW(0.13);
   gStyle->SetStatH(0.11);

   // 创建TF1对象
   auto gaus1 = new TF1("gaus1", Form("TMath::Gaus(x,%f,%f)", mean1, sigma1), xmin, xmax);
   auto gaus2 = new TF1("gaus2", Form("TMath::Gaus(x,%f,%f)", mean2, sigma2), xmin, xmax);
   auto bg1   = new TF1("bg1","8*x",xmin,xmax);

   auto g1    = new TF1("g1","gaus",0,10);
   auto g2    = new TF1("g2","gaus",9,14);
   auto b3    = new TF1("b3","x*[0]",16,20);
   auto total = new TF1("mstotal",function1,0,20,7);

   auto gauss1 = new TH1D("gauss1","gauss1",nbins,xmin,xmax);
   auto gauss2 = new TH1D("gauss2","gauss2",nbins,xmin,xmax);
   auto bg     = new TH1D("bg","bg",nbins,xmin,xmax);
   auto hs     = new TH1D("hs","hs",nbins,xmin,xmax);

   gauss1->FillRandom("gaus1", 3500);
   gauss2->FillRandom("gaus2", 3300);
   bg->FillRandom("bg1", 3200);

   auto list = new TList;
   list->Add(gauss1);
   list->Add(gauss2);
   list->Add(bg);
   hs->Merge(list);

   hs->SetLineColor(kRed);
   gauss1->SetLineColor(kBlue);
   gauss2->SetLineColor(kGreen);
   bg->SetLineColor(kBlack);

   Double_t par[7];
   // Fit each function
   hs->Fit(g1,"R0");
   hs->Fit(g2,"R0+");
   hs->Fit(b3,"R0+");
   g1->GetParameters(&par[0]);
   g2->GetParameters(&par[3]);
   b3->GetParameters(&par[6]);

   // Use the parameters on the sum
   total->SetParameters(par);
   total->SetParName(0, "amplitude1");
   total->SetParName(1, "mean1");
   total->SetParName(2, "sigma1");
   total->SetParName(3, "amplitude2");
   total->SetParName(4, "mean2");
   total->SetParName(5, "sigma2");
   total->SetParName(6, "line_par");

   hs->Fit(total);
   gauss1->Draw("same");
   gauss2->Draw("same");
   bg->Draw("same");
}

Hey, thanks a lot. But I dont understand that, the ‘+’ option means “Adds this new fitted function to the list of fitted functions. By default, the previous function is deleted and only the last one is kept.” but it seems that the first fit “g1” is saved and the statbox only display ‘g1’ fit parameter if i use this ‘hs->Fit(total,“R+”);’

@moneta will know better about the fitting options.

Hi,
The option + is used to store in the histogram the fitted funciton, with our deleting the previous one. For the statistic box is different, @couet can maybe comment on this, but when fitting multiple functions only the first fit is reported in the stat box

Lorenzo

Yes, with in the previous example the fit box "sees"the parameters produced by hs->Fit(total);

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