# Problem with 2D fit

Hello root users,
I’m trying to perform a 2D fit of a TH2F (or a TGraph2DErrors, I get the same results).
My points lie in a certain region in the x-y plane, around the bisector, so I have defined the limits of my function as a rectangle. I’m trying to fit my points with a plane but I always get an error matrix accurate at 50% and parameters value which are consistent with the xy plane, like if the fit is ignoring my points. I guess the problem is that I should tell to the function to fit just where the bin are not-zero, but I don’t know how to require it.
Here you have the main piece of my code:

void fit2D(){
TH2F *h=new TH2F(“h”,“h”,23,0,22,18,0,17);
h->SetMinimum(-5);
h->SetBinContent(0,1,-0.487812);
h->SetBinError(0,1,0.00771375);
h->SetBinContent(1,1,0.0114149);
h->SetBinError(1,1,0.0123048);
h->SetBinContent(2,1,0.540351);
h->SetBinError(2,1,0.0182454);
h->SetBinContent(1,2,-0.614683);
h->SetBinError(1,2,0.0256728);
h->SetBinContent(2,2,-0.0707187);
h->SetBinError(2,2,0.00701989);

h->Draw(“CONTZ”);
Double_t delta=0.0427394;

TF2 f2d=new TF2(“f2d”,"[0]([1]x+[2]y)",“cutg”);//,0,22,0,17);
f2d->SetParName(1,“alfa”);
f2d->SetParName(2,“beta”);
f2d->SetParLimits(1,0,100
delta);
f2d->SetParLimits(2,-100
delta,0.);
h->Fit(f2d,“M”);
}

The expected values of my parameters are alpha ~ -beta and alpha ~ 0.7
Where is the mistake?

Thank you very much for your help
Cheers
Paola

Paola,

Could you post a short running script reproducing your problem?
In your code snippet the call to the TF2 constructor is wrong.

Rene

Hi Rene,
sorry, I was trying to define my function in a limited region and I forgot to paste here the previuos version.
Now it should work:

