12#if __GNUC__ >= 14 && defined(__aarch64__)
13# pragma GCC diagnostic ignored "-Warray-bounds"
34constexpr double s_thetaGainDampingValue = 0.1;
35const AmgVector(5) s_cov0Vec {250., 250., 0.25, 0.25, 0.000001};
51 std::array<element, GSFConstants::maxNumberofStateComponents> elements{};
52 size_t numElements = 0;
56int measurementCoord_1D(
int paramKey) {
57 for (
int i = 0;
i < 5; ++
i) {
58 if (paramKey & (1 << i)) {
73thetaPhiWithinRange_5D(
const AmgVector(5) & V,
const RangeCheckDef rcd)
83thetaWithinRange_5D(
const AmgVector(5) & V)
91 const RangeCheckDef rcd)
112 M.fillSymmetric(0, 3, -M(0, 3));
113 M.fillSymmetric(1, 3, -M(1, 3));
114 M.fillSymmetric(2, 3, -M(2, 3));
115 M.fillSymmetric(3, 4, -M(3, 4));
135 if (key == 3 || key == 7 || key == 15) {
137 return M.topLeftCorner<DIM, DIM>();
140 Eigen::Matrix<int, DIM, 1, 0, DIM, 1> iv;
142 for (
int i = 0, k = 0;
i < 5; ++
i) {
143 iv[
k++] =
key & (1 <<
i) ? i : 0;
147 covSubMatrix.setZero();
148 for (
int i = 0;
i < DIM; ++
i) {
149 for (
int j = 0; j < DIM; ++j) {
150 covSubMatrix(i, j) = M(iv(i), iv(j));
173 const int mk = measurementCoord_1D(paramKey);
175 const AmgVector(5)& trkPar = TP.parameters();
177 const double r = measPar - trkPar(mk);
178 double R = measCov + trkCov(mk, mk);
188 if (!thetaWithinRange_5D(newPar)) {
191 (std::abs(R *
r) > 1.0 ||
198 newPar = trkPar + dampedCov * R *
r;
209 trkCov.
similarity(M) + K * measCov * K.transpose();
211 if (!thetaPhiWithinRange_5D(newPar, absoluteCheck) &&
212 !correctThetaPhiRange_5D(newPar, newCov, absoluteCheck)) {
216 const double predictedResidual = (measPar - newPar(mk));
219 double chiSquared = measCov - updatedCov(mk, mk);
220 if (chiSquared != 0.0) {
238 const AmgVector(5)& trkPar = TP.parameters();
253 fQ =
Trk::FitQualityOnSurface(chiSquared, 5);
254 TP.updateParameters(newPar, newCov);
260calculateFilterStep_T(
Trk::TrackParameters& TP,
265 Trk::FitQualityOnSurface& fQ)
269 const AmgVector(5)& trkPar = TP.parameters();
272 s_reMatrices.expansionMatrix(paramKey).topLeftCorner<DIM, 5>();
275 if (paramKey == 3 || paramKey == 7 || paramKey == 15) {
276 projTrkPar = trkPar.head<DIM>();
278 projTrkPar =
H * trkPar;
283 const AmgVector(DIM)
r = measPar - projTrkPar;
286 (measCov + projection_T<DIM>(trkCov, paramKey)).inverse();
288 const AmgMatrix(5, DIM) K = trkCov *
H.transpose() * R;
289 const AmgMatrix(5, 5) M = s_unitMatrix - K *
H;
295 trkCov.
similarity(M) + K * measCov * K.transpose();
298 fQ =
Trk::FitQualityOnSurface(chiSquared, DIM);
300 TP.updateParameters(newPar, newCov);
312filterStep(
Trk::TrackParameters& trackParameters,
313 Trk::FitQualityOnSurface& fitQos,
314 const Trk::LocalParameters& measurement,
318 const AmgSymMatrix(5)* trkCov = trackParameters.covariance();
323 const int nLocCoord = measCovariance.cols();
324 if (!(measurement.dimension() == nLocCoord)) {
330 return calculateFilterStep_1D(trackParameters,
333 measCovariance(0, 0),
334 measurement.parameterKey(),
338 return calculateFilterStep_T<2>(trackParameters,
340 measurement.head<2>(),
341 measCovariance.topLeftCorner<2, 2>(),
342 measurement.parameterKey(),
346 return calculateFilterStep_T<3>(trackParameters,
348 measurement.head<3>(0),
349 measCovariance.topLeftCorner<3, 3>(),
350 measurement.parameterKey(),
354 return calculateFilterStep_T<4>(trackParameters,
356 measurement.head<4>(0),
357 measCovariance.topLeftCorner<4, 4>(),
358 measurement.parameterKey(),
362 return calculateFilterStep_5D(trackParameters,
364 measurement.head<5>(),
365 measCovariance.topLeftCorner<5, 5>(),
387 const int mk = measurementCoord_1D(paramKey);
388 const double r = valRio - trkPar(mk);
391 if (chiSquared == 0.0) {
411 s_reMatrices.expansionMatrix(paramKey).topLeftCorner<DIM, 5>();
414 AmgSymMatrix(DIM) R = projection_T<DIM>(trkCov, paramKey);
418 updatedFitQoS =
Trk::FitQualityOnSurface(chiSquared, DIM);
423stateFitQuality(
Trk::FitQualityOnSurface& updatedFitQoS,
425 const Trk::LocalParameters& position,
428 if (!trkPar.covariance()) {
433 const int nLocCoord = covariance.cols();
436 return makeChi2_1D(updatedFitQoS,
438 (*trkPar.covariance()),
444 return makeChi2_T<2>(updatedFitQoS,
446 (*trkPar.covariance()),
448 covariance.topLeftCorner<2, 2>(),
463std::pair<double, double>
472 s_reMatrices.expansionMatrix(paramKey).topLeftCorner<DIM, 5>();
475 const AmgVector(DIM)
r = measPar -
H * componentTrackParameters->parameters();
480 const double det = R.determinant();
488std::pair<double, double>
491 const double measPar,
492 const double measCov,
495 const int mk = measurementCoord_1D(paramKey);
497 const double r = measPar - (componentTrackParameters->parameters())(mk);
500 const double R = measCov + (*predictedCov)(mk, mk);
505 return { R,
r *
r / R };
508std::pair<double, double>
516 measPar - componentTrackParameters->parameters().
head<2>();
521 const double det = R.determinant();
534 const size_t predictedStateSize = predictedState.size();
535 if (predictedStateSize == 0) {
539 throw std::runtime_error(
"PosteriorWeightsCalculator :Invalid predictedState size");
544 if (nLocCoord < 1 || nLocCoord > 5) {
552 componentsCache determinantRandChi2{};
553 double minimumChi2(10.e10);
555 for (
const auto& component : returnMultiComponentState) {
558 component.params.get();
559 if (!componentTrackParameters) {
563 componentTrackParameters->covariance();
568 std::pair<double, double>
result(0, 0);
571 result = calculateWeight_1D(componentTrackParameters,
573 measurementLocalParameters(0),
579 result = calculateWeight_2D_3(
580 componentTrackParameters,
582 measurementLocalParameters.head<2>(),
585 result = calculateWeight_T<2>(
586 componentTrackParameters,
588 measurementLocalParameters.head<2>(),
595 calculateWeight_T<3>(componentTrackParameters,
597 measurementLocalParameters.head<3>(),
603 calculateWeight_T<4>(componentTrackParameters,
605 measurementLocalParameters.head<4>(),
611 calculateWeight_T<5>(componentTrackParameters,
613 measurementLocalParameters.head<5>(),
625 determinantRandChi2.elements[determinantRandChi2.numElements] = {
628 ++determinantRandChi2.numElements;
629 if (
result.second < minimumChi2) {
630 minimumChi2 =
result.second;
635 if (determinantRandChi2.numElements != predictedStateSize) {
641 double sumWeights(0.);
642 std::array<double, GSFConstants::maxNumberofStateComponents> fallBackWeights{};
643 auto componentItr = returnMultiComponentState.begin();
644 for (; componentItr != returnMultiComponentState.end();
645 ++componentItr, ++
index) {
647 const double chi2 = determinantRandChi2.elements[
index].chi2 - minimumChi2;
648 const double priorWeight = componentItr->weight;
649 fallBackWeights[
index] = priorWeight;
650 double updatedWeight(0.);
653 if (determinantRandChi2.elements[
index].determinantR > 1e-20) {
656 sqrt(1. / determinantRandChi2.elements[
index].determinantR) *
659 updatedWeight = 1
e-10;
661 componentItr->weight = updatedWeight;
662 sumWeights += updatedWeight;
664 if (sumWeights > 0.) {
665 const double invertSumWeights = 1. / sumWeights;
667 for (
auto& returnComponent : returnMultiComponentState) {
668 returnComponent.weight *= invertSumWeights;
672 size_t fallbackIndex(0);
673 for (
auto& returnComponent : returnMultiComponentState) {
674 returnComponent.weight = fallBackWeights[fallbackIndex];
678 return returnMultiComponentState;
688 const auto* measuredCov = trackParameters->covariance();
693 for (
int i = 0;
i < 5; ++
i) {
694 if ((*measuredCov)(i, i) <= 0.) {
706 std::move(stateBeforeUpdate);
711 const bool rebuildCov = invalidComponent(trackParameters);
714 const double covarianceScaler = 1.;
715 bigNewCovarianceMatrix(0, 0) = 250. * covarianceScaler;
716 bigNewCovarianceMatrix(1, 1) = 250. * covarianceScaler;
717 bigNewCovarianceMatrix(2, 2) = 0.25;
718 bigNewCovarianceMatrix(3, 3) = 0.25;
719 bigNewCovarianceMatrix(4, 4) = 0.001 * 0.001;
720 component.params->updateParameters(trackParameters->parameters(),
721 bigNewCovarianceMatrix);
724 return stateWithInsertedErrors;
734Trk::MultiComponentState
735calculateFilterStep(
Trk::MultiComponentState&& stateBeforeUpdate,
736 const Trk::MeasurementBase& measurement,
737 Trk::FitQualityOnSurface& fitQoS)
741 if (stateBeforeUpdate.empty()) {
747 weights(std::move(stateBeforeUpdate), measurement);
749 if (stateWithNewWeights.empty()) {
756 if (stateWithNewWeights.size() > 1 &&
757 std::abs(component.params->parameters()[
Trk::qOverP]) > 0.033333) {
762 bool const updateSuccess = filterStep(*(component.params),
764 measurement.localParameters(),
765 measurement.localCovariance());
766 if (!updateSuccess) {
770 if (invalidComponent(component.params.get())) {
782 if (degreesOfFreedom == 0.0) {
788 std::move(component));
794 if (assembledUpdatedState.empty()) {
798 fitQoS.setChiSquared(chiSquared);
799 fitQoS.setNumberDoF(degreesOfFreedom);
802 return assembledUpdatedState;
816 bool rebuildStateWithErrors =
false;
820 rebuildStateWithErrors =
821 rebuildStateWithErrors || invalidComponent(component.params.get());
824 if (rebuildStateWithErrors) {
826 rebuildState(std::move(stateBeforeUpdate));
829 std::move(stateWithInsertedErrors), measurement, fitQoS);
830 if (updatedState.empty()) {
838 std::move(stateBeforeUpdate), measurement, fitQoS);
840 if (updatedState.empty()) {
853 stateFitQuality(componentFitQuality,
854 *component.params.get(),
Scalar theta() const
theta method
Matrix< Scalar, OtherDerived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime > similarity(const MatrixBase< OtherDerived > &m) const
similarity method : yields ms = m*s*m^T
#define AmgSymMatrix(dim)
#define AmgMatrix(rows, cols)
Code for performing kalman filter update step on multi-component states for the gaussian-sum filter.
int numberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as integer
double chiSquared() const
returns the of the overall track fit
int parameterKey() const
Identifier key for matrix expansion/reduction.
int dimension() const
Dimension of this localParameters() vector.
This class is the pure abstract base class for all fittable tracking measurements.
const LocalParameters & localParameters() const
Interface method to get the LocalParameters.
const Amg::MatrixX & localCovariance() const
Interface method to get the localError.
void updateParameters(const AmgVector(DIM) &, const AmgSymMatrix(DIM) &)
Update parameters and covariance , passing covariance by ref.
the matrices to access the variably-dimensioned local parameters and map them to the defined five tra...
double chi2(TH1 *h0, TH1 *h1)
std::string head(std::string s, const std::string &pattern)
head of a string
Definition of ATLAS Math & Geometry primitives (Amg)
double chi2(const T &precision, const U &residual, const int sign=1)
constexpr int8_t maxNumberofStateComponents
The state is described by N Gaussian components The Beth Heitler Material effect are also described b...
float chiSquared(const U &p)
FitQualityOnSurface fitQuality(const MultiComponentState &, const MeasurementBase &)
Method for determining the chi2 of the multi-component state and the number of degrees of freedom.
MultiComponentState update(Trk::MultiComponentState &&, const Trk::MeasurementBase &, FitQualityOnSurface &fitQoS)
Method for updating the multi-state with a new measurement and calculate the fit qaulity at the same ...
MultiComponentState assembledState(MultiComponentStateAssembler::Cache &&cache)
Method to return the cached state object - it performs a reweighting before returning the object base...
void addComponent(MultiComponentStateAssembler::Cache &cache, ComponentParameters &&multiComponentState)
Method to add a single set of Trk::ComponentParameters to the cached Trk::MultiComponentState object ...
void renormaliseState(MultiComponentState &, double norm=1)
Performing renormalisation of total state weighting to one.
Ensure that the ATLAS eigen extensions are properly loaded.
std::vector< ComponentParameters > MultiComponentState
ParametersBase< TrackParametersDim, Charged > TrackParameters