Improve fit by trigonometric function

I need to fit around 10^5 TGraph that are basically trigonometric functions with different amplitude and phase. I am specifically interested in the frequency. For that, I define:

TF1 *Cosine = new TF1("Cosine", "[0]*cos([1]*x*1.e6 + [2])", 0., tmax)

and then before the fit

Cosine->SetParameters(1., omega_0*1.e-6, 0.); 
Cosine->SetParLimits(0, 0., 1.);
Cosine->SetParLimits(2, -M_PI/2, M_PI/2);

I fit with the option M:

gr_Projection->Fit(“Cosine”, “M”);

omega_0 is around 1.5, and most particles give a reasonable fit, but some of them give something completely wrong, like this one:

Minimizer is Minuit / MigradImproved
Chi2 = 4678.99
NDf = 9997
Edm = 1.61618e-08
NCalls = 129
p0 = 0.216015 +/- 0.00963817 (limited)
p1 = 2.3494 +/- 0.00935233
p2 = -0.0734677 +/- 0.0731648 (limited)

If I look at the plot of this particle that I am trying to fit, I can see easily that the amplitude is very close to 1, and the frequency is similar to omega_0, but for some reason, the result of the fit is not good (Chi2 is very large also).

Is there a way to improve this result, maybe forcing the minimizer to do more iterations?

Try with “WM” fit options.

BTW. I assume, before every fit, you always “SetParameters” (so that every fit “starts” from well defined reasonable initial values).

I tried the “WM” option, but I keep getting similar results.

And yes, I always set the parameters before each fit.

I guess you would need to attach one or two graphs which fail so that one can try to fit them.

Sorry, I think It would be very complicated to reproduce the graphs that I am trying to fit. I tried the verbose option, because I realized that some fits gave me the error " Invalid FitResult (status = 4 )".

The error messages are now too long, and I don’t think I need to copy the entire thing here. However, it is interesting to notice the lines:


As I suspected, for some particles, the Minimizer does not converge. I believe that my initial guess for the parameters, as well as the limits, is accurate (most of the particles have a nice fit).

How can I force TMinuit to converge, even if it needs more computing time? I have tried with

TVirtualFitter::SetMaxIterations( 100000 );

as suggested in other forum questions, but it doesn’t seem to change significantly. Please let me know if you need more information from the verbose output.