Ifit.C

Go to the documentation of this file.
00001 //
00002 //   Example of a program to fit non-equidistant data points
00003 //   =======================================================
00004 //
00005 //   The fitting function fcn is a simple chisquare function
00006 //   The data consists of 5 data points (arrays x,y,z) + the errors in errorsz
00007 //   More details on the various functions or parameters for these functions
00008 //   can be obtained in an interactive ROOT session with:
00009 //    Root > TMinuit *minuit = new TMinuit(10);
00010 //    Root > minuit->mnhelp("*")  to see the list of possible keywords
00011 //    Root > minuit->mnhelp("SET") explains most parameters
00012 //Author: Rene Brun
00013 
00014 #include "TMinuit.h"
00015 
00016 Float_t z[5],x[5],y[5],errorz[5];
00017 
00018 //______________________________________________________________________________
00019 Double_t func(float x,float y,Double_t *par)
00020 {
00021  Double_t value=( (par[0]*par[0])/(x*x)-1)/ ( par[1]+par[2]*y-par[3]*y*y);
00022  return value;
00023 }
00024 
00025 //______________________________________________________________________________
00026 void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
00027 {
00028    const Int_t nbins = 5;
00029    Int_t i;
00030 
00031 //calculate chisquare
00032    Double_t chisq = 0;
00033    Double_t delta;
00034    for (i=0;i<nbins; i++) {
00035      delta  = (z[i]-func(x[i],y[i],par))/errorz[i];
00036      chisq += delta*delta;
00037    }
00038    f = chisq;
00039 }
00040 
00041 //______________________________________________________________________________
00042 void Ifit()
00043 {
00044 // The z values
00045         z[0]=1;
00046         z[1]=0.96;
00047         z[2]=0.89;
00048         z[3]=0.85;
00049         z[4]=0.78;
00050 // The errors on z values
00051         Float_t error = 0.01;
00052         errorz[0]=error;
00053         errorz[1]=error;
00054         errorz[2]=error;
00055         errorz[3]=error;
00056         errorz[4]=error;
00057 // the x values
00058         x[0]=1.5751;
00059         x[1]=1.5825;
00060         x[2]=1.6069;
00061         x[3]=1.6339;
00062         x[4]=1.6706;
00063 // the y values
00064         y[0]=1.0642;
00065         y[1]=0.97685;
00066         y[2]=1.13168;
00067         y[3]=1.128654;
00068         y[4]=1.44016;
00069 
00070    TMinuit *gMinuit = new TMinuit(5);  //initialize TMinuit with a maximum of 5 params
00071    gMinuit->SetFCN(fcn);
00072 
00073    Double_t arglist[10];
00074    Int_t ierflg = 0;
00075 
00076    arglist[0] = 1;
00077    gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);
00078 
00079 // Set starting values and step sizes for parameters
00080    static Double_t vstart[4] = {3, 1 , 0.1 , 0.01};
00081    static Double_t step[4] = {0.1 , 0.1 , 0.01 , 0.001};
00082    gMinuit->mnparm(0, "a1", vstart[0], step[0], 0,0,ierflg);
00083    gMinuit->mnparm(1, "a2", vstart[1], step[1], 0,0,ierflg);
00084    gMinuit->mnparm(2, "a3", vstart[2], step[2], 0,0,ierflg);
00085    gMinuit->mnparm(3, "a4", vstart[3], step[3], 0,0,ierflg);
00086 
00087 // Now ready for minimization step
00088    arglist[0] = 500;
00089    arglist[1] = 1.;
00090    gMinuit->mnexcm("MIGRAD", arglist ,2,ierflg);
00091 
00092 // Print results
00093    Double_t amin,edm,errdef;
00094    Int_t nvpar,nparx,icstat;
00095    gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
00096    //gMinuit->mnprin(3,amin);
00097 
00098 }
00099 

Generated on Tue Jul 5 15:44:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1