Hi, Rooters,
My code is about the two-dimension fit, where use my custom function fun2 to fit TH2F *h2
And there are four parameters. I don’t know why the fit result is always status:FAILED.
Can you help me to find the wrong of my code?
My code is here:
#include "TF2.h"
#include "TH2.h"
#include "TMath.h"
Double_t fun2(Double_t *x, Double_t *par) {
//par[0]=N kappa = par[1] longp = par[2] trans = par[3]
double re = 0.665/(8*TMath::Pi()/3);
double dsig0 = re/(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]));
double dlongi = re*par[2]/(par[1]*par[1]*pow((1+x[0]),3))*x[0]*(x[0]+2)*(par[1]-2*x[0]);
double dtrans = -par[3]*re/(par[1]*par[1]*pow((1+x[0]),3))*2*x[0]*sqrt(x[0]*(par[1]-x[0]))*TMath::Sin(x[1]);
return par[0]*(dsig0+dlongi+dtrans);
// return par[0]*dsig0;
}
void fit2() {
//gStyle->SetOptFit("1111");
const Int_t npar = 4;
Double_t f2params[npar] = {26330,2.2794,0.05,0.1};
TF2 *f2 = new TF2("f2",fun2,0,2.2794,0,2*TMath::Pi(), npar);
f2->SetParameters(f2params);
f2->SetParLimits(0,25000,28000);
f2->SetParLimits(1,2.268,2.29);
f2->SetParLimits(2,0.045,0.055);
f2->SetParLimits(3,0.095,0.103);
//Create an histogram and fill it randomly with f2
TH2F *h2 = new TH2F("h2","from f2",200,0,2.2794,100,0,2*TMath::Pi());
Int_t nentries = 1E7;
h2->FillRandom("f2",nentries);
h2->GetXaxis()->SetTitle("X Axis");
h2->GetYaxis()->SetTitle("Y Axis");
//ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(1000000);
//ROOT::Math::MinimizerOptions::SetDefaultTolerance(1);
h2->Fit("f2","V");
h2->Draw("SURF2");
//f2->Draw("SURF same");
cout<<"Inegral of f2: "<<f2->Integral(0,2.2794,0,2*TMath::Pi())<<endl;
}
ROOT Version: 6.20/02
Best Regards,
hurricane.