From 5e4ebdfcdeea498204d01573cecab9a8ff2e4d14 Mon Sep 17 00:00:00 2001
From: Tobias Boeckh <tobias.boeckh@cern.ch>
Date: Mon, 23 Jan 2023 13:14:41 +0100
Subject: [PATCH] set xAOD::EventInfo::Error if we get an ACTS propagation
 error and add coresponding variable to NtupleDumperAlg

---
 .../NtupleDumper/src/NtupleDumperAlg.cxx         | 10 ++++++++++
 .../NtupleDumper/src/NtupleDumperAlg.h           |  6 +++++-
 Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx | 16 ++++++++++++++--
 Tracking/Acts/FaserActsKalmanFilter/src/CKF2.h   |  2 ++
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.cxx b/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.cxx
index b61be213..744606b0 100644
--- a/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.cxx
+++ b/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.cxx
@@ -106,6 +106,7 @@ StatusCode NtupleDumperAlg::initialize()
 
   ATH_CHECK(m_preshowerCalibratedContainer.initialize());
   ATH_CHECK(m_ecalCalibratedContainer.initialize());
+  ATH_CHECK(m_eventInfoKey.initialize());
 
   ATH_CHECK(detStore()->retrieve(m_sctHelper,       "FaserSCT_ID"));
   ATH_CHECK(detStore()->retrieve(m_vetoNuHelper,    "VetoNuID"));
@@ -197,6 +198,7 @@ StatusCode NtupleDumperAlg::initialize()
   m_tree->Branch("TrackSegment_pz", &m_trackseg_pz);
 
   //TrackCollection
+  m_tree->Branch("Track_PropagationError", &m_propagationError, "Track_PropagationError/B");
   m_tree->Branch("longTracks", &m_longTracks, "longTracks/I");
   m_tree->Branch("Track_Chi2", &m_Chi2);
   m_tree->Branch("Track_nDoF", &m_DoF);
@@ -681,6 +683,14 @@ StatusCode NtupleDumperAlg::execute(const EventContext &ctx) const
     }
   }
 
+  SG::ReadDecorHandle<xAOD::EventInfo,uint32_t> eventInfo (m_eventInfoKey, ctx);
+  if (eventInfo->errorState(xAOD::EventInfo_v1::SCT) == xAOD::EventInfo::Error) {
+    m_propagationError = true;
+    ATH_MSG_DEBUG("NtupleDumper: xAOD::EventInfo::SCT::Error");
+  } else {
+    m_propagationError = false;
+  }
+
   // get geometry context
   FaserActsGeometryContext faserGeometryContext = m_trackingGeometryTool->getNominalGeometryContext();
   auto gctx = faserGeometryContext.context();
diff --git a/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.h b/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.h
index 5cc34202..4e694c21 100644
--- a/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.h
+++ b/PhysicsAnalysis/NtupleDumper/src/NtupleDumperAlg.h
@@ -21,6 +21,8 @@
 #include "FaserActsKalmanFilter/IFiducialParticleTool.h"
 #include "FaserActsKalmanFilter/ITrackTruthMatchingTool.h"
 #include "TrackerSimEvent/FaserSiHitCollection.h"
+#include "xAODEventInfo/EventInfo.h"
+#include "StoreGate/ReadDecorHandle.h"
 
 #include <vector>
 
@@ -84,7 +86,8 @@ private:
 
   SG::ReadHandleKey<xAOD::FaserTriggerData> m_FaserTriggerData     { this, "FaserTriggerDataKey", "FaserTriggerData", "ReadHandleKey for xAOD::FaserTriggerData"};
   SG::ReadHandleKey<xAOD::WaveformClock> m_ClockWaveformContainer     { this, "WaveformClockKey", "WaveformClock", "ReadHandleKey for ClockWaveforms Container"};
-  ToolHandle<IFaserActsExtrapolationTool> m_extrapolationTool { this, "ExtrapolationTool", "FaserActsExtrapolationTool" };  
+  SG::ReadDecorHandleKey<xAOD::EventInfo> m_eventInfoKey{this, "EventInfoKey", "EventInfo"};
+  ToolHandle<IFaserActsExtrapolationTool> m_extrapolationTool { this, "ExtrapolationTool", "FaserActsExtrapolationTool" };
   ToolHandle<IFaserActsTrackingGeometryTool> m_trackingGeometryTool {this, "TrackingGeometryTool", "FaserActsTrackingGeometryTool"};
   ToolHandle<ITrackTruthMatchingTool> m_trackTruthMatchingTool {this, "TrackTruthMatchingTool", "TrackTruthMatchingTool"};
   ToolHandle<IFiducialParticleTool> m_fiducialParticleTool {this, "FiducialParticleTool", "FiducialParticleTool"};
