[ROOT] I can not fit 2d histo with 2d gaus funtion.

From: Harufumi Tsuchiya (harufumi@icrr.u-tokyo.ac.jp)
Date: Thu Mar 14 2002 - 10:07:29 MET


Dear ROOTers,

I am using ROOT v3.03/02 on IRIX 6.4 with gcc 2.95.2.

Now, I am trying to fit 2 dimentional data points(x, y) with 
2d gaus function. 
My set of data is following (bottom this mail).
data consist of x, y, z and zerr.

I found fit2.C in root/tutorials directory and 
modified it to my application like below.

Using my macro "Test2D.C", I could draw the picture using TH2F with
"surf2" option. But, I could not fit the data using TF2 "f2", which 
is 2-dimentional gaus function. I only got result of nan for all parameters.
Maybe, my procedure is someting wrong. 
So, please tell me the correct way.

And, finally, I would like to fit the data points with errors, 
which indicate zerr in my macro. In this case, Can I fit the data using
2d gaus fucnction ?



------------------ Test2D.C -----------------------------
#include "TF2.h"
#include "TH2.h"
Double_t g2(Double_t *x, Double_t *par) {
   Double_t r1 = Double_t((x[0]-par[1])/par[2]);
   Double_t r2 = Double_t((x[1]-par[3])/par[4]);
   return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
}   
Double_t fun2(Double_t *x, Double_t *par) {
   Double_t *p1 = &par[0];
   Double_t result = g2(x,p1);
   return result;
}

void Test2D(char *file) {
  gStyle->SetPalette(1, 0);
  const Int_t npar = 5;
  Double_t f2params[npar] = {-500, 0, 0.2, 0, 0.2};
  TF2 *f2 = new TF2("f2", fun2, -1.75, 1.75, -1.75, 1.75, npar);
  f2->SetParameters(f2params);

  ifstream data(file);

  TH2F *h2 = new TH2F("h2", "Test", 8, -1.75, 1.75, 8, -1.75, 1.75);
  Float_t x, y, z, zerr, xadd, yadd;
  xadd = yadd = 1.75;
  Int_t binx, biny;
  while ( !data.eof() ) {
    if ( data.eof() ) break;
    data >> x >> y >> z >> zerr;
    binx = 1 + Int_t((x + xadd)*2); biny = 1 + Int_t((y + yadd)*2);
    h2->SetBinContent(binx, biny, z);
    // h2->Fill(x, y, z)
  }
  TCanvas *c1 = new TCanvas("c1", "", 10, 100, 500, 500);
  h2->Draw("surf2 z");
  //  h2->Fit("f2");
}


----------------- data ----------------
   x          y        z       zerr
---------------------------------------
-1.750000 -1.750000 -177.364 88.4113
-1.750000 -1.250000 23.8819 89.5173
-1.750000 -0.750000 -169.408 90.4699
-1.750000 -0.250000 14.1323 91.6545
-1.750000 0.250000 -28.0623 92.2061
-1.750000 0.750000 2.88085 93.4896
-1.750000 1.250000 -108.494 94.5778
-1.750000 1.750000 27.6635 95.6723
-1.250000 -1.750000 -21.3866 88.2509
-1.250000 -1.250000 -57.2445 89.6165
-1.250000 -0.750000 -97.7985 90.5366
-1.250000 -0.250000 -147.786 91.3785
-1.250000 0.250000 -169.611 92.645
-1.250000 0.750000 -278.408 93.9902
-1.250000 1.250000 -210.075 94.6681
-1.250000 1.750000 75.6192 95.6346
-0.750000 -1.750000 -13.6447 88.403
-0.750000 -1.250000 -11.9387 89.2608
-0.750000 -0.750000 -196.141 90.4865
-0.750000 -0.250000 -343.435 91.0842
-0.750000 0.250000 -236.105 92.3592
-0.750000 0.750000 -191.582 93.7229
-0.750000 1.250000 -52.6134 94.4583
-0.750000 1.750000 90.6348 95.313
-0.250000 -1.750000 -207.86 88.7357
-0.250000 -1.250000 -118.233 89.4734
-0.250000 -0.750000 -332.12 90.7167
-0.250000 -0.250000 -669.193 91.4756
-0.250000 0.250000 -512.535 92.4841
-0.250000 0.750000 -385.905 93.391
-0.250000 1.250000 10.6888 94.5607
-0.250000 1.750000 18.3322 95.4434
0.250000 -1.750000 -101.211 88.3915
0.250000 -1.250000 -153.832 89.1503
0.250000 -0.750000 -225.612 90.2201
0.250000 -0.250000 -355.538 91.4372
0.250000 0.250000 -359.276 92.605
0.250000 0.750000 -101.366 93.36
0.250000 1.250000 -86.0116 94.7367
0.250000 1.750000 -126.872 95.6148
0.750000 -1.750000 18.9985 88.4382
0.750000 -1.250000 -2.56898 89.532
0.750000 -0.750000 -89.8189 90.4403
0.750000 -0.250000 -195.499 91.6973
0.750000 0.250000 -60.4349 92.6743
0.750000 0.750000 -24.0375 93.5119
0.750000 1.250000 -118.055 94.4758
0.750000 1.750000 -88.8576 95.5508
1.250000 -1.750000 -100.037 88.2104
1.250000 -1.250000 -79.9338 89.6215
1.250000 -0.750000 -10.1272 90.2623
1.250000 -0.250000 -165.224 91.7862
1.250000 0.250000 61.9772 92.4866
1.250000 0.750000 -60.333 93.8211
1.250000 1.250000 -128.538 94.4395
1.250000 1.750000 -264.961 95.4229
1.750000 -1.750000 -34.8958 88.5176
1.750000 -1.250000 2.97855 89.6195
1.750000 -0.750000 19.1471 90.4522
1.750000 -0.250000 -45.2502 91.5662
1.750000 0.250000 35.7344 92.6695
1.750000 0.750000 38.8704 93.5341
1.750000 1.250000 192.175 94.2912
1.750000 1.750000 82.0293 95.3584



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:45 MET