Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

NewtonFitter.C

Go to the documentation of this file.
00001 
00002 // Class NewtonFitter
00003 //
00004 // Author: Benno List
00005 // Last update: $Date: 2004/06/01 16:40:10 $
00006 //          by: $Author: blist $
00007 // 
00008 // Description: kinematic fit using Newton method
00009 //               
00011 
00012 #undef NDEBUG
00013 
00014 #include "jbltools/kinfit/NewtonFitter.h" 
00015 
00016 #include<iostream>
00017 #include<cmath>
00018 #include<cassert>
00019 
00020 #include "jbltools/kinfit/BaseFitObject.h"
00021 #include "jbltools/kinfit/BaseConstraint.h"
00022 #include "jbltools/kinfit/ftypes.h"
00023 #include "jbltools/kinfit/cernlib.h"
00024 
00025 using std::cout;
00026 using std::cerr;
00027 using std::endl;
00028 
00029 // constructor
00030 NewtonFitter::NewtonFitter() 
00031 : npar (0), ncon (0) 
00032 {}
00033 
00034 // destructor
00035 NewtonFitter::~NewtonFitter() {};
00036 
00037 double NewtonFitter::fit() {
00038 
00039   // cout statements
00040   bool debug = false;
00041 
00042   // order parameters etc
00043   initialize();
00044   
00045   // initialize eta, etasv, y 
00046   int idim = npar+ncon;
00047   FDouble *x  = new FDouble [idim];
00048   FDouble *dx = new FDouble [idim];
00049   FDouble *y  = new FDouble [idim];
00050   FDouble *M  = new FDouble [idim*idim];
00051   
00052   for (int i = 0; i < idim) x[i] = y[i] = 0;
00053   
00054   // Get starting values
00055   for (FitObjectIterator i = fitobjects.begin(); i != fitobjects.end(); ++i) {
00056     BaseFitObject *fo = *i;
00057     assert (fo);
00058     for (int ilocal = 0; ilocal < fo.getNPar(); ++ilocal) {
00059       int iglobal = fo->getGlobalParNum (ilocal);
00060       assert (iglobal >= 0 && iglobal < npar);
00061       x[iglobal] = fo->getParam (ilocal);
00062     }
00063   }
00064   
00065   // Set initial lambda values all to 1
00066   for (int i = npar; i < idim; i++) x[i] = 1;
00067   
00068   // Set M to 0
00069   for (int i = 0; i < idim*idim) M[i] = 0;
00070   // Compose M: First, all terms d^2 chi^2/dx1 dx2
00071   for (FitObjectIterator i = fitobjects.begin(); i != fitobjects.end(); ++i) {
00072     BaseFitObject *fo = *i;
00073     assert (fo);
00074     fo->addToGlobalDerMatrix (idim, M);
00075   }
00076   
00077   
00078   
00079   
00080   
00081   // Update values in Fitobjects
00082   for (FitObjectIterator i = fitobjects.begin(); i != fitobjects.end(); ++i) {
00083     BaseFitObject *fo = *i;
00084     assert (fo);
00085     for (int ilocal = 0; ilocal < fo.getNPar(); ++ilocal) {
00086       int iglobal = fo->getGlobalParNum (ilocal);
00087       assert (iglobal >= 0 && iglobal < npar);
00088       fo->getParam (ilocal) = x[iglobal];
00089     }
00090   }
00091   
00092 
00093   
00094 // *-- End of iterations - calculate errors.
00095 
00096 //    ====> HERE GOES ERROR CALCULATION <======
00097 
00098   delete[] x;
00099   delete[] dx;
00100   delete[] y;
00101   delete[] M;
00102   
00103 
00104 // *-- Turn chisq into probability.
00105   FReal chi = FReal(chinew);
00106 
00107   return prob(chi,ncon-nunm);
00108     
00109 };
00110 
00111 bool NewtonFitter::initialize() {
00112   // tell fitobjects the global ordering of their parameters:
00113   int iglobal = 0;
00114   // measured parameters first!
00115   for (int ifitobj = 0; ifitobj < fitobjects.size(); ++ifitobj) {
00116     for (int ilocal = 0; ilocal < fitobjects[ifitobj]->getNPar(); ++ilocal) {
00117       fitobjects[ifitobj]->setGlobalParNum (ilocal, iglobal);
00118       ++iglobal;
00119     }
00120   }
00121   npar = iglobal;
00122   int nunm = 0;
00123   // noe unmeasured parameters!
00124   for (int ifitobj = 0; ifitobj < fitobjects.size(); ++ifitobj) {
00125     for (int ilocal = 0; ilocal < fitobjects[ifitobj]->getNPar(); ++ilocal) {
00126       if (!fitobjects[ifitobj]->getMeasured(ilocal)) ++nunm;
00127     }
00128   }
00129   
00130   // set number of constraints
00131   ncon = constraints.size();
00132   
00133   if (nunm > ncon) {
00134     cerr << "NewtonFitter::initialize: nunm=" << nunm << " > ncon=" << ncon << endl;
00135   }
00136   
00137   return true;
00138 
00139 };
00140   

Generated on Fri Sep 14 17:38:21 2007 for Kinfit by doxygen 1.3.2