{ auto c41 = new TCanvas("c41","c41",200,10,600,300);c41->Divide(2,1);double x[]= {0,1,2,3,4};double y[]= {0,2,4,1,3};double ex[]= {0.1,0.2,0.3,0.4,0.5};double ey[]= {1,0.5,1,0.5,1}; auto ge = new TGraphErrors(5, x, y, ex, ey);ge->SetTitle("Errors as a band");ge->SetFillColor(4);ge->SetFillStyle(3010);c41->cd(1);ge->Draw("a3");c41->cd(2);ge->SetFillColor(6);ge->SetFillStyle(3005);ge->Draw("a4");}
#include<fstream>intfit(){ TGraph *gr = new TGraph("data1.txt");double pi=3.14159265; TF1 *f1 = new TF1("f1","[0]*sin([1]*pi*x)*exp([2]*x)+[3]",0,10);f1->SetParameter(0,1); // 第零个参数的参考值f1->SetParameter(1,1);f1->SetParameter(2,-1);f1->SetParameter(3,0.5);// be equal in value with below// fi->SetParameters(1,1,-1,0.5);gr->Fit("f1");gr->Draw();}
voidmultigraph(){gStyle->SetOptFit(); auto c1 = new TCanvas("c1","multigraph",700,500);c1->SetGrid();// draw a frame to define the range auto mg = new TMultiGraph();// create first graphconstInt_t n1 =10;Double_t px1[]= {-0.1,0.05,0.25,0.35,0.5,0.61,0.7,0.85,0.89,0.95};Double_t py1[]= {-1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};Double_t ex1[]= {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};Double_t ey1[]= {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8}; auto gr1 = new TGraphErrors(n1,px1,py1,ex1,ey1);gr1->SetMarkerColor(kBlue);gr1->SetMarkerStyle(21);gr1->Fit("gaus","q"); auto func1 = (TF1 *) gr1->GetListOfFunctions()->FindObject("gaus");func1->SetLineColor(kBlue);mg->Add(gr1);// create second graphconstInt_t n2 =10;Float_t x2[]= {-0.28,0.005,0.19,0.29,0.45,0.56,0.65,0.80,0.90,1.01};Float_t y2[]= {2.1,3.86,7,9,10,10.55,9.64,7.26,5.42,2};Float_t ex2[]= {.04,.12,.08,.06,.05,.04,.07,.06,.08,.04};Float_t ey2[]= {.6,.8,.7,.4,.3,.3,.4,.5,.6,.7}; auto gr2 = new TGraphErrors(n2,x2,y2,ex2,ey2);gr2->SetMarkerColor(kRed);gr2->SetMarkerStyle(20);gr2->Fit("pol5","q"); auto func2 = (TF1 *) gr2->GetListOfFunctions()->FindObject("pol5");func2->SetLineColor(kRed);func2->SetLineStyle(2);mg->Add(gr2);mg->Draw("ap");//force drawing of canvas to generate the fit TPaveStatsc1->Update(); auto stats1 = (TPaveStats*) gr1->GetListOfFunctions()->FindObject("stats"); auto stats2 = (TPaveStats*) gr2->GetListOfFunctions()->FindObject("stats");if (stats1 && stats2) {stats1->SetTextColor(kBlue);stats2->SetTextColor(kRed);stats1->SetX1NDC(0.12); stats1->SetX2NDC(0.32); stats1->SetY1NDC(0.82);stats2->SetX1NDC(0.72); stats2->SetX2NDC(0.92); stats2->SetY1NDC(0.75);c1->Modified(); }}
含本底拟合
E.g. 含本底拟合
#include<Fit/Fitter.h>#include<Fit/BinData.h>#include<Fit/Chi2FCN.h>#include<TH1.h>#include<Math/WrappedMultiTF1.h>#include<HFitInterface.h>#include<TCanvas.h>#include<TStyle.h>// definition of shared parameter// background functionint iparB[2] = {0, // exp amplitude in B histo2 // exp common parameter};// signal + background functionint iparSB[5] = {1, // exp amplitude in S+B histo2, // exp common parameter3, // Gaussian amplitude4, // Gaussian mean5 // Gaussian sigma};// Create the GlobalCHi2 structurestruct GlobalChi2 {GlobalChi2(ROOT::Math::IMultiGenFunction &f1, ROOT::Math::IMultiGenFunction &f2) : fChi2_1(&f1),fChi2_2(&f2) {}// parameter vector is first background (in common 1 and 2)// and then is signal (only in 2)doubleoperator()(constdouble*par) const {double p1[2];for (int i =0; i <2; ++i) p1[i] = par[iparB[i]];double p2[5];for (int i =0; i <5; ++i) p2[i] = par[iparSB[i]];return (*fChi2_1)(p1) + (*fChi2_2)(p2); }const ROOT::Math::IMultiGenFunction *fChi2_1;const ROOT::Math::IMultiGenFunction *fChi2_2;};voidcombinedFit(){ TH1D *hB = new TH1D("hB","histo B",100,0,100); TH1D *hSB = new TH1D("hSB","histo S+B",100,0,100); TF1 *fB = new TF1("fB","expo",0,100);fB->SetParameters(1,-0.05);hB->FillRandom("fB"); TF1 *fS = new TF1("fS","gaus",0,100);fS->SetParameters(1,30,5);hSB->FillRandom("fB",2000);hSB->FillRandom("fS",1000);// perform now global fit TF1 *fSB = new TF1("fSB","expo + gaus(2)",0,100); ROOT::Math::WrappedMultiTF1 wfB(*fB,1); ROOT::Math::WrappedMultiTF1 wfSB(*fSB,1); ROOT::Fit::DataOptions opt; ROOT::Fit::DataRange rangeB;// set the data rangerangeB.SetRange(10,90); ROOT::Fit::BinData dataB(opt, rangeB);ROOT::Fit::FillData(dataB, hB); ROOT::Fit::DataRange rangeSB;rangeSB.SetRange(10,50); ROOT::Fit::BinData dataSB(opt, rangeSB);ROOT::Fit::FillData(dataSB, hSB); ROOT::Fit::Chi2Function chi2_B(dataB, wfB); ROOT::Fit::Chi2Function chi2_SB(dataSB, wfSB); GlobalChi2 globalChi2(chi2_B, chi2_SB); ROOT::Fit::Fitter fitter;constint Npar =6;double par0[Npar] = {5,5,-0.1,100,30,10};// create before the parameter settings in order to fix or set range on themfitter.Config().SetParamsSettings(6, par0);// fix 5-th parameterfitter.Config().ParSettings(4).Fix();// set limits on the third and 4-th parameterfitter.Config().ParSettings(2).SetLimits(-10,-1.E-4);fitter.Config().ParSettings(3).SetLimits(0,10000);fitter.Config().ParSettings(3).SetStepSize(5);fitter.Config().MinimizerOptions().SetPrintLevel(0);fitter.Config().SetMinimizer("Minuit2","Migrad");// fit FCN function directly// (specify optionally data size and flag to indicate that is a chi2 fit)fitter.FitFCN(6, globalChi2, nullptr,dataB.Size() +dataSB.Size(),true); ROOT::Fit::FitResult result =fitter.Result();result.Print(std::cout); TCanvas *c1 = new TCanvas("Simfit","Simultaneous fit of two histograms",10,10,700,700);c1->Divide(1,2);c1->cd(1);gStyle->SetOptFit(1111);fB->SetFitResult(result, iparB);fB->SetRange(rangeB().first, rangeB().second);fB->SetLineColor(kBlue);hB->GetListOfFunctions()->Add(fB);hB->Draw();c1->cd(2);fSB->SetFitResult(result, iparSB);fSB->SetRange(rangeSB().first, rangeSB().second);fSB->SetLineColor(kRed);hSB->GetListOfFunctions()->Add(fSB);hSB->Draw();}
拟合误差通常以各种统计指标来表示,比如残差平方和(residual sum of squares)、平均绝对误差(mean absolute error)、均方根误差(root mean square error)等。
E.g. 拟合误差棒和拟合误差带
#include"TGraphErrors.h"#include"TGraph2DErrors.h"#include"TCanvas.h"#include"TF2.h"#include"TH1.h"#include"TVirtualFitter.h"#include"TRandom.h"voidConfidenceIntervals(){ TCanvas *myc = new TCanvas("myc","Confidence intervals on the fitted function",1000,500);myc->Divide(3,1);//### 1. A graph//Create and fill a graphint ngr =100; TGraph *gr = new TGraph(ngr);gr->SetName("GraphNoError");double x, y;int i;for (i=0; i<ngr; i++){ x =gRandom->Uniform(-1,1); y =-1+2*x +gRandom->Gaus(0,1);gr->SetPoint(i, x, y); }//Create the fitting function TF1 *fpol = new TF1("fpol","pol1",-1,1);fpol->SetLineWidth(2);gr->Fit(fpol,"Q"); /*Create a TGraphErrors to hold the confidence intervals*/ TGraphErrors *grint = new TGraphErrors(ngr);grint->SetTitle("Fitted line with .95 conf. band");for (i=0; i<ngr; i++)grint->SetPoint(i,gr->GetX()[i],0); /*Compute the confidence intervals at the x points of the created graph*/ (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint);//Now the "grint" graph contains function values as its y-coordinates//and confidence intervals as the errors on these coordinates//Draw the graph, the function and the confidence intervalsmyc->cd(1);grint->SetLineColor(kRed);grint->Draw("ap");gr->SetMarkerStyle(5);gr->SetMarkerSize(0.7);gr->Draw("psame");//### 2. A histogrammyc->cd(2);//Create, fill and fit a histogramint nh=5000; TH1D *h = new TH1D("h","Fitted Gaussian with .95 conf.band",100,-3,3);h->FillRandom("gaus", nh); TF1 *f = new TF1("fgaus","gaus",-3,3);f->SetLineWidth(2);h->Fit(f,"Q");h->Draw(); /*Create a histogram to hold the confidence intervals*/ TH1D *hint = new TH1D("hint","Fitted Gaussian with .95 conf.band",100,-3,3); (TVirtualFitter::GetFitter())->GetConfidenceIntervals(hint,0.68);//Now the "hint" histogram has the fitted function values as the//bin contents and the confidence intervals as bin errorshint->SetStats(false);hint->SetFillColor(2);hint->Draw("e3 same");//### 3. A 2d graph//Create and fill the graphint ngr2 =100;double z, rnd, e=0.3; TGraph2D *gr2 = new TGraph2D(ngr2);gr2->SetName("Graph2DNoError"); TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+250",-6,6,-6,6);f2->SetParameters(1,1);for (i=0; i<ngr2; i++){f2->GetRandom2(x,y);// Generate a random number in [-e,e] rnd =2*gRandom->Rndm()*e-e; z =f2->Eval(x,y)*(1+rnd);gr2->SetPoint(i,x,y,z); }//Create a graph with errors to store the intervals TGraph2DErrors *grint2 = new TGraph2DErrors(ngr2);for (i=0; i<ngr2; i++)grint2->SetPoint(i,gr2->GetX()[i],gr2->GetY()[i],0);//Fit the graphf2->SetParameters(0.5,1.5);gr2->Fit(f2,"Q"); /*Compute the confidence intervals*/ (TVirtualFitter::GetFitter())->GetConfidenceIntervals(grint2);//Now the "grint2" graph contains function values as z-coordinates//and confidence intervals as their errors//drawmyc->cd(3);f2->SetNpx(30);f2->SetNpy(30);f2->SetFillColor(kBlue);f2->Draw("surf4");grint2->SetNpx(20);grint2->SetNpy(20);grint2->SetMarkerStyle(24);grint2->SetMarkerSize(0.7);grint2->SetMarkerColor(kRed);grint2->SetLineColor(kRed);grint2->Draw("E0 same");grint2->SetTitle("Fitted 2d function with .95 error bars");myc->cd();}
拟合框
设置直方图拟合框TStyle::SetOptFit(pcev)。
p = 1;打印概率
c = 1; 打印卡方/自由度数
e = 1; 打印误差(如果 e = 1,v 必须为 1)
v = 1;打印参数名称/值
By default, for each bin, the sum of weights is computed at fill time. You can also call TH1::Sumw2() to force the storage and computation of the sum of the square of weights per bin. If Sumw2() has been called, the error per bin is computed as the sqrt(sum of squares of weights). Otherwise, the error is set equal to the `sqrt(bin content).
To return the error for a given bin number, use:
double error = h->GetBinError(bin);
Empty bins are excluded in the fit when using the Chi-square fit method. When fitting an histogram representing counts (that is with Poisson statistics) it is recommended to use the Log-Likelihood method (option L or WL), particularly in case of low statistics.
参数返回
// TFitResultPtr contains only the fit status.
int fitStatus = hist->Fit(myFunction);
// TFitResultPtr contains the TFitResult.
TFitResultPtr r = hist->Fit(myFunction,"S");
// Access the covariance matrix.
TMatrixDSym cov = r->GetCovarianceMatrix();
// Retrieve the fit chi2.
double chi2 = r->Chi2();
// Retrieve the value for the parameter 0.
double par0 = r->Parameter(0);
// Retrieve the error for the parameter 0.
double err0 = r->ParError(0);
// Print the full information of the fit including covariance matrix.
r->Print("V");
// Store the result in a ROOT file.
r->Write();