// Hi brun, // this is a program to fit a 2D graph with legendre polynomial. // i have a problem. // you know, the function have 24 parameters, so i construct a 2F // function to fit it: //--------------- TF2 *f2=new TF2("fit",fit_f,4,150,0,180,24); // and fit : f->Fit("fit","E"); // the resule is only p0 and p1 is not zero. // But I found if i change TF2 into: //--------------- TF2 *f2=new TF2("fit",fit_f,4,150,0,180,25); // then fit it, i can get the result ,and the FCN is about 100. // I did not know why? //---------this program is to draw the 2D graph of en ,angle,cs ,cserror //-------and fit them with legendre #include #include #include #include #include #include #include #include #include //----------first ,construct the function using to fit //===================================================================== // the sub-function 0 Double_t fit_0( Double_t *x, Double_t *par) { Double_t fitval=par[0]*TMath::Exp(par[1]*x[1])+par[2]*TMath::Exp(par[3]*x[0])+(par[4]+par[5]*x[0])/(1+par[6]*(x[0]-par[7])*(x[0]-par[7])); return fitval; } //====================================================================== // the sub-function 1 Double_t fit_1( Double_t *x, Double_t *par) { Double_t fitval=(par[0]*TMath::Exp(par[1]*x[0])+par[2]*TMath::Exp(par[3]*x[0]))*ROOT::Math::assoc_legendre(1,0,cos(x[1]*TMath::Pi()/180)); return fitval; } //====================================================================== // the sub-function 2 Double_t fit_2( Double_t *x, Double_t *par) { Double_t fitval=(par[0]*TMath::Exp(par[1]*x[0])+par[2]*TMath::Exp(par[3]*x[0]))*ROOT::Math::assoc_legendre(2,0,cos(x[1]*TMath::Pi()/180)); return fitval; } //====================================================================== // the sub-function 3 Double_t fit_3( Double_t *x, Double_t *par) { Double_t fitval=(par[0]*TMath::Exp(par[1]*x[0])+par[2]*TMath::Exp(par[3]*x[0]))*ROOT::Math::assoc_legendre(3,0,cos(x[1]*TMath::Pi()/180)); return fitval; } //====================================================================== // the sub-function 4 Double_t fit_4( Double_t *x, Double_t *par) { Double_t fitval=(par[0]*TMath::Exp(par[1]*x[0])+par[2]*TMath::Exp(par[3]*x[0]))*ROOT::Math::assoc_legendre(4,0,cos(x[1]*TMath::Pi()/180)); return fitval; } //====================================================================== //sum of the fit function Double_t fit_f( Double_t *x, Double_t *par) { Double_t fitval=fit_3(x,&par[16])+fit_2(x,&par[12])+ fit_1(x,&par[8])+fit_0(x,par)+fit_4(x,&par[20]); ; return fitval; } //============================================================== //-------------------------------------------------------------- // construct the main function //-------------------------------------------------------------- //============================================================== void fit2Dg_of_diff() { int counter=71; // number of data point Double_t en[71],angle[71],cs[71],cserror[71]; Double_t ex[71]={0.0}; Double_t ey[71]={0.0}; double zero; int i; gSystem->Load("libMathMore.dll"); gStyle->SetOptFit(1111); // open the data file and read them ifstream alldata("c:\\root\\3he\\all_normalize_data\\all_diff_data.txt"); if(!alldata) cout<<"No file"<>en[i]>>angle[i]>>cs[i]>>zero>>cserror[i]; cout<Draw("P0 TRI1"); //fit TF2 *f2=new TF2("fit",fit_f,4,150,0,180,25); f->Fit("fit","E"); }