Logo ROOT   6.12/07
Reference Guide
Ifit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -nodraw
4 /// Example of a program to fit non-equidistant data points
5 ///
6 /// The fitting function fcn is a simple chisquare function
7 /// The data consists of 5 data points (arrays x,y,z) + the errors in errorsz
8 /// More details on the various functions or parameters for these functions
9 /// can be obtained in an interactive ROOT session with:
10 ///
11 /// ~~~{.cpp}
12 /// Root > TMinuit *minuit = new TMinuit(10);
13 /// ~~~
14 ///
15 /// ~~~{.cpp}
16 /// Root > minuit->mnhelp("*") to see the list of possible keywords
17 /// Root > minuit->mnhelp("SET") explains most parameters
18 /// ~~~
19 ///
20 /// \macro_output
21 /// \macro_code
22 ///
23 /// \author Rene Brun
24 
25 #include "TMinuit.h"
26 
27 Float_t z[5],x[5],y[5],errorz[5];
28 
29 //______________________________________________________________________________
30 Double_t func(float x,float y,Double_t *par)
31 {
32  Double_t value=( (par[0]*par[0])/(x*x)-1)/ ( par[1]+par[2]*y-par[3]*y*y);
33  return value;
34 }
35 
36 //______________________________________________________________________________
37 void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
38 {
39  const Int_t nbins = 5;
40  Int_t i;
41 
42 //calculate chisquare
43  Double_t chisq = 0;
44  Double_t delta;
45  for (i=0;i<nbins; i++) {
46  delta = (z[i]-func(x[i],y[i],par))/errorz[i];
47  chisq += delta*delta;
48  }
49  f = chisq;
50 }
51 
52 //______________________________________________________________________________
53 void Ifit()
54 {
55 // The z values
56  z[0]=1;
57  z[1]=0.96;
58  z[2]=0.89;
59  z[3]=0.85;
60  z[4]=0.78;
61 // The errors on z values
62  Float_t error = 0.01;
63  errorz[0]=error;
64  errorz[1]=error;
65  errorz[2]=error;
66  errorz[3]=error;
67  errorz[4]=error;
68 // the x values
69  x[0]=1.5751;
70  x[1]=1.5825;
71  x[2]=1.6069;
72  x[3]=1.6339;
73  x[4]=1.6706;
74 // the y values
75  y[0]=1.0642;
76  y[1]=0.97685;
77  y[2]=1.13168;
78  y[3]=1.128654;
79  y[4]=1.44016;
80 
81  TMinuit *gMinuit = new TMinuit(5); //initialize TMinuit with a maximum of 5 params
82  gMinuit->SetFCN(fcn);
83 
84  Double_t arglist[10];
85  Int_t ierflg = 0;
86 
87  arglist[0] = 1;
88  gMinuit->mnexcm("SET ERR", arglist ,1,ierflg);
89 
90 // Set starting values and step sizes for parameters
91  static Double_t vstart[4] = {3, 1 , 0.1 , 0.01};
92  static Double_t step[4] = {0.1 , 0.1 , 0.01 , 0.001};
93  gMinuit->mnparm(0, "a1", vstart[0], step[0], 0,0,ierflg);
94  gMinuit->mnparm(1, "a2", vstart[1], step[1], 0,0,ierflg);
95  gMinuit->mnparm(2, "a3", vstart[2], step[2], 0,0,ierflg);
96  gMinuit->mnparm(3, "a4", vstart[3], step[3], 0,0,ierflg);
97 
98 // Now ready for minimization step
99  arglist[0] = 500;
100  arglist[1] = 1.;
101  gMinuit->mnexcm("MIGRAD", arglist ,2,ierflg);
102 
103 // Print results
104  Double_t amin,edm,errdef;
105  Int_t nvpar,nparx,icstat;
106  gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
107  //gMinuit->mnprin(3,amin);
108 
109 }
110 
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:7645
Implementation in C++ of the Minuit package written by Fred James.
Definition: TMinuit.h:27
float Float_t
Definition: RtypesCore.h:53
int Int_t
Definition: RtypesCore.h:41
R__EXTERN TMinuit * gMinuit
Definition: TMinuit.h:271
Double_t x[n]
Definition: legend1.C:17
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:5674
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
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:2673
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
virtual void SetFCN(void(*fcn)(Int_t &, Double_t *, Double_t &f, Double_t *, Int_t))
To set the address of the minimization function.
Definition: TMinuit.cxx:929