@@ -189,6 +192,7 @@ private:
   mutable std::vector<double> m_trackseg_pz;
 
   mutable int    m_longTracks;
+  mutable bool m_propagationError;
   mutable std::vector<double> m_Chi2;
   mutable std::vector<double> m_DoF;
   mutable std::vector<double> m_xup;
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
index 3647ac98..f990be94 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.cxx
@@ -2,6 +2,7 @@
 
 #include "StoreGate/ReadHandle.h"
 #include "StoreGate/ReadCondHandleKey.h"
+#include "StoreGate/WriteDecorHandle.h"
 #include "TrackerSpacePoint/FaserSCT_SpacePointCollection.h"
 #include "TrackerSpacePoint/FaserSCT_SpacePoint.h"
 #include "TrackerIdentifier/FaserSCT_ID.h"
@@ -28,9 +29,9 @@
 #include "Acts/Propagator/Propagator.hpp"
 #include "Acts/TrackFitting/GainMatrixSmoother.hpp"
 #include "Acts/TrackFitting/GainMatrixUpdater.hpp"
-
-
 #include "Acts/EventData/Measurement.hpp"
+#include "Acts/Propagator/PropagatorError.hpp"
+#include "Acts/TrackFinding/CombinatorialKalmanFilterError.hpp"
 
 size_t CKF2::TrajectoryInfo::nClusters {0};
 
@@ -49,6 +50,7 @@ StatusCode CKF2::initialize() {
   ATH_CHECK(m_kalmanFitterTool1.retrieve());
   ATH_CHECK(m_createTrkTrackTool.retrieve());
   ATH_CHECK(m_trackCollection.initialize());
+  ATH_CHECK(m_eventInfoKey.initialize());
   if (m_performanceWriter && !m_noDiagnostics) {
     ATH_CHECK(m_performanceWriterTool.retrieve());
   }
@@ -78,6 +80,8 @@ StatusCode CKF2::execute() {
   const EventContext& ctx = Gaudi::Hive::currentContext();
   m_numberOfEvents++;
 
+  SG::WriteDecorHandle<xAOD::EventInfo,uint32_t> eventInfo (m_eventInfoKey, ctx);
+
   SG::WriteHandle trackContainer{m_trackCollection, ctx};
   std::unique_ptr<TrackCollection> outputTracks = std::make_unique<TrackCollection>();
 
@@ -150,6 +154,14 @@ StatusCode CKF2::execute() {
   std::list<TrajectoryInfo> allTrajectories;
   for (auto &result : results) {
     if (not result.ok()) {
+      // TODO use status bits for different errors
+      // result.error() == Acts::CombinatorialKalmanFilterError::NoTrackFound
+      if (result.error() == Acts::PropagatorError::StepCountLimitReached ||
+          result.error() == Acts::CombinatorialKalmanFilterError::PropagationReachesMaxSteps) {
+        if (!eventInfo->updateErrorState(xAOD::EventInfo::SCT, xAOD::EventInfo::Error)) {
+          ATH_MSG_WARNING (" cannot set error state for SCT");
+        }
+      }
       continue;
     }
     CKFResult ckfResult = result.value();
diff --git a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.h b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.h
index 2111afcf..170f0353 100644
--- a/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.h
+++ b/Tracking/Acts/FaserActsKalmanFilter/src/CKF2.h
@@ -21,6 +21,7 @@
 #include "KalmanFitterTool.h"
 #include <boost/dynamic_bitset.hpp>
 #include "CreateTrkTrackTool.h"
+#include "xAODEventInfo/EventInfo.h"
 using ConstTrackStateProxy = Acts::detail_lt::TrackStateProxy<IndexSourceLink, 6, true>;
 using ClusterSet = boost::dynamic_bitset<>;
 
@@ -148,6 +149,7 @@ private:
   ToolHandle<CreateTrkTrackTool> m_createTrkTrackTool {this, "CreateTrkTrackTool", "CreateTrkTrackTool"};
   Gaudi::Property<bool> m_isMC {this, "isMC", false};
   SG::WriteHandleKey<TrackCollection> m_trackCollection { this, "OutputCollection", "CKFTrackCollection", "Output track collection name" };
+  SG::WriteDecorHandleKey<xAOD::EventInfo> m_eventInfoKey{this, "EventInfoKey", "EventInfo"};
 };
 
 #endif // FASERACTSKALMANFILTER_CKF2_H
-- 
GitLab