From 3686f5c4796228865a8c93539b213677591f5180 Mon Sep 17 00:00:00 2001
From: Tobias Bockh <tobias.boeckh@cern.ch>
Date: Sat, 25 Nov 2023 04:26:30 +0100
Subject: [PATCH] Fix tracking issues

---
 .../Acts/FaserActsKalmanFilter/CMakeLists.txt |  4 +-
 .../FaserActsKalmanFilter/ITrackSeedTool.h    |  2 +-
 .../python/CKF2Config.py                      | 13 ++--
 .../src/ActsTrackSeedTool.cxx                 |  2 +-
 .../src/ActsTrackSeedTool.h                   |  2 +-
 .../Acts/FaserActsKalmanFilter/src/CKF2.cxx   | 16 +++--
 .../src/CircleFitTrackSeedTool.cxx            | 61 ++++++++++++-------
 .../src/CircleFitTrackSeedTool.h              |  8 +--
 .../src/CreateTrkTrackTool.cxx                |  7 ++-
 .../src/CreateTrkTrackTool.h                  |  2 +-
 .../src/KalmanFitterTool.cxx                  | 33 +++++-----
 .../src/KalmanFitterTool.h                    |  2 +-
 .../src/MyTrackSeedTool.cxx                   |  2 +-
 .../src/MyTrackSeedTool.h                     |  2 +-
 .../src/ThreeStationTrackSeedTool.cxx         |  2 +-
 .../src/ThreeStationTrackSeedTool.h           |  2 +-
 .../FaserActsKalmanFilter_entries.cxx         |  4 +-
 17 files changed, 96 insertions(+), 68 deletions(-)

