File indexing completed on 2025-08-05 08:18:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include "MeasuredStateOnPlane.h"
0021 #include "AbsTrackRep.h"
0022 #include "Exception.h"
0023 #include "Tools.h"
0024 #include "IO.h"
0025
0026 #include <cassert>
0027
0028 #include "TDecompChol.h"
0029
0030 namespace genfit {
0031
0032 void MeasuredStateOnPlane::Print(Option_t*) const {
0033 printOut << "genfit::MeasuredStateOnPlane ";
0034 printOut << "my address " << this << " my plane's address " << this->sharedPlane_.get() << "; use count: " << sharedPlane_.use_count() << std::endl;
0035 printOut << " state vector: "; state_.Print();
0036 printOut << " covariance matrix: "; cov_.Print();
0037 if (sharedPlane_ != nullptr) {
0038 printOut << " defined in plane "; sharedPlane_->Print();
0039 TVector3 pos, mom;
0040 TMatrixDSym cov(6,6);
0041 getRep()->getPosMomCov(*this, pos, mom, cov);
0042 printOut << " 3D position: "; pos.Print();
0043 printOut << " 3D momentum: "; mom.Print();
0044
0045 }
0046 }
0047
0048 void MeasuredStateOnPlane::blowUpCov(double blowUpFac, bool resetOffDiagonals, double maxVal) {
0049
0050 unsigned int dim = cov_.GetNcols();
0051
0052 if (resetOffDiagonals) {
0053 for (unsigned int i=0; i<dim; ++i) {
0054 for (unsigned int j=0; j<dim; ++j) {
0055 if (i != j)
0056 cov_(i,j) = 0;
0057 else
0058 cov_(i,j) *= blowUpFac;
0059 }
0060 }
0061 }
0062 else
0063 cov_ *= blowUpFac;
0064
0065
0066 if (maxVal > 0.)
0067 for (unsigned int i=0; i<dim; ++i) {
0068 for (unsigned int j=0; j<dim; ++j) {
0069 cov_(i,j) = std::min(cov_(i,j), maxVal);
0070 }
0071 }
0072
0073 }
0074
0075
0076 MeasuredStateOnPlane calcAverageState(const MeasuredStateOnPlane& forwardState, const MeasuredStateOnPlane& backwardState) {
0077
0078 if (forwardState.getPlane() != backwardState.getPlane()) {
0079 Exception e("KalmanFitterInfo::calcAverageState: forwardState and backwardState are not defined in the same plane.", __LINE__,__FILE__);
0080 throw e;
0081 }
0082
0083
0084
0085
0086 #if 0
0087
0088
0089 TMatrixDSym fCovInv, bCovInv, smoothed_cov;
0090 tools::invertMatrix(forwardState.getCov(), fCovInv);
0091 tools::invertMatrix(backwardState.getCov(), bCovInv);
0092
0093 tools::invertMatrix(fCovInv + bCovInv, smoothed_cov);
0094
0095 MeasuredStateOnPlane retVal(forwardState);
0096 retVal.setState(smoothed_cov*(fCovInv*forwardState.getState() + bCovInv*backwardState.getState()));
0097 retVal.setCov(smoothed_cov);
0098 return retVal;
0099 #endif
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 TDecompChol d1(forwardState.getCov());
0144 bool success = d1.Decompose();
0145 TDecompChol d2(backwardState.getCov());
0146 success &= d2.Decompose();
0147
0148 if (!success) {
0149 Exception e("KalmanFitterInfo::calcAverageState: ill-conditioned covariance matrix.", __LINE__,__FILE__);
0150 throw e;
0151 }
0152
0153 int nRows = d1.GetU().GetNrows();
0154 assert(nRows == d2.GetU().GetNrows());
0155 TMatrixD S1inv, S2inv;
0156 tools::transposedInvert(d1.GetU(), S1inv);
0157 tools::transposedInvert(d2.GetU(), S2inv);
0158
0159 TMatrixD A(2*nRows, nRows);
0160 TVectorD b(2 * nRows);
0161 double *const bk = b.GetMatrixArray();
0162 double *const Ak = A.GetMatrixArray();
0163 const double* S1invk = S1inv.GetMatrixArray();
0164 const double* S2invk = S2inv.GetMatrixArray();
0165
0166 for (int i = 0; i < nRows; ++i) {
0167 double sum1 = 0;
0168 double sum2 = 0;
0169 for (int j = 0; j <= i; ++j) {
0170 Ak[i*nRows + j] = S1invk[i*nRows + j];
0171 Ak[(i + nRows)*nRows + j] = S2invk[i*nRows + j];
0172 sum1 += S1invk[i*nRows + j]*forwardState.getState().GetMatrixArray()[j];
0173 sum2 += S2invk[i*nRows + j]*backwardState.getState().GetMatrixArray()[j];
0174 }
0175 bk[i] = sum1;
0176 bk[i + nRows] = sum2;
0177 }
0178
0179 tools::QR(A, b);
0180 A.ResizeTo(nRows, nRows);
0181
0182 TMatrixD inv;
0183 tools::transposedInvert(A, inv);
0184 b.ResizeTo(nRows);
0185 for (int i = 0; i < nRows; ++i) {
0186 double sum = 0;
0187 for (int j = i; j < nRows; ++j) {
0188 sum += inv.GetMatrixArray()[j*nRows+i] * b[j];
0189 }
0190 b.GetMatrixArray()[i] = sum;
0191 }
0192 return MeasuredStateOnPlane(b,
0193 TMatrixDSym(TMatrixDSym::kAtA, inv),
0194 forwardState.getPlane(),
0195 forwardState.getRep(),
0196 forwardState.getAuxInfo());
0197 }
0198
0199
0200 }