From 6ab131af4e4be7f6636bb2565efb0ab77222c02b Mon Sep 17 00:00:00 2001
From: Peter Berta <peter.berta@cern.ch>
Date: Wed, 27 Oct 2021 19:46:25 +0200
Subject: [PATCH] Merge branch 'ATLASRECTS-6628' into '22.0-mc20'

TrkExUtils - Escape from infinite loop

See merge request atlas/athena!47445

(cherry picked from commit f2db74e8a4635c8764b2c13ca8fe3a7a0defd4ff)

b07136e3 Lift the extrapolator from the limbo if the next step is beyond ATLAS... Do...
---
 .../src/RungeKuttaPropagator.cxx                     | 12 ++++++------
 .../TrkExUtils/src/RungeKuttaUtils.cxx               |  7 ++++++-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/Tracking/TrkExtrapolation/TrkExRungeKuttaPropagator/src/RungeKuttaPropagator.cxx b/Tracking/TrkExtrapolation/TrkExRungeKuttaPropagator/src/RungeKuttaPropagator.cxx
index 0d2cdff518b8..b856e9c5fc15 100755
--- a/Tracking/TrkExtrapolation/TrkExRungeKuttaPropagator/src/RungeKuttaPropagator.cxx
+++ b/Tracking/TrkExtrapolation/TrkExRungeKuttaPropagator/src/RungeKuttaPropagator.cxx
@@ -397,7 +397,6 @@ Trk::RungeKuttaPropagator::propagate
     if(D < 0) S = -S;
     S = straightLineStep(useJac,S,Po);
   }
-
   double Wmax  = 50000.    ; // Max pass
   double W     = 0.        ; // Current pass
   double Smax  = 100.      ; // Max step
@@ -406,6 +405,7 @@ Trk::RungeKuttaPropagator::propagate
 
   std::multimap<double,int> DN; double Scut[3];
   int Nveto = Trk::RungeKuttaUtils::fillDistancesMap(DS,DN,Po,W,&Tp.associatedSurface(),Scut);
+  
 
   // Test conditions tor start propagation and chocse direction if D == 0
   //
@@ -440,7 +440,7 @@ Trk::RungeKuttaPropagator::propagate
     if(cache.m_mcondition) {
 
       //----------------------------------Niels van Eldik patch
-      if (reverted_P && St == last_St && InS == last_InS /*&& condition_fulfiled*/) {
+      if (reverted_P && std::abs(St - last_St) <= DBL_EPSILON &&  InS==last_InS /*&& condition_fulfiled*/) {
           // inputs are not changed will get same result.
           break;
       }
@@ -454,7 +454,7 @@ Trk::RungeKuttaPropagator::propagate
     else  {
 
       //----------------------------------Niels van Eldik patch
-      if (reverted_P && St == last_St /*&& !condition_fulfiled*/) {
+      if (reverted_P && std::abs(St- last_St) <= DBL_EPSILON /*&& !condition_fulfiled*/) {
           // inputs are not changed will get same result.
           break;
       }
@@ -468,8 +468,8 @@ Trk::RungeKuttaPropagator::propagate
     reverted_P=false;
     //----------------------------------
 
-    bool next = 0; SN=Trk::RungeKuttaUtils::stepEstimator(DS,DN,Po,Pn,W,m_straightStep,Nveto,next);
-
+    bool next {false}; 
+    SN=Trk::RungeKuttaUtils::stepEstimator(DS,DN,Po,Pn,W,m_straightStep,Nveto,next);
     if(next) {for(int i=0; i!=45; ++i) Po[i]=Pn[i]; W+=S; Nveto=-1; }
     else     {for(int i=0; i!=45; ++i) Pn[i]=Po[i]; reverted_P=true; cache.m_newfield= true;}
 
@@ -488,7 +488,7 @@ Trk::RungeKuttaPropagator::propagate
 	if(auto To {crossPoint(Tp,DS,Sol,Pn,SN)};To) return To;
 	Nveto = SN.second; St = Sl;
       }
-    }
+    } else if (std::abs(S)< DBL_EPSILON) return nullptr;
   }
   return nullptr;
 }
diff --git a/Tracking/TrkExtrapolation/TrkExUtils/src/RungeKuttaUtils.cxx b/Tracking/TrkExtrapolation/TrkExUtils/src/RungeKuttaUtils.cxx
index 16035e020d70..fda4e94bd747 100755
--- a/Tracking/TrkExtrapolation/TrkExUtils/src/RungeKuttaUtils.cxx
+++ b/Tracking/TrkExtrapolation/TrkExUtils/src/RungeKuttaUtils.cxx
@@ -1058,7 +1058,12 @@ Trk::RungeKuttaUtils::stepEstimator(
   double D[3] = { Pout[0] - Pinp[0], Pout[1] - Pinp[1], Pout[2] - Pinp[2] };
   double Smax = sqrt(D[0] * D[0] + D[1] * D[1] + D[2] * D[2]);
   double Sign = D[0] * Pinp[3] + D[1] * Pinp[4] + D[2] * Pinp[5];
-
+  // The magnitude of the vector is essentially 0. No 
+  /// sensible estimate of the next step possible.
+  if (Smax < DBL_EPSILON){
+      next = true;
+      return std::make_pair(0., -1);
+  }
   Amg::Vector3D pos(Pinp[0], Pinp[1], Pinp[2]);
   Amg::Vector3D dir(D[0] / Smax, D[1] / Smax, D[2] / Smax);
 
-- 
GitLab