From 3fd56f8abcb7bb525d576380c6e2ecdd50a67d18 Mon Sep 17 00:00:00 2001
From: Xiaocong Ai <xiaocong.ai@cern.ch>
Date: Tue, 3 Dec 2019 21:09:19 -0800
Subject: [PATCH] rebase

---
 .../Acts/EventData/MultiTrajectory.hpp        | 14 ++++++++
 .../Acts/EventData/MultiTrajectory.ipp        |  1 +
 Core/include/Acts/EventData/TrackState.hpp    |  6 ++--
 Core/include/Acts/Fitter/KalmanFitter.hpp     | 34 +++++++++----------
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/Core/include/Acts/EventData/MultiTrajectory.hpp b/Core/include/Acts/EventData/MultiTrajectory.hpp
index d0f366946..27e4305bc 100644
--- a/Core/include/Acts/EventData/MultiTrajectory.hpp
+++ b/Core/include/Acts/EventData/MultiTrajectory.hpp
@@ -108,6 +108,7 @@ struct IndexData {
 
   double chi2;
   double pathLength;
+  std::bitset<TrackStateFlag::NumTrackStateFlags> typeFlags;
 
   IndexType iuncalibrated = kInvalid;
   IndexType icalibrated = kInvalid;
@@ -465,6 +466,19 @@ class TrackStateProxy {
   /// @return The path length of this track state
   double pathLength() const { return data().pathLength; }
 
+  /// Getter for the type flags associated with the track state.
+  /// This overloaded is only enabled if not read-only, and returns a mutable
+  /// reference.
+  /// @return reference to the type flags.
+  template <bool RO = ReadOnly, typename = std::enable_if_t<!RO>>
+  TrackStateType& typeFlags() {
+    return data().typeFlags;
+  }
+
+  /// Getter for the type flags. Returns a copy of the type flags value.
+  /// @return The type flags of this track state
+  TrackStateType typeFlags() const { return data().typeFlags; }
+
  private:
   // Private since it can only be created by the trajectory.
   TrackStateProxy(ConstIf<MultiTrajectory<SourceLink>, ReadOnly>& trajectory,
diff --git a/Core/include/Acts/EventData/MultiTrajectory.ipp b/Core/include/Acts/EventData/MultiTrajectory.ipp
index dec9dcd12..3d68920c0 100644
--- a/Core/include/Acts/EventData/MultiTrajectory.ipp
+++ b/Core/include/Acts/EventData/MultiTrajectory.ipp
@@ -222,6 +222,7 @@ inline size_t MultiTrajectory<SL>::addTrackState(
 
   nts.chi2() = ts.parameter.chi2;
   nts.pathLength() = ts.parameter.pathLength;
+  nts.typeFlags() = ts.type();
 
   return index;
 }
diff --git a/Core/include/Acts/EventData/TrackState.hpp b/Core/include/Acts/EventData/TrackState.hpp
index 652bac185..d9bd0b9e0 100644
--- a/Core/include/Acts/EventData/TrackState.hpp
+++ b/Core/include/Acts/EventData/TrackState.hpp
@@ -29,6 +29,8 @@ enum TrackStateFlag {
   NumTrackStateFlags = 5
 };
 
+using TrackStateType = std::bitset<TrackStateFlag::NumTrackStateFlags>;
+
 class Surface;
 
 /// @class TrackState
@@ -127,7 +129,7 @@ class TrackState {
   }
 
   /// @brief return method for the type flags
-  std::bitset<NumTrackStateFlags> type() const { return m_typeFlags; }
+  TrackStateType type() const { return m_typeFlags; }
 
   /// @brief number of Measured parameters, forwarded
   /// @note This only returns a value if there is a calibrated measurement
@@ -174,6 +176,6 @@ class TrackState {
   /// The surface of this TrackState
   const Surface* m_surface = nullptr;
   /// The type flag of this TrackState
-  std::bitset<NumTrackStateFlags> m_typeFlags;
+  TrackStateType m_typeFlags;
 };
 }  // namespace Acts
diff --git a/Core/include/Acts/Fitter/KalmanFitter.hpp b/Core/include/Acts/Fitter/KalmanFitter.hpp
index 8ec7c12b4..7ecad6dff 100644
--- a/Core/include/Acts/Fitter/KalmanFitter.hpp
+++ b/Core/include/Acts/Fitter/KalmanFitter.hpp
@@ -192,7 +192,7 @@ class KalmanFitter {
   template <typename source_link_t, typename parameters_t>
   class Actor {
    public:
-    using TrackStateType = TrackState<source_link_t, parameters_t>;
+    using TrackState = TrackState<source_link_t, parameters_t>;
 
     /// Explicit constructor with updater and calibrator
     Actor(updater_t pUpdater = updater_t(), smoother_t pSmoother = smoother_t(),
@@ -307,9 +307,8 @@ class KalmanFitter {
     Result<void> filter(const Surface* surface, propagator_state_t& state,
                         const stepper_t& stepper, result_type& result) const {
       // Transport & bind the state to the current surface
-      std::tuple<BoundParameters,
-                 typename TrackStateType::Parameters::CovMatrix_t, double>
-          boundState = stepper.boundState(state.stepping, *surface, true);
+      auto [boundParams, jacobian, pathLength] =
+          stepper.boundState(state.stepping, *surface, true);
 
       // Try to find the surface in the measurement surfaces
       auto sourcelink_it = inputMeasurements.find(surface);
@@ -329,10 +328,6 @@ class KalmanFitter {
         // assign the source link to the track state
         trackStateProxy.uncalibrated() = sourcelink_it->second;
 
-        // Transport & bind the state to the current surface
-        auto [boundParams, jacobian, pathLength] =
-            stepper.boundState(state.stepping, *surface, true);
-
         // Fill the track state
         trackStateProxy.predicted() = boundParams.parameters();
         trackStateProxy.predictedCovariance() = *boundParams.covariance();
@@ -348,8 +343,10 @@ class KalmanFitter {
             m_calibrator(trackStateProxy.uncalibrated(),
                          trackStateProxy.predicted()));
 
-        // Set the parameter type flag
-        trackState.setType(TrackStateFlag::ParameterFlag);
+        // Get and set the type flags
+        auto typeFlags = trackStateProxy.typeFlags();
+        typeFlags.set(TrackStateFlag::MeasurementFlag);
+        typeFlags.set(TrackStateFlag::ParameterFlag);
 
         // If the update is successful, set covariance and
         auto updateRes = m_updater(state.geoContext, trackStateProxy);
@@ -371,30 +368,31 @@ class KalmanFitter {
       } else {
         // When no measurement on this surface,
         // create a track state from predicted parameter
-        result.fittedStates.emplace_back(std::get<0>(boundState));
-        TrackStateType& trackState = result.fittedStates.back();
+        TrackState trackState(boundParams);
 
         // Fill the track state
-        trackState.parameter.jacobian = std::get<1>(boundState);
-        trackState.parameter.pathLength = std::get<2>(boundState);
+        trackState.parameter.jacobian = jacobian;
+        trackState.parameter.pathLength = pathLength;
 
         // @todo: set the filtered parameter by updating the predicted parameter
         // with material effects
 
         if (surface->associatedDetectorElement() != nullptr) {
-          // If the surface is sensitive,
-          // set the hole type flag
+          // If the surface is sensitive, set the hole type flag
           trackState.setType(TrackStateFlag::HoleFlag);
 
           // Count the missed surface
           ACTS_VERBOSE("Detected hole on " << surface->geoID());
           result.missedActiveSurfaces.push_back(surface);
         } else {
-          // If the surface is in-sensitive,
-          // set the material type flag
+          // If the surface is in-sensitive, set the material type flag
           trackState.setType(TrackStateFlag::MaterialFlag);
           ACTS_VERBOSE("Detected in-sensitive surface " << surface->geoID());
         }
+
+        // Add the track state to the fittedStates
+        result.trackTip =
+            result.fittedStates.addTrackState(trackState, result.trackTip);
       }
       return Result<void>::success();
     }
-- 
GitLab