Hi Benoit,
Fitting in ROOT is via the TMinuit class. With TMinuit, one can perform
fits on N dimensional distributions with P parameters.
In case of 1-D, 2-D and 3-D histograms, Profile histograms and TGraph
Fit functions are provided for these classes.
In the tutorials section, we show several examples of fits on 1-D histograms.
In the test program in $ROOTSYS/test/stress.cxx, you can see an example
(stress4)
of a fit on a 2-D histogram.
In this example, the fit function f2form is given as a simple expression
made of standard basic functions. In real life problems, one better uses
a standard C++ function of a form always the same for all dimensions and
problems:
Double_t myFunction(Double_t *x, Double_t *params)
Here is a variant of stress4 using the second form
Rene Brun
// file fit2.C
#include "TF2.h"
#include "TH2.h"
// This tutorial illustrates :
// - how to create a 2-d function
// - fill a 2-d histogram randomly from this function
// - fit the histogram
// - display the fitted function on top of the histogram (scatter-plot)
//
// This example can be executed via the interpreter or/and the compiler
// root > .x fit2.C
// root > .x fit2.C++
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 *p2 = &par[5];
Double_t *p3 = &par[10];
Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
return result;
}
void fit2() {
const Int_t npar = 15;
Double_t f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
f2->SetParameters(f2params);
//Create an histogram and fill it randomly with f2
TH2F *h2 = new TH2F("h2","from f2",40,-10,10,40,-10,10);
Int_t nentries = 100000;
h2->FillRandom("f2",nentries);
//Fit h2 with original function f2
Float_t ratio = 4*nentries/100000;
f2params[ 0] *= ratio;
f2params[ 5] *= ratio;
f2params[10] *= ratio;
f2->SetParameters(f2params);
h2->Fit("f2");
f2->Draw("cont1 same");
}
FORESTIER Benoit wrote:
>
> hi,
> is it possible, using ROOT, to perform a 2D fit?
>
> Benoit Forestier
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:40 MET