ATLAS Offline Software
Loading...
Searching...
No Matches
iPatGlobalFitter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 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 if (alignCache.m_derivMatrix != nullptr) {
39 delete alignCache.m_derivMatrix;
40 }
41 alignCache.m_derivMatrix = nullptr;
42
43 if (alignCache.m_fullCovarianceMatrix != nullptr) {
44 delete alignCache.m_fullCovarianceMatrix;
45 }
46 alignCache.m_fullCovarianceMatrix = nullptr;
47 alignCache.m_iterationsOfLastFit = 0;
48
49 auto [refittedTrack, fitState] =
50 fitWithState(Gaudi::Hive::currentContext(), trk, runOutlier, matEffects);
51
52 if (refittedTrack) {
53 alignCache.m_derivMatrix = derivMatrix(*fitState).release();
54 alignCache.m_fullCovarianceMatrix =
55 fullCovarianceMatrix(*fitState).release();
56 alignCache.m_iterationsOfLastFit = iterationsOfLastFit(*fitState);
57 }
58 return refittedTrack.release();
59}
60
61std::unique_ptr<Amg::MatrixX> iPatGlobalFitter::derivMatrix(
62 const FitState& fitState) const {
63 // copy derivatives to a new HepMatrix
64 if (!fitState.hasMeasurements() || !fitState.parameters) {
65 return nullptr;
66 }
67
68 int numberParameters = 5;
69 if (m_allParameters) {
70 numberParameters = fitState.parameters->numberParameters();
71 }
72 int rows = 0;
73
74 for (const FitMeasurement* m : fitState.getMeasurements()) {
75 if (!m->isPositionMeasurement()) {
76 continue;
77 }
78 rows += m->numberDoF();
79 }
80
81 if (!numberParameters || !rows) {
82 return nullptr;
83 }
84
85 ATH_MSG_VERBOSE(" DerivMatrix : " << fitState.getMeasurements().size()
86 << " measurement objects giving " << rows
87 << " rows and " << numberParameters
88 << " columns (parameters)");
89
90 auto derivativeMatrix =
91 std::make_unique<Amg::MatrixX>(rows, numberParameters);
92 int row = 0;
93 for (const FitMeasurement* m : fitState.getMeasurements()) {
94 if (!m->numberDoF() || !m->isPositionMeasurement()) {
95 continue;
96 }
97 double norm = 0.;
98 if (m->weight() > 0.) {
99 norm = 1. / m->weight();
100 }
101
102 for (int col = 0; col < numberParameters; ++col) {
103 (*derivativeMatrix)(row, col) = norm * m->derivative(col);
104 }
105
106 // take care of units for momentum derivs
107 (*derivativeMatrix)(row, 4) *= Gaudi::Units::TeV;
108 if (fitState.parameters->fitEnergyDeposit()) {
109 (*derivativeMatrix)(row, 5) *= Gaudi::Units::TeV;
110 }
111 ++row;
112 if (m->numberDoF() < 2) {
113 continue;
114 }
115
116 // pixel measurements
117 norm = 0.;
118 if (m->weight2() > 0.)
119 norm = 1. / m->weight2();
120 for (int col = 0; col < numberParameters; ++col) {
121 (*derivativeMatrix)(row, col) = norm * m->derivative2(col);
122 }
123 (*derivativeMatrix)(row, 4) *= Gaudi::Units::TeV;
124 if (fitState.parameters->fitEnergyDeposit()) {
125 (*derivativeMatrix)(row, 5) *= Gaudi::Units::TeV;
126 }
127 ++row;
128 }
129
130 if (row != rows) {
131 ATH_MSG_WARNING("iPatGlobalFitter: inconsistent #rows in deriv matrix ");
132 }
133
134 return derivativeMatrix;
135}
136
137std::unique_ptr<Amg::MatrixX> iPatGlobalFitter::fullCovarianceMatrix(
138 const FitState& fitState) const {
139 int numberParameters = 5;
140
141 if (m_allParameters) {
142 numberParameters = fitState.parameters->numberParameters();
143 }
144 ATH_MSG_VERBOSE(" FullCovarianceMatrix for " << numberParameters
145 << " parameters");
146
147 return std::make_unique<Amg::MatrixX>(Trk::FitProcedure::fullCovariance()->block(
148 0, 0, numberParameters, numberParameters));
149}
150
152 return fitState.iterations;
153}
154
155void iPatGlobalFitter::setMinIterations(int minIterations) {
156 m_fitProcedure->setMinIterations(minIterations);
157}
158} // 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.
int m_iterationsOfLastFit
returns the number of iterations used by the last fit (count starts at 1 for a single-iteration fit)
Amg::MatrixX * m_derivMatrix
access to the matrix of derivatives used during the latest global-chi2 track fit.
Amg::MatrixX * m_fullCovarianceMatrix
access to the global fitter's full covariance matrix.