Warning, file /acts/Core/include/Acts/TrackFitting/detail/GsfActor.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Definitions/Common.hpp"
0012 #include "Acts/Definitions/TrackParametrization.hpp"
0013 #include "Acts/EventData/MultiTrajectory.hpp"
0014 #include "Acts/EventData/Types.hpp"
0015 #include "Acts/Propagator/detail/PointwiseMaterialInteraction.hpp"
0016 #include "Acts/Surfaces/Surface.hpp"
0017 #include "Acts/TrackFitting/BetheHeitlerApprox.hpp"
0018 #include "Acts/TrackFitting/GsfComponent.hpp"
0019 #include "Acts/TrackFitting/GsfOptions.hpp"
0020 #include "Acts/TrackFitting/detail/GsfComponentMerging.hpp"
0021 #include "Acts/TrackFitting/detail/GsfUtils.hpp"
0022 #include "Acts/Utilities/Helpers.hpp"
0023
0024 #include <map>
0025
0026 namespace Acts::detail::Gsf {
0027
0028 template <typename traj_t>
0029 struct GsfResult {
0030
0031 traj_t* fittedStates{nullptr};
0032
0033
0034 TrackIndexType currentTip = kTrackIndexInvalid;
0035
0036
0037 TrackIndexType lastMeasurementTip = kTrackIndexInvalid;
0038
0039
0040
0041 std::vector<std::tuple<double, BoundVector, BoundMatrix>>
0042 lastMeasurementComponents;
0043
0044
0045 const Acts::Surface* lastMeasurementSurface = nullptr;
0046
0047
0048 std::size_t measurementStates = 0;
0049 std::size_t measurementHoles = 0;
0050 std::size_t processedStates = 0;
0051
0052 std::vector<const Surface*> visitedSurfaces;
0053 std::vector<const Surface*> surfacesVisitedBwdAgain;
0054
0055
0056 Updatable<std::size_t> nInvalidBetheHeitler;
0057 Updatable<double> maxPathXOverX0;
0058 Updatable<double> sumPathXOverX0;
0059
0060
0061 std::vector<BetheHeitlerApprox::Component> betheHeitlerCache;
0062
0063
0064 std::vector<GsfComponent> componentCache;
0065 };
0066
0067
0068 template <typename traj_t>
0069 struct GsfActor {
0070
0071 GsfActor() = default;
0072
0073
0074 using result_type = GsfResult<traj_t>;
0075
0076
0077 struct Config {
0078
0079 std::size_t maxComponents = 16;
0080
0081
0082 const std::map<GeometryIdentifier, SourceLink>* inputMeasurements = nullptr;
0083
0084
0085
0086 const BetheHeitlerApprox* bethe_heitler_approx = nullptr;
0087
0088
0089 bool multipleScattering = true;
0090
0091
0092 double weightCutoff = 1.0e-4;
0093
0094
0095
0096
0097 bool disableAllMaterialHandling = false;
0098
0099
0100 bool abortOnError = false;
0101
0102
0103
0104 std::optional<std::size_t> numberMeasurements;
0105
0106
0107 GsfExtensions<traj_t> extensions;
0108
0109
0110
0111
0112 bool inReversePass = false;
0113
0114
0115 ComponentMergeMethod mergeMethod = ComponentMergeMethod::eMaxWeight;
0116
0117 const Logger* logger{nullptr};
0118
0119
0120 const CalibrationContext* calibrationContext{nullptr};
0121
0122 } m_cfg;
0123
0124 const Logger& logger() const { return *m_cfg.logger; }
0125
0126 using TemporaryStates = detail::Gsf::TemporaryStates<traj_t>;
0127
0128 using FiltProjector = MultiTrajectoryProjector<StatesType::eFiltered, traj_t>;
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 template <typename propagator_state_t, typename stepper_t,
0140 typename navigator_t>
0141 Result<void> act(propagator_state_t& state, const stepper_t& stepper,
0142 const navigator_t& navigator, result_type& result,
0143 const Logger& ) const {
0144 assert(result.fittedStates && "No MultiTrajectory set");
0145
0146
0147
0148 const ScopedGsfInfoPrinterAndChecker printer(state, stepper, navigator,
0149 logger());
0150
0151
0152 if (navigator.currentSurface(state.navigation) == nullptr) {
0153 return Result<void>::success();
0154 }
0155
0156 const auto& surface = *navigator.currentSurface(state.navigation);
0157 ACTS_VERBOSE("Step is at surface " << surface.geometryId());
0158
0159
0160
0161 [[maybe_unused]] auto stepperComponents =
0162 stepper.constComponentIterable(state.stepping);
0163 assert(weightsAreNormalized(stepperComponents,
0164 [](const auto& cmp) { return cmp.weight(); }));
0165
0166
0167
0168
0169 using Status [[maybe_unused]] = IntersectionStatus;
0170 assert(std::all_of(
0171 stepperComponents.begin(), stepperComponents.end(),
0172 [](const auto& cmp) { return cmp.status() == Status::onSurface; }));
0173
0174
0175
0176 const bool visited = rangeContainsValue(result.visitedSurfaces, &surface);
0177
0178 if (visited) {
0179 ACTS_VERBOSE("Already visited surface, return");
0180 return Result<void>::success();
0181 }
0182
0183 result.visitedSurfaces.push_back(&surface);
0184
0185
0186 const auto foundSourceLink =
0187 m_cfg.inputMeasurements->find(surface.geometryId());
0188 const bool haveMaterial =
0189 surface.hasMaterial() && !m_cfg.disableAllMaterialHandling;
0190 const bool haveMeasurement =
0191 foundSourceLink != m_cfg.inputMeasurements->end();
0192
0193 ACTS_VERBOSE(std::boolalpha << "haveMaterial " << haveMaterial
0194 << ", haveMeasurement: " << haveMeasurement);
0195
0196
0197
0198
0199
0200
0201
0202 const bool holeOnly = !haveMaterial && !haveMeasurement;
0203 if (holeOnly && (result.processedStates == 0 || !surface.isSensitive())) {
0204 return Result<void>::success();
0205 }
0206
0207
0208
0209
0210 if (haveMeasurement) {
0211 result.maxPathXOverX0.update();
0212 result.sumPathXOverX0.update();
0213 result.nInvalidBetheHeitler.update();
0214 }
0215
0216
0217
0218 Result<BoundMatrix> jacobian =
0219 stepper.transportToBound(state.stepping, surface);
0220 if (!jacobian.ok()) {
0221 if (m_cfg.abortOnError) {
0222 std::abort();
0223 }
0224 return jacobian.error();
0225 }
0226
0227 if (holeOnly) {
0228 TemporaryStates tmpStates;
0229 Result<void> res = noMeasurementUpdate(state, stepper, surface, result,
0230 tmpStates, *jacobian);
0231 if (!res.ok()) {
0232 if (m_cfg.abortOnError) {
0233 std::abort();
0234 }
0235 return res.error();
0236 }
0237 return Result<void>::success();
0238 }
0239
0240 if (m_cfg.multipleScattering && haveMaterial) {
0241 if (haveMeasurement) {
0242 const Result<void> materialInteractionRes = applyMultipleScattering(
0243 state, stepper, surface,
0244 determineMaterialUpdateMode(state, navigator,
0245 MaterialUpdateMode::PreUpdate),
0246 logger());
0247 if (!materialInteractionRes.ok()) {
0248 return materialInteractionRes.error();
0249 }
0250 } else {
0251 const Result<void> materialInteractionRes = applyMultipleScattering(
0252 state, stepper, surface,
0253 determineMaterialUpdateMode(state, navigator,
0254 MaterialUpdateMode::FullUpdate),
0255 logger());
0256 if (!materialInteractionRes.ok()) {
0257 return materialInteractionRes.error();
0258 }
0259 }
0260 }
0261
0262
0263
0264
0265 if (!haveMaterial) {
0266 TemporaryStates tmpStates;
0267
0268 auto res = kalmanUpdate(state, stepper, surface, result, tmpStates,
0269 foundSourceLink->second, *jacobian);
0270
0271 if (!res.ok()) {
0272 if (m_cfg.abortOnError) {
0273 std::abort();
0274 }
0275 return res.error();
0276 }
0277
0278 updateStepper(state, stepper, tmpStates, m_cfg.weightCutoff);
0279 }
0280
0281
0282
0283 else {
0284 TemporaryStates tmpStates;
0285 Result<void> res;
0286
0287 if (haveMeasurement) {
0288 res = kalmanUpdate(state, stepper, surface, result, tmpStates,
0289 foundSourceLink->second, *jacobian);
0290 } else {
0291 res = noMeasurementUpdate(state, stepper, surface, result, tmpStates,
0292 *jacobian);
0293 }
0294
0295 if (!res.ok()) {
0296 if (m_cfg.abortOnError) {
0297 std::abort();
0298 }
0299 return res.error();
0300 }
0301
0302
0303 std::vector<GsfComponent>& componentCache = result.componentCache;
0304 componentCache.clear();
0305
0306 double pathXOverX0 = 0.0;
0307 for (const TrackIndexType idx : tmpStates.tips) {
0308 auto proxy = tmpStates.traj.getTrackState(idx);
0309
0310 const BoundTrackParameters bound(
0311 surface.getSharedPtr(), proxy.filtered(),
0312 proxy.filteredCovariance(),
0313 stepper.particleHypothesis(state.stepping));
0314
0315 pathXOverX0 += applyBetheHeitler(
0316 state.options.geoContext, surface, state.options.direction, bound,
0317 tmpStates.weights.at(idx), *m_cfg.bethe_heitler_approx,
0318 result.betheHeitlerCache, m_cfg.weightCutoff, componentCache,
0319 result.nInvalidBetheHeitler.tmp(), result.maxPathXOverX0.tmp(),
0320 logger());
0321 }
0322
0323
0324 result.sumPathXOverX0.tmp() += pathXOverX0 / tmpStates.tips.size();
0325
0326 if (componentCache.empty()) {
0327 ACTS_WARNING(
0328 "No components left after applying energy loss. "
0329 "Is the weight cutoff "
0330 << m_cfg.weightCutoff << " too high?");
0331 ACTS_WARNING("Return to propagator without applying energy loss");
0332 return Result<void>::success();
0333 }
0334
0335
0336 const auto finalCmpNumber = std::min(
0337 static_cast<std::size_t>(stepper.maxComponents), m_cfg.maxComponents);
0338 m_cfg.extensions.mixtureReducer(componentCache, finalCmpNumber, surface);
0339
0340 removeLowWeightComponents(componentCache, m_cfg.weightCutoff);
0341
0342 updateStepper(state, stepper, surface, componentCache);
0343 }
0344
0345
0346 if (m_cfg.multipleScattering && haveMaterial && haveMeasurement) {
0347 const Result<void> materialInteractionRes = applyMultipleScattering(
0348 state, stepper, surface,
0349 determineMaterialUpdateMode(state, navigator,
0350 MaterialUpdateMode::PostUpdate),
0351 logger());
0352 if (!materialInteractionRes.ok()) {
0353 return materialInteractionRes.error();
0354 }
0355 }
0356
0357 return Result<void>::success();
0358 }
0359
0360 template <typename propagator_state_t, typename stepper_t,
0361 typename navigator_t>
0362 bool checkAbort(propagator_state_t& , const stepper_t& ,
0363 const navigator_t& , const result_type& result,
0364 const Logger& ) const {
0365 if (m_cfg.numberMeasurements &&
0366 result.measurementStates == m_cfg.numberMeasurements) {
0367 ACTS_VERBOSE("Stop navigation because all measurements are found");
0368 return true;
0369 }
0370
0371 return false;
0372 }
0373
0374
0375
0376 template <typename propagator_state_t, typename stepper_t>
0377 Result<void> kalmanUpdate(propagator_state_t& state, const stepper_t& stepper,
0378 const Surface& surface, result_type& result,
0379 TemporaryStates& tmpStates,
0380 const SourceLink& sourceLink,
0381 const BoundMatrix& jacobian) const {
0382
0383 std::vector<TrackIndexType> allTips;
0384 allTips.reserve(stepper.numberComponents(state.stepping));
0385
0386 for (auto cmp : stepper.componentIterable(state.stepping)) {
0387 auto singleState = cmp.singleState(state);
0388 const auto& singleStepper = cmp.singleStepper(stepper);
0389
0390
0391
0392
0393
0394
0395
0396 TrackStatePropMask mask =
0397 TrackStatePropMask::Predicted | TrackStatePropMask::Calibrated;
0398 typename traj_t::TrackStateProxy trackStateProxy =
0399 tmpStates.traj.makeTrackState(mask, kTrackIndexInvalid);
0400 typename traj_t::ConstTrackStateProxy trackStateProxyConst{
0401 trackStateProxy};
0402
0403
0404
0405 {
0406 trackStateProxy.setReferenceSurface(surface.getSharedPtr());
0407
0408 auto res = singleStepper.boundParameters(singleState.stepping, surface);
0409 if (!res.ok()) {
0410 ACTS_DEBUG("Propagate to surface " << surface.geometryId()
0411 << " failed: " << res.error());
0412 return res.error();
0413 }
0414
0415
0416 trackStateProxy.predicted() = res->parameters();
0417 trackStateProxy.predictedCovariance() =
0418 singleStepper.covariance(singleState.stepping).value();
0419
0420 trackStateProxy.pathLength() =
0421 singleStepper.pathLength(singleState.stepping);
0422 }
0423
0424
0425
0426 m_cfg.extensions.calibrator(state.geoContext, *m_cfg.calibrationContext,
0427 sourceLink, trackStateProxy);
0428
0429 if (!m_cfg.extensions.outlierFinder(trackStateProxyConst)) {
0430
0431 trackStateProxy.addComponents(TrackStatePropMask::Filtered);
0432
0433 auto updateRes = m_cfg.extensions.updater(state.geoContext,
0434 trackStateProxy, logger());
0435 if (!updateRes.ok()) {
0436 ACTS_DEBUG("Update step failed: " << updateRes.error());
0437 return updateRes.error();
0438 }
0439
0440 tmpStates.tips.push_back(trackStateProxy.index());
0441 tmpStates.weights[trackStateProxy.index()] = cmp.weight();
0442 }
0443
0444 allTips.push_back(trackStateProxy.index());
0445 }
0446
0447 const bool isOutlier = tmpStates.tips.empty();
0448
0449 if (!isOutlier) {
0450 computePosteriorWeights(tmpStates.traj, tmpStates.tips,
0451 tmpStates.weights);
0452 normalizeWeights(tmpStates.tips, [&](auto idx) -> double& {
0453 return tmpStates.weights.at(idx);
0454 });
0455 } else {
0456 auto cmps = stepper.componentIterable(state.stepping);
0457 for (const auto [cmp, idx] : zip(cmps, allTips)) {
0458 typename traj_t::TrackStateProxy trackStateProxy =
0459 tmpStates.traj.getTrackState(idx);
0460
0461
0462
0463 trackStateProxy.shareFrom(trackStateProxy,
0464 TrackStatePropMask::Predicted,
0465 TrackStatePropMask::Filtered);
0466
0467 tmpStates.tips.push_back(trackStateProxy.index());
0468 tmpStates.weights[trackStateProxy.index()] = cmp.weight();
0469 }
0470 }
0471
0472
0473 ++result.processedStates;
0474 if (!isOutlier) {
0475 ++result.measurementStates;
0476 }
0477
0478 updateMultiTrajectory(result, tmpStates, surface,
0479 TrackStateType()
0480 .setHasParameters()
0481 .setHasMaterial(surface.hasMaterial())
0482 .setHasMeasurement()
0483 .setIsOutlier(isOutlier),
0484 jacobian);
0485
0486 result.lastMeasurementTip = result.currentTip;
0487 result.lastMeasurementSurface = &surface;
0488
0489
0490
0491 result.lastMeasurementComponents.clear();
0492
0493 FiltProjector proj{tmpStates.traj, tmpStates.weights};
0494 for (const auto& idx : tmpStates.tips) {
0495 const auto& [w, p, c] = proj(idx);
0496
0497 if (w > 0.0) {
0498 result.lastMeasurementComponents.push_back({w, p, c});
0499 }
0500 }
0501
0502
0503 return Result<void>::success();
0504 }
0505
0506 template <typename propagator_state_t, typename stepper_t>
0507 Result<void> noMeasurementUpdate(propagator_state_t& state,
0508 const stepper_t& stepper,
0509 const Surface& surface, result_type& result,
0510 TemporaryStates& tmpStates,
0511 const BoundMatrix& jacobian) const {
0512 for (auto cmp : stepper.componentIterable(state.stepping)) {
0513 auto& singleState = cmp.state();
0514 const auto& singleStepper = cmp.singleStepper(stepper);
0515
0516
0517
0518 TrackStatePropMask mask = TrackStatePropMask::Predicted;
0519 typename traj_t::TrackStateProxy trackStateProxy =
0520 tmpStates.traj.makeTrackState(mask, kTrackIndexInvalid);
0521
0522
0523
0524 {
0525 trackStateProxy.setReferenceSurface(surface.getSharedPtr());
0526
0527 auto res = singleStepper.boundParameters(singleState, surface);
0528 if (!res.ok()) {
0529 return res.error();
0530 }
0531
0532
0533 trackStateProxy.predicted() = res->parameters();
0534 trackStateProxy.predictedCovariance() =
0535 singleStepper.covariance(singleState).value();
0536
0537 trackStateProxy.pathLength() = singleStepper.pathLength(singleState);
0538
0539
0540
0541 trackStateProxy.shareFrom(trackStateProxy,
0542 TrackStatePropMask::Predicted,
0543 TrackStatePropMask::Filtered);
0544 }
0545
0546 tmpStates.tips.push_back(trackStateProxy.index());
0547 tmpStates.weights[trackStateProxy.index()] = cmp.weight();
0548 }
0549
0550 const bool precedingMeasurementExists = result.processedStates > 0;
0551 const bool isHole = surface.isSensitive();
0552
0553
0554 ++result.processedStates;
0555 if (precedingMeasurementExists && isHole) {
0556 ++result.measurementHoles;
0557 }
0558
0559 updateMultiTrajectory(result, tmpStates, surface,
0560 TrackStateType()
0561 .setHasParameters()
0562 .setHasMaterial(surface.hasMaterial())
0563 .setIsHole(isHole),
0564 jacobian);
0565
0566 return Result<void>::success();
0567 }
0568
0569 void updateMultiTrajectory(result_type& result,
0570 const TemporaryStates& tmpStates,
0571 const Surface& surface, TrackStateType type,
0572 const BoundMatrix& jacobian) const {
0573 using PrtProjector =
0574 MultiTrajectoryProjector<StatesType::ePredicted, traj_t>;
0575 using FltProjector =
0576 MultiTrajectoryProjector<StatesType::eFiltered, traj_t>;
0577
0578 if (!m_cfg.inReversePass) {
0579 assert(!tmpStates.tips.empty() &&
0580 "No components to update multi-trajectory");
0581
0582 const auto firstCmpProxy =
0583 tmpStates.traj.getTrackState(tmpStates.tips.front());
0584
0585
0586
0587 auto combinedStateMask =
0588 TrackStatePropMask::Predicted | TrackStatePropMask::Jacobian;
0589 if (type.isMeasurement()) {
0590 combinedStateMask |=
0591 TrackStatePropMask::Calibrated | TrackStatePropMask::Filtered;
0592 } else if (type.isOutlier()) {
0593 combinedStateMask |= TrackStatePropMask::Calibrated;
0594 }
0595 auto combinedState = result.fittedStates->makeTrackState(
0596 combinedStateMask, result.currentTip);
0597 result.currentTip = combinedState.index();
0598
0599
0600 auto copyMask = TrackStatePropMask::None;
0601 if (ACTS_CHECK_BIT(combinedStateMask, TrackStatePropMask::Calibrated)) {
0602
0603 copyMask |= TrackStatePropMask::Calibrated;
0604 }
0605 combinedState.copyFrom(firstCmpProxy, copyMask);
0606 combinedState.typeFlags() = type;
0607 combinedState.jacobian() = jacobian;
0608
0609 auto [prtMean, prtCov] = mergeGaussianMixture(
0610 tmpStates.tips, PrtProjector{tmpStates.traj, tmpStates.weights},
0611 surface, m_cfg.mergeMethod);
0612 combinedState.predicted() = prtMean;
0613 combinedState.predictedCovariance() = prtCov;
0614
0615 if (type.isMeasurement()) {
0616 auto [fltMean, fltCov] = mergeGaussianMixture(
0617 tmpStates.tips, FltProjector{tmpStates.traj, tmpStates.weights},
0618 surface, m_cfg.mergeMethod);
0619 combinedState.filtered() = fltMean;
0620 combinedState.filteredCovariance() = fltCov;
0621 } else {
0622 combinedState.shareFrom(TrackStatePropMask::Predicted,
0623 TrackStatePropMask::Filtered);
0624 }
0625
0626 } else {
0627 assert((result.currentTip != kTrackIndexInvalid && "tip not valid"));
0628
0629 result.fittedStates->applyBackwards(
0630 result.currentTip, [&](auto trackState) {
0631 if (&trackState.referenceSurface() != &surface) {
0632 return true;
0633 }
0634
0635 result.surfacesVisitedBwdAgain.push_back(&surface);
0636
0637
0638
0639 if (trackState.typeFlags().isMeasurement()) {
0640 const auto [smtMean, smtCov] = mergeGaussianMixture(
0641 tmpStates.tips,
0642 FltProjector{tmpStates.traj, tmpStates.weights}, surface,
0643 m_cfg.mergeMethod);
0644
0645 trackState.addComponents(TrackStatePropMask::Smoothed);
0646 trackState.smoothed() = smtMean;
0647 trackState.smoothedCovariance() = smtCov;
0648 }
0649
0650 return false;
0651 });
0652 }
0653 }
0654
0655
0656
0657 void setOptions(const GsfOptions<traj_t>& options) {
0658 m_cfg.maxComponents = options.maxComponents;
0659 m_cfg.extensions = options.extensions;
0660 m_cfg.abortOnError = options.abortOnError;
0661 m_cfg.disableAllMaterialHandling = options.disableAllMaterialHandling;
0662 m_cfg.weightCutoff = options.weightCutoff;
0663 m_cfg.mergeMethod = options.componentMergeMethod;
0664 m_cfg.calibrationContext = &options.calibrationContext.get();
0665 }
0666 };
0667
0668 }