ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Ifit.C
Go to the documentation of this file.
1 //
2 // Example of a program to fit non-equidistant data points
3 // =======================================================
4 //
5 // The fitting function fcn is a simple chisquare function
6 // The data consists of 5 data points (arrays x,y,z) + the errors in errorsz
7 // More details on the various functions or parameters for these functions
8 // can be obtained in an interactive ROOT session with:
9 // Root > TMinuit *minuit = new TMinuit(10);
10 // Root > minuit->mnhelp("*") to see the list of possible keywords
11 // Root > minuit->mnhelp("SET") explains most parameters
12 //Author: Rene Brun
13 
14 #include "TMinuit.h"
15 
16 Float_t z[5],x[5],y[5],errorz[5];
17 
18 //______________________________________________________________________________
19 Double_t func(float x,float y,Double_t *par)
20 {
21  Double_t value=( (par[0]*par[0])/(x*x)-1)/ ( par[1]+par[2]*y-par[3]*y*y);
22  return value;
23 }
24 
25 //______________________________________________________________________________
26 void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
27 {
28  const Int_t nbins = 5;
29  Int_t i;
30 
31 //calculate chisquare
32  Double_t chisq = 0;
33  Double_t delta;
34  for (i=0;i<nbins; i++) {
35  delta = (z[i]-func(x[i],y[i],par))/errorz[i];
36  chisq += delta*delta;
37  }
38  f = chisq;
39 }
40 
41 //______________________________________________________________________________
42 void Ifit()
43 {
44 // The z values
45  z[0]=1;
46  z[1]=0.96;
47  z[2]=0.89;
48  z[3]=0.85;
49  z[4]=0.78;
50 // The errors on z values
51  Float_t error = 0.01;
52  errorz[0]=error;
53  errorz[1]=error;
54  errorz[2]=error;
55  errorz[3]=error;
56  errorz[4]=error;
57 // the x values
58  x[0]=1.5751;
59  x[1]=1.5825;
60  x[2]=1.6069;
61  x[3]=1.6339;
62  x[4]=1.6706;
63 // the y values
64  y[0]=1.0642;
65  y[1]=0.97685;
66  y[2]=1.13168;
67  y[3]=1.128654;
68  y[4]=1.44016;
69 
70  TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params
71  gMinuit->SetFCN(fcn);
72 
73  Double_t arglist[10];
74  Int_t ierflg = 0;
75 
76  arglist[0] = 1;
77  gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);
78 
79 // Set starting values and step sizes for parameters
80  static Double_t vstart[4] = {3, 1 , 0.1 , 0.01};
81  static Double_t step[4] = {0.1 , 0.1 , 0.01 , 0.001};
82  gMinuit->mnparm(0, "a1", vstart[0], step[0], 0,0,ierflg);
83  gMinuit->mnparm(1, "a2", vstart[1], step[1], 0,0,ierflg);
84  gMinuit->mnparm(2, "a3", vstart[2], step[2], 0,0,ierflg);
85  gMinuit->mnparm(3, "a4", vstart[3], step[3], 0,0,ierflg);
86 
87 // Now ready for minimization step
88  arglist[0] = 500;
89  arglist[1] = 1.;
90  gMinuit->mnexcm("MIGRAD", arglist ,2,ierflg);
91 
92 // Print results
93  Double_t amin,edm,errdef;
94  Int_t nvpar,nparx,icstat;
95  gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
96  //gMinuit->mnprin(3,amin);
97 
98 }
99 
double par[1]
Definition: unuranDistr.cxx:38
virtual void mnstat(Double_t &fmin, Double_t &fedm, Double_t &errdef, Int_t &npari, Int_t &nparx, Int_t &istat)
*-*-*-*-*Returns concerning the current status of the minimization*-*-*-*-* *-* =====================...
Definition: TMinuit.cxx:7699
void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
Definition: Ifit.C:26
Implementation in C++ of the Minuit package written by F.
Definition: TMinuit.h:34
float Float_t
Definition: RtypesCore.h:53
Float_t x[5]
Definition: Ifit.C:16
int Int_t
Definition: RtypesCore.h:41
int nbins[3]
Float_t y[5]
Definition: Ifit.C:16
TFile * f
virtual void SetFCN(void *fcn)
*-*-*-*-*-*-*To set the address of the minimization function*-*-*-*-*-*-*-* *-* =====================...
Definition: TMinuit.cxx:947
R__EXTERN TMinuit * gMinuit
Definition: TMinuit.h:279
Float_t errorz[5]
Definition: Ifit.C:16
Float_t z[5]
Definition: Ifit.C:16
virtual void mnparm(Int_t k, TString cnamj, Double_t uk, Double_t wk, Double_t a, Double_t b, Int_t &ierflg)
*-*-*-*-*-*-*-*-*Implements one parameter definition*-*-*-*-*-*-*-*-*-*-*-* *-* =====================...
Definition: TMinuit.cxx:5728
void Ifit()
Definition: Ifit.C:42
double Double_t
Definition: RtypesCore.h:55
virtual void mnexcm(const char *comand, Double_t *plist, Int_t llist, Int_t &ierflg)
*-*-*-*-*-*Interprets a command and takes appropriate action*-*-*-*-*-*-*-* *-* =====================...
Definition: TMinuit.cxx:2688
Double_t func(float x, float y, Double_t *par)
Definition: Ifit.C:19
float value
Definition: math.cpp:443