8 #include "Acts/EventData/Types.hpp"
9 #include "GaudiKernel/TypeNameString.h"
14 #include "GaudiKernel/EventContext.h"
20 #include "Acts/EventData/Types.hpp"
21 #include "Acts/Definitions/TrackParametrization.hpp"
22 #include "Acts/Definitions/Units.hpp"
23 #include "Acts/Propagator/SympyStepper.hpp"
24 #include "Acts/Propagator/Navigator.hpp"
25 #include "Acts/Propagator/Propagator.hpp"
26 #include "Acts/Surfaces/PerigeeSurface.hpp"
27 #include "Acts/Surfaces/Surface.hpp"
28 #include "Acts/TrackFitting/KalmanFitter.hpp"
29 #include "Acts/Utilities/Helpers.hpp"
30 #include "Acts/Utilities/Logger.hpp"
31 #include "Acts/Utilities/CalibrationContext.hpp"
32 #include "Acts/EventData/VectorTrackContainer.hpp"
48 #include "Acts/Propagator/DirectNavigator.hpp"
60 template <
typename trajectory_t>
62 const Acts::CalibrationContext& ,
63 const Acts::SourceLink& sl,
64 typename trajectory_t::TrackStateProxy trackState)
const {
67 trackState.setUncalibratedSourceLink(Acts::SourceLink{sl});
69 const Acts::BoundTrackParameters actsParam(trackState.referenceSurface().getSharedPtr(),
70 trackState.predicted(),
71 trackState.predictedCovariance(),
91 if (slsurf !=
nullptr) {
105 }
else if (plsurf !=
nullptr) {
106 if ((trkParam.get())->covariance() !=
nullptr) {
128 int dim = (*rot).localParameters().dimension();
129 trackState.allocateCalibrated(
dim);
134 throw std::runtime_error(
"Cannot create dim 0 measurement");
135 }
else if (
dim == 1) {
136 trackState.template calibrated<1>() = (*rot).localParameters().template head<1>();
137 trackState.template calibratedCovariance<1>() = (*rot).localCovariance().template topLeftCorner<1, 1>();
138 Acts::BoundSubspaceIndices subspaceIndices;
140 subspaceIndices = {Acts::eBoundLoc1};
142 subspaceIndices = {Acts::eBoundLoc0};
144 trackState.setBoundSubspaceIndices(subspaceIndices);
148 trackState.template calibrated<2>() = (*rot).localParameters().template head<2>();
149 trackState.template calibratedCovariance<2>() = (*rot).localCovariance().template topLeftCorner<2, 2>();
150 Acts::BoundSubspaceIndices subspaceIndices = {Acts::eBoundLoc0, Acts::eBoundLoc1};
151 trackState.setBoundSubspaceIndices(subspaceIndices);
156 " currently not supported.");
170 const IInterface*
p) :
188 auto field = std::make_shared<ATLASMagneticFieldWrapper>();
191 Acts::SympyStepper stepper(
field);
193 logger().cloneWithSuffix(
"Navigator"));
194 Acts::Propagator<Acts::SympyStepper, Acts::Navigator> propagator(stepper,
195 std::move(navigator),
196 logger().cloneWithSuffix(
"Prop"));
198 m_fitter = std::make_unique<Fitter>(std::move(propagator),
199 logger().cloneWithSuffix(
"KalmanFitter"));
202 Acts::DirectNavigator directNavigator(
logger().cloneWithSuffix(
"DirectNavigator") );
203 Acts::Propagator<Acts::SympyStepper, Acts::DirectNavigator> directPropagator(std::move(stepper),
204 std::move(directNavigator),
205 logger().cloneWithSuffix(
"DirectPropagator"));
207 m_directFitter = std::make_unique<DirectFitter>(std::move(directPropagator),
208 logger().cloneWithSuffix(
"DirectKalmanFitter"));
217 m_kfExtensions.updater.connect<&ActsTrk::detail::FitterHelperFunctions::gainMatrixUpdate<ActsTrk::MutableTrackStateBackend>>();
218 m_kfExtensions.smoother.connect<&ActsTrk::detail::FitterHelperFunctions::mbfSmoother<ActsTrk::MutableTrackStateBackend>>();
219 m_kfExtensions.calibrator.connect<&TrkMeasurementCalibrator::calibrate<ActsTrk::MutableTrackStateBackend>>(
m_calibrator.get());
221 return StatusCode::SUCCESS;
226 std::unique_ptr<Trk::Track>
232 std::unique_ptr<Trk::Track>
track =
nullptr;
233 ATH_MSG_VERBOSE (
"--> enter KalmanFitter::fit(Track,,) with Track from author = "
238 ATH_MSG_DEBUG(
"called to refit empty track or track with too little information, reject fit");
244 ATH_MSG_DEBUG(
"input fails to provide track parameters for seeding the KF, reject fit");
249 auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
250 Acts::Vector3{0., 0., 0.});
255 Acts::CalibrationContext calContext = Acts::CalibrationContext();
257 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
260 kfExtensions.surfaceAccessor.connect<&ATLASSourceLinkSurfaceAccessor::operator()>(&surfaceAccessor);
262 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
265 Acts::KalmanFitterOptions
266 kfOptions(tgContext, mfContext, calContext,
271 std::vector<Acts::SourceLink> trackSourceLinks =
m_ATLASConverterTool->trkTrackToSourceLinks(tgContext,inputTrack);
273 if (trackSourceLinks.empty()) {
274 ATH_MSG_DEBUG(
"input contain measurement but no source link created, probable issue with the converter, reject fit ");
282 Acts::BoundSquareMatrix scaledCov = Acts::BoundSquareMatrix::Identity();
283 for (
int i=0;
i<6; ++
i) {
285 (scaledCov)(
i,
i) =
scale * initialParams.covariance().value()(
i,
i);
291 const Acts::BoundTrackParameters scaledInitialParams(initialParams.referenceSurface().getSharedPtr(),
292 initialParams.parameters(),
299 auto result =
m_fitter->fit(trackSourceLinks.begin(), trackSourceLinks.end(),
300 scaledInitialParams, kfOptions, tracks);
309 std::unique_ptr<Trk::Track>
316 std::unique_ptr<Trk::Track>
track =
nullptr;
319 if (inputMeasSet.size() < 2) {
320 ATH_MSG_DEBUG(
"called to refit empty measurement set or a measurement set with too little information, reject fit");
325 auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
326 Acts::Vector3{0., 0., 0.});
331 Acts::CalibrationContext calContext = Acts::CalibrationContext();
333 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
336 kfExtensions.surfaceAccessor.connect<&ATLASSourceLinkSurfaceAccessor::operator()>(&surfaceAccessor);
338 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
341 Acts::KalmanFitterOptions
342 kfOptions(tgContext, mfContext, calContext,
347 std::vector<Acts::SourceLink> trackSourceLinks;
348 trackSourceLinks.reserve(inputMeasSet.size());
350 for (
auto it = inputMeasSet.begin();
it != inputMeasSet.end(); ++
it){
354 if (trackSourceLinks.empty()) {
355 ATH_MSG_DEBUG(
"input contain measurement but no source link created, probable issue with the converter, reject fit ");
359 const auto& initialParams =
m_ATLASConverterTool->trkTrackParametersToActsParameters(estimatedStartParameters, tgContext);
364 auto result =
m_fitter->fit(trackSourceLinks.begin(), trackSourceLinks.end(),
365 initialParams, kfOptions, tracks);
374 std::unique_ptr<Trk::Track>
384 std::unique_ptr<Trk::Track>
track =
nullptr;
387 auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
388 Acts::Vector3{0., 0., 0.});
393 Acts::CalibrationContext calContext = Acts::CalibrationContext();
395 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
401 kfExtensions.calibrator.connect<&PRDSourceLinkCalibrator::calibrate<ActsTrk::MutableTrackStateBackend>>(&calibrator);
404 kfExtensions.surfaceAccessor.connect<&PRDSourceLinkSurfaceAccessor::operator()>(&surfaceAccessor);
406 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
409 Acts::KalmanFitterOptions
410 kfOptions(tgContext, mfContext, calContext,
416 std::vector<Acts::SourceLink> trackSourceLinks;
417 trackSourceLinks.reserve(inputPRDColl.size());
420 trackSourceLinks.push_back(Acts::SourceLink{
PRDSourceLink{prd}});
423 if (trackSourceLinks.empty()) {
424 ATH_MSG_WARNING(
"input contain measurement but no source link created, probable issue with the converter, reject fit ");
429 const auto& initialParams =
m_ATLASConverterTool->trkTrackParametersToActsParameters(estimatedStartParameters, tgContext);
434 auto result =
m_fitter->fit(trackSourceLinks.begin(), trackSourceLinks.end(),
435 initialParams, kfOptions, tracks);
444 std::unique_ptr< ActsTrk::MutableTrackContainer >
446 const std::vector< ActsTrk::ATLASUncalibSourceLink> & clusterList,
447 const Acts::BoundTrackParameters& initialParams,
448 const Acts::GeometryContext& tgContext,
449 const Acts::MagneticFieldContext& mfContext,
450 const Acts::CalibrationContext& calContext,
452 const Acts::Surface* targetSurface)
const{
453 ATH_MSG_DEBUG(
"--> entering KalmanFitter::fit(xAODMeasure...things,TP,)");
455 std::vector<Acts::SourceLink> sourceLinks;
456 sourceLinks.reserve(clusterList.size());
458 std::vector<const Acts::Surface*> surfaces;
459 surfaces.reserve(clusterList.size());
461 const Acts::TrackingGeometry *
463 if (!actsTrackingGeometry) {
464 throw std::runtime_error(
"No Acts tracking geometry.");
468 sourceLinks.emplace_back(
el );
472 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
475 kfExtensions.surfaceAccessor.connect<&ActsTrk::ATLASUncalibSourceLinkSurfaceAccessor::operator()>(&surfaceAccessor);
478 ::NoCalibration(*actsTrackingGeometry, detectorElementToGeometryIdMap);
482 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
486 std::shared_ptr<Acts::Surface> pSurface{
nullptr};
488 pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(Acts::Vector3{0., 0., 0.});
489 targetSurface = pSurface.get();
493 Acts::KalmanFitterOptions<ActsTrk::MutableTrackStateBackend>
494 kfOptions(tgContext, mfContext, calContext,
499 std::unique_ptr< ActsTrk::MutableTrackContainer > tracks = std::make_unique< ActsTrk::MutableTrackContainer >();
520 std::unique_ptr<Trk::Track>
531 if (addMeasColl.empty()) {
532 ATH_MSG_DEBUG(
"client tries to add an empty MeasurementSet to the track fit." );
533 return fit(ctx,inputTrack);
538 ATH_MSG_DEBUG(
"called to refit empty track or track with too little information, reject fit");
544 ATH_MSG_DEBUG(
"input fails to provide track parameters for seeding the KF, reject fit");
548 std::unique_ptr<Trk::Track>
track =
nullptr;
551 auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
552 Acts::Vector3{0., 0., 0.});
557 Acts::CalibrationContext calContext = Acts::CalibrationContext();
559 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
561 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
564 Acts::KalmanFitterOptions
565 kfOptions(tgContext, mfContext, calContext,
570 std::vector<Acts::SourceLink> trackSourceLinks =
m_ATLASConverterTool->trkTrackToSourceLinks(tgContext, inputTrack);
573 for (
auto it = addMeasColl.begin();
it != addMeasColl.end(); ++
it)
578 if (trackSourceLinks.empty()) {
579 ATH_MSG_DEBUG(
"input contain measurement but no source link created, probable issue with the converter, reject fit ");
585 auto result =
m_fitter->fit(trackSourceLinks.begin(), trackSourceLinks.end(),
586 initialParams, kfOptions, tracks);
595 std::unique_ptr<Trk::Track>
602 ATH_MSG_DEBUG(
"Fit of Track with additional PrepRawDataSet not yet implemented");
608 std::unique_ptr<Trk::Track>
621 ATH_MSG_DEBUG(
"input #2 is empty try to fit track 1 alone" );
622 return fit(ctx,intrk1);
627 ATH_MSG_DEBUG(
"input #1 is empty try to fit track 2 alone" );
628 return fit(ctx,intrk2);
633 ATH_MSG_DEBUG(
"input #1 fails to provide track parameters for seeding the KF, reject fit");
637 std::unique_ptr<Trk::Track>
track =
nullptr;
640 auto pSurface = Acts::Surface::makeShared<Acts::PerigeeSurface>(
641 Acts::Vector3{0., 0., 0.});
646 Acts::CalibrationContext calContext = Acts::CalibrationContext();
648 Acts::KalmanFitterExtensions<ActsTrk::MutableTrackStateBackend> kfExtensions =
m_kfExtensions;
650 Acts::PropagatorPlainOptions propagationOption(tgContext, mfContext);
653 Acts::KalmanFitterOptions
654 kfOptions(tgContext, mfContext, calContext,
659 std::vector<Acts::SourceLink> trackSourceLinks =
m_ATLASConverterTool->trkTrackToSourceLinks(tgContext, intrk1);
660 std::vector<Acts::SourceLink> trackSourceLinks2 =
m_ATLASConverterTool->trkTrackToSourceLinks(tgContext, intrk2);
661 trackSourceLinks.insert(trackSourceLinks.end(), trackSourceLinks2.begin(), trackSourceLinks2.end());
663 if (trackSourceLinks.empty()) {
664 ATH_MSG_DEBUG(
"input contain measurement but no source link created, probable issue with the converter, reject fit ");
672 Acts::BoundSquareMatrix scaledCov = Acts::BoundSquareMatrix::Identity();
673 for (
int i=0;
i<6; ++
i) {
675 (scaledCov)(
i,
i) =
scale * initialParams.covariance().value()(
i,
i);
678 const Acts::BoundTrackParameters scaledInitialParams(initialParams.referenceSurface().getSharedPtr(),
679 initialParams.parameters(),
686 auto result =
m_fitter->fit(trackSourceLinks.begin(), trackSourceLinks.end(),
687 scaledInitialParams, kfOptions, tracks);
694 std::unique_ptr<Trk::Track>
696 Acts::GeometryContext& tgContext,
698 Acts::Result<ActsTrk::MutableTrackContainer::TrackProxy, std::error_code>& fitResult,
bool SourceLinkType)
const {
700 if (not fitResult.ok())
703 std::unique_ptr<Trk::Track> newtrack =
nullptr;
705 const auto& acts_track = fitResult.value();
706 auto finalTrajectory = std::make_unique<Trk::TrackStates>();
708 int numberOfDeadPixel = 0;
709 int numberOfDeadSCT = 0;
711 std::vector<std::unique_ptr<const Acts::BoundTrackParameters>> actsSmoothedParam;
713 tracks.trackStateContainer().visitBackwards(acts_track.tipIndex(),
714 [&] (
const auto &state) ->
void
717 auto flag = state.typeFlags();
718 const auto* associatedDetEl = state.referenceSurface().associatedDetectorElement();
719 if (not associatedDetEl)
722 const auto* actsElement = dynamic_cast<const ActsDetectorElement*>(associatedDetEl);
726 const auto* upstreamDetEl = actsElement->upstreamDetectorElement();
727 if (not upstreamDetEl)
730 ATH_MSG_VERBOSE(
"Try casting to TRT for if");
731 if (dynamic_cast<const InDetDD::TRT_BaseElement*>(upstreamDetEl))
734 const auto* trkDetElem = dynamic_cast<const Trk::TrkDetElementBase*>(upstreamDetEl);
738 ATH_MSG_VERBOSE(
"trkDetElem type: " << static_cast<std::underlying_type_t<Trk::DetectorElemType>>(trkDetElem->detectorType()));
740 ATH_MSG_VERBOSE(
"Try casting to SiDetectorElement");
741 const auto* detElem = dynamic_cast<const InDetDD::SiDetectorElement*>(upstreamDetEl);
744 ATH_MSG_VERBOSE(
"detElem = " << detElem);
747 std::bitset<Trk::TrackStateOnSurface::NumberOfTrackStateOnSurfaceTypes> typePattern;
748 std::unique_ptr<Trk::TrackParameters> parm;
751 if (flag.test(Acts::TrackStateFlag::HoleFlag)){
752 ATH_MSG_VERBOSE(
"State is a hole (no associated measurement), use predicted parameters");
753 const Acts::BoundTrackParameters actsParam(state.referenceSurface().getSharedPtr(),
755 state.predictedCovariance(),
756 acts_track.particleHypothesis());
757 parm = m_ATLASConverterTool->actsTrackParametersToTrkParameters(actsParam, tgContext);
758 auto boundaryCheck = m_boundaryCheckTool->boundaryCheck(*parm);
761 ATH_MSG_VERBOSE(
"Check if this is a hole, a dead sensors or a state outside the sensor boundary");
762 if(boundaryCheck == Trk::BoundaryCheckResult::DeadElement){
763 if (detElem->isPixel()) {
766 else if (detElem->isSCT()) {
771 } else if (boundaryCheck != Trk::BoundaryCheckResult::Candidate){
775 typePattern.set(Trk::TrackStateOnSurface::Hole);
778 else if (
flag.test(Acts::TrackStateFlag::OutlierFlag) or !state.hasSmoothed()) {
779 ATH_MSG_VERBOSE(
"The state was tagged as an outlier or was missed in the reverse filtering, use filtered parameters");
780 const Acts::BoundTrackParameters actsParam(state.referenceSurface().getSharedPtr(),
782 state.filteredCovariance(),
783 acts_track.particleHypothesis());
784 parm = m_ATLASConverterTool->actsTrackParametersToTrkParameters(actsParam, tgContext);
785 typePattern.set(Trk::TrackStateOnSurface::Outlier);
789 ATH_MSG_VERBOSE(
"The state is a measurement state, use smoothed parameters");
791 const Acts::BoundTrackParameters actsParam(state.referenceSurface().getSharedPtr(),
793 state.smoothedCovariance(),
794 acts_track.particleHypothesis());
796 actsSmoothedParam.push_back(std::make_unique<const Acts::BoundTrackParameters>(Acts::BoundTrackParameters(actsParam)));
797 parm = m_ATLASConverterTool->actsTrackParametersToTrkParameters(actsParam, tgContext);
798 typePattern.set(Trk::TrackStateOnSurface::Measurement);
800 std::unique_ptr<Trk::MeasurementBase> measState;
801 if (state.hasUncalibratedSourceLink() && !SourceLinkType){
802 auto sl = state.getUncalibratedSourceLink().template get<ATLASSourceLink>();
804 measState = sl->uniqueClone();
806 else if (state.hasUncalibratedSourceLink() && SourceLinkType){
807 auto sl = state.getUncalibratedSourceLink().template get<PRDSourceLink>().prd;
810 const IdentifierHash idHash = sl->detectorElement()->identifyHash();
811 int dim = state.calibratedSize();
812 std::unique_ptr<Trk::RIO_OnTrack> rot;
814 const InDet::SCT_Cluster* sct_Cluster = dynamic_cast<const InDet::SCT_Cluster*>(sl);
816 ATH_MSG_ERROR(
"ERROR could not cast PRD to SCT_Cluster");
819 rot = std::make_unique<InDet::SCT_ClusterOnTrack>(sct_Cluster,Trk::LocalParameters(Trk::DefinedParameter(state.template calibrated<1>()[0], Trk::loc1)), state.template calibratedCovariance<1>(),idHash);
825 ATH_MSG_VERBOSE(
"Dimension is 2 but we need SCT_Cluster for this measurment");
830 rot = std::make_unique<InDet::PixelClusterOnTrack>(
pixelCluster,
Trk::LocalParameters(state.template calibrated<2>()),state.template calibratedCovariance<2>(),idHash);
834 throw std::domain_error(
"Cannot handle measurement dim>2");
836 measState = rot->uniqueClone();
838 double nDoF = state.calibratedSize();
843 ATH_MSG_VERBOSE(
"State succesfully creates, adding it to the trajectory");
844 finalTrajectory->insert(finalTrajectory->begin(), perState);
848 const Acts::BoundTrackParameters actsPer(acts_track.referenceSurface().getSharedPtr(),
849 acts_track.parameters(),
850 acts_track.covariance(),
851 acts_track.particleHypothesis());
852 std::unique_ptr<Trk::TrackParameters> per = m_ATLASConverterTool->actsTrackParametersToTrkParameters(actsPer, tgContext);
853 std::bitset<Trk::TrackStateOnSurface::NumberOfTrackStateOnSurfaceTypes> typePattern;
856 if (perState) finalTrajectory->insert(finalTrajectory->begin(), perState);
861 newtrack = std::make_unique<Trk::Track>(newInfo, std::move(finalTrajectory),
nullptr);
864 if (!newtrack->trackSummary()) {
865 newtrack->setTrackSummary(std::make_unique<Trk::TrackSummary>());
872 m_trkSummaryTool->updateTrackSummary(ctx, *newtrack,
true);
877 std::unique_ptr< ActsTrk::MutableTrackContainer >
880 const Acts::BoundTrackParameters& initialParams,
881 const Acts::GeometryContext& tgContext,
882 const Acts::MagneticFieldContext& mfContext,
883 const Acts::CalibrationContext& calContext,
886 const Acts::TrackingGeometry *
887 actsTrackingGeometry = m_trackingGeometryTool->trackingGeometry().get();
888 if (!actsTrackingGeometry) {
889 throw std::runtime_error(
"No Acts tracking geometry.");
892 std::vector<ActsTrk::ATLASUncalibSourceLink> sourceLinks;
893 sourceLinks.reserve(6);
895 std::vector<const Acts::Surface*> surfaces;
898 const auto& sps = seed.sp();
900 const auto& measurements = sp->measurements();
903 sourceLinks.emplace_back(
el );
907 return fit(ctx, sourceLinks, initialParams, tgContext, mfContext, calContext, detectorElementToGeometryIdMap, surfaces.front());