About: Abnormal termination of minimization


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.

If you use a smaller initial value for the first parameter and remove the ParLimits, the fit converges, e.g. doing this:

   Double_t f2params[npar] = {2633,2.2794,0.05,0.1};  // up to par[0] ~ 12900 seems to still work
   //...
/*
   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);
*/
  //...

I’ll let you check if the fit is good, and also leave the explanation to the experts (I can be very wrong here but it seems like the problem could be a combination of: your initial values being too close to the real ones, and the large difference in scale between par[0] and the others).

1 Like

OK, thank you for your reply.
And I check the fit result-------if the data is the sample by:

Int_t nentries = 1E7;
h2->FillRandom(“f2”,nentries);

the fit result is very well.

But if the sample is set by acceptance-rejection sampling, the fit value has a large deviation from true value when one don’t set the Parameters Limits.
So it still puzzles me…

What do you mean exactly with

are you sure you are sampling your distribution correctly ?

Lorenzo

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