From 48837dfe18c65b206faa4cd176b8f42573a1532c Mon Sep 17 00:00:00 2001
From: Christos Anastopoulos <christos.anastopoulos@cern.ch>
Date: Tue, 7 Jul 2020 19:12:05 +0000
Subject: [PATCH] Make sure that distances wrt to a merged component are always
 float max

---
 .../KLGaussianMixtureReduction.h              |  1 +
 .../src/KLGaussianMixtureReduction.cxx        | 21 ++++++++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/KLGaussianMixtureReduction.h b/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/KLGaussianMixtureReduction.h
index 2d40c26d26c..b44311b4fc9 100644
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/KLGaussianMixtureReduction.h
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/KLGaussianMixtureReduction.h
@@ -86,6 +86,7 @@ constexpr size_t alignment = 32;
 
 /**
  * @brief struct representing 1D component
+ * Negative weight means invalidated component
  */
 struct Component1D
 {
diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/src/KLGaussianMixtureReduction.cxx b/Tracking/TrkFitter/TrkGaussianSumFilter/src/KLGaussianMixtureReduction.cxx
index a95dc5998ea..294223d8bf1 100644
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/src/KLGaussianMixtureReduction.cxx
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/src/KLGaussianMixtureReduction.cxx
@@ -87,7 +87,7 @@ combine(GSFUtils::Component1D& updated, GSFUtils::Component1D& removed)
   removed.mean = 1e10;
   removed.cov = 1e10;
   removed.invCov = 1e10;
-  removed.weight = 1;
+  removed.weight = -1;
 }
 
 /**
@@ -108,17 +108,32 @@ recalculateDistances(const Component1D* componentsIn,
 
   const int32_t j = mini;
   const int32_t indexConst = (j - 1) * j / 2;
-  // Rows
+  //This is the component that has been updated
+  //so we calculate distances wrt.
   const Component1D componentJ = components[j];
+  
+  // Rows
   for (int32_t i = 0; i < j; ++i) {
     const Component1D componentI = components[i];
     const int32_t index = indexConst + i;
+    //This component has been merged to/removed
+    //so keep the distance wrt to it max always
+    if (componentI.weight < 0) {
+      distances[index] = std::numeric_limits<float>::max();
+      continue;
+    }
     distances[index] = symmetricKL(componentI, componentJ);
   }
   // Columns
   for (int32_t i = j + 1; i < n; ++i) {
-    const int32_t index = (i - 1) * i / 2 + j;
     const Component1D componentI = components[i];
+    const int32_t index = (i - 1) * i / 2 + j;
+    //This component has been merged to/removed
+    //so keep the distance wrt to it max always
+    if (componentI.weight < 0) {
+      distances[index] = std::numeric_limits<float>::max();
+      continue;
+    }
     distances[index] = symmetricKL(componentI, componentJ);
   }
 }
-- 
GitLab