From 195e17c8ed68251407536f20bd4ccdc3bee6dd6e Mon Sep 17 00:00:00 2001
From: Anthony Morley <anthony.morley@cern.ch>
Date: Thu, 7 Dec 2017 20:25:22 +0000
Subject: [PATCH] Fix to crash in GSF MultiStateCombiner (ATLASRECTS-4241)

Former-commit-id: b3a22d9f5ec10799da263e376c4e14dc757ee084
---
 .../GsfMeasurementUpdator.h                   |   5 +
 .../src/GsfMeasurementUpdator.cxx             | 306 +++++++-----------
 .../TrkGaussianSumFilter/src/GsfSmoother.cxx  |  66 ++--
 .../src/MultiComponentStateCombiner.cxx       |  20 +-
 4 files changed, 167 insertions(+), 230 deletions(-)

diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/GsfMeasurementUpdator.h b/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/GsfMeasurementUpdator.h
index be5341413d3..6957884f715 100755
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/GsfMeasurementUpdator.h
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/TrkGaussianSumFilter/GsfMeasurementUpdator.h
@@ -73,6 +73,11 @@ class GsfMeasurementUpdator : public AthAlgTool, virtual public IMultiStateMeasu
   const MultiComponentState* calculateFilterStep( const MultiComponentState&, 
                                                   const MeasurementBase&, 
                                                   std::unique_ptr<FitQualityOnSurface>& fitQoS ) const;
+                                                  
+  bool invalidComponent(const Trk::TrackParameters* trackParameters ) const;
+  
+  Trk::MultiComponentState*  rebuildState(const Trk::MultiComponentState& stateBeforeUpdate) const;
+                                                  
 
  private:
   int                                      m_outputlevel;                      //!< to cache current output level
diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfMeasurementUpdator.cxx b/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfMeasurementUpdator.cxx
index d80b72b4120..6b58c498277 100755
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfMeasurementUpdator.cxx
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfMeasurementUpdator.cxx
@@ -47,31 +47,30 @@ StatusCode Trk::GsfMeasurementUpdator::initialize()
 
   // Request the Chrono Service
   if ( m_chronoSvc.retrieve().isFailure() ) {
-   msg(MSG::FATAL) << "Failed to retrieve service " << m_chronoSvc << endmsg;
+   ATH_MSG_FATAL("Failed to retrieve service " << m_chronoSvc);
    return StatusCode::FAILURE;
   } else 
-   msg(MSG::INFO) << "Retrieved service " << m_chronoSvc << endmsg;
+   ATH_MSG_INFO("Retrieved service " << m_chronoSvc);
 
   // Retrieve the updator tool
   if ( m_updator.retrieve().isFailure() ){
-    msg(MSG::FATAL)
-        << "Could not retrieve measurement updator AlgTool ... Exiting!" << endmsg;
+    ATH_MSG_FATAL("Could not retrieve measurement updator AlgTool ... Exiting!");
     return StatusCode::FAILURE;
   }
 
   // Retrieve the Posterior Weights Calculator
   if ( m_posteriorWeightsCalculator.retrieve().isFailure() ){
-    msg(MSG::FATAL) << "Could not find the Posterior Weights Calculator Service... Exiting!" << endmsg;
+    ATH_MSG_FATAL("Could not find the Posterior Weights Calculator Service... Exiting!");
     return StatusCode::FAILURE;
   }
 
   // Request an instance of the MultiComponentStateAssembler
   if ( m_stateAssembler.retrieve().isFailure() ){
-    msg(MSG::ERROR) << "Could not access the MultiComponentStateAssembler Service" << endmsg;
+    ATH_MSG_ERROR("Could not access the MultiComponentStateAssembler Service");
     return StatusCode::FAILURE;
   }
 
-  msg(MSG::INFO) << "Initialisation of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("Initialisation of " << name() << " was successful");
   return StatusCode::SUCCESS;
 
 }
@@ -79,7 +78,7 @@ StatusCode Trk::GsfMeasurementUpdator::initialize()
 StatusCode Trk::GsfMeasurementUpdator::finalize()
 {
 
-  msg(MSG::INFO) << "Finalisation of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("Finalisation of " << name() << " was successful");
   return StatusCode::SUCCESS;
 
 }
