Logo ROOT   6.08/07
Reference Guide
PaulTest3.cxx
Go to the documentation of this file.
1 // @(#)root/minuit2:$Id$
2 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei 2003-2005
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2005 LCG ROOT Math team, CERN/PH-SFT *
7  * *
8  **********************************************************************/
9 
10 #include "GaussFcn2.h"
12 #include "Minuit2/MnMigrad.h"
13 #include "Minuit2/MnMinos.h"
15 #include "Minuit2/MnPrint.h"
16 
17 #include <iostream>
18 #include <fstream>
19 
20 
21 #ifdef USE_SEALBASE
22 #include "SealBase/Filename.h"
23 #include "SealBase/ShellEnvironment.h"
24 #endif
25 
26 using namespace ROOT::Minuit2;
27 
28 
29 int main() {
30  std::vector<double> positions;
31  std::vector<double> measurements;
32  std::vector<double> var;
33  double nmeas = 0;
34 
35 #ifdef USE_SEALBASE
36  seal::Filename inputFile (seal::Filename ("$SEAL/src/MathLibs/Minuit/tests/MnSim/paul3.txt").substitute (seal::ShellEnvironment ()));
37  std::ifstream in(inputFile.Name() );
38 #else
39  std::ifstream in("paul3.txt");
40 #endif
41  if (!in) {
42  std::cerr << "Error opening input data file" << std::endl;
43  return 1;
44  }
45 
46 
47  // read input data
48  {
49  double x = 0., y = 0., width = 0., err = 0., un1 = 0., un2 = 0.;
50  while(in>>x>>y>>width>>err>>un1>>un2) {
51  if(err < 1.e-8) continue;
52  positions.push_back(x);
53  measurements.push_back(y);
54  var.push_back(err*err);
55  nmeas += y;
56  // xout<<x<<", ";
57  // yout<<y<<", ";
58  // eout<<err*err<<", ";
59  }
60  std::cout<<"size= "<<var.size()<<std::endl;
61  std::cout<<"nmeas: "<<nmeas<<std::endl;
62  }
63 
64  // create FCN function
65  GaussFcn2 fFCN(measurements, positions, var);
66 
67  std::vector<double> meas = fFCN.Measurements();
68  std::vector<double> pos = fFCN.Positions();
69 
70  // create initial starting values for parameters
71  double x = 0.;
72  double x2 = 0.;
73  double norm = 0.;
74  double dx = pos[1]-pos[0];
75  double area = 0.;
76  for(unsigned int i = 0; i < meas.size(); i++) {
77  norm += meas[i];
78  x += (meas[i]*pos[i]);
79  x2 += (meas[i]*pos[i]*pos[i]);
80  area += dx*meas[i];
81  }
82  double mean = x/norm;
83  double rms2 = x2/norm - mean*mean;
84 
85  std::cout<<"initial mean: "<<mean<<std::endl;
86  std::cout<<"initial sigma: "<<sqrt(rms2)<<std::endl;
87  std::cout<<"initial area: "<<area<<std::endl;
88  std::vector<double> init_val(6);
89 
90  init_val[0] = mean;
91  init_val[1] = sqrt(rms2);
92  init_val[2] = area;
93  init_val[3] = mean;
94  init_val[4] = sqrt(rms2);
95  init_val[5] = area;
96 
97 // ('Norm', 'Mean', 'Sigma')
98 // (3286.919999999996, 8676.4053709004238, 3123.5358310131301)
99 
100 // (1343.311786775236, 10344.596646633145, 3457.8037717416009)
101 // >>> gauss.Parameters()
102 // (1802.4364028493396, 7090.3704658021443, 1162.144685781906)
103 
104  /*
105  init_val[0] = 10000.;
106  init_val[1] = 3000;
107  init_val[2] = 1300;
108  init_val[3] = 7000;
109  init_val[4] = 1000;
110  init_val[5] = 1800;
111  */
112  std::cout<<"initial fval: "<<fFCN(init_val)<<std::endl;
113 
114  MnUserParameters upar;
115  upar.Add("mean1", mean, 10.);
116  upar.Add("sig1", sqrt(rms2), 10.);
117  upar.Add("area1", area, 10.);
118  upar.Add("mean2", mean, 10.);
119  upar.Add("sig2", sqrt(rms2), 10.);
120  upar.Add("area2", area, 10.);
121 
122  MnMigrad migrad(fFCN, upar);
123  std::cout<<"start migrad "<<std::endl;
124  FunctionMinimum min = migrad();
125  if(!min.IsValid()) {
126  //try with higher strategy
127  std::cout<<"FM is invalid, try with strategy = 2."<<std::endl;
128  MnMigrad migrad2(fFCN, upar, 2);
129  min = migrad2();
130  }
131  std::cout<<"minimum: "<<min<<std::endl;
132  return 0;
133 }
std::vector< double > Positions() const
Definition: GaussFcn2.h:41
API class for minimization using Variable Metric technology ("MIGRAD"); allows for user interaction: ...
Definition: MnMigrad.h:31
bool Add(const std::string &, double, double)
Add free Parameter Name, Value, Error.
double sqrt(double)
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
class holding the full result of the minimization; both internal and external (MnUserParameterState) ...
std::vector< double > Measurements() const
Definition: GaussFcn2.h:40
API class for the user interaction with the parameters; serves as input to the minimizer as well as o...
int main()
Definition: PaulTest3.cxx:29
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40