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
00042 TrackMomentumConstraint::~TrackMomentumConstraint () {}
00043
00044
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
00050
00051
00052
00053 }
00054
00055 return factor*sum - value;
00056 }
00057
00058
00059
00060
00061
00062
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
00109
00110 if (lambda == 0) return;
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 if (!cachevalid) updateCache();
00121
00122 int *globalParNum = new int[nparams];
00123 double *der = new double[nparams];
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
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