next up previous contents index
Next: Polynomial Up: Module NTools Previous: Module NTools   Contents   Index


Fitting

Fitting is done with two classes GeneralFit and GeneralFitData and is based on the Levenberg-Marquardt method. GeneralFitData is a class which provide a description of the data to be fitted. GeneralFit is the fitter class. Parametrized functions can be given as classes which inherit GeneralFunction or as simple C functions. Classes of pre-defined functions are provided (see files fct1dfit.h and fct2dfit.h). The user interface is very close from that of the CERN Minuit fitter. Number of objects (Histo, HProf ...) are interfaced with GeneralFit and can be easily fitted.
Here is a very simple example for fitting the previously created NTuple with a Gaussian :

#include "fct1dfit.h"
// ...

// Read from ppf file
NTuple nt;
{
PInPersist pis("myfile.ppf");
string tag = "NT";  pis.GetObject(nt,tag);
}

// Fill GeneralData
GeneralData mGdata(nt.NEntry());
for(int i=0; i<nt.NEntry(); i++)
  mGdata.AddData1(xnt[1],xnt[2],xnt[3]); // Fill x, y and error on y
mGData.PrintStatus();

// Function for fitting : y = f(x) + noise
Gauss1DPol mFunction; // gaussian + constant

// Prepare for fit
GeneralFit mFit(&mFunction);  // create a fitter for the choosen function
mFit.SetData(&mGData);        // connect data to the fitter

// Set and initialise the parameters (that's non-linear fitting!)
//           (num par, name, guess start, step, [limits min and max])
mFit.SetParam(0,"high",90.,1..);
mFit.SetParam(1,"xcenter",0.05,0.01);
mFit.SetParam(2,"sigma",sig,0.05,0.01,10.);
                              // Give limits to avoid division by zero
mFit.SetParam(3,"constant",0.,1.);

// Fit and print result
int rcfit = mFit.Fit();
mFit.PrintFit();
if(rcfit>0) {)
  cout<<"Reduce_Chisquare = "<<mFit.GetChi2Red()
      <<" nstep="<<mFit.GetNStep()<<" rc="<<rcfit<<endl;
} else {
  cout<<"Fit_Error, rc = "<<rcfit<<"  nstep="<<mFit.GetNStep()<<endl;
  mFit.PrintFitErr(rcfit);
}

// Get the result for further use
TVector<r_8> ParResult = mFit.GetParm();
cout<<ParResult;

Much more usefull possibilities and detailed informations might be found in the HTML pages of the Sophya manual.



Reza Ansari
2001-03-07