#include <stdio.h>
#include <Riostream.h>
#include <iostream.h>
#include <TF2.h>
#include <TH2.h>
void fit2D(){
TH2F *h=new TH2F(“h”,“h”,23,0,22,18,0,17);
h->SetMinimum(-5);
h->SetBinContent(0,1,-0.487812);
h->SetBinError(0,1,0.00771375);
h->SetBinContent(1,1,0.0114149);
h->SetBinError(1,1,0.0123048);
h->SetBinContent(2,1,0.540351);
h->SetBinError(2,1,0.0182454);
h->SetBinContent(1,2,-0.614683);
h->SetBinError(1,2,0.0256728);
h->SetBinContent(2,2,-0.0707187);
h->SetBinError(2,2,0.00701989);
h->SetBinContent(4,2,1.32414);
h->SetBinError(4,2,0.0663372);
h->SetBinContent(3,3,-0.107608);
h->SetBinError(3,3,0.0428034);
h->SetBinContent(4,3,0.335772);
h->SetBinError(4,3,0.0395426);
h->SetBinContent(3,4,-0.765134);
h->SetBinError(3,4,0.0580094);
h->SetBinContent(6,2,2.92252);
h->SetBinError(6,2,0.391068);
h->SetBinContent(5,3,0.950505);
h->SetBinError(5,3,0.0780463);
h->SetBinContent(6,3,1.60981);
h->SetBinError(6,3,0.232201);
h->SetBinContent(5,4,0.393094);
h->SetBinError(5,4,0.0565881);
h->SetBinContent(4,5,-0.902492);
h->SetBinError(4,5,0.655065);
h->SetBinContent(6,4,0.932567);
h->SetBinError(6,4,0.0754298);
h->SetBinContent(5,5,-0.163799);
h->SetBinError(5,5,0.0525497);
h->SetBinContent(7,4,1.62972);
h->SetBinError(7,4,0.297529);
h->SetBinContent(6,5,0.334462);
h->SetBinError(6,5,0.0416216);
h->SetBinContent(5,6,-0.840158);
h->SetBinError(5,6,0.0714593);
h->SetBinContent(7,5,0.935788);
h->SetBinError(7,5,0.096568);
h->SetBinContent(6,6,-0.271717);
h->SetBinError(6,6,0.0389002);
h->SetBinContent(8,5,1.50752);
h->SetBinError(8,5,0.211056);
h->SetBinContent(7,6,0.192132);
h->SetBinError(7,6,0.0458492);
h->SetBinContent(6,7,-1.32007);
h->SetBinError(6,7,0.121486);
h->SetBinContent(8,6,0.90872);
h->SetBinError(8,6,0.0751261);
h->SetBinContent(7,7,-0.271307);
h->SetBinError(7,7,0.0501235);
h->SetBinContent(9,6,2.96722);
h->SetBinError(9,6,0.441983);
h->SetBinContent(8,7,0.314563);
h->SetBinError(8,7,0.0437325);
h->SetBinContent(7,8,-1.20055);
h->SetBinError(7,8,0.0993301);
h->SetBinContent(9,7,0.835046);
h->SetBinError(9,7,0.099458);
h->SetBinContent(8,8,-0.482027);
h->SetBinError(8,8,0.0438957);
h->SetBinContent(10,7,1.38831);
h->SetBinError(10,7,0.171625);
h->SetBinContent(9,8,0.114414);
h->SetBinError(9,8,0.0586271);
h->SetBinContent(8,9,-1.47435);
h->SetBinError(8,9,0.185775);
h->SetBinContent(10,8,0.751375);
h->SetBinError(10,8,0.0773047);
h->SetBinContent(9,9,-0.380347);
h->SetBinError(9,9,0.0796451);
h->SetBinContent(11,8,1.62519);
h->SetBinError(11,8,0.196295);
h->SetBinContent(10,9,0.200471);
h->SetBinError(10,9,0.0647798);
h->SetBinContent(9,10,-1.30316);
h->SetBinError(9,10,0.147234);
h->SetBinContent(12,8,2.20917);
h->SetBinError(12,8,0.509264);
h->SetBinContent(11,9,0.910363);
h->SetBinError(11,9,0.0850836);
h->SetBinContent(10,10,-0.505681);
h->SetBinError(10,10,0.0684179);
h->SetBinContent(12,9,1.48823);
h->SetBinError(12,9,0.132217);
h->SetBinContent(11,10,0.0306258);
h->SetBinError(11,10,0.0584673);
h->SetBinContent(10,11,-2.27044);
h->SetBinError(10,11,0.226155);
h->SetBinContent(13,9,1.58311);
h->SetBinError(13,9,0.345569);
h->SetBinContent(12,10,0.689927);
h->SetBinError(12,10,0.0670461);
h->SetBinContent(11,11,-0.799846);
h->SetBinError(11,11,0.0773451);
h->SetBinContent(13,10,1.06345);
h->SetBinError(13,10,0.112009);
h->SetBinContent(12,11,-0.163768);
h->SetBinError(12,11,0.0564672);
h->SetBinContent(11,12,-1.81676);
h->SetBinError(11,12,0.18148);
h->SetBinContent(14,10,2.06685);
h->SetBinError(14,10,0.298325);
h->SetBinContent(13,11,0.59586);
h->SetBinError(13,11,0.0746976);
h->SetBinContent(12,12,-0.58803);
h->SetBinError(12,12,0.0729569);
h->SetBinContent(14,11,1.41698);
h->SetBinError(14,11,0.126987);
h->SetBinContent(13,12,-0.137893);
h->SetBinError(13,12,0.0619973);
h->SetBinContent(12,13,-1.57219);
h->SetBinError(12,13,0.225464);
h->SetBinContent(15,11,1.88218);
h->SetBinError(15,11,0.248527);
h->SetBinContent(14,12,0.413951);
h->SetBinError(14,12,0.0664471);
h->SetBinContent(13,13,-0.612643);
h->SetBinError(13,13,0.0938202);
h->SetBinContent(15,12,0.844641);
h->SetBinError(15,12,0.111332);
h->SetBinContent(14,13,-0.169116);
h->SetBinError(14,13,0.0612087);
h->SetBinContent(13,14,-1.74719);
h->SetBinError(13,14,0.402536);
h->SetBinContent(16,12,2.52204);
h->SetBinError(16,12,0.34976);
h->SetBinContent(15,13,0.409126);
h->SetBinError(15,13,0.0782132);
h->SetBinContent(14,14,-1.0863);
h->SetBinError(14,14,0.101803);
h->SetBinContent(16,13,1.01496);
h->SetBinError(16,13,0.116808);
h->SetBinContent(15,14,-0.515689);
h->SetBinError(15,14,0.0788456);
h->SetBinContent(14,15,-2.05038);
h->SetBinError(14,15,0.8547);
h->SetBinContent(17,13,1.78754);
h->SetBinError(17,13,0.246717);
h->SetBinContent(16,14,0.127458);
h->SetBinError(16,14,0.0793795);
h->SetBinContent(15,15,-1.94501);
h->SetBinError(15,15,0.476356);
h->SetBinContent(17,14,0.73143);
h->SetBinError(17,14,0.140341);
h->SetBinContent(16,15,-0.837697);
h->SetBinError(16,15,0.15784);
h->SetBinContent(15,16,-2.96667);
h->SetBinError(15,16,0.532283);
h->SetBinContent(18,14,1.85347);
h->SetBinError(18,14,0.305376);
h->SetBinContent(17,15,0.279087);
h->SetBinError(17,15,0.117522);
h->SetBinContent(16,16,-1.54247);
h->SetBinError(16,16,0.182182);
h->SetBinContent(18,15,0.859294);
h->SetBinError(18,15,0.138565);
h->SetBinContent(17,16,-0.821997);
h->SetBinError(17,16,0.133157);
h->SetBinContent(19,15,1.74161);
h->SetBinError(19,15,0.239647);
h->SetBinContent(18,16,-0.0585065);
h->SetBinError(18,16,0.13898);
h->SetBinContent(17,17,-1.50033);
h->SetBinError(17,17,0.57716);
h->SetBinContent(20,15,2.35285);
h->SetBinError(20,15,0.465003);
h->SetBinContent(19,16,0.797616);
h->SetBinError(19,16,0.204282);
h->SetBinContent(18,17,-0.7238);
h->SetBinError(18,17,0.216061);
h->SetBinContent(20,16,1.40079);
h->SetBinError(20,16,0.323073);
h->SetBinContent(19,17,0.316748);
h->SetBinError(19,17,0.203092);
h->SetBinContent(20,17,0.997748);
h->SetBinError(20,17,0.309874);
h->SetBinContent(21,17,1.62698);
h->SetBinError(21,17,0.470594);

h->Draw(“CONTZ”);
Double_t delta=0.0427394;

TF2 f2d=new TF2(“f2d”,"[0]([1]x+[2]y)",0,22,0,17);
f2d->SetParName(1,“alfa”);
f2d->SetParName(2,“beta”);
f2d->SetParLimits(1,0,100
delta);
f2d->SetParLimits(2,-100
delta,0.);
// h->Fit(f2d,“VBMRF”);
h->Fit(f2d,“M”);
}

