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

TrackMomentumConstraint.C

Go to the documentation of this file.
00001 
00009 #include "jbltools/kinfit/TrackMomentumConstraint.h"
00010 #include "jbltools/kinfit/TrackFitObject.h"
00011 
00012 #include<iostream>
00013 #include<cassert>
00014 
00015 using std::cout;
00016 using std::endl;
00017 
00018 TrackMomentumConstraint::TrackMomentumConstraint (double pxfact_, double pyfact_, double pzfact_,
00019                           double efact_, double value_) 
00020     : factor (efact_, -pxfact_, -pyfact_, -pzfact_),  
00021       value (value_),
00022       cachevalid(false)
00023 {}
00024 
00025 TrackMomentumConstraint::TrackMomentumConstraint (int axis, double value_) 
00026     : value (value_),
00027       cachevalid(false)
00028 {
00029   switch (axis) {
00030     case 1: factor.setValues (0, -1, 0, 0);
00031       break;
00032     case 2: factor.setValues (0, 0, -1, 0);
00033       break;
00034     case 3: factor.setValues (0, 0, 0, -1);
00035       break;
00036     default: factor.setValues (1, 0, 0, 0);
00037       break;
00038   }
00039 }
00040 
00041 // destructor
00042 TrackMomentumConstraint::~TrackMomentumConstraint () {}
00043 
00044 // calculate current value of constraint function
00045 double TrackMomentumConstraint::getValue() const {
00046   FourVector sum;
00047   for (unsigned int i = 0; i < fitobjects.size(); i++) {
00048     sum += sign[i]*(fitobjects[i]->getMomentum (flags[i]));
00049 //     cout <<  "TrackMomentumConstraint::getValue: i=" << i << ", sign=" << sign[i]
00050 //          << ", mom=" << fitobjects[i]->getMomentum (flags[i])
00051 //          << ", sign*mom=" << sign[i]*fitobjects[i]->getMomentum (flags[i])
00052 //          << ", new sum=" << sum << endl;
00053   }
00054 //   cout <<  "factor=" << factor << ", result=" << factor*sum - value << endl;
00055   return factor*sum - value;
00056 }
00057 
00058 // calculate vector/array of derivatives of this contraint 
00059 // w.r.t. to ALL parameters of all fitobjects
00060 // here: d sum(px) /d par(i,j) 
00061 //                      = d sum(px) /d px(i) * d px(i) /d par(i, j)
00062 //                                      =  1 * d px(i) /d par(i, j)
00063 void TrackMomentumConstraint::getDerivatives(int idim, double der[]) const {
00064   for (int iglobal = 0; iglobal < idim; iglobal++) der[iglobal] = 0;
00065   for (unsigned int i = 0; i < fitobjects.size(); i++) {
00066     for (int ilocal = 0; ilocal < fitobjects[i]->getNPar(); ilocal++) {
00067       if (!fitobjects[i]->isParamFixed(ilocal)) {
00068         int iglobal = fitobjects[i]->getGlobalParNum (ilocal);
00069         assert (iglobal >= 0 && iglobal < idim);
00070         der[iglobal] += factor*(sign[i]*fitobjects[i]->getMomentumDerivative (flags[i], ilocal));
00071       }
00072     }
00073   }
00074 }
00075   
00076 void TrackMomentumConstraint::add1stDerivativesToMatrix(int idim, double *M) const {
00077 
00078   assert (0);
00079 
00080   assert (M);
00081   int kglobal = getGlobalNum();
00082   assert (kglobal >= 0 && kglobal < idim);
00083   
00084   for (ConstFitObjectIterator i = fitobjects.begin(); i != fitobjects.end(); ++i) {
00085     const TrackFitObject *fo = *i;
00086     assert (fo);
00087     for (int ilocal = 0; ilocal < fo->getNPar(); ++ilocal) {
00088       if (!fo->isParamFixed(ilocal)) {
00089         int iglobal = fo->getGlobalParNum (ilocal);
00090         assert (iglobal >= 0 && iglobal < idim);
00091         double d = 0;
00092         M[idim*iglobal+kglobal] += d;
00093         M[idim*kglobal+iglobal] += d;
00094       }
00095     }
00096     
00097   }
00098 }
00099   
00100 void TrackMomentumConstraint::add2ndDerivativesToMatrix(int idim, double *M, double lambda) const {
00101 
00102   assert (0);
00103 }
00104     
00105 void TrackMomentumConstraint::addToGlobalDerMatrix (double lambda, int idim, double *M) const {
00106 
00107   assert (0);
00108   // Add lambda*d^2 g / d x_i dx_j to global matrix
00109   
00110   if (lambda == 0) return;
00111   
00112   // d^2 g / (dx_i dx_j) = 
00113   //   = sum_k,l d^2 g/(dpx_k dpx_l) * dpx_k/dx_i dpx_l/dx_j
00114   //     + sum_k dg/dpx_k * d^2 px_k/(dx_i dx_j)
00115   //   = sum_k,l      1              * dpx_k/dx_i dpx_l/dx_j
00116   
00117   // assume here that different 4-vectors always depend on 
00118   // different parameters!
00119   
00120   if (!cachevalid) updateCache();
00121   
00122   int *globalParNum = new int[nparams];
00123   double *der = new double[nparams];
00124   
00125 //   ipar = 0;
00126 //   for (int i = 0; i < fitobjects.size(); i++) {
00127 //     for (int ilocal = 0; ilocal < fitobjects[i]->getNPar(); ilocal++) {
00128 //       int iglobal = fitobjects[i]->getGlobalParNum (ilocal);
00129 //       if (iglobal >= 0) {
00130 //         assert (ipar < nparams);
00131 //         globalParNum[ipar] = iglobal;
00132 //         der[ipar] = fitobjects[i]->getDPx (ilocal);
00133 //         ipar++;
00134 //       }
00135 //   }
00136   
00137   for (int ipar = 0; ipar < nparams; ipar++) {
00138     int iglobal = globalParNum[ipar];
00139     double der_i = der[ipar];
00140     for (int jpar = ipar; jpar < nparams; jpar++) {
00141       int jglobal = globalParNum[ipar];
00142       double der_j = der[jpar];
00143       double l_der_ij = lambda*der_i*der_j;
00144       M[idim*iglobal+jglobal] += l_der_ij;
00145       if (ipar != jpar) M[idim*jglobal+iglobal] += l_der_ij;
00146     }
00147   }       
00148 }
00149 
00150 void TrackMomentumConstraint::invalidateCache() const {
00151   cachevalid = false;
00152 }
00153 
00154 void TrackMomentumConstraint::updateCache() const {
00155   nparams = 0;
00156   for (unsigned int i = 0; i < fitobjects.size(); i++) {
00157     for (int ilocal = 0; ilocal < fitobjects[i]->getNPar(); ilocal++) {
00158       if (!fitobjects[i]->isParamFixed(ilocal)) {
00159         int iglobal = fitobjects[i]->getGlobalParNum (ilocal);
00160         if (iglobal >= 0) nparams++;
00161       }
00162     }
00163   }
00164   cachevalid = true;
00165 }
00166   

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