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>(),
624 determinantRandChi2.elements[determinantRandChi2.numElements] = {
627 ++determinantRandChi2.numElements;
628 if (
result.second < minimumChi2) {
629 minimumChi2 =
result.second;
634 if (determinantRandChi2.numElements != predictedStateSize) {
640 double sumWeights(0.);
641 std::array<double, GSFConstants::maxNumberofStateComponents> fallBackWeights{};
642 auto componentItr = returnMultiComponentState.begin();
643 for (; componentItr != returnMultiComponentState.end();
644 ++componentItr, ++
index) {
646 const double chi2 = determinantRandChi2.elements[
index].chi2 - minimumChi2;
647 const double priorWeight = componentItr->weight;
648 fallBackWeights[
index] = priorWeight;
649 double updatedWeight(0.);
652 if (determinantRandChi2.elements[
index].determinantR > 1e-20) {
655 sqrt(1. / determinantRandChi2.elements[
index].determinantR) *
658 updatedWeight = 1
e-10;
660 componentItr->weight = updatedWeight;
661 sumWeights += updatedWeight;
663 if (sumWeights > 0.) {
664 const double invertSumWeights = 1. / sumWeights;
666 for (
auto& returnComponent : returnMultiComponentState) {
667 returnComponent.weight *= invertSumWeights;
671 size_t fallbackIndex(0);
672 for (
auto& returnComponent : returnMultiComponentState) {
673 returnComponent.weight = fallBackWeights[fallbackIndex];
677 return returnMultiComponentState;
687 const auto* measuredCov = trackParameters->covariance();
692 for (
int i = 0;
i < 5; ++
i) {
693 if ((*measuredCov)(i, i) <= 0.) {
705 std::move(stateBeforeUpdate);
710 const bool rebuildCov = invalidComponent(trackParameters);
713 const double covarianceScaler = 1.;
714 bigNewCovarianceMatrix(0, 0) = 250. * covarianceScaler;
715 bigNewCovarianceMatrix(1, 1) = 250. * covarianceScaler;
716 bigNewCovarianceMatrix(2, 2) = 0.25;
717 bigNewCovarianceMatrix(3, 3) = 0.25;
718 bigNewCovarianceMatrix(4, 4) = 0.001 * 0.001;
719 component.params->updateParameters(trackParameters->parameters(),
720 bigNewCovarianceMatrix);
723 return stateWithInsertedErrors;
733Trk::MultiComponentState
734calculateFilterStep(
Trk::MultiComponentState&& stateBeforeUpdate,
735 const Trk::MeasurementBase& measurement,
736 Trk::FitQualityOnSurface& fitQoS)
740 if (stateBeforeUpdate.empty()) {
746 weights(std::move(stateBeforeUpdate), measurement);
748 if (stateWithNewWeights.empty()) {
755 if (stateWithNewWeights.size() > 1 &&
756 std::abs(component.params->parameters()[
Trk::qOverP]) > 0.033333) {
761 bool const updateSuccess = filterStep(*(component.params),
763 measurement.localParameters(),
764 measurement.localCovariance());
765 if (!updateSuccess) {
769 if (invalidComponent(component.params.get())) {
781 if (degreesOfFreedom == 0.0) {
787 std::move(component));
793 if (assembledUpdatedState.empty()) {
797 fitQoS.setChiSquared(chiSquared);
798 fitQoS.setNumberDoF(degreesOfFreedom);
801 return assembledUpdatedState;
815 bool rebuildStateWithErrors =
false;
819 rebuildStateWithErrors =
820 rebuildStateWithErrors || invalidComponent(component.params.get());
823 if (rebuildStateWithErrors) {
825 rebuildState(std::move(stateBeforeUpdate));
828 std::move(stateWithInsertedErrors), measurement, fitQoS);
829 if (updatedState.empty()) {
837 std::move(stateBeforeUpdate), measurement, fitQoS);
839 if (updatedState.empty()) {
852 stateFitQuality(componentFitQuality,
853 *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)
double R(const INavigable4Momentum *p1, const double v_eta, const double v_phi)
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)
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)
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
const IIntersectionCache * cache() const
Retrieve the associated cache block, if it exists.