ATLAS Offline Software
Loading...
Searching...
No Matches
MeasurementToTrackParticleDecorationAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
15#include "Acts/Surfaces/AnnulusBounds.hpp"
16#include "Acts/Utilities/TrackHelpers.hpp"
19
20
21using namespace Acts::UnitLiterals;
22
23
24namespace ActsTrk {
25
27 ISvcLocator *pSvcLocator) :
28 AthReentrantAlgorithm(name,pSvcLocator)
29 {}
30
32 {
33 ATH_MSG_DEBUG("Initializing " << name() << " ...");
34
35 ATH_CHECK(m_trackParticlesKey.initialize());
36
37 // Decorations
40 ATH_CHECK(m_measurementLayerKey.initialize());
41 ATH_CHECK(m_chi2HitPredictedKey.initialize());
42 ATH_CHECK(m_chi2HitFilteredKey.initialize());
43 ATH_CHECK(m_measurementTypeKey.initialize());
46 ATH_CHECK(m_residualLocXkey.initialize());
47 ATH_CHECK(m_pullLocXkey.initialize());
48 ATH_CHECK(m_measurementLocXkey.initialize());
52 ATH_CHECK(m_residualLocYkey.initialize());
53 ATH_CHECK(m_pullLocYkey.initialize());
54 ATH_CHECK(m_measurementLocYkey.initialize());
58
60
61 return StatusCode::SUCCESS;
62 }
63
64 StatusCode MeasurementToTrackParticleDecorationAlg::execute(const EventContext& ctx) const
65 {
66 ATH_MSG_DEBUG("Executing " << name() << " ...");
67
68 auto tgContext = m_trackingGeometryTool->getGeometryContext(ctx).context();
69
76
79
86
93
95 ATH_CHECK(trackParticlesHandle.isValid());
96 const xAOD::TrackParticleContainer *track_particles = trackParticlesHandle.cptr();
97
98 for (const xAOD::TrackParticle *track_particle : *track_particles) {
99
100 std::optional<ActsTrk::TrackContainer::ConstTrackProxy> optional_track = getActsTrack(*track_particle);
101
102 if ( not optional_track.has_value() ) {
103 ATH_MSG_WARNING("Invalid track link for particle " << track_particle->index() << ". Skipping track..");
104 continue;
105 }
106
107 ATH_MSG_DEBUG("Track link found for track particle with index " << track_particle->index());
108 ActsTrk::TrackContainer::ConstTrackProxy track = optional_track.value();
109
110 std::vector<int>& regions{measurementRegionHandle(*track_particle)};
111 regions.reserve(track.nMeasurements());
112 std::vector<int>& detectors{measurementDetectorHandle(*track_particle)};
113 detectors.reserve(track.nMeasurements());
114 std::vector<int>& layers{measurementLayerHandle(*track_particle)};
115 layers.reserve(track.nMeasurements());
116 std::vector<int>& types{measurementTypeHandle(*track_particle)};
117 types.reserve(track.nMeasurements());
118 std::vector<float>& predchi2s{chi2HitPredictedHandle(*track_particle)};
119 predchi2s.reserve(track.nMeasurements());
120 std::vector<float>& filtchi2s{chi2HitFilteredHandle(*track_particle)};
121 filtchi2s.reserve(track.nMeasurements());
122
123 std::vector<int>& sizesPhi{measurementPhiWidthHandle(*track_particle)};
124 sizesPhi.reserve(track.nMeasurements());
125 std::vector<int>& sizesEta{measurementEtaWidthHandle(*track_particle)};
126 sizesEta.reserve(track.nMeasurements());
127 std::vector<float>& residualsLocX{residualLocXhandle(*track_particle)};
128 residualsLocX.reserve(track.nMeasurements());
129 std::vector<float>& pullsLocX{pullLocXhandle(*track_particle)};
130 pullsLocX.reserve(track.nMeasurements());
131 std::vector<float>& measurementsLocX{measurementLocXhandle(*track_particle)};
132 measurementsLocX.reserve(track.nMeasurements());
133 std::vector<float>& trackParametersLocX{trackParameterLocXhandle(*track_particle)};
134 trackParametersLocX.reserve(track.nMeasurements());
135 std::vector<float>& measurementsLocCovX{measurementLocCovXhandle(*track_particle)};
136 measurementsLocCovX.reserve(track.nMeasurements());
137 std::vector<float>& trackParametersLocCovX{trackParameterLocCovXhandle(*track_particle)};
138 trackParametersLocCovX.reserve(track.nMeasurements());
139 std::vector<float>& residualsLocY{residualLocYhandle(*track_particle)};
140 residualsLocY.reserve(track.nMeasurements());
141 std::vector<float>& pullsLocY{pullLocYhandle(*track_particle)};
142 pullsLocY.reserve(track.nMeasurements());
143 std::vector<float>& measurementsLocY{measurementLocYhandle(*track_particle)};
144 measurementsLocY.reserve(track.nMeasurements());
145 std::vector<float>& trackParametersLocY{trackParameterLocYhandle(*track_particle)};
146 trackParametersLocY.reserve(track.nMeasurements());
147 std::vector<float>& measurementsLocCovY{measurementLocCovYhandle(*track_particle)};
148 measurementsLocCovY.reserve(track.nMeasurements());
149 std::vector<float>& trackParametersLocCovY{trackParameterLocCovYhandle(*track_particle)};
150 trackParametersLocCovY.reserve(track.nMeasurements());
151
152 for (const auto state : track.trackStatesReversed()) {
153
154 auto flag = state.typeFlags();
155 // consider holes and measurements (also outliers)
156 bool anyHit = flag.isHole() or flag.hasMeasurement();
157 if (not anyHit) {
158 ATH_MSG_DEBUG("--- This is not a hit measurement, skipping...");
159 continue;
160 }
161
162 // starting with invalid values and setting them where needed.
163
164 int detector = -999;
165 int region = -999;
166 int layer = -999;
167 int type = -999;
168 float chi2_hit_predicted = -999.;
169 float chi2_hit_filtered = -999.;
170 int sizePhi = -999;
171 int sizeEta = -999;
172 float residualLocX = -999.;
173 float pullLocX = -999.;
174 float measurementLocX = -999.;
175 float trackParameterLocX = -999.;
176 float measurementLocCovX = -999.;
177 float trackParameterLocCovX = -999;
178 float residualLocY = -999.;
179 float pullLocY = -999.;
180 float measurementLocY = -999.;
181 float trackParameterLocY = -999.;
182 float measurementLocCovY = -999.;
183 float trackParameterLocCovY = -999.;
184 bool isAnnulusBound = false;
185
186 // Get the measurement type
187 if (flag.isHole()) {
189 ATH_MSG_DEBUG("--- This is a hole");
190 } else if (flag.isOutlier()) {
192 ATH_MSG_DEBUG("--- This is an outlier");
193 } else {
195 ATH_MSG_DEBUG("--- This is a hit");
196 }
197
198 // Check the location of the state
199 if (state.hasReferenceSurface() and state.referenceSurface().isSensitive()) {
200 const ActsDetectorElement * detectorElement = dynamic_cast<const ActsDetectorElement *>(state.referenceSurface().surfacePlacement());
201 if (!detectorElement) {
202 ATH_MSG_WARNING("--- TrackState reference surface returned an invalid associated detector element");
203 continue;
204 }
205 const InDetDD::SiDetectorElement * siliconDetectorElement = dynamic_cast<const InDetDD::SiDetectorElement *>(detectorElement->upstreamDetectorElement());
206 if (!siliconDetectorElement) {
207 ATH_MSG_WARNING("--- TrackState associated detector element is not silicon");
208 continue;
209 }
210
211 const Acts::AnnulusBounds* annulusBounds = dynamic_cast<const Acts::AnnulusBounds*>(&(state.referenceSurface().bounds()));
212 isAnnulusBound = annulusBounds ? true : false;
213
214 if (siliconDetectorElement) {
215 Identifier detectorIdentifier = siliconDetectorElement->identify();
216 if (siliconDetectorElement->isPixel()) {
217 const PixelID* pixel_id = static_cast<const PixelID *>(siliconDetectorElement->getIdHelper());
218 int layer_disk = pixel_id->layer_disk(detectorIdentifier);
219 layer = layer_disk;
220 if (pixel_id->barrel_ec(detectorIdentifier) == 0) {
221 if (layer_disk == 0) {
223 } else detector = Subdetector::PIXEL;
224 region = Region::BARREL;
225 } else {
226 detector = Subdetector::PIXEL;
227 region = Region::ENDCAP;
228 }
229 } else if (siliconDetectorElement->isSCT()) {
230 const SCT_ID* sct_id = static_cast<const SCT_ID *>(siliconDetectorElement->getIdHelper());
231 detector = Subdetector::STRIP;
232 region = sct_id->barrel_ec(detectorIdentifier) == 0 ?
234 layer = sct_id->layer_disk(detectorIdentifier);;
235 } else ATH_MSG_WARNING("--- Unknown detector type - It is not pixel nor strip detecor element!");
236 } else ATH_MSG_WARNING("--- Missing silicon detector element!");
237 } else ATH_MSG_WARNING("--- Missing reference surface or associated detector element!");
238
239
240
241 // If I have a measurement (hit or outlier) then proceed with computing the residuals / pulls
242
244
245 //Get the Chi2 computation
246
247 if (type == MeasurementType::HIT) {
248 chi2_hit_filtered = state.chi2();
249 }
250
251 if (state.hasUncalibratedSourceLink()) {
252 chi2_hit_predicted = getChi2Contribution(state);
253 }
254
255 // Skip all states without smoothed parameters or without projector
256 if (!state.hasSmoothed() || !state.hasProjector())
257 continue;
258
259 // Calling effective Calibrated has some runtime overhead
260 const auto &calibratedParameters = state.effectiveCalibrated();
261 const auto &calibratedCovariance = state.effectiveCalibratedCovariance();
262
263 // We evaluate the unbiased parameters for:
264 // - measurements added to the fit. For outliers, the measurement is not part of the fit, hence track parameters are already unbiased
265 // - if the filtered parameters and the projector exist.
266 bool evaluateUnbiased = flag.isMeasurement();
267
268 if (evaluateUnbiased) {
269 ATH_MSG_DEBUG("--- Good for unbiased parameters evaluation!");
271 // if unbiased, access the associated uncalibrated measurement and store the size
272 if (state.hasUncalibratedSourceLink()) {
273 const xAOD::UncalibratedMeasurement* uncalibratedMeasurement = detail::xAODUncalibMeasCalibrator::unpack(state.getUncalibratedSourceLink());;
274 const xAOD::UncalibMeasType measurementType = uncalibratedMeasurement->type();
275 if (measurementType == xAOD::UncalibMeasType::PixelClusterType) {
276 auto pixelCluster = static_cast<const xAOD::PixelCluster *>(uncalibratedMeasurement);
277 sizePhi = pixelCluster->channelsInPhi();
278 sizeEta = pixelCluster->channelsInEta();
279 } else if (measurementType == xAOD::UncalibMeasType::StripClusterType) {
280 auto stripCluster = static_cast<const xAOD::StripCluster *>(uncalibratedMeasurement);
281 sizePhi = stripCluster->channelsInPhi();
282 } else {
283 ATH_MSG_DEBUG("xAOD::UncalibratedMeasurement is neither xAOD::PixelCluster nor xAOD::StripCluster");
284 }
285 }
286 }
287
288 const auto& [unbiasedParameters, unbiasedCovariance] =
289 evaluateUnbiased ? Acts::calculateUnbiasedParametersCovariance(state) : std::make_pair(state.parameters(), state.covariance());
290
291 measurementLocX = calibratedParameters[Acts::eBoundLoc0];
292 measurementLocCovX = calibratedCovariance(Acts::eBoundLoc0, Acts::eBoundLoc0);
293
294 if (!isAnnulusBound) {
295
296 trackParameterLocX = unbiasedParameters[Acts::eBoundLoc0];
297 residualLocX = (measurementLocX - trackParameterLocX) / 1_um; //in um
298 trackParameterLocCovX = unbiasedCovariance(Acts::eBoundLoc0, Acts::eBoundLoc0);
299 }
300 else {
301 // TODO:: use directly phi instead of r*phi in the future
302
303 float locR = unbiasedParameters[Acts::eBoundLoc0];
304 float covR = unbiasedCovariance(Acts::eBoundLoc0,Acts::eBoundLoc0);
305 float locphi = unbiasedParameters[Acts::eBoundLoc1];
306 float covphi = unbiasedCovariance(Acts::eBoundLoc1,Acts::eBoundLoc1);
307 float covRphi = unbiasedCovariance(Acts::eBoundLoc0,Acts::eBoundLoc1);
308
309 trackParameterLocX = locphi;
310 residualLocX = locR * (measurementLocX - trackParameterLocX) / 1_um;
311 // Compute the error on the local rphi
312 trackParameterLocCovX = locR*locR*covphi + locphi*locphi*covR + 2*locphi*locR*covRphi;
313
314 // Rescale the error of the measurement to Rphi.
315 measurementLocCovX = locR*locR * measurementLocCovX;
316 }
317
318 pullLocX = evaluatePull(residualLocX, measurementLocCovX,
319 trackParameterLocCovX, evaluateUnbiased);
320
321 if (state.calibratedSize() == 2) {
322 measurementLocY = calibratedParameters[Acts::eBoundLoc1];
323 trackParameterLocY = unbiasedParameters[Acts::eBoundLoc1];
324 residualLocY = (measurementLocY - trackParameterLocY) / 1_um;
325 measurementLocCovY = calibratedCovariance(Acts::eBoundLoc1, Acts::eBoundLoc1);
326 trackParameterLocCovY = unbiasedCovariance(Acts::eBoundLoc1, Acts::eBoundLoc1);
327 pullLocY = evaluatePull(residualLocY, measurementLocCovY,
328 trackParameterLocCovY, evaluateUnbiased);
329 }
330
331 } // hit or outliers
332
333 else if (type == MeasurementType::HOLE) {
334
335 // Get the predicted position on sensor
336 auto pred = state.predicted();
337 trackParameterLocX = pred[Acts::eBoundLoc0];
338 trackParameterLocY = pred[Acts::eBoundLoc1];
339 }
340
341 // Always fill with this information
342
343 regions.push_back(region);
344 detectors.push_back(detector);
345 layers.push_back(layer);
346 types.push_back(type);
347 predchi2s.push_back(chi2_hit_predicted);
348 filtchi2s.push_back(chi2_hit_filtered);
349 sizesPhi.push_back(sizePhi);
350 sizesEta.push_back(sizeEta);
351 residualsLocX.push_back(residualLocX);
352 pullsLocX.push_back(pullLocX);
353 measurementsLocX.push_back(measurementLocX);
354 trackParametersLocX.push_back(trackParameterLocX);
355 measurementsLocCovX.push_back(measurementLocCovX);
356 trackParametersLocCovX.push_back(trackParameterLocCovX);
357 residualsLocY.push_back(residualLocY);
358 pullsLocY.push_back(pullLocY);
359 measurementsLocY.push_back(measurementLocY);
360 trackParametersLocY.push_back(trackParameterLocY);
361 measurementsLocCovY.push_back(measurementLocCovY);
362 trackParametersLocCovY.push_back(trackParameterLocCovY);
363
364
365 } // loop on states
366
367 } // loop on tracks
368
369 return StatusCode::SUCCESS;
370 }
371
372 float MeasurementToTrackParticleDecorationAlg::getChi2Contribution(const typename ActsTrk::TrackStateBackend::ConstTrackStateProxy &state) const {
373
374 auto pred = state.predicted();
375 auto predC = state.predictedCovariance();
376
377 return Acts::visit_measurement(
378 state.calibratedSize(),
379 [&]<std::size_t measdim>(std::integral_constant<std::size_t, measdim>) {
380 Acts::FixedBoundSubspaceHelper<measdim> subspaceHelper =
381 state.template projectorSubspaceHelper<measdim>();
382
383 // TODO use subspace helper for projection instead
384 auto H = subspaceHelper.projector();
385
386 const auto calibrated = state.template calibrated<measdim>();
387 const auto calibratedCov = state.template calibratedCovariance<measdim>();
388
389 auto residual = (H * pred - calibrated).eval();
390 auto rescov = (H * predC * H.transpose() + calibratedCov).eval();
391
392 return ((residual.transpose() * rescov.inverse() * residual).eval())(0,0);
393 });
394
395
396
397 }
398
399
401 const float measurementCovariance,
402 const float trackParameterCovariance,
403 const bool evaluateUnbiased) const {
404 float correlation = evaluateUnbiased ? 1. : -1.;
405 float residualCovariance = measurementCovariance + correlation*trackParameterCovariance;
406 if (residualCovariance<=0.) {
407 // If the total covariance is non-positive return 0
408 ATH_MSG_DEBUG("--- Total covariance for pull evaluation is non-positive! Returning pulls = 0!");
409 return 0.;
410 }
411 return 0.001 * residual/std::sqrt(residualCovariance);
412 }
413
414} // namespace
415
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static const std::vector< std::string > types
static const std::vector< std::string > regions
This is an Identifier helper class for the Pixel subdetector.
This is an Identifier helper class for the SCT subdetector.
const GeoVDetectorElement * upstreamDetectorElement() const
Returns the underllying GeoModel detectorelement that this one is based on.
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_chi2HitPredictedKey
float evaluatePull(const float residual, const float measurementCovariance, const float trackParameterCovariance, const bool evaluateUnbiased) const
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementRegionKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_residualLocYkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_trackParameterLocYkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementLayerKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementDetectorKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementLocYkey
PublicToolHandle< ActsTrk::ITrackingGeometryTool > m_trackingGeometryTool
float getChi2Contribution(const typename ActsTrk::TrackStateBackend::ConstTrackStateProxy &state) const
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementEtaWidthKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_pullLocYkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementLocXkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementTypeKey
MeasurementToTrackParticleDecorationAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_residualLocXkey
virtual StatusCode execute(const EventContext &ctx) const override
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_pullLocXkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementLocCovXkey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackParticlesKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_trackParameterLocXkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_trackParameterLocCovYkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementLocCovYkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_trackParameterLocCovXkey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_chi2HitFilteredKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_measurementPhiWidthKey
static const xAOD::UncalibratedMeasurement * unpack(const Acts::SourceLink &sl)
Helper method to unpack an Acts source link to an uncalibrated measurement.
An algorithm that can be simultaneously executed in multiple threads.
Class to hold geometrical description of a silicon detector element.
virtual Identifier identify() const override final
identifier of this detector element (inline)
const AtlasDetectorID * getIdHelper() const
Returns the id helper (inline).
This is an Identifier helper class for the Pixel subdetector.
Definition PixelID.h:69
int layer_disk(const Identifier &id) const
Definition PixelID.h:602
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0).
Definition PixelID.h:595
This is an Identifier helper class for the SCT subdetector.
Definition SCT_ID.h:68
int layer_disk(const Identifier &id) const
Definition SCT_ID.h:687
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0).
Definition SCT_ID.h:681
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
Handle class for adding a decoration to an object.
virtual xAOD::UncalibMeasType type() const =0
Returns the type of the measurement type as a simple enumeration.
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
std::optional< ActsTrk::TrackContainer::ConstTrackProxy > getActsTrack(const xAOD::TrackParticle &trkPart)
Return the proxy to the Acts track from which the track particle was made frome.
Definition Decoration.cxx:9
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
StripCluster_v1 StripCluster
Define the version of the strip cluster class.
UncalibratedMeasurement_v1 UncalibratedMeasurement
Define the version of the uncalibrated measurement class.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
PixelCluster_v1 PixelCluster
Define the version of the pixel cluster class.
UncalibMeasType
Define the type of the uncalibrated measurement.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".