# Having problems fitting with multiple subranges

Hi,

As the title suggests, I’m having problems trying to fit custom functions on a histogram, based off the /fit/FittingDemo.c & /fit/Multifit.C tutorial scripts (I am using ROOT 5.34).

I am trying to plot a fit that sums two sigmoid functions. To do this I’m using a custom function that adds two sigmoid functions with different parameters, where the initial parameters used in the fitting are taken from two additional sigmoid fits, previously fitted over adjacent subranges.

There isn’t any problems with the two single sigmoid fits over the subranges, but when their parameters are given to the multiple sigmoid function to fit with, it always ends up with error, or never converges.

Here are the important bits of code:

``` //General Logistic Function Double_t Sigmoid(Double_t *x, Double_t *par) { return par[0] + (par[1] - par[0]) / pow(par[2] + par[3] * exp(-par[4] * x[0]),1 / par[5]); } //Combine Two General Logistic Function Double_t Sigmoid2(Double_t *x, Double_t *par) { return (Sigmoid(x, &par[0]) + Sigmoid(x, &par[6])); }```

[code] //Create Sigmoid Fit
TF1 *ForFit0 = new TF1(“ForFit0”, Sigmoid, -20, -3, 6);
TF1 *ForFit1 = new TF1(“ForFit1”, Sigmoid, -3, 20, 6);

``````TF1 *ForFitt = new TF1("ForFitt", Sigmoid2, -20, 20, 12);

Double_t par[12];

//Set Parameters & Fit Sigmoid Functions

ForFit0->SetParameters(0, 100, 0.001, 0.005, 0.5, 1.5); ForFit0->FixParameter(0, 0); ForFit0->FixParameter(2, 1); Forescatter->Fit("ForFit0","R");
ForFit1->SetParameters(0, 500, 0.001, 0.005, 0.5, 1.5); ForFit1->FixParameter(0, 0); ForFit1->FixParameter(2, 1); Forescatter->Fit("ForFit1","R+");

//Get Parameters

ForFit0->GetParameters(&par[0]);
ForFit1->GetParameters(&par[6]);

//Set Parameters & Fit Sigmoid Sum Function

ForFitt->SetParameters(par); ForFitt->FixParameter(0, 0); ForFitt->FixParameter(6, 0); ForFitt->FixParameter(2, 1); ForFitt->FixParameter(8, 1);

Forescatter->Fit(ForFitt, "R+");[/code]
``````

When running the script I usually get something like:

[quote] FCN=119.179 FROM MIGRAD STATUS=CONVERGED 1232 CALLS 1233 TOTAL
EDM=9.0723e-007 STRATEGY= 1 ERROR MATRIX UNCERTAINTY
1.4 per cent
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+000 fixed
2 p1 1.90653e+008 8.13973e+009 4.18957e+006 2.15502e-009
3 p2 1.00000e+000 fixed
4 p3 1.40755e+005 6.00936e+006 3.09305e+003 -2.92498e-006
5 p4 4.57169e-001 6.51741e-003 5.94084e-007 -2.01751e+000
6 p5 9.98161e-001 7.06383e-003 3.20432e-006 5.81286e+000
FCN=514.453 FROM MIGRAD STATUS=CONVERGED 776 CALLS 777 TOTAL
EDM=3.42893e-008 STRATEGY= 1 ERROR MATRIX UNCERTAINTY
1.3 per cent
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+000 fixed
2 p1 6.09965e+003 7.67073e+000 -8.32673e-003 5.00458e-005
3 p2 1.00000e+000 fixed
4 p3 3.50921e-001 3.11544e-002 3.60777e-005 -1.86754e-002
5 p4 4.67385e-001 5.50180e-003 5.70937e-006 1.87322e-001
6 p5 2.75749e-001 2.06667e-002 2.43730e-005 -6.87850e-003
Warning in : Abnormal termination of minimization.
FCN=119634 FROM MIGRAD STATUS=CALL LIMIT 5012 CALLS 5013 TOTAL
EDM=75.2994 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 13.2
per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+000 fixed
2 p1 7.23650e+006 1.07609e+004 -1.12233e+004 -7.01729e-002
3 p2 1.00000e+000 fixed
4 p3 1.49527e+005 7.20568e+001 -1.73343e+002 2.46048e+000
5 p4 4.31206e-001 5.47197e-005 1.66648e-004 -6.48028e+006
6 p5 1.41453e+000 4.19714e-004 -5.63656e-004 -1.11179e+006
7 p6 0.00000e+000 fixed
8 p7 -5.37648e+010 9.15677e+006 -1.14452e+007 -9.33529e-006
9 p8 1.00000e+000 fixed
10 p9 9.58418e-001 8.46179e-004 -5.93764e-003 -5.10069e+006
11 p10 3.15053e-002 9.14702e-006 3.10038e-006 8.54638e+007
12 p11 3.65622e-002 2.67768e-005 -1.84904e-004 1.65329e+008[/quote]

If I increease the call limit, It ends up failing:

[quote]Error: exp param[0]=3463.41 up:709 low:-1.#INF out of range C:\ROOT\macros\Sigmo
idFit.c(20)
Warning in : Abnormal termination of minimization.
FCN=1.15195e+006 FROM MIGRAD STATUS=FAILED 27848 CALLS 27849 TOTA
L
EDM=4.01418 STRATEGY= 1 ERROR MATRIX UNCERTAINTY 100.0
per cent
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 0.00000e+000 fixed
2 p1 3.66445e+006 7.40293e+001 0.00000e+000 -9.70996e-003
3 p2 1.00000e+000 fixed
4 p3 7.56937e+004 2.41334e+000 -0.00000e+000 3.10026e-001
5 p4 4.70701e-001 1.73924e-006 0.00000e+000 -4.32463e+005
6 p5 1.44962e+000 1.53492e-005 0.00000e+000 -3.15228e+004
7 p6 0.00000e+000 fixed
8 p7 -5.02165e+010 1.02740e+006 0.00000e+000 -7.29181e-007
9 p8 1.00000e+000 fixed
10 p9 4.98331e-003 8.97328e-009 0.00000e+000 -7.78178e+007
11 p10 2.67309e-002 9.81160e-008 -0.00000e+000 7.90390e+006
12 p11 2.67758e-004 4.81406e-010 -0.00000e+000 1.45049e+009
(class TFitResultPtr)71709808
*** Interpreter error recovered ***[/quote]

Here is what the fit looks like:

I’m at a loss at what to do. I’ve tried fiddling around with the initial parameters, disabled FixParameters, to no avail. Is there anything I can do?

Hi,

I don’t understand fully your problem You would like to to a combined fit of f1 +f2 ? But I am not sure this will work. In the first fit the two sigmoid functions are defined only in their restricted range, while in the combined in the full range.
You might need to think on how to define correctly the fit function.

Lorenzo