diff --git a/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt b/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
index 874d60424..143f819de 100755
--- a/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
+++ b/Tracking/Acts/FaserActsKalmanFilter/CMakeLists.txt
@@ -29,7 +29,7 @@ atlas_add_component(FaserActsKalmanFilter
     CircleFit.h
     CircleFitTrackSeedTool.h
     CKF2.h
-    CKF2Alignment.h
+    # CKF2Alignment.h
     CombinatorialKalmanFilterAlg.h
     EffPlotTool.h
     FASERSourceLink.h
@@ -74,7 +74,7 @@ atlas_add_component(FaserActsKalmanFilter
     src/CircleFit.cxx
     src/CircleFitTrackSeedTool.cxx
     src/CKF2.cxx
-    src/CKF2Alignment.cxx
+    # src/CKF2Alignment.cxx
     src/CreateTrkTrackTool.h
     src/CreateTrkTrackTool.cxx
 #    src/ClusterTrackSeedTool.cxx
diff --git a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/ITrackSeedTool.h b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/ITrackSeedTool.h
index 1ef1a7921..7a429b3e2 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/ITrackSeedTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/FaserActsKalmanFilter/ITrackSeedTool.h
@@ -20,7 +20,7 @@ class ITrackSeedTool : virtual public IAlgTool {
 public:
   DeclareInterfaceID(ITrackSeedTool, 1, 0);
 
-  virtual StatusCode run(std::vector<int> maskedLayers = {}) = 0;
+  virtual StatusCode run(std::vector<int> maskedLayers = {}, bool backward = false) = 0;
   virtual const std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialTrackParameters() const = 0;
   virtual const std::shared_ptr<const Acts::Surface> initialSurface() const = 0;
   virtual const std::shared_ptr<std::vector<IndexSourceLink>> sourceLinks() const = 0;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/python/CKF2Config.py b/Tracking/Acts/FaserActsKalmanFilter/python/CKF2Config.py
index 5d6ed5c4f..259a66557 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/python/CKF2Config.py
+++ b/Tracking/Acts/FaserActsKalmanFilter/python/CKF2Config.py
@@ -60,16 +60,16 @@ def CKF2Cfg(flags, **kwargs):
     track_seed_tool.TrackCollection = "Segments"
 
     trajectory_states_writer_tool = CompFactory.RootTrajectoryStatesWriterTool()
-    trajectory_states_writer_tool.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    trajectory_states_writer_tool.noDiagnostics = kwargs.get("noDiagnostics", True)
     trajectory_states_writer_tool1 = CompFactory.RootTrajectoryStatesWriterTool()
-    trajectory_states_writer_tool1.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    trajectory_states_writer_tool1.noDiagnostics = kwargs.get("noDiagnostics", True)
     trajectory_states_writer_tool1.FilePath = "track_states_ckf1.root"
 
     trajectory_summary_writer_tool = CompFactory.RootTrajectorySummaryWriterTool()
-    trajectory_summary_writer_tool.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    trajectory_summary_writer_tool.noDiagnostics = kwargs.get("noDiagnostics", True)
     trajectory_summary_writer_tool1 = CompFactory.RootTrajectorySummaryWriterTool()
     trajectory_summary_writer_tool1.FilePath = "track_summary_ckf1.root"
-    trajectory_summary_writer_tool1.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    trajectory_summary_writer_tool1.noDiagnostics = kwargs.get("noDiagnostics", True)
 
     actsExtrapolationTool = CompFactory.FaserActsExtrapolationTool("FaserActsExtrapolationTool")
     actsExtrapolationTool.MaxSteps = 1000
@@ -81,11 +81,11 @@ def CKF2Cfg(flags, **kwargs):
 
     trajectory_performance_writer_tool = CompFactory.PerformanceWriterTool("PerformanceWriterTool")
     trajectory_performance_writer_tool.ExtrapolationTool = actsExtrapolationTool
-    trajectory_performance_writer_tool.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    trajectory_performance_writer_tool.noDiagnostics = kwargs.get("noDiagnostics", True)
 
     ckf = CompFactory.CKF2(**kwargs)
     kalman_fitter1 = CompFactory.KalmanFitterTool(name="fitterTool1")
-    kalman_fitter1.noDiagnostics = kwargs.pop("noDiagnostics", True)
+    kalman_fitter1.noDiagnostics = kwargs.get("noDiagnostics", True)
     kalman_fitter1.ActsLogging = "INFO"
     kalman_fitter1.SummaryWriter = True
     kalman_fitter1.StatesWriter = False
@@ -102,6 +102,7 @@ def CKF2Cfg(flags, **kwargs):
     ckf.PerformanceWriterTool = trajectory_performance_writer_tool
     ckf.TrackingGeometryTool=actsTrackingGeometryTool
     ckf.isMC = flags.Input.isMC
+    ckf.noDiagnostics = kwargs.get("noDiagnostics", True)
     ckf.SummaryWriter = True
     ckf.StatesWriter = False
     ckf.PerformanceWriter = False
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.cxx
index 817fcfea1..0f41f2d91 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.cxx
@@ -27,7 +27,7 @@ StatusCode ActsTrackSeedTool::initialize() {
 }
 
 
-StatusCode ActsTrackSeedTool::run(std::vector<int> /*maskedLayers*/) {
+StatusCode ActsTrackSeedTool::run(std::vector<int> /*maskedLayers*/, bool /*backward*/) {
   SG::ReadHandle<TrackCollection> trackCollection {m_trackCollection};
   ATH_CHECK(trackCollection.isValid());
 
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.h
index 41aebd49f..e957fa8f2 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/ActsTrackSeedTool.h
@@ -18,7 +18,7 @@ public:
   virtual ~ActsTrackSeedTool() = default;
   virtual StatusCode initialize() override;
   virtual StatusCode finalize() override;
-  virtual StatusCode run(std::vector<int> /*maskedLayers*/) override;
+  virtual StatusCode run(std::vector<int> /*maskedLayers*/, bool /*backward*/) override;
 
   const std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialTrackParameters() const override;
   const std::shared_ptr<const Acts::Surface> initialSurface() const override;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
index 14616c6f4..57d78d9d5 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
@@ -97,7 +97,7 @@ StatusCode CKF2::execute() {
   Acts::MagneticFieldContext magFieldContext = getMagneticFieldContext(ctx);
   Acts::CalibrationContext calibContext;
 
-  CHECK(m_trackSeedTool->run(m_maskedLayers));
+  CHECK(m_trackSeedTool->run(m_maskedLayers, m_backwardPropagation));
   std::shared_ptr<const Acts::Surface> initialSurface =
       m_trackSeedTool->initialSurface();
   std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialParameters =
@@ -117,6 +117,11 @@ StatusCode CKF2::execute() {
 
   Acts::PropagatorPlainOptions pOptions;
   pOptions.maxSteps = m_maxSteps;
+  if (m_backwardPropagation) {
+    pOptions.direction = Acts::backward;
+  } else {
+    pOptions.direction = Acts::forward;
+  }
 
   Acts::MeasurementSelector::Config measurementSelectorCfg = {
       {Acts::GeometryIdentifier(), {m_chi2Max, m_nMax}},
@@ -208,10 +213,10 @@ StatusCode CKF2::execute() {
   //   ATH_MSG_DEBUG("  position: " << params.position(gctx).transpose());
   //   ATH_MSG_DEBUG("  momentum: " << params.momentum().transpose());
   //   ATH_MSG_DEBUG("  charge:   " << params.charge());
-  //   std::unique_ptr<Trk::Track> track = m_createTrkTrackTool->createTrack(gctx, traj);
+  //   std::unique_ptr<Trk::Track> track = m_createTrkTrackTool->createTrack(gctx, traj, m_backwardPropagation);
   //   if (track != nullptr) {
   //     m_numberOfSelectedTracks++;
-  //     std::unique_ptr<Trk::Track> track2 = m_kalmanFitterTool1->fit(ctx, gctx, track.get(), Acts::BoundVector::Zero(), m_isMC, origin);
+  //     std::unique_ptr<Trk::Track> track2 = m_kalmanFitterTool1->fit(ctx, gctx, track.get(), Acts::BoundVector::Zero(), m_isMC);
   //     if (track2) {
   //       outputAllTracks->push_back(std::move(track2));
   //     } else {
@@ -239,10 +244,11 @@ StatusCode CKF2::execute() {
     ATH_MSG_DEBUG("  position: " << params.position(gctx).transpose());
     ATH_MSG_DEBUG("  momentum: " << params.momentum().transpose());
     ATH_MSG_DEBUG("  charge:   " << params.charge());
-    std::unique_ptr<Trk::Track> track = m_createTrkTrackTool->createTrack(gctx, traj);
+    std::unique_ptr<Trk::Track> track = m_createTrkTrackTool->createTrack(gctx, traj, m_backwardPropagation);
     if (track != nullptr) {
       m_numberOfSelectedTracks++;
-      std::unique_ptr<Trk::Track> track2 = m_kalmanFitterTool1->fit(ctx, gctx, track.get(), Acts::BoundVector::Zero(), m_isMC, origin);
+      std::unique_ptr<Trk::Track> track2 = m_kalmanFitterTool1->fit(
+        ctx, gctx, track.get(), Acts::BoundVector::Zero(), m_isMC);
       if (track2) {
         outputTracks->push_back(std::move(track2));
       } else {
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.cxx
index bbae70e3b..51703a125 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.cxx
@@ -34,7 +34,8 @@ StatusCode CircleFitTrackSeedTool::initialize() {
 }
 
 
-StatusCode CircleFitTrackSeedTool::run(std::vector<int> maskedLayers) {
+StatusCode CircleFitTrackSeedTool::run(std::vector<int> maskedLayers, bool backward) {
+
   SG::ReadHandle<Tracker::FaserSCT_ClusterContainer> clusterContainer {m_clusterContainerKey};
       ATH_CHECK(clusterContainer.isValid());
 
@@ -114,17 +115,17 @@ StatusCode CircleFitTrackSeedTool::run(std::vector<int> maskedLayers) {
   std::vector<Segment> combination {};
   std::vector<Seed> seeds {};
   // create seeds from four stations
-  go(segments, combination, seeds, 0, 4);
+  go(segments, combination, seeds, 0, 4, backward);
   if (seeds.size() < 2) {
     // create seeds from three stations
-    go(segments, combination, seeds, 0, 3);
+    go(segments, combination, seeds, 0, 3, backward);
   }
   // create seeds from two stations
   if (seeds.size() < 2) {
-    go(segments, combination, seeds, 0, 2);
+    go(segments, combination, seeds, 0, 2, backward);
   }
   if (seeds.size() < 2) {
-    go(segments, combination, seeds, 0, 1);
+    go(segments, combination, seeds, 0, 1, backward);
   }
 
   std::list<Seed> allSeeds;
@@ -163,17 +164,22 @@ StatusCode CircleFitTrackSeedTool::run(std::vector<int> maskedLayers) {
   cov(Acts::eBoundQOverP, Acts::eBoundQOverP) = m_covQOverP;
   cov(Acts::eBoundTime, Acts::eBoundTime) = m_covTime;
 
-  auto minSeed = std::min_element(
-      selectedSeeds.begin(), selectedSeeds.end(), [](const Seed &lhs, const Seed &rhs) {
-        return lhs.minZ < rhs.minZ;
-      });
-  double origin = !selectedSeeds.empty() ? minSeed->minZ - 10 : 0;
+  double origin;
+  if (!backward) {
+    auto minSeed = std::min_element(
+        selectedSeeds.begin(), selectedSeeds.end(), [](const Seed &lhs, const Seed &rhs) {
+          return lhs.minZ < rhs.minZ;
+        });
+    origin = !selectedSeeds.empty() ? minSeed->minZ - 10 : 0;
+  } else {
+    origin = 2470;
+  }
   m_targetZPosition = origin;
   std::vector<Acts::CurvilinearTrackParameters> initParams {};
   ATH_MSG_DEBUG("Sorted seed properties:");
   for (const Seed &seed : selectedSeeds) {
     ATH_MSG_DEBUG("seed size: " << seed.size << ", chi2: " << seed.chi2);
-    initParams.push_back(seed.get_params(origin, cov));
+    initParams.push_back(seed.get_params(origin, cov, backward));
   }
 
   m_initialTrackParameters = std::make_shared<std::vector<Acts::CurvilinearTrackParameters>>(initParams);
@@ -198,15 +204,16 @@ StatusCode CircleFitTrackSeedTool::finalize() {
 void CircleFitTrackSeedTool::go(const std::array<std::vector<Segment>, 4> &v,
                                 std::vector<Segment> &combination,
                                 std::vector<Seed> &seeds,
-                                int offset, int k) {
+                                int offset, int k,
+                                bool backward) {
   if (k == 0) {
-    seeds.push_back(Seed(combination));
+    seeds.push_back(Seed(combination, backward));
     return;
   }
   for (std::size_t i = offset; i < v.size() + 1 - k; ++i) {
     for (const auto& ve : v[i]) {
       combination.push_back(ve);
-      go(v, combination, seeds, i+1, k-1);
+      go(v, combination, seeds, i+1, k-1, backward);
       combination.pop_back();
     }
   }
@@ -242,7 +249,7 @@ CircleFitTrackSeedTool::Segment::Segment(const Trk::Track* track, const FaserSCT
   fakePositions.push_back(position + 30 * momentum.normalized());
 }
 
-CircleFitTrackSeedTool::Seed::Seed(const std::vector<Segment> &segments) :
+CircleFitTrackSeedTool::Seed::Seed(const std::vector<Segment> &segments, bool backward) :
     clusterSet(CircleFitTrackSeedTool::s_indexMap.size()) {
   for (const Segment &seg : segments) {
     clusters.insert(clusters.end(), seg.clusters.begin(), seg.clusters.end());
@@ -261,8 +268,11 @@ CircleFitTrackSeedTool::Seed::Seed(const std::vector<Segment> &segments) :
       } );
   minZ = (*minCluster)->globalPosition().z();
 
-  if (segments.size() > 1) {
+  if ((segments.size() > 1) && (!backward)) {
     direction = positions[1] - positions[0];
+  } else if ((segments.size() > 1) && (backward)) {
+    size_t size = segments.size();
+    direction = positions[size-1] - positions[size-2];
   } else {
     direction = segments[0].momentum;
   }
@@ -329,14 +339,21 @@ void CircleFitTrackSeedTool::Seed::getChi2() {
     chi2 += (m_dy * m_dy) / (m_sigma_y * m_sigma_y);
   }
 
-  for (const Acts::Vector3 &pos : positions) {
-    m_dx = pos.x() - getX(pos.x());
+  for (const Acts::Vector3 &pos : fakePositions) {
+    m_dx = pos.x() - getX(pos.z());
     chi2 += (m_dx * m_dx) / (m_sigma_x * m_sigma_x);
   }
 }
 
-Acts::CurvilinearTrackParameters CircleFitTrackSeedTool::Seed::get_params(double origin, Acts::BoundSymMatrix cov) const {
-  Acts::Vector3 pos = positions[0] - (positions[0].z() - origin)/direction.z() * direction;
-  Acts::Vector4 pos4 {pos.x(), pos.y(), pos.z(), 0};
-  return Acts::CurvilinearTrackParameters(pos4, direction.normalized(), momentum, charge, cov);
+Acts::CurvilinearTrackParameters CircleFitTrackSeedTool::Seed::get_params(double origin, Acts::BoundSymMatrix cov, bool backward) const {
+  if (!backward) {
+    Acts::Vector3 pos = positions[0] - (positions[0].z() - origin)/direction.z() * direction;
+    Acts::Vector4 pos4 {pos.x(), pos.y(), pos.z(), 0};
+    return Acts::CurvilinearTrackParameters(pos4, direction.normalized(), momentum, charge, cov);
+  } else {
+    size_t size = positions.size();
+    Acts::Vector3 pos = positions[size-1] + (origin - positions[size-1].z())/direction.z() * direction;
+    Acts::Vector4 pos4 {pos.x(), pos.y(), pos.z(), 0};
+    return Acts::CurvilinearTrackParameters(pos4, direction.normalized(), momentum, charge, cov);
+  }
 }
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.h
index a4d53c629..ea3d0263a 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CircleFitTrackSeedTool.h
@@ -30,7 +30,7 @@ public:
   virtual ~CircleFitTrackSeedTool() = default;
   virtual StatusCode initialize() override;
   virtual StatusCode finalize() override;
-  virtual StatusCode run(std::vector<int> maskedLayers = {}) override;
+  virtual StatusCode run(std::vector<int> maskedLayers = {}, bool backward=false) override;
 
   const std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialTrackParameters() const override;
   const std::shared_ptr<const Acts::Surface> initialSurface() const override;
@@ -56,7 +56,7 @@ private:
   };
 
   struct Seed {
-    Seed(const std::vector<Segment> &segments);
+    Seed(const std::vector<Segment> &segments, bool backward=false);
 
     ClusterSet clusterSet;
     std::vector<const Tracker::FaserSCT_Cluster*> clusters;
@@ -67,7 +67,7 @@ private:
     double c0, c1, cx, cy, r, chi2, momentum, charge, minZ;
     Acts::Vector3 direction;
     size_t size, stations, constraints;
-    Acts::CurvilinearTrackParameters get_params(double origin, Acts::BoundSymMatrix cov) const;
+    Acts::CurvilinearTrackParameters get_params(double origin, Acts::BoundSymMatrix cov, bool backward=false) const;
 
   private:
     void getChi2();
@@ -83,7 +83,7 @@ private:
   };
 
   void go(const std::array<std::vector<Segment>, 4> &v, std::vector<Segment> &combination,
-          std::vector<Seed> &seeds, int offset, int k);
+          std::vector<Seed> &seeds, int offset, int k, bool backward=false);
 
   static std::map<Identifier, Index> s_indexMap;
   static std::map<Identifier, const Tracker::FaserSCT_SpacePoint*> s_spacePointMap;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.cxx
index 994689010..9a9bf115c 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.cxx
@@ -17,7 +17,7 @@ StatusCode CreateTrkTrackTool::finalize() {
 }
 
 std::unique_ptr<Trk::Track>
-CreateTrkTrackTool::createTrack(const Acts::GeometryContext &gctx, const FaserActsRecMultiTrajectory &traj) const {
+CreateTrkTrackTool::createTrack(const Acts::GeometryContext &gctx, const FaserActsRecMultiTrajectory &traj, bool backwardPropagation) const {
   std::unique_ptr<Trk::Track> newtrack = nullptr;
   DataVector<const Trk::TrackStateOnSurface>* finalTrajectory = new DataVector<const Trk::TrackStateOnSurface>{};
   using ConstTrackStateProxy = Acts::detail_lt::TrackStateProxy<IndexSourceLink, 6, true>;
@@ -69,9 +69,10 @@ CreateTrkTrackTool::createTrack(const Acts::GeometryContext &gctx, const FaserAc
 									      quality,
 									      nullptr,
 									      typePattern);
-      if (perState)
-      {
+      if ((perState) && (!backwardPropagation)) {
 	        finalTrajectory->insert(finalTrajectory->begin(), perState);
+      } else if ((perState) && (backwardPropagation)) {
+	        finalTrajectory->push_back(perState);
       }
     }
     return;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.h
index d0edfd5cb..5fcf1f8c4 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CreateTrkTrackTool.h
@@ -18,7 +18,7 @@ public:
   virtual ~CreateTrkTrackTool() = default;
   virtual StatusCode initialize() override;
   virtual StatusCode finalize() override;
-  std::unique_ptr<Trk::Track> createTrack(const Acts::GeometryContext &gctx, const FaserActsRecMultiTrajectory &traj) const;
+  std::unique_ptr<Trk::Track> createTrack(const Acts::GeometryContext &gctx, const FaserActsRecMultiTrajectory &traj, bool backwardPropagation=false) const;
   const Trk::TrackParameters* ConvertActsTrackParameterToATLAS(const Acts::BoundTrackParameters &actsParameter, const Acts::GeometryContext& gctx) const;
 private:
   const FaserSCT_ID* m_idHelper {nullptr};
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.cxx
index c0d02183f..5315404c7 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.cxx
@@ -45,7 +45,7 @@ StatusCode KalmanFitterTool::finalize() {
 std::vector<TSOS4Residual>
 KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::GeometryContext &gctx,
                       Trk::Track* inputTrack, double clusz, const Acts::BoundVector& inputVector,
-                      bool isMC, double origin) const {
+                      bool /*isMC*/, double origin) const {
   std::vector<TSOS4Residual> resi;
   resi.clear();
   std::vector<FaserActsRecMultiTrajectory> myTrajectories;
@@ -126,14 +126,14 @@ KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::Geome
 	    state.referenceSurface().getSharedPtr(),
 	    state.smoothed(),
 	    state.smoothedCovariance());
-	auto covariance = state.smoothedCovariance();
+	    // auto covariance = state.smoothedCovariance();
 	    auto H = state.effectiveProjector();
 	    auto residual = state.effectiveCalibrated() - H * state.smoothed();
 	    const Tracker::FaserSCT_Cluster* cluster = state.uncalibrated().hit();
-	    const auto& surface = state.referenceSurface();
+	    // const auto& surface = state.referenceSurface();
 	    Acts::BoundVector meas = state.projector().transpose() * state.calibrated();
 	    Acts::Vector2 local(meas[Acts::eBoundLoc0], meas[Acts::eBoundLoc1]);
-	    const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
+	    // const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
 	    resi.push_back({local.x(),local.y(),parameter.position(gctx).x(), parameter.position(gctx).y(), parameter.position(gctx).z(), cluster,residual(Acts::eBoundLoc0),parameter});
 	    ATH_MSG_DEBUG("  residual/global position:   " << residual(Acts::eBoundLoc0)<<" "<<parameter.position(gctx).x()<<" "<<parameter.position(gctx).y()<<" "<<parameter.position(gctx).z());
 	    }
@@ -155,8 +155,8 @@ KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::Geome
 //
 std::vector<TSOS4Residual>
 KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::GeometryContext &gctx,
-                      Trk::Track* inputTrack,  const Acts::BoundVector& inputVector,
-                      bool isMC, double origin, std::vector<const Tracker::FaserSCT_Cluster*>& clusters, const Acts::BoundTrackParameters ini_Param) const {
+                      Trk::Track* inputTrack,  const Acts::BoundVector& /*inputVector*/,
+                      bool /*isMC*/, double origin, std::vector<const Tracker::FaserSCT_Cluster*>& clusters, const Acts::BoundTrackParameters ini_Param) const {
   std::vector<TSOS4Residual> resi;
   resi.clear();
   std::vector<FaserActsRecMultiTrajectory> myTrajectories;
@@ -232,14 +232,14 @@ KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::Geome
 	    state.referenceSurface().getSharedPtr(),
 	    state.smoothed(),
 	    state.smoothedCovariance());
-	auto covariance = state.smoothedCovariance();
+	    // auto covariance = state.smoothedCovariance();
 	    auto H = state.effectiveProjector();
 	    auto residual = state.effectiveCalibrated() - H * state.smoothed();
 	    const Tracker::FaserSCT_Cluster* cluster = state.uncalibrated().hit();
-	    const auto& surface = state.referenceSurface();
+	    // const auto& surface = state.referenceSurface();
 	    Acts::BoundVector meas = state.projector().transpose() * state.calibrated();
 	    Acts::Vector2 local(meas[Acts::eBoundLoc0], meas[Acts::eBoundLoc1]);
-	    const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
+	    // const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
 	    resi.push_back({local.x(),local.y(),parameter.position(gctx).x(), parameter.position(gctx).y(), parameter.position(gctx).z(), cluster,residual(Acts::eBoundLoc0),parameter});
 	    }
 	    return true;
@@ -261,7 +261,7 @@ KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::Geome
 std::vector<TSOS4Residual>
 KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::GeometryContext &gctx,
                       Trk::Track* inputTrack,  const Acts::BoundVector& inputVector,
-                      bool isMC, double origin) const {
+                      bool /*isMC*/, double origin) const {
   std::vector<TSOS4Residual> resi;
   resi.clear();
   std::vector<FaserActsRecMultiTrajectory> myTrajectories;
@@ -339,14 +339,14 @@ KalmanFitterTool::getUnbiasedResidual(const EventContext &ctx, const Acts::Geome
 	    state.referenceSurface().getSharedPtr(),
 	    state.smoothed(),
 	    state.smoothedCovariance());
-	auto covariance = state.smoothedCovariance();
+	    // auto covariance = state.smoothedCovariance();
 	    auto H = state.effectiveProjector();
 	    auto residual = state.effectiveCalibrated() - H * state.smoothed();
 	    const Tracker::FaserSCT_Cluster* cluster = state.uncalibrated().hit();
-	    const auto& surface = state.referenceSurface();
+	    // const auto& surface = state.referenceSurface();
 	    Acts::BoundVector meas = state.projector().transpose() * state.calibrated();
 	    Acts::Vector2 local(meas[Acts::eBoundLoc0], meas[Acts::eBoundLoc1]);
-	    const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
+	    // const Acts::Vector3 dir = Acts::makeDirectionUnitFromPhiTheta(meas[Acts::eBoundPhi], meas[Acts::eBoundTheta]);
 resi.push_back({local.x(),local.y(),parameter.position(gctx).x(), parameter.position(gctx).y(), parameter.position(gctx).z(), cluster,residual(Acts::eBoundLoc0),parameter});
 	    }
 	    return true;
@@ -365,7 +365,7 @@ resi.push_back({local.x(),local.y(),parameter.position(gctx).x(), parameter.posi
 std::unique_ptr<Trk::Track>
 KalmanFitterTool::fit(const EventContext &ctx, const Acts::GeometryContext &gctx,
                       Trk::Track* inputTrack, const Acts::BoundVector& inputVector,
-                      bool isMC, double origin) const {
+                      bool isMC) const {
   std::unique_ptr<Trk::Track> newTrack = nullptr;
   std::vector<FaserActsRecMultiTrajectory> myTrajectories;
 
@@ -380,6 +380,9 @@ KalmanFitterTool::fit(const EventContext &ctx, const Acts::GeometryContext &gctx
   }
 
 
+  // set the start position 5 mm in front of the first track measurement
+  double origin = inputTrack->trackParameters()->front()->position().z() - 10;
+
   auto pSurface = Acts::Surface::makeShared<Acts::PlaneSurface>(
       Acts::Vector3 {0, 0, origin}, Acts::Vector3{0, 0, -1});
 
@@ -544,7 +547,7 @@ KalmanFitterTool::getMeasurementsFromTrack(Trk::Track *track, std::vector<const
 }
 
 std::tuple<std::vector<IndexSourceLink>, std::vector<Measurement>>
-KalmanFitterTool::getMeasurementsFromTrack(Trk::Track *track, Identifier& wafer_id) const {
+KalmanFitterTool::getMeasurementsFromTrack(Trk::Track *track, Identifier& /*wafer_id*/) const {
   const int kSize = 1;
   std::array<Acts::BoundIndices, kSize> Indices = {Acts::eBoundLoc0};
   using ThisMeasurement = Acts::Measurement<IndexSourceLink, Acts::BoundIndices, kSize>;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.h
index 584cda2c9..4901d324f 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/KalmanFitterTool.h
@@ -90,7 +90,7 @@ public:
   std::unique_ptr<Trk::Track> fit(const EventContext &ctx, const Acts::GeometryContext &gctx,
                                   Trk::Track *inputTrack,
                                   const Acts::BoundVector& inputVector = Acts::BoundVector::Zero(),
-                                  bool isMC=false, double origin=0) const;
+                                  bool isMC=false) const;
   std::vector<TSOS4Residual> getUnbiasedResidual(const EventContext &ctx, const Acts::GeometryContext &gctx,
                                   Trk::Track *inputTrack,
                                   const Acts::BoundVector& inputVector = Acts::BoundVector::Zero(),
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.cxx
index b5a5e0489..4477735fa 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.cxx
@@ -26,7 +26,7 @@ StatusCode MyTrackSeedTool::initialize() {
 }
 
 
-StatusCode MyTrackSeedTool::run(std::vector<int> /*maskedLayers*/) {
+StatusCode MyTrackSeedTool::run(std::vector<int> /*maskedLayers*/, bool /*backward*/) {
 
   // create track seeds for multiple tracks
   SG::ReadHandle<TrackCollection> trackCollection {m_trackCollection};
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.h
index df545c34a..26e857b7b 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/MyTrackSeedTool.h
@@ -25,7 +25,7 @@ public:
   virtual ~MyTrackSeedTool() = default;
   virtual StatusCode initialize() override;
   virtual StatusCode finalize() override;
-  virtual StatusCode run(std::vector<int> /*maskedLayers*/) override;
+  virtual StatusCode run(std::vector<int> /*maskedLayers*/, bool /*backward*/) override;
 
   const std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialTrackParameters() const override;
   const std::shared_ptr<const Acts::Surface> initialSurface() const override;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.cxx
index 15db30010..65d15e723 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.cxx
@@ -23,7 +23,7 @@ StatusCode ThreeStationTrackSeedTool::initialize() {
 }
 
 
-StatusCode ThreeStationTrackSeedTool::run(std::vector<int> /*maskedLayers*/) {
+StatusCode ThreeStationTrackSeedTool::run(std::vector<int> /*maskedLayers*/, bool /*backward*/) {
   // create track seeds for multiple tracks
   SG::ReadHandle<TrackCollection> trackCollection {m_trackCollection};
   ATH_CHECK(trackCollection.isValid());
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.h b/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.h
index deab07bb1..7c4c521b8 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/ThreeStationTrackSeedTool.h
@@ -23,7 +23,7 @@ public:
   virtual ~ThreeStationTrackSeedTool() = default;
   virtual StatusCode initialize() override;
   virtual StatusCode finalize() override;
-  virtual StatusCode run(std::vector<int> /*maskedLayers*/) override;
+  virtual StatusCode run(std::vector<int> /*maskedLayers*/, bool /*backward*/) override;
 
   const std::shared_ptr<std::vector<Acts::CurvilinearTrackParameters>> initialTrackParameters() const override;
   const std::shared_ptr<const Acts::Surface> initialSurface() const override;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/components/FaserActsKalmanFilter_entries.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/components/FaserActsKalmanFilter_entries.cxx
index be32f4959..41fd70070 100755
--- a/Tracking/Acts/FaserActsKalmanFilter/src/components/FaserActsKalmanFilter_entries.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/components/FaserActsKalmanFilter_entries.cxx
@@ -23,7 +23,7 @@
 #include "../TrackSeedWriterTool.h"
 #include "../ActsTrackSeedTool.h"
 #include "../CKF2.h"
-#include "../CKF2Alignment.h"
+// #include "../CKF2Alignment.h"
 #include "../KalmanFitterTool.h"
 #include "../MyTrackSeedTool.h"
 #include "../SeedingAlg.h"
@@ -54,7 +54,7 @@ DECLARE_COMPONENT(PerformanceWriterTool)
 DECLARE_COMPONENT(TrackSeedWriterTool)
 DECLARE_COMPONENT(ActsTrackSeedTool)
 DECLARE_COMPONENT(CKF2)
-DECLARE_COMPONENT(CKF2Alignment)
+// DECLARE_COMPONENT(CKF2Alignment)
 DECLARE_COMPONENT(KalmanFitterTool)
 DECLARE_COMPONENT(MyTrackSeedTool)
 DECLARE_COMPONENT(SeedingAlg)
-- 
GitLab