These are all my points.
Thank you very much for your help

Cheers
Paola

Paola,

-calling f2d->SetParameters(with the 3 initial guess values);
-calling h->Fit(f2d"); //ie without option “m”

Rene

Ok, thank you very much Rene.

When I call
f2d->SetParameters(with the 3 initial guess values);
the fit results strongly depend on the guess. It seems like that the fit ends in local minima:
I have tried
f2d->SetParameters(0.7, 0.7, -0.7)
and I get:
FCN=283.174 FROM MIGRAD STATUS=CONVERGED 85 CALLS 86 TOTAL
EDM=4.9826e-10 STRATEGY= 1 ERR MATRIX NOT POS-DEF
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 9.30977e-01 2.67026e-02 9.72406e-05 8.25749e-04
2 alfa 6.71283e-01 1.73045e-02 5.79094e-06 -4.06335e-02
3 beta -7.51119e-01 1.93627e-02 6.24364e-06 -3.96546e-02

and f2d->SetParameters(0.6, -0.9, -0.9)
FCN=283.174 FROM MIGRAD STATUS=CONVERGED 84 CALLS 85 TOTAL
EDM=7.16911e-10 STRATEGY= 1 ERR MATRIX NOT POS-DEF
EXT PARAMETER APPROXIMATE STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 7.26141e-01 2.08274e-02 7.58450e-05 -3.13154e-03
2 alfa 8.60644e-01 2.21857e-02 6.73533e-06 2.53756e-02
3 beta -9.63000e-01 2.48245e-02 7.29114e-06 2.78393e-02

I know that the expected value for alfa should be around 0.7, but it could be 0.6 or 0.8…

Thank you very much
Cheers
Paola

Hi,

the fit converges to the minimum, but your 3 parameters are not uniquely defined. The function is not written correctly. In this case, you should define your TF2 with only two parameters as:

``TF2 *f2d=new TF2("f2d","[0]*x+[1]*y",0,22,0,17);``

Best Regards

Lorenzo

Thank you very much Lorenzo!
I didn’t notice that letting the constant as a free parameter would have let my other two parameters free.

Best regards
Paola