Belle II Software  release-05-01-25
NewtonFitterGSL.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * See https://github.com/tferber/OrcaKinfit, forked from *
4  * https://github.com/iLCSoft/MarlinKinfit *
5  * *
6  * Further information about the fit engine and the user interface *
7  * provided in MarlinKinfit can be found at *
8  * https://www.desy.de/~blist/kinfit/doc/html/ *
9  * and in the LCNotes LC-TOOL-2009-001 and LC-TOOL-2009-004 available *
10  * from http://www-flc.desy.de/lcnotes/ *
11  * *
12  * Adopted by: Torben Ferber (torben.ferber@desy.de) (TF) *
13  * *
14  * This software is provided "as is" without any warranty. *
15  **************************************************************************/
16 
17 #ifndef __NEWTONFITTERGSL_H
18 #define __NEWTONFITTERGSL_H
19 
20 #include "analysis/OrcaKinFit/BaseFitter.h"
21 
22 #include <gsl/gsl_vector.h>
23 #include <gsl/gsl_matrix.h>
24 #include <gsl/gsl_permutation.h>
25 #include <gsl/gsl_eigen.h>
26 
27 // Class NewtonFitterGSL
29 
30 namespace Belle2 {
36  namespace OrcaKinFit {
37 
38  class NewtonFitterGSL : public BaseFitter {
39  public:
43  virtual ~NewtonFitterGSL();
44 
46  virtual double fit() override;
47 
49  virtual int getError() const override;
50 
52  virtual double getProbability() const override;
54  virtual double getChi2() const override;
56  virtual int getDoF() const override;
58  virtual int getIterations() const override;
59 
61  virtual int getNcon() const;
62 
64  virtual int getNsoft() const;
65 
67  virtual int getNpar() const;
68 
70  virtual int getNunm() const;
71 
73  virtual bool initialize() override;
74 
76  virtual void setDebug(int debuglevel);
77 
78  protected:
80  virtual double calcChi2();
81 
83  int calcDx();
84 
86  int calcDxSVD();
87 
89  void printMy(double M[], double y[], int idim);
90 
91  bool updateParams(gsl_vector* xnew);
92  void fillxold();
93  void fillperr();
94  int calcM(bool errorpropagation = false);
95  int calcy();
96  int optimizeScale();
97  int invertM();
98 
99  void calcCovMatrix();
100 
101  double meritFunction(double mu);
102  double meritFunctionDeriv();
103 
104  enum {NPARMAX = 50, NCONMAX = 10, NUNMMAX = 10};
105 
106  int npar;
107  int ncon;
108  int nsoft;
109  int nunm;
110  int ierr;
111  int nit;
112 
113  double fitprob;
114  double chi2;
115 
116  static void ini_gsl_permutation(gsl_permutation*& p, unsigned int size);
117  static void ini_gsl_vector(gsl_vector*& v, int unsigned size);
118  static void ini_gsl_matrix(gsl_matrix*& m, int unsigned size1, unsigned int size2);
119 
120  static void debug_print(gsl_matrix* m, const char* name);
121  static void debug_print(gsl_vector* v, const char* name);
122 
123  private:
124  unsigned int idim;
125  gsl_vector* x;
126  gsl_vector* xold;
127  gsl_vector* xbest;
128  gsl_vector* dx;
129  gsl_vector* dxscal;
130  gsl_vector* grad;
131  gsl_vector* y;
132  gsl_vector* yscal;
133  gsl_vector* perr;
134  gsl_vector* v1;
135  gsl_vector* v2;
136  gsl_vector* Meval;
137 
138  gsl_matrix* M;
139  gsl_matrix* Mscal;
140  gsl_matrix* M1;
141  gsl_matrix* M2;
142  gsl_matrix* M3;
143  gsl_matrix* M4;
144  gsl_matrix* M5;
145  gsl_matrix* Mevec;
146  gsl_matrix* CC;
147  gsl_matrix* CC1;
148  gsl_matrix* CCinv;
149 
150  gsl_permutation* permM;
151  gsl_eigen_symmv_workspace* ws;
152  unsigned int wsdim;
153 
154  double chi2best;
155  double chi2new;
156  double chi2old;
157  double fvalbest;
158  double scale;
159  double scalebest;
160  double stepsize;
161  double stepbest;
162  enum {NITMAX = 100};
163  double scalevals[NITMAX];
164  double fvals[NITMAX];
165 
166  int imerit;
167 
168  int debug;
169  };
170 
171  }// end OrcaKinFit namespace
173 } // end Belle2 namespace
174 
175 #endif // __NEWTONFITTERGSL_H
Belle2::OrcaKinFit::NewtonFitterGSL::nsoft
int nsoft
total number of soft constraints
Definition: NewtonFitterGSL.h:122
Belle2::OrcaKinFit::NewtonFitterGSL::getChi2
virtual double getChi2() const override
Get the chi**2 of the last fit.
Definition: NewtonFitterGSL.cc:499
Belle2::OrcaKinFit::NewtonFitterGSL::nit
int nit
Number of iterations.
Definition: NewtonFitterGSL.h:125
Belle2::OrcaKinFit::NewtonFitterGSL::printMy
void printMy(double M[], double y[], int idim)
Print a Matrix M and a vector y of dimension idim.
Definition: NewtonFitterGSL.cc:488
Belle2::OrcaKinFit::NewtonFitterGSL::getNcon
virtual int getNcon() const
Get the number of hard constraints of the last fit.
Definition: NewtonFitterGSL.cc:695
Belle2::OrcaKinFit::NewtonFitterGSL::NewtonFitterGSL
NewtonFitterGSL()
Constructor.
Definition: NewtonFitterGSL.cc:53
Belle2::OrcaKinFit::NewtonFitterGSL::getError
virtual int getError() const override
Get the error code of the last fit: 0=OK, 1=failed.
Definition: NewtonFitterGSL.cc:497
Belle2::OrcaKinFit::NewtonFitterGSL::fitprob
double fitprob
fit probability
Definition: NewtonFitterGSL.h:127
Belle2::OrcaKinFit::NewtonFitterGSL::getNunm
virtual int getNunm() const
Get the number of unmeasured parameters of the last fit.
Definition: NewtonFitterGSL.cc:697
Belle2::OrcaKinFit::NewtonFitterGSL::getNsoft
virtual int getNsoft() const
Get the number of soft constraints of the last fit.
Definition: NewtonFitterGSL.cc:696
Belle2::OrcaKinFit::NewtonFitterGSL::ncon
int ncon
total number of hard constraints
Definition: NewtonFitterGSL.h:121
Belle2::OrcaKinFit::NewtonFitterGSL::nunm
int nunm
total number of unmeasured parameters
Definition: NewtonFitterGSL.h:123
Belle2::OrcaKinFit::NewtonFitterGSL::getDoF
virtual int getDoF() const override
Get the number of degrees of freedom of the last fit.
Definition: NewtonFitterGSL.cc:500
Belle2::OrcaKinFit::NewtonFitterGSL::initialize
virtual bool initialize() override
Initialize the fitter.
Definition: NewtonFitterGSL.cc:394
Belle2::OrcaKinFit::NewtonFitterGSL::setDebug
virtual void setDebug(int debuglevel)
Set the Debug Level.
Definition: NewtonFitterGSL.cc:1006
Belle2::OrcaKinFit::NewtonFitterGSL::calcChi2
virtual double calcChi2()
Calculate the chi2.
Definition: NewtonFitterGSL.cc:474
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::OrcaKinFit::NewtonFitterGSL::chi2
double chi2
final chi2
Definition: NewtonFitterGSL.h:128
Belle2::OrcaKinFit::NewtonFitterGSL::fit
virtual double fit() override
The fit method, returns the fit probability.
Definition: NewtonFitterGSL.cc:128
Belle2::OrcaKinFit::NewtonFitterGSL::~NewtonFitterGSL
virtual ~NewtonFitterGSL()
Virtual destructor.
Definition: NewtonFitterGSL.cc:70
Belle2::OrcaKinFit::NewtonFitterGSL::calcDxSVD
int calcDxSVD()
Calculate the vector dx to update the parameters; returns fail code, 0=OK.
Definition: NewtonFitterGSL.cc:548
Belle2::OrcaKinFit::NewtonFitterGSL::getNpar
virtual int getNpar() const
Get the number of all parameters of the last fit.
Definition: NewtonFitterGSL.cc:698
Belle2::OrcaKinFit::NewtonFitterGSL::ierr
int ierr
Error status.
Definition: NewtonFitterGSL.h:124
Belle2::OrcaKinFit::NewtonFitterGSL::calcDx
int calcDx()
Calculate the vector dx to update the parameters; returns fail code, 0=OK.
Definition: NewtonFitterGSL.cc:503
Belle2::OrcaKinFit::NewtonFitterGSL::getIterations
virtual int getIterations() const override
Get the number of iterations of the last fit.
Definition: NewtonFitterGSL.cc:501
Belle2::OrcaKinFit::NewtonFitterGSL::npar
int npar
total number of parameters
Definition: NewtonFitterGSL.h:120
Belle2::OrcaKinFit::NewtonFitterGSL::getProbability
virtual double getProbability() const override
Get the fit probability of the last fit.
Definition: NewtonFitterGSL.cc:498