ATLAS Offline Software
Loading...
Searching...
No Matches
iPatGlobalFitter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
4
6// iPatGlobalFitter.cxx
7// access full derivative and covariance matrices
8//
9// (c) ATLAS Detector software
11
13
14#include "GaudiKernel/SystemOfUnits.h"
15#include "GaudiKernel/ThreadLocalContext.h"
20
21namespace Trk {
23 const std::string& name,
24 const IInterface* parent)
25 : iPatFitter(type, name, parent, true), m_allParameters(false) {
26 declareInterface<IGlobalTrackFitter>(this);
27 declareProperty("AllParameters", m_allParameters);
28}
29
31
33 AlignmentCache& alignCache, const Track& trk,
34 const RunOutlierRemoval runOutlier,
35 const ParticleHypothesis matEffects) const {
36 // @TODO ensure the number of iterations is passed through to the fitter
37 // setMinIterations (alignCache.m_minIterations);
38 alignCache.m_derivMatrix.reset();
39 alignCache.m_fullCovarianceMatrix.reset();
40 alignCache.m_iterationsOfLastFit = 0;
41
42 auto [refittedTrack, fitState] =
43 fitWithState(Gaudi::Hive::currentContext(), trk, runOutlier, matEffects);
44
45 if (refittedTrack) {
46 alignCache.m_derivMatrix = derivMatrix(*fitState);
47 alignCache.m_fullCovarianceMatrix = fullCovarianceMatrix(*fitState);
48 alignCache.m_iterationsOfLastFit = iterationsOfLastFit(*fitState);
49 }
50 return refittedTrack.release();
51}
52
53std::unique_ptr<Amg::MatrixX> iPatGlobalFitter::derivMatrix(
54 const FitState& fitState) const {
55 // copy derivatives to a new HepMatrix
56 if (!fitState.hasMeasurements() || !fitState.parameters) {
57 return nullptr;
58 }
59
60 int numberParameters = 5;
61 if (m_allParameters) {
62 numberParameters = fitState.parameters->numberParameters();
63 }
64 int rows = 0;
65
66 for (const FitMeasurement* m : fitState.getMeasurements()) {
67 if (!m->isPositionMeasurement()) {
68 continue;
69 }
70 rows += m->numberDoF();
71 }
72
73 if (!numberParameters || !rows) {
74 return nullptr;
75 }
76
77 ATH_MSG_VERBOSE(" DerivMatrix : " << fitState.getMeasurements().size()
78 << " measurement objects giving " << rows
79 << " rows and " << numberParameters
80 << " columns (parameters)");
81
82 auto derivativeMatrix =
83 std::make_unique<Amg::MatrixX>(rows, numberParameters);
84 int row = 0;
85 for (const FitMeasurement* m : fitState.getMeasurements()) {
86 if (!m->numberDoF() || !m->isPositionMeasurement()) {
87 continue;
88 }
89 double norm = 0.;
90 if (m->weight() > 0.) {
91 norm = 1. / m->weight();
92 }
93
94 for (int col = 0; col < numberParameters; ++col) {
95 (*derivativeMatrix)(row, col) = norm * m->derivative(col);
96 }
97
98 // take care of units for momentum derivs
99 (*derivativeMatrix)(row, 4) *= Gaudi::Units::TeV;
100 if (fitState.parameters->fitEnergyDeposit()) {
101 (*derivativeMatrix)(row, 5) *= Gaudi::Units::TeV;
102 }
103 ++row;
104 if (m->numberDoF() < 2) {
105 continue;
106 }
107
108 // pixel measurements
109 norm = 0.;
110 if (m->weight2() > 0.)
111 norm = 1. / m->weight2();
112 for (int col = 0; col < numberParameters; ++col) {
113 (*derivativeMatrix)(row, col) = norm * m->derivative2(col);
114 }
115 (*derivativeMatrix)(row, 4) *= Gaudi::Units::TeV;
116 if (fitState.parameters->fitEnergyDeposit()) {
117 (*derivativeMatrix)(row, 5) *= Gaudi::Units::TeV;
118 }
119 ++row;
120 }
121
122 if (row != rows) {
123 ATH_MSG_WARNING("iPatGlobalFitter: inconsistent #rows in deriv matrix ");
124 }
125
126 return derivativeMatrix;
127}
128
129std::unique_ptr<Amg::MatrixX> iPatGlobalFitter::fullCovarianceMatrix(
130 const FitState& fitState) const {
131 int numberParameters = 5;
132
133 if (m_allParameters) {
134 numberParameters = fitState.parameters->numberParameters();
135 }
136 ATH_MSG_VERBOSE(" FullCovarianceMatrix for " << numberParameters
137 << " parameters");
138
139 return std::make_unique<Amg::MatrixX>(Trk::FitProcedure::fullCovariance()->block(
140 0, 0, numberParameters, numberParameters));
141}
142
144 return fitState.iterations;
145}
146
147void iPatGlobalFitter::setMinIterations(int minIterations) {
148 m_fitProcedure->setMinIterations(minIterations);
149}
150} // namespace Trk
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
static Amg::MatrixX * fullCovariance()
bool hasMeasurements() const
Definition iPatFitter.h:132
std::vector< FitMeasurement * > & getMeasurements()
Definition iPatFitter.h:116
std::unique_ptr< FitParameters > parameters
Definition iPatFitter.h:134
iPatFitter(const std::string &type, const std::string &name, const IInterface *parent, bool isGlobalFit=false)
std::pair< std::unique_ptr< Track >, std::unique_ptr< FitState > > fitWithState(const EventContext &ctx, const Track &, const RunOutlierRemoval runOutlier=false, const ParticleHypothesis particleHypothesis=Trk::nonInteracting) const
std::unique_ptr< FitProcedure > m_fitProcedure
Definition iPatFitter.h:148
std::unique_ptr< Amg::MatrixX > derivMatrix(const FitState &fitState) const
GlobalTrackFitter methods: access to the matrix of derivatives used during the latest track fit.
static int iterationsOfLastFit(const FitState &fitState)
access to the number of iterations taken by the latest track fit
void setMinIterations(int minIterations)
set method for the minimum number of iterations for (alignment) friend
Track * alignmentFit(AlignmentCache &, const Track &, const RunOutlierRemoval runOutlier=false, const ParticleHypothesis matEffects=Trk::nonInteracting) const
RE-FIT A TRACK FOR ALIGNMENT.
std::unique_ptr< Amg::MatrixX > fullCovarianceMatrix(const FitState &fitState) const
access to the global fitter's full covariance matrix
iPatGlobalFitter(const std::string &type, const std::string &name, const IInterface *parent)
Ensure that the ATLAS eigen extensions are properly loaded.
bool RunOutlierRemoval
switch to toggle quality processing after fit
Definition FitterTypes.h:22
ParticleHypothesis
Enumeration for Particle hypothesis respecting the interaction with material.
std::unique_ptr< Amg::MatrixX > m_fullCovarianceMatrix
access to the global fitter's full covariance matrix.
int m_iterationsOfLastFit
returns the number of iterations used by the last fit (count starts at 1 for a single-iteration fit)
std::unique_ptr< Amg::MatrixX > m_derivMatrix
access to the matrix of derivatives used during the latest global-chi2 track fit.