Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
chisqMin.cc
Go to the documentation of this file.
1 #include <CLHEP/Matrix/Vector.h>
2 #include <CLHEP/Matrix/Matrix.h>
3 
4 using namespace std;
5 
6 #include "chisqMin.h"
7 
8 #define _DBG__ cerr<<__FILE__<<":"<<__LINE__<<endl;
9 
10 void print_state (size_t iter, gsl_multifit_fdfsolver * s)
11 {
12  printf ("print_state: iter: %3u "
13  "|f(x)| = %g\n",
14  iter,
15  gsl_blas_dnrm2 (s->f));
16 }
17 
18 chisqMin::chisqMin(residFunc *resid, int level)
19  : debug_level(level), residFuncPtr(resid)
20 {
21  unsigned int p = residFuncPtr->getP();
22  unsigned int n = residFuncPtr->getN();
23  if (n <= p) {
24  cout << "number of points less than or equal to number of parameters\n";
25  int error = 5;
26  throw error;
27  }
28  covar = gsl_matrix_alloc(p, p); /* allocate the covariance matrix */
29  T = gsl_multifit_fdfsolver_lmsder; /* choose the solver */
30  s = gsl_multifit_fdfsolver_alloc (T, (size_t)n, (size_t)p); /* allocate work space,
31  get solver pointer */
32 }
33 
35  gsl_multifit_fdfsolver_free(s);
36  gsl_matrix_free(covar);
37 }
38 
39 void chisqMin::setStartParams(HepVector &xIn) {
40  if (debug_level >= 2) {
41  cout << "setStartParams called\n";
42  cout << "start params: " << xIn << endl;
43  }
44  unsigned int n = residFuncPtr->getN();
45  unsigned int p = residFuncPtr->getP();
46  if (debug_level >=2) cout << "number of data points = " << n << " number of parameters = " << p << endl;
47  f.f = &fGsl;
48  f.df = &dfGsl;
49  f.fdf = &fdfGsl;
50  f.n = (size_t)n;
51  f.p = (size_t)p;
52  f.params = NULL; /* pointer to something that gets the data,
53  don't know if this is needed yet */
54  gsl_vector *xGslPtr;
55  xGslPtr = gsl_vector_alloc((size_t)p);
56  for (unsigned int i = 0; i < p; i++) {
57  gsl_vector_set(xGslPtr, (size_t)i, xIn(i + 1));
58  if (debug_level >=2) cout << i << " " << gsl_vector_get(xGslPtr, (size_t)i) << endl;
59  }
60  if (debug_level >=2) cout << "initialize the solver" << endl;
61  gsl_multifit_fdfsolver_set(s, &f, xGslPtr); /* initialize the solver */
62  gsl_vector_free(xGslPtr);
63 }
64 
65 void chisqMin::getParams(HepVector& xOut) {
66  unsigned int p = residFuncPtr->getP();
67  for (unsigned int i = 0; i < p; i++) {
68  xOut(i + 1) = gsl_vector_get (s->x, (size_t)i);
69  }
70 }
72  double chi = gsl_blas_dnrm2(s->f);
73  return chi*chi;
74 }
75 
76 void chisqMin::fit() {
77  if (debug_level > 1) cout << "fit called" << endl;
78  iter = 0;
79  if (debug_level > 1) print_state(iter, s);
80  do {
81  iter++;
82  if (debug_level >= 2) cout << "== begin iteration " << iter << " ==\n";
83  status = gsl_multifit_fdfsolver_iterate(s); /* go to next point */
84  if (debug_level >= 2) {
85  printf("chisqMin::fit: iter = %d, status = %d, text = %s\n", iter, status, gsl_strerror (status));
86  print_state(iter, s);
87  }
88  if (status) continue;
89  if (debug_level >= 2) cout << "chisqMin::fit: continue not taken, do delta test" << endl;
90  status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
91  if (debug_level >= 2) {
92  printf("chisqMin::fit: from delta test, status = %d, text = %s\n", status, gsl_strerror (status));
93  }
94  }
95  while (status == GSL_CONTINUE && iter < 100);
96 }
97 
99  return residFuncPtr->getP();
100 }
101 
103  return residFuncPtr->getN();
104 }
105 
106 HepMatrix chisqMin::getCovar() {
107  int nparams = residFuncPtr->getP();
108  // allocate memory for the gsl covariance matrix
109  gsl_matrix* gslCovarPtr = gsl_matrix_alloc(nparams, nparams);
110  // calculate the covariance matrix
111  gsl_multifit_covar(s->J, 0.0, gslCovarPtr);
112  HepMatrix covar(nparams, nparams);
113  // copy the gsl matrix into the hep matrix
114  for (int i = 0; i < nparams; i++) {
115  for (int j = 0; j < nparams; j++) {
116  covar(i + 1, j + 1) = gsl_matrix_get(gslCovarPtr, i, j);
117  }
118  }
119  gsl_matrix_free(gslCovarPtr); // free the gsl matrix memory
120  return covar;
121 }
122 
124  return iter;
125 }
int getP()
Definition: chisqMin.cc:98
~chisqMin()
Definition: chisqMin.cc:34
int status
Definition: chisqMin.h:26
void fit()
Definition: chisqMin.cc:76
residFunc * residFuncPtr
Definition: chisqMin.h:24
int getIter()
Definition: chisqMin.cc:123
gsl_matrix * covar
Definition: chisqMin.h:27
virtual unsigned int getP()=0
TEllipse * e
virtual unsigned int getN()=0
int getN()
Definition: chisqMin.cc:102
chisqMin(residFunc *f_in, int level=1)
Definition: chisqMin.cc:18
gsl_multifit_function_fdf f
Definition: chisqMin.h:23
residFunc * residFuncPtr
Definition: globalGslFuncs.h:4
int fGsl(const gsl_vector *x, void *data, gsl_vector *f)
Definition: globalGslFuncs.h:6
const gsl_multifit_fdfsolver_type * T
Definition: chisqMin.h:21
int debug_level
Definition: chisqMin.h:16
HepMatrix getCovar()
Definition: chisqMin.cc:106
gsl_multifit_fdfsolver * s
Definition: chisqMin.h:22
void setStartParams(HepVector &x)
Definition: chisqMin.cc:39
double getChi2()
Definition: chisqMin.cc:71
int fdfGsl(const gsl_vector *x, void *data, gsl_vector *f, gsl_matrix *J)
unsigned int iter
Definition: chisqMin.h:25
printf("string=%s", string)
int dfGsl(const gsl_vector *x, void *data, gsl_matrix *J)
HepVector getParams()
void print_state(size_t iter, gsl_multifit_fdfsolver *s)
Definition: chisqMin.cc:10