ATLAS Offline Software
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 
21 namespace 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 
61 std::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 
137 std::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>(m_fitProcedure->fullCovariance()->block(
148  0, 0, numberParameters, numberParameters));
149 }
150 
152  return fitState.iterations;
153 }
154 
155 void iPatGlobalFitter::setMinIterations(int minIterations) {
156  m_fitProcedure->setMinIterations(minIterations);
157 }
158 } // namespace Trk
query_example.row
row
Definition: query_example.py:24
PlotCalibFromCool.norm
norm
Definition: PlotCalibFromCool.py:100
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
Trk::IGlobalTrackFitter::AlignmentCache::m_derivMatrix
Amg::MatrixX * m_derivMatrix
access to the matrix of derivatives used during the latest global-chi2 track fit.
Definition: IGlobalTrackFitter.h:42
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Trk::iPatFitter::FitState::hasMeasurements
bool hasMeasurements() const
Definition: iPatFitter.h:132
Trk::iPatFitter
Main Fitter tool providing the implementation for the different fitting, extension and refitting use ...
Definition: iPatFitter.h:37
Trk::iPatGlobalFitter::derivMatrix
std::unique_ptr< Amg::MatrixX > derivMatrix(const FitState &fitState) const
GlobalTrackFitter methods: access to the matrix of derivatives used during the latest track fit.
Definition: iPatGlobalFitter.cxx:61
Trk::IGlobalTrackFitter::AlignmentCache::m_iterationsOfLastFit
int m_iterationsOfLastFit
returns the number of iterations used by the last fit (count starts at 1 for a single-iteration fit)
Definition: IGlobalTrackFitter.h:49
python.SystemOfUnits.TeV
int TeV
Definition: SystemOfUnits.py:158
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
FitProcedure.h
FitParameters.h
Trk::RunOutlierRemoval
bool RunOutlierRemoval
switch to toggle quality processing after fit
Definition: FitterTypes.h:22
Trk::IGlobalTrackFitter::AlignmentCache::m_fullCovarianceMatrix
Amg::MatrixX * m_fullCovarianceMatrix
access to the global fitter's full covariance matrix.
Definition: IGlobalTrackFitter.h:45
ExtrapolationType.h
Trk::ParticleHypothesis
ParticleHypothesis
Definition: ParticleHypothesis.h:25
Trk::iPatFitter::FitState::parameters
std::unique_ptr< FitParameters > parameters
Definition: iPatFitter.h:134
Trk::IGlobalTrackFitter::AlignmentCache
Definition: IGlobalTrackFitter.h:38
iPatGlobalFitter.h
Trk::FitMeasurement
Definition: FitMeasurement.h:40
test_pyathena.parent
parent
Definition: test_pyathena.py:15
Trk::iPatFitter::m_fitProcedure
std::unique_ptr< FitProcedure > m_fitProcedure
Definition: iPatFitter.h:148
beamspotnt.rows
list rows
Definition: bin/beamspotnt.py:1112
Trk::iPatGlobalFitter::setMinIterations
void setMinIterations(int minIterations)
set method for the minimum number of iterations for (alignment) friend
Definition: iPatGlobalFitter.cxx:155
Trk::iPatGlobalFitter::iPatGlobalFitter
iPatGlobalFitter(const std::string &type, const std::string &name, const IInterface *parent)
Definition: iPatGlobalFitter.cxx:22
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::iPatFitter::FitState
Definition: iPatFitter.h:99
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
Trk::iPatGlobalFitter::iterationsOfLastFit
static int iterationsOfLastFit(const FitState &fitState)
access to the number of iterations taken by the latest track fit
Definition: iPatGlobalFitter.cxx:151
query_example.col
col
Definition: query_example.py:7
FitMeasurement.h
Trk::iPatFitter::fitWithState
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
Definition: iPatFitter.cxx:179
Trk::iPatGlobalFitter::alignmentFit
Track * alignmentFit(AlignmentCache &, const Track &, const RunOutlierRemoval runOutlier=false, const ParticleHypothesis matEffects=Trk::nonInteracting) const
RE-FIT A TRACK FOR ALIGNMENT.
Definition: iPatGlobalFitter.cxx:32
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::iPatGlobalFitter::fullCovarianceMatrix
std::unique_ptr< Amg::MatrixX > fullCovarianceMatrix(const FitState &fitState) const
access to the global fitter's full covariance matrix
Definition: iPatGlobalFitter.cxx:137
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Trk::iPatGlobalFitter::m_allParameters
bool m_allParameters
Definition: iPatGlobalFitter.h:52
Trk::iPatGlobalFitter::~iPatGlobalFitter
~iPatGlobalFitter(void)
Track
Definition: TriggerChamberClusterOnTrackCreator.h:21
Trk::iPatFitter::FitState::getMeasurements
std::vector< FitMeasurement * > & getMeasurements()
Definition: iPatFitter.h:116
Trk::iPatFitter::FitState::iterations
int iterations
Definition: iPatFitter.h:135