@@ -87,8 +86,7 @@ StatusCode Trk::GsfMeasurementUpdator::finalize()
 const Trk::MultiComponentState* Trk::GsfMeasurementUpdator::update (const Trk::MultiComponentState& stateBeforeUpdate, const Trk::MeasurementBase& measurement) const
 {
 
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Updating using GsfMeasurementUpdator" << endmsg;
+  ATH_MSG_VERBOSE( "Updating using GsfMeasurementUpdator");
 
   const Trk::MultiComponentState* updatedState = 0;
 
@@ -96,72 +94,20 @@ const Trk::MultiComponentState* Trk::GsfMeasurementUpdator::update (const Trk::M
   Updator updator = &Trk::IUpdator::addToState;
 
   // Check all components have associated error matricies
-  double weight = 0.;
-
-  const Trk::TrackParameters* trackParameters = 0;
-  const AmgSymMatrix(5)* measuredCov = 0;
   Trk::MultiComponentState::const_iterator component = stateBeforeUpdate.begin();
 
   bool rebuildStateWithErrors = false;
 
   // Perform initial check of state awaiting update. If all states have associated error matricies then no need to perform the rebuild
   for ( ; component != stateBeforeUpdate.end(); ++component ) {
-
-    trackParameters = component->first;
-
-    measuredCov = trackParameters->covariance();
-
-    if ( !measuredCov ){
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Component in the state awaiting update has no error matrix... rebuilding the entire state" << endmsg;
-      rebuildStateWithErrors = true;
-    }
-
+    rebuildStateWithErrors = rebuildStateWithErrors || invalidComponent( component->first ) ;
   }
 
   if ( rebuildStateWithErrors ){
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Rebuilding state with errors" << endmsg;
-
-    Trk::MultiComponentState* stateWithInsertedErrors = new Trk::MultiComponentState();
-    const Trk::TrackParameters* trackParametersWithError = 0;
-
-    component = stateBeforeUpdate.begin();
-
-    for ( ; component != stateBeforeUpdate.end(); ++component ){
-
-      trackParameters = component->first;
-      weight = component->second;
+    ATH_MSG_VERBOSE( "Rebuilding state with errors");
 
-      measuredCov = trackParameters->covariance();
-
-
-      if ( !measuredCov ){
-
-        if (m_outputlevel <= 0) 
-          msg(MSG::DEBUG) << "No measurement associated with track parameters, creating a big one" << endmsg;
-        AmgSymMatrix(5)* bigNewCovarianceMatrix = new AmgSymMatrix(5);
-        bigNewCovarianceMatrix->setZero();
-        double covarianceScaler = 1.;
-        (*bigNewCovarianceMatrix)(0,0) = 250. * covarianceScaler;
-        (*bigNewCovarianceMatrix)(1,1) = 250. * covarianceScaler;
-        (*bigNewCovarianceMatrix)(2,2) = 0.25;
-        (*bigNewCovarianceMatrix)(3,3) = 0.25;
-        (*bigNewCovarianceMatrix)(4,4) = 0.001 * 0.001;
-      
-        const AmgVector(5)& par = trackParameters->parameters();
-        trackParametersWithError = trackParameters->associatedSurface().createTrackParameters(par[Trk::loc1],par[Trk::loc2],par[Trk::phi],par[Trk::theta],par[Trk::qOverP], bigNewCovarianceMatrix );
-
-        Trk::ComponentParameters componentParametersWithError( trackParametersWithError, weight );
-        stateWithInsertedErrors->push_back( componentParametersWithError );
-
-      }
-
-      else
-        stateWithInsertedErrors->push_back( *component );
-
-    }
+    Trk::MultiComponentState* stateWithInsertedErrors = rebuildState( stateBeforeUpdate );
 
     // Perform the measurement update with the modified state
     updatedState = calculateFilterStep(*stateWithInsertedErrors, measurement, updator);
@@ -169,8 +115,7 @@ const Trk::MultiComponentState* Trk::GsfMeasurementUpdator::update (const Trk::M
     delete stateWithInsertedErrors;
 
     if ( !updatedState ) {
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Updated state could not be calculated... Returning 0" << endmsg;
+      ATH_MSG_DEBUG("Updated state could not be calculated... Returning 0" );
       return 0;
     }
 
@@ -182,8 +127,7 @@ const Trk::MultiComponentState* Trk::GsfMeasurementUpdator::update (const Trk::M
   updatedState = calculateFilterStep(stateBeforeUpdate, measurement, updator);
 
   if ( !updatedState ) {
-    if (m_outputlevel <= 0) 
-      msg(MSG::DEBUG) << "Updated state could not be calculated... Returning 0" << endmsg;
+    ATH_MSG_DEBUG("Updated state could not be calculated... Returning 0" );
     return 0;
   }
 
@@ -212,7 +156,7 @@ Trk::GsfMeasurementUpdator::fitQuality (const MultiComponentState& updatedState,
   // Fit quality assumes that a state that has been updated by the measurement updator has been supplied to it
 
   if ( updatedState.empty() ){
-    msg(MSG::WARNING) << "Attempting to calculate chi2 of a hit with respect to an empty multiple-component state" << endmsg;
+    ATH_MSG_WARNING( "Attempting to calculate chi2 of a hit with respect to an empty multiple-component state" );
     return 0;
   }
 
@@ -255,8 +199,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
              const Updator updator) const
 {
 
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Calculate Filter Step" << endmsg;
+  ATH_MSG_VERBOSE( "Calculate Filter Step");
 
   // Start the timer
   //Chrono chrono( &(*m_chronoSvc), "GsfMeasurementUpdate" );
@@ -265,13 +208,12 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   bool isAssemblerReset = m_stateAssembler->reset();
 
   if ( !isAssemblerReset ){
-    if (m_outputlevel <= 0) 
-      msg(MSG::ERROR) << "Could not reset the state assembler... returning 0" << endmsg;
+    ATH_MSG_DEBUG("Could not reset the state assembler... returning 0");
     return 0;
   }
 
   if ( stateBeforeUpdate.empty() ){
-    msg(MSG::WARNING) << "Cannot update multi-state with no components!" << endmsg;
+    ATH_MSG_WARNING("Cannot update multi-state with no components!");
     return 0;
   }
 
@@ -281,13 +223,11 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   stateWithNewWeights = m_posteriorWeightsCalculator->weights(stateBeforeUpdate, measurement);
 
   if ( !stateWithNewWeights ) {
-    if (m_outputlevel <= 0) 
-      msg(MSG::DEBUG) << "Cacluation of state posterior weights failed... Exiting!" << endmsg;
+    ATH_MSG_DEBUG( "Cacluation of state posterior weights failed... Exiting!");
     return 0;
   }
   else
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Calculation of state posterior weights successful" << endmsg;
+    ATH_MSG_VERBOSE( "Calculation of state posterior weights successful");
 
   // Update each component using the specified updator
   Trk::MultiComponentState::const_iterator component = stateWithNewWeights->begin();
@@ -296,8 +236,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
 
     const Trk::TrackParameters* updatedTrackParameters = 0;
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Performing update of predicted component state with measurement..." << endmsg;
+    ATH_MSG_VERBOSE( "Performing update of predicted component state with measurement...");
 
     Trk::FitQualityOnSurface* fitQuality = 0;
 
@@ -308,21 +247,13 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     //updatedTrackParameters = m_updator->addToState( *(*component).first, measurement.localParameters(), measurement.localCovariance(), fitQuality );
 
     if ( !updatedTrackParameters ) {
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Update of state with Measurement has failed 1... Exiting!" << endmsg;
+      ATH_MSG_DEBUG( "Update of state with Measurement has failed 1... Exiting!");
       if ( fitQuality )  delete fitQuality;
       continue;
     }
-
-    //std::cout << "  A  \n " << *updatedTrackParameters <<std::endl;
-    //std::cout << "  B   \n" << *updatedTrackParameters2 <<std::endl;
-    //delete updatedTrackParameters2;
-    //delete fitQuality2;
-    
     
     if ( fitQuality && fitQuality->chiSquared() <= 0. ){
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Fit quality of update failed... Exiting!" << endmsg;
+      ATH_MSG_DEBUG( "Fit quality of update failed... Exiting!");
       delete updatedTrackParameters;      
       delete fitQuality;
       continue;
@@ -331,8 +262,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     // Clean up memory
     delete fitQuality;
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Successful measurement update with Measurement" << endmsg;
+    ATH_MSG_VERBOSE( "Successful measurement update with Measurement");
 
     // Updator does not change the weighting
     Trk::ComponentParameters updatedComponentParameters(updatedTrackParameters, component->second);
@@ -341,8 +271,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     bool componentAdded = m_stateAssembler->addComponent(updatedComponentParameters);
 
     if ( !componentAdded )
-      if (m_outputlevel <= 0) 
-        msg(MSG::WARNING) << "Component could not be added to the state in the assembler" << endmsg;
+      ATH_MSG_DEBUG( "Component could not be added to the state in the assembler");
 
     delete updatedTrackParameters;
   
@@ -364,8 +293,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   delete assembledUpdatedState;
 
   
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Successful calculation of filter step" << endmsg; 
+  ATH_MSG_VERBOSE( "Successful calculation of filter step"); 
 
   return renormalisedUpdatedState;
 
@@ -377,85 +305,33 @@ Trk::GsfMeasurementUpdator::update (const Trk::MultiComponentState& stateBeforeU
                                     std::unique_ptr<FitQualityOnSurface>&   fitQoS ) const
 {
 
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Updating using GsfMeasurementUpdator" << endmsg;
+  ATH_MSG_VERBOSE( "Updating using GsfMeasurementUpdator");
 
   const Trk::MultiComponentState* updatedState = 0;
 
   // Check all components have associated error matricies
-  double weight = 0.;
-
-  const Trk::TrackParameters* trackParameters = 0;
-  const AmgSymMatrix(5)* measuredCov = 0;
   Trk::MultiComponentState::const_iterator component = stateBeforeUpdate.begin();
 
   bool rebuildStateWithErrors = false;
 
   // Perform initial check of state awaiting update. If all states have associated error matricies then no need to perform the rebuild
   for ( ; component != stateBeforeUpdate.end(); ++component ) {
-
-    trackParameters = component->first;
-
-    measuredCov  = trackParameters->covariance();
-
-    if ( !measuredCov ){
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Component in the state awaiting update has no error matrix... rebuilding the entire state" << endmsg;
-      rebuildStateWithErrors = true;
-    }
+    rebuildStateWithErrors = rebuildStateWithErrors || invalidComponent( component->first ) ;
   }
 
   if ( rebuildStateWithErrors ){
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Rebuilding state with errors" << endmsg;
-
-    Trk::MultiComponentState* stateWithInsertedErrors = new Trk::MultiComponentState();
-    const Trk::TrackParameters* trackParametersWithError = 0;
-
-    component = stateBeforeUpdate.begin();
-
-    for ( ; component != stateBeforeUpdate.end(); ++component ){
-
-      trackParameters = component->first;
-      weight = component->second;
-
-      measuredCov = trackParameters->covariance();
-
-
-      if ( !measuredCov ){
-
-        if (m_outputlevel <= 0) 
-          msg(MSG::DEBUG) << "No measurement associated with track parameters, creating a big one" << endmsg;
-        AmgSymMatrix(5)* bigNewCovarianceMatrix = new AmgSymMatrix(5);
-        bigNewCovarianceMatrix->setZero();
-        double covarianceScaler = 1.;
-        (*bigNewCovarianceMatrix)(0,0) = 250. * covarianceScaler;
-        (*bigNewCovarianceMatrix)(1,1) = 250. * covarianceScaler;
-        (*bigNewCovarianceMatrix)(2,2) = 0.25;
-        (*bigNewCovarianceMatrix)(3,3) = 0.25;
-        (*bigNewCovarianceMatrix)(4,4) = 0.001 * 0.001;
-      
-        AmgVector(5) par = trackParameters->parameters();
-        trackParametersWithError = trackParameters->associatedSurface().createTrackParameters(par[Trk::loc1],par[Trk::loc2],par[Trk::phi],par[Trk::theta],par[Trk::qOverP], bigNewCovarianceMatrix );
-        Trk::ComponentParameters componentParametersWithError( trackParametersWithError, weight );
-        stateWithInsertedErrors->push_back( componentParametersWithError );
-
-      }
-
-      else
-        stateWithInsertedErrors->push_back( *component );
-
-    }
+    ATH_MSG_VERBOSE( "Rebuilding state with errors");
 
+    Trk::MultiComponentState* stateWithInsertedErrors = rebuildState( stateBeforeUpdate );
+ 
     // Perform the measurement update with the modified state
     updatedState = calculateFilterStep(*stateWithInsertedErrors, measurement, fitQoS);
     
     delete stateWithInsertedErrors;
 
     if ( !updatedState ) {
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Updated state could not be calculated... Returning 0" << endmsg;
+      ATH_MSG_DEBUG( "Updated state could not be calculated... Returning 0");
       fitQoS.reset();
       return 0;
     }
@@ -468,8 +344,7 @@ Trk::GsfMeasurementUpdator::update (const Trk::MultiComponentState& stateBeforeU
   updatedState = calculateFilterStep(stateBeforeUpdate, measurement, fitQoS);
 
   if ( !updatedState ) {
-    if (m_outputlevel <= 0) 
-      msg(MSG::DEBUG) << "Updated state could not be calculated... Returning 0" << endmsg;
+    ATH_MSG_DEBUG( "Updated state could not be calculated... Returning 0");
     fitQoS.reset();
     return 0;
   }
@@ -485,8 +360,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
              std::unique_ptr<FitQualityOnSurface>& fitQoS) const
 {
 
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Calculate Filter Step" << endmsg;
+  ATH_MSG_VERBOSE( "Calculate Filter Step");
 
   // Start the timer
   //Chrono chrono( &(*m_chronoSvc), "GsfMeasurementUpdate" );
@@ -495,13 +369,12 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   bool isAssemblerReset = m_stateAssembler->reset();
 
   if ( !isAssemblerReset ){
-    if (m_outputlevel <= 0) 
-      msg(MSG::ERROR) << "Could not reset the state assembler... returning 0" << endmsg;
+    ATH_MSG_ERROR("Could not reset the state assembler... returning 0");
     return 0;
   }
 
   if ( stateBeforeUpdate.empty() ){
-    msg(MSG::WARNING) << "Cannot update multi-state with no components!" << endmsg;
+    ATH_MSG_WARNING( "Cannot update multi-state with no components!");
     return 0;
   }
 
@@ -511,13 +384,11 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   stateWithNewWeights = m_posteriorWeightsCalculator->weights(stateBeforeUpdate, measurement);
 
   if ( !stateWithNewWeights ) {
-    if (m_outputlevel <= 0) 
-      msg(MSG::DEBUG) << "Cacluation of state posterior weights failed... Exiting!" << endmsg;
+    ATH_MSG_DEBUG( "Cacluation of state posterior weights failed... Exiting!");
     return 0;
   }
   else
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Calculation of state posterior weights successful" << endmsg;
+    ATH_MSG_VERBOSE( "Calculation of state posterior weights successful");
 
   // Update each component using the specified updator
   Trk::MultiComponentState::const_iterator component = stateWithNewWeights->begin();
@@ -530,12 +401,11 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
 
     const Trk::TrackParameters* updatedTrackParameters = 0;
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Performing update of predicted component state with measurement..." << endmsg;
+    ATH_MSG_VERBOSE( "Performing update of predicted component state with measurement...");
 
     if (fabs((*component).first->parameters()[Trk::qOverP])>0.033333) { //GC: protection against low momentum tracks getting lost
-      if (m_outputlevel<=0)                                                            // cutoff is 30MeV
-        msg(MSG::DEBUG) << "About to update component with p<50MeV...skipping component! (2)"<<endmsg;
+                                                                       // cutoff is 30MeV
+      ATH_MSG_DEBUG( "About to update component with p<30MeV...skipping component! (2)");
       continue;
     }
 
@@ -545,21 +415,28 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     updatedTrackParameters = m_updator->addToState( *(*component).first, measurement.localParameters(), measurement.localCovariance(), componentFitQuality );
 
     if ( !updatedTrackParameters ) {
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Update of state with Measurement has failed 2... Exiting!" << endmsg;
+      ATH_MSG_DEBUG( "Update of state with Measurement has failed 2... Exiting!");
       if ( componentFitQuality ) delete componentFitQuality;
       continue;
     }
-
-    //std::cout << "  A  \n " << *updatedTrackParameters <<std::endl;
-    //std::cout << "  B   \n" << *updatedTrackParameters2 <<std::endl;
-    //delete updatedTrackParameters2;
-    //delete fitQuality2;
-
+    
+    if( invalidComponent(updatedTrackParameters)  ){
+      ATH_MSG_DEBUG( "Invalid cov matrix after update... Exiting!");
+      ATH_MSG_VERBOSE("Original TP \n" <<  *(*component).first ); 
+      if((*component).first->covariance())
+        ATH_MSG_VERBOSE("Original has a COV\n " << *(*component).first->covariance() );
+      ATH_MSG_VERBOSE("Measurement  \n" <<  measurement ); 
+      ATH_MSG_VERBOSE("Result  \n" <<  *updatedTrackParameters ); 
+      if(updatedTrackParameters->covariance())
+        ATH_MSG_VERBOSE("Result has a COV\n" << *updatedTrackParameters->covariance() );
+      
+      delete updatedTrackParameters;      
+      delete componentFitQuality;
+      continue;    
+    }
 
     if ( !componentFitQuality || componentFitQuality->chiSquared() <= 0. ){
-      if (m_outputlevel <= 0) 
-        msg(MSG::DEBUG) << "Fit quality of update failed... Exiting!" << endmsg;
+      ATH_MSG_DEBUG( "Fit quality of update failed... Exiting!");
       delete updatedTrackParameters;      
       delete componentFitQuality;
       continue;
@@ -576,8 +453,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     // Clean up memory
     delete componentFitQuality;
 
-    if (m_outputlevel < 0) 
-      msg(MSG::VERBOSE) << "Successful measurement update with Measurement" << endmsg;
+    ATH_MSG_VERBOSE( "Successful measurement update with Measurement");
 
     // Updator does not change the weighting
     Trk::ComponentParameters updatedComponentParameters(updatedTrackParameters, component->second);
@@ -586,8 +462,7 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
     bool componentAdded = m_stateAssembler->addComponent(updatedComponentParameters);
 
     if ( !componentAdded )
-      if (m_outputlevel <= 0) 
-        msg(MSG::WARNING) << "Component could not be added to the state in the assembler" << endmsg;
+      ATH_MSG_DEBUG( "Component could not be added to the state in the assembler");
 
     delete updatedTrackParameters;
 
@@ -613,10 +488,69 @@ Trk::GsfMeasurementUpdator::calculateFilterStep( const Trk::MultiComponentState&
   delete assembledUpdatedState;
 
   
-  if (m_outputlevel < 0) 
-    msg(MSG::VERBOSE) << "Successful calculation of filter step: " << renormalisedUpdatedState->size() << endmsg; 
+  ATH_MSG_VERBOSE( "Successful calculation of filter step: " << renormalisedUpdatedState->size()); 
 
   return renormalisedUpdatedState;
 
 }
 
+bool Trk::GsfMeasurementUpdator::invalidComponent(const Trk::TrackParameters* trackParameters ) const
+{
+  auto measuredCov = trackParameters->covariance();
+  bool rebuildCov = false; 
+  if (!measuredCov){
+    rebuildCov = true;
+  } else {
+    for (int i(0); i<5; ++i){
+      if( (*measuredCov)(i,i)  <= 0.)
+        rebuildCov = true;
+    }
+  }
+  
+  return rebuildCov;
+}
+
+Trk::MultiComponentState*  Trk::GsfMeasurementUpdator::rebuildState(const Trk::MultiComponentState& stateBeforeUpdate) const
+{
+  Trk::MultiComponentState*   stateWithInsertedErrors = new Trk::MultiComponentState();
+  const Trk::TrackParameters* trackParametersWithError = 0;
+
+  auto component = stateBeforeUpdate.begin();
+
+  for ( ; component != stateBeforeUpdate.end(); ++component ){
+
+    auto trackParameters = component->first;
+    auto weight = component->second;
+
+    bool rebuildCov = invalidComponent(trackParameters);
+
+    if ( rebuildCov ){
+
+      if (m_outputlevel <= 0) 
+        ATH_MSG_DEBUG( "No measurement associated with track parameters, creating a big one");
+      AmgSymMatrix(5)* bigNewCovarianceMatrix = new AmgSymMatrix(5);
+      bigNewCovarianceMatrix->setZero();
+      double covarianceScaler = 1.;
+      (*bigNewCovarianceMatrix)(0,0) = 250. * covarianceScaler;
+      (*bigNewCovarianceMatrix)(1,1) = 250. * covarianceScaler;
+      (*bigNewCovarianceMatrix)(2,2) = 0.25;
+      (*bigNewCovarianceMatrix)(3,3) = 0.25;
+      (*bigNewCovarianceMatrix)(4,4) = 0.001 * 0.001;
+  
+      AmgVector(5) par = trackParameters->parameters();
+      trackParametersWithError = trackParameters->associatedSurface().createTrackParameters(par[Trk::loc1],par[Trk::loc2],par[Trk::phi],par[Trk::theta],par[Trk::qOverP], bigNewCovarianceMatrix );
+      Trk::ComponentParameters componentParametersWithError( trackParametersWithError, weight );
+      stateWithInsertedErrors->push_back( componentParametersWithError );
+
+    }
+
+    else
+      stateWithInsertedErrors->push_back( *component );
+
+  }
+
+  return stateWithInsertedErrors;
+}
+
+
+
diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfSmoother.cxx b/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfSmoother.cxx
index 2170bdc36bd..e0985d5ecc6 100755
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfSmoother.cxx
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/src/GsfSmoother.cxx
@@ -49,17 +49,17 @@ StatusCode Trk::GsfSmoother::initialize()
   
   // Retrieve an instance of the component merger
   if ( m_merger.retrieve().isFailure() ){
-    msg(MSG::FATAL) << "Could not retrieve the component merger tool... Exiting!" << endmsg;
+    ATH_MSG_FATAL("Could not retrieve the component merger tool... Exiting!");
     return StatusCode::FAILURE;
   }
 
   // Request an instance of the state combiner
   if ( m_combiner.retrieve().isFailure() ){
-    msg(MSG::FATAL) << "Could not retrieve an instance of the multi component state combiner... Exiting!" << endmsg;
+    ATH_MSG_FATAL("Could not retrieve an instance of the multi component state combiner... Exiting!");
     return StatusCode::FAILURE;
   }
   
-  msg(MSG::INFO) << "Initialisation of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("Initialisation of " << name() << " was successful");
 
   return StatusCode::SUCCESS;
 
@@ -68,7 +68,7 @@ StatusCode Trk::GsfSmoother::initialize()
 StatusCode Trk::GsfSmoother::finalize()
 {
 
-  msg(MSG::INFO) << "Finalisation of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("Finalisation of " << name() << " was successful");
 
   return StatusCode::SUCCESS;
 
@@ -80,7 +80,7 @@ StatusCode Trk::GsfSmoother::configureTools(const ToolHandle<IMultiStateExtrapol
   m_extrapolator = extrapolator;
   m_updator      = measurementUpdator;
 
-  msg(MSG::INFO) << "Configuration of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("Configuration of " << name() << " was successful");
 
   return StatusCode::SUCCESS;
 
@@ -91,33 +91,32 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
                                                 const Trk::CaloCluster_OnTrack * ccot ) const
 {
 
-  if (m_outputlevel<0)
-    msg(MSG::VERBOSE) << "This is the GSF Smoother!" << endmsg;
+  ATH_MSG_VERBOSE("This is the GSF Smoother!");
 
   // Check that extrapolator and updator are instansiated
   if (!m_updator) {
-    msg(MSG::ERROR) << "The measurement updator is not configured... Exiting!" << endmsg;
+    ATH_MSG_ERROR("The measurement updator is not configured... Exiting!");
     return 0;
   }
 
   if (!m_extrapolator) {
-    msg(MSG::ERROR) << "The extrapolator is not configured... Exiting!" << endmsg;
+    ATH_MSG_ERROR("The extrapolator is not configured... Exiting!");
     return 0;
   }
 
   // Check that the forward trajectory is filled
   if ( forwardTrajectory.empty() ){
-    msg(MSG::ERROR) << "Attempting to smooth an empty forward trajectory... Exiting!" << endmsg;
+    ATH_MSG_ERROR("Attempting to smooth an empty forward trajectory... Exiting!");
     return 0;
   }
   
   if (m_outputlevel<0){
   
     if ( particleHypothesis == Trk::nonInteracting )
-      msg(MSG::VERBOSE) << "Material effects are switched off in the Gsf Smoother" << endmsg;
+      ATH_MSG_VERBOSE("Material effects are switched off in the Gsf Smoother");
   
     else
-      msg(MSG::VERBOSE) << "Material effects are switched on in the Gsf Smoother (type): " << particleHypothesis << endmsg;
+      ATH_MSG_VERBOSE("Material effects are switched on in the Gsf Smoother (type): " << particleHypothesis);
 
   }
     
@@ -140,7 +139,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
   const Trk::MultiComponentStateOnSurface* smootherPredictionMultiStateOnSurface = dynamic_cast<const Trk::MultiComponentStateOnSurface*>(smootherPredictionStateOnSurface);
 
   if (!smootherPredictionMultiStateOnSurface) {
-    msg(MSG::DEBUG) << "GSF smoother has a single component state as starting point" << endmsg;
+    ATH_MSG_DEBUG("GSF smoother has a single component state as starting point");
 
     // Build new multi-component state
     Trk::ComponentParameters smootherPredictionComponent(smootherPredictionStateOnSurface->trackParameters(), 1.);
@@ -160,7 +159,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
   const Trk::MeasurementBase* firstSmootherMeasurementOnTrack = smootherPredictionStateOnSurface->measurementOnTrack()->clone();
 
   if ( !firstSmootherMeasurementOnTrack ){
-    msg(MSG::WARNING) << "Initial state on surface in smoother does not have an associated MeasurementBase object... returning 0" << endmsg;
+    ATH_MSG_WARNING("Initial state on surface in smoother does not have an associated MeasurementBase object... returning 0");
     return 0;
   }
 
@@ -173,8 +172,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
     if (!smootherPredictionMultiStateOnSurface)
       delete smootherPredictionMultiState;
 
-    if (m_outputlevel<=0)
-      msg(MSG::DEBUG) << "First GSF smoothing update failed... Exiting!" << endmsg;
+    ATH_MSG_DEBUG("First GSF smoothing update failed... Exiting!");
     return 0;
   }
 
@@ -209,7 +207,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
   // =============================================================================================================================
 
   if ( !firstSmoothedState->isMeasured() ){
-    msg(MSG::WARNING) << "Updated state is not measured. Rejecting smoothed state... returning 0" << endmsg;
+    ATH_MSG_WARNING("Updated state is not measured. Rejecting smoothed state... returning 0");
     return 0;
   }
 
@@ -220,7 +218,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
     std::unique_ptr<const Trk::MultiComponentState> (firstSmoothedState->cloneWithScaledError( 15., 5., 15., 5., 15. ));
 
   if ( !smoothedStateWithScaledError ){
-    msg(MSG::WARNING) << "Covariance scaling could not be performed... returning 0" << endmsg;
+    ATH_MSG_WARNING("Covariance scaling could not be performed... returning 0");
     return 0;
   }
 
@@ -229,7 +227,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
     std::unique_ptr<const Trk::MultiComponentState> (m_updator->update(*smoothedStateWithScaledError, *firstSmootherMeasurementOnTrack));
 
   if ( !updatedState ){
-    msg(MSG::WARNING) << "Smoother prediction could not be determined... returning 0" << endmsg;
+    ATH_MSG_WARNING("Smoother prediction could not be determined... returning 0");
     return 0;
   }
 
@@ -253,7 +251,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
     const Trk::MeasurementBase* measurement_in = (*trackStateOnSurface)->measurementOnTrack();
 
     if ( !measurement_in ){
-      msg(MSG::WARNING) << "MeasurementBase object could not be extracted from a measurement... continuing" << endmsg;
+      ATH_MSG_WARNING("MeasurementBase object could not be extracted from a measurement... continuing");
       continue;
     }
 
@@ -310,11 +308,11 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
         varQoverP = (*measuredCov)(Trk::qOverP,Trk::qOverP);
       }
 
-      msg(MSG::DEBUG) << "Finishing extrapolation parameters:\t" 
+      ATH_MSG_DEBUG("Finishing extrapolation parameters:\t" 
             << combinedState->parameters()[Trk::phi] << "\t"
             << combinedState->parameters()[Trk::theta] << "\t" 
             << combinedState->parameters()[Trk::qOverP] << "\t"
-            << varQoverP << endmsg;
+            << varQoverP);
     }
     
       // Original measurement was flagged as  an outlier
@@ -336,7 +334,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
     updatedState = std::unique_ptr<const Trk::MultiComponentState> (m_updator->update( *extrapolatedState, *measurement, fitQuality ) );
 
     if (!updatedState) {
-      msg(MSG::WARNING) << "Could not update the multi-component state... rejecting track!" << endmsg;
+      ATH_MSG_WARNING("Could not update the multi-component state... rejecting track!");
       return 0;
     }
 
@@ -353,12 +351,12 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
         varQoverP = (*measuredCov)(Trk::qOverP,Trk::qOverP);
       }
 
-      msg(MSG::DEBUG) << "Update finished parameters:\t\t" 
+      ATH_MSG_DEBUG("Update finished parameters:\t\t" 
             << combinedState->parameters()[Trk::phi] << "\t"
             << combinedState->parameters()[Trk::theta] << "\t" 
             << combinedState->parameters()[Trk::qOverP] << "\t"
-            << varQoverP << endmsg;
-      msg(MSG::DEBUG) << "-----------------------------------------------------------------------------" << endmsg;
+            << varQoverP);
+      ATH_MSG_DEBUG("-----------------------------------------------------------------------------");
     }
     
     /* =============================================================
@@ -390,7 +388,7 @@ Trk::SmoothedTrajectory* Trk::GsfSmoother::fit (const ForwardTrajectory& forward
       //   delete forwardsMultiState;
       
       if (!combinedState2) {
-        msg(MSG::WARNING) << "Could not combine state from forward fit with smoother state... rejecting track!" << endmsg;
+        ATH_MSG_WARNING("Could not combine state from forward fit with smoother state... rejecting track!");
         // delete updatedState;
         // delete measurement;
         // delete smoothedTrajectory;
@@ -467,7 +465,7 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
     const AmgSymMatrix(5)* forwardMeasuredCov = forwardsComponent->first->covariance();
 
     if ( !forwardMeasuredCov )
-      msg(MSG::DEBUG) << "No measurement associated with forwards component... continuing for now" << endmsg;
+      ATH_MSG_DEBUG("No measurement associated with forwards component... continuing for now");
 
     /* ====================================================
        Loop over all components in the smoother multi-state
@@ -481,13 +479,13 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
       const AmgSymMatrix(5)* smootherMeasuredCov = smootherComponent->first->covariance();
 
       if ( !smootherMeasuredCov && !forwardMeasuredCov ){
-        msg(MSG::WARNING) << "Cannot combine two components both without associated errors... returning 0" << endmsg;
+        ATH_MSG_WARNING("Cannot combine two components both without associated errors... returning 0");
         return 0;
       }
 
       if ( !forwardMeasuredCov ){
         if (m_outputlevel<=0) 
-          msg(MSG::DEBUG) << "Forwards state without error matrix... using smoother state only" << endmsg;
+          ATH_MSG_DEBUG("Forwards state without error matrix... using smoother state only");
         Trk::ComponentParameters smootherComponentOnly( smootherComponent->first->clone(), smootherComponent->second );
         combinedMultiState->push_back( smootherComponentOnly );
         continue;
@@ -495,7 +493,7 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
 
       if ( !smootherMeasuredCov ){
         if (m_outputlevel<=0) 
-          msg(MSG::DEBUG) << "Smoother state withour error matrix... using forwards state only" << endmsg;
+          ATH_MSG_DEBUG("Smoother state withour error matrix... using forwards state only");
         Trk::ComponentParameters forwardComponentOnly( forwardsComponent->first->clone(), forwardsComponent->second );
         combinedMultiState->push_back( forwardComponentOnly );
         continue;
@@ -506,7 +504,7 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
       const AmgSymMatrix(5) K = *forwardMeasuredCov * summedCovariance.inverse();
 
       //if (matrixInversionError) {
-      //  msg(MSG::WARNING) << "Matrix inversion failed... Exiting!" << endmsg;
+      //  ATH_MSG_WARNING("Matrix inversion failed... Exiting!");
       //  return 0;
       //}
 
@@ -527,7 +525,7 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
       const AmgSymMatrix(5) invertedSummedCovariance = summedCovariance.inverse();
 
       //if ( matrixInversionError ){
-      //  msg(MSG::WARNING) << "Matrix inversion failed... exiting" << endmsg;
+      //  ATH_MSG_WARNING("Matrix inversion failed... exiting");
       //  return 0;
       //}
 
@@ -559,7 +557,7 @@ const Trk::MultiComponentState* Trk::GsfSmoother::combine (const Trk::MultiCompo
     delete mergedState;
 
   if (m_outputlevel<0) 
-    msg(MSG::VERBOSE) << "Size of combined state from smoother: " << renormalisedMergedState->size() << endmsg;
+    ATH_MSG_VERBOSE("Size of combined state from smoother: " << renormalisedMergedState->size());
 
   return renormalisedMergedState;
 
diff --git a/Tracking/TrkFitter/TrkGaussianSumFilter/src/MultiComponentStateCombiner.cxx b/Tracking/TrkFitter/TrkGaussianSumFilter/src/MultiComponentStateCombiner.cxx
index b705ca5dd83..8687b37ef7c 100755
--- a/Tracking/TrkFitter/TrkGaussianSumFilter/src/MultiComponentStateCombiner.cxx
+++ b/Tracking/TrkFitter/TrkGaussianSumFilter/src/MultiComponentStateCombiner.cxx
@@ -46,7 +46,7 @@ StatusCode Trk::MultiComponentStateCombiner::initialize()
 
    // Request the mode calculator
   if ( m_modeCalculator.retrieve().isFailure() ){
-    msg(MSG::FATAL) << "Unable to retrieve the mode calculator... Exiting!" << endmsg;
+    ATH_MSG_FATAL( "Unable to retrieve the mode calculator... Exiting!" );
     return StatusCode::FAILURE;
   }
   
@@ -62,7 +62,7 @@ StatusCode Trk::MultiComponentStateCombiner::initialize()
     m_fractionPDFused = 1;   
   }
   
-  if (msgLvl(MSG::VERBOSE)) msg() << "Initialisation of " << name() << " was successful" << endmsg;
+  if (msgLvl(MSG::VERBOSE)) ATH_MSG_VERBOSE( "Initialisation of " << name() << " was successful" );
 
   return StatusCode::SUCCESS;
 
@@ -71,11 +71,11 @@ StatusCode Trk::MultiComponentStateCombiner::initialize()
 StatusCode Trk::MultiComponentStateCombiner::finalize()
 {
 
-  msg(MSG::INFO) << "-----------------------------------------------"<< endmsg;
-  msg(MSG::INFO) << "         GSF MCS Combiner  Statistics          "<< endmsg;
-  msg(MSG::INFO) << "-----------------------------------------------"<< endmsg;
-  msg(MSG::INFO) << "Number of Calls    " << m_NumberOfCalls          << endmsg;
-  msg(MSG::INFO) << "Finalisation of " << name() << " was successful" << endmsg;
+  ATH_MSG_INFO("-----------------------------------------------");
+  ATH_MSG_INFO("         GSF MCS Combiner  Statistics          ");
+  ATH_MSG_INFO("-----------------------------------------------");
+  ATH_MSG_INFO("Number of Calls    " << m_NumberOfCalls          );
+  ATH_MSG_INFO("Finalisation of " << name() << " was successful" );
   
   return StatusCode::SUCCESS;
 
@@ -102,7 +102,7 @@ const Trk::ComponentParameters* Trk::MultiComponentStateCombiner::compute( const
 {
   ++m_NumberOfCalls;
   if ( uncombinedState->empty() ){
-    msg(MSG::WARNING) << "Trying to collapse state with zero components" << endmsg;
+    ATH_MSG_WARNING( "Trying to collapse state with zero components" );
     return 0;
   }
 
@@ -226,7 +226,7 @@ const Trk::ComponentParameters* Trk::MultiComponentStateCombiner::compute( const
       modes = m_modeCalculator->calculateMode( *uncombinedState );
 
       if (  msgLvl(MSG::VERBOSE) && modes[4] )
-        msg(MSG::VERBOSE) << "Calculated mode q/p is: " << modes[4] << endmsg;
+        ATH_MSG_VERBOSE( "Calculated mode q/p is: " << modes[4] );
   
       //  Replace mean with mode if qOverP mode is not 0
       if (modes[4] != 0){
@@ -293,7 +293,7 @@ const Trk::ComponentParameters* Trk::MultiComponentStateCombiner::compute( const
       }
     } else {
 
-      if (msgLvl(MSG::DEBUG)) msg() << " Dimension != 5 not updating q/p to mode q/p"<< endmsg;
+      if (msgLvl(MSG::DEBUG)) ATH_MSG_DEBUG( " Dimension != 5 not updating q/p to mode q/p");
 
     }
 
-- 
GitLab