From 491a9842c521edadfa43734dd4d1188496913a1f Mon Sep 17 00:00:00 2001
From: christos <christos@cern.ch>
Date: Fri, 10 May 2019 16:23:34 +0100
Subject: [PATCH] ATLAS THREAD SAFETY fixes

---
 .../TrkCaloClusterROI/CaloClusterROI.h        | 23 ++---
 .../TrkCaloClusterROI/src/CaloClusterROI.cxx  |  8 +-
 .../CaloCluster_OnTrack.h                     | 16 ++--
 .../src/CaloCluster_OnTrack.cxx               |  7 +-
 .../ATLAS_CHECK_THREAD_SAFETY                 |  1 +
 .../TrkLinks/ATLAS_CHECK_THREAD_SAFETY        |  1 +
 .../ATLAS_CHECK_THREAD_SAFETY                 |  1 +
 .../PseudoMeasurementOnTrack.h                | 18 ++--
 .../src/PseudoMeasurementOnTrack.cxx          | 53 +++++++----
 .../TrkSegment/ATLAS_CHECK_THREAD_SAFETY      |  1 +
 .../TrkEvent/TrkSegment/TrkSegment/Segment.h  | 10 +-
 .../TrkSegment/TrkSegment/TrackSegment.h      | 18 ++--
 Tracking/TrkEvent/TrkSegment/src/Segment.cxx  |  2 +-
 .../TrkEvent/TrkSegment/src/TrackSegment.cxx  | 95 +++++++++++--------
 14 files changed, 146 insertions(+), 108 deletions(-)
 create mode 100644 Tracking/TrkEvent/TrkCompetingRIOsOnTrack/TrkCompetingRIOsOnTrack/ATLAS_CHECK_THREAD_SAFETY
 create mode 100644 Tracking/TrkEvent/TrkLinks/TrkLinks/ATLAS_CHECK_THREAD_SAFETY
 create mode 100644 Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/ATLAS_CHECK_THREAD_SAFETY
 create mode 100644 Tracking/TrkEvent/TrkSegment/TrkSegment/ATLAS_CHECK_THREAD_SAFETY

diff --git a/Tracking/TrkEvent/TrkCaloClusterROI/TrkCaloClusterROI/CaloClusterROI.h b/Tracking/TrkEvent/TrkCaloClusterROI/TrkCaloClusterROI/CaloClusterROI.h
index 957bbd64fa3..a271b134db8 100644
--- a/Tracking/TrkEvent/TrkCaloClusterROI/TrkCaloClusterROI/CaloClusterROI.h
+++ b/Tracking/TrkEvent/TrkCaloClusterROI/TrkCaloClusterROI/CaloClusterROI.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef TRKCALOCLUSTERROI_H
@@ -40,26 +40,25 @@ namespace Trk {
                       const double  widthEta = 0.1);
     
       /** Destructor */
-      virtual ~CaloClusterROI();
+      ~CaloClusterROI();
 
-      /** Pseudo-constructor, needed to avoid excessive RTTI*/
-      virtual CaloClusterROI* clone() const;
+      /** Pseudo-constructor*/
+      CaloClusterROI* clone() const;
             
       /** returns the LocalParameters */
-      virtual const LocalParameters& localParameters() const;
+      const LocalParameters& localParameters() const;
         
       /** returns the surface for the local to global transformation */
-      virtual const Surface& associatedSurface() const;
+      const Surface& associatedSurface() const;
      
       /** method to get the global Position */
-      virtual const Amg::Vector3D globalPosition() const;
+      const Amg::Vector3D globalPosition() const;
  
- 
-      virtual double energy() const;
+      double energy() const;
       
-      virtual double phiWidth() const;
+      double phiWidth() const;
       
-      virtual double etaWidth() const;
+      double etaWidth() const;
       
 
     private:
@@ -98,6 +97,4 @@ inline double Trk::CaloClusterROI::phiWidth() const
 inline double Trk::CaloClusterROI::etaWidth() const
 { return m_etaWidth; }
 
-
-
 #endif //TRKCaloClusterROI_H
diff --git a/Tracking/TrkEvent/TrkCaloClusterROI/src/CaloClusterROI.cxx b/Tracking/TrkEvent/TrkCaloClusterROI/src/CaloClusterROI.cxx
index 938a4ec371a..89bd1acecc0 100644
--- a/Tracking/TrkEvent/TrkCaloClusterROI/src/CaloClusterROI.cxx
+++ b/Tracking/TrkEvent/TrkCaloClusterROI/src/CaloClusterROI.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 */
 
 //CaloClusterROI.cxx
@@ -8,7 +8,6 @@
 //Trk
 #include "TrkCaloClusterROI/CaloClusterROI.h"
 #include "TrkEventPrimitives/LocalParameters.h"
-//#include "TrkEventPrimitives/GlobalPosition.h"
 #include "TrkSurfaces/Surface.h"
 
 
@@ -81,7 +80,10 @@ const Amg::Vector3D Trk::CaloClusterROI::globalPosition() const
     if(m_localParams){
       return *(m_surface->localToGlobal(*m_localParams));
     }
-    return Amg::Vector3D(m_surface->center());
+    if(m_surface){
+      return Amg::Vector3D(m_surface->center());
+    }
+    return Amg::Vector3D();
 }
 
 
diff --git a/Tracking/TrkEvent/TrkCaloCluster_OnTrack/TrkCaloCluster_OnTrack/CaloCluster_OnTrack.h b/Tracking/TrkEvent/TrkCaloCluster_OnTrack/TrkCaloCluster_OnTrack/CaloCluster_OnTrack.h
index 122a5f8d4b3..7ee4ed46a32 100644
--- a/Tracking/TrkEvent/TrkCaloCluster_OnTrack/TrkCaloCluster_OnTrack/CaloCluster_OnTrack.h
+++ b/Tracking/TrkEvent/TrkCaloCluster_OnTrack/TrkCaloCluster_OnTrack/CaloCluster_OnTrack.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef TRKCaloCluster_OnTrack_H
@@ -47,7 +47,7 @@ namespace Trk {
       virtual ~CaloCluster_OnTrack();
 
       /** Pseudo-constructor, needed to avoid excessive RTTI*/
-      virtual CaloCluster_OnTrack* clone() const override;
+      virtual CaloCluster_OnTrack* clone() const override final;
             
       /** returns the surface for the local to global transformation 
       - interface from MeasurementBase */
@@ -55,21 +55,21 @@ namespace Trk {
      
       /** Interface method to get the global Position
       - interface from MeasurementBase */
-      virtual const Amg::Vector3D& globalPosition() const override ;
+      virtual const Amg::Vector3D& globalPosition() const override final;
 
       /** Extended method to get the EnergyLoss */
       const Trk::EnergyLoss* energyLoss() const;
 
       /** Extended method checking the type*/
-       virtual bool type(MeasurementBaseType::Type type) const override {
+       virtual bool type(MeasurementBaseType::Type type) const override final{
          return (type==MeasurementBaseType::CaloCluster_OnTrack);
        }
 
-      /**returns the some information about this RIO_OnTrack. */
-      virtual MsgStream&    dump( MsgStream& out ) const override;  
+      /**returns the some information about this CaloCluster_OnTrack. */
+      virtual MsgStream&    dump( MsgStream& out ) const override final;  
 
-      /**returns the some information about this RIO_OnTrack. */
-      virtual std::ostream& dump( std::ostream& out ) const override;
+      /**returns the some information about this CaloCluster_OnTrack. */
+      virtual std::ostream& dump( std::ostream& out ) const override final;
  
 
     protected:
diff --git a/Tracking/TrkEvent/TrkCaloCluster_OnTrack/src/CaloCluster_OnTrack.cxx b/Tracking/TrkEvent/TrkCaloCluster_OnTrack/src/CaloCluster_OnTrack.cxx
index a184e15380f..e372c801fef 100644
--- a/Tracking/TrkEvent/TrkCaloCluster_OnTrack/src/CaloCluster_OnTrack.cxx
+++ b/Tracking/TrkEvent/TrkCaloCluster_OnTrack/src/CaloCluster_OnTrack.cxx
@@ -14,9 +14,10 @@
 #include <limits>
 #include <ostream>
 
-static const double NaN(std::numeric_limits<double>::quiet_NaN());
-static const Amg::Vector3D INVALID_VECTOR3D(NaN, NaN, NaN);
-
+namespace{
+const double NaN(std::numeric_limits<double>::quiet_NaN());
+alignas(16) const Amg::Vector3D INVALID_VECTOR3D(NaN, NaN, NaN);
+}
 
 Trk::CaloCluster_OnTrack::CaloCluster_OnTrack(  const Trk::LocalParameters& locpars,
                                                 const Amg::MatrixX& locerr,
diff --git a/Tracking/TrkEvent/TrkCompetingRIOsOnTrack/TrkCompetingRIOsOnTrack/ATLAS_CHECK_THREAD_SAFETY b/Tracking/TrkEvent/TrkCompetingRIOsOnTrack/TrkCompetingRIOsOnTrack/ATLAS_CHECK_THREAD_SAFETY
new file mode 100644
index 00000000000..4651cf13b4d
--- /dev/null
+++ b/Tracking/TrkEvent/TrkCompetingRIOsOnTrack/TrkCompetingRIOsOnTrack/ATLAS_CHECK_THREAD_SAFETY
@@ -0,0 +1 @@
+Tracking/TrkEvent/TrkCompetingRIOsOnTrack
diff --git a/Tracking/TrkEvent/TrkLinks/TrkLinks/ATLAS_CHECK_THREAD_SAFETY b/Tracking/TrkEvent/TrkLinks/TrkLinks/ATLAS_CHECK_THREAD_SAFETY
new file mode 100644
index 00000000000..bee06ea3426
--- /dev/null
+++ b/Tracking/TrkEvent/TrkLinks/TrkLinks/ATLAS_CHECK_THREAD_SAFETY
@@ -0,0 +1 @@
+Tracking/TrkEvent/TrkLinks
diff --git a/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/ATLAS_CHECK_THREAD_SAFETY b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/ATLAS_CHECK_THREAD_SAFETY
new file mode 100644
index 00000000000..09e4ed141db
--- /dev/null
+++ b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/ATLAS_CHECK_THREAD_SAFETY
@@ -0,0 +1 @@
+Tracking/TrkEvent/TrkPseudoMeasurementOnTrack
diff --git a/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/PseudoMeasurementOnTrack.h b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/PseudoMeasurementOnTrack.h
index d7488a0b484..d0f58bef11e 100755
--- a/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/PseudoMeasurementOnTrack.h
+++ b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/TrkPseudoMeasurementOnTrack/PseudoMeasurementOnTrack.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 */
 
 ///////////////////////////////////////////////////////////////////
@@ -62,7 +62,7 @@ namespace Trk{
       virtual ~PseudoMeasurementOnTrack() override;
 
       //! virtual constructor, not absolutely needed but given for EDM symmetry 
-      virtual PseudoMeasurementOnTrack* clone() const override;
+      virtual PseudoMeasurementOnTrack* clone() const override final;
 
       //! move constructor
       PseudoMeasurementOnTrack(PseudoMeasurementOnTrack&& pmot);
@@ -71,31 +71,31 @@ namespace Trk{
       PseudoMeasurementOnTrack& operator=(PseudoMeasurementOnTrack&& pmot);   
 
       //! returns the surface for the local to global transformation (interface from MeasurementBase)
-      virtual const Surface& associatedSurface() const override;
+      virtual const Surface& associatedSurface() const override final;
 
       //! Test to see if an associated surface exists.
       bool hasSurface() const;
 
       //! returns the global Position (interface from MeasurementBase)
-      virtual const Amg::Vector3D& globalPosition() const override;
+      virtual const Amg::Vector3D& globalPosition() const override final;
 
       /** Extended method checking the type*/
-       virtual bool type(MeasurementBaseType::Type type) const override {
+       virtual bool type(MeasurementBaseType::Type type) const override final{
          return (type==MeasurementBaseType::PseudoMeasurementOnTrack);
        }
 
       //! produces logfile output about its content in MsgStream form. 
-      virtual MsgStream&    dump( MsgStream& out ) const override;
+      virtual MsgStream&    dump( MsgStream& out ) const override final;
       //! produces logfile output about its content in stdout form. 
-      virtual std::ostream& dump( std::ostream& out ) const override;
+      virtual std::ostream& dump( std::ostream& out ) const override final;
 
     protected:
 
       //! holds the surface to which the PMoT is associated. The surface is responsible for the correct local-to-global transformation.
-      mutable const Surface* m_associatedSurface;
+      const Surface* m_associatedSurface;
 
       //! Global position of the PMoT
-      mutable const Amg::Vector3D*  m_globalPosition;
+      const Amg::Vector3D*  m_globalPosition;
   };
 
   inline PseudoMeasurementOnTrack* PseudoMeasurementOnTrack::clone() const 
diff --git a/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/src/PseudoMeasurementOnTrack.cxx b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/src/PseudoMeasurementOnTrack.cxx
index fdab96f523e..766a2e3cbfd 100755
--- a/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/src/PseudoMeasurementOnTrack.cxx
+++ b/Tracking/TrkEvent/TrkPseudoMeasurementOnTrack/src/PseudoMeasurementOnTrack.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
 */
 
 ///////////////////////////////////////////////////////////////////
@@ -23,6 +23,9 @@ namespace {
     src = nullptr;
     return tmp;
   }
+
+const double NaN(std::numeric_limits<double>::quiet_NaN());
+alignas(16) const Amg::Vector3D INVALID_VECTOR3D(NaN, NaN, NaN);
 }
 
 
@@ -30,10 +33,11 @@ Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack(const LocalParameters& l
                                                         const Amg::MatrixX& locerr,
                                                         const Surface&     assocSurf) :
   Trk::MeasurementBase(locpars,locerr),
-  m_globalPosition(0)
+  m_globalPosition(nullptr)
 {
     // Copy if belongs to DE, clone() otherwise...
     m_associatedSurface = assocSurf.isFree() ? assocSurf.clone() : &assocSurf;
+    m_globalPosition = new Amg::Vector3D(m_associatedSurface->center());
 }
 
 Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack(const LocalParameters& locpars,
@@ -41,31 +45,37 @@ Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack(const LocalParameters& l
                                                         Trk::ConstSurfaceUniquePtr  assocSurf) :
   Trk::MeasurementBase(locpars,locerr),
   m_associatedSurface (assocSurf.release()),
-  m_globalPosition(0)
+  m_globalPosition(nullptr)
 {
+  m_globalPosition = new Amg::Vector3D(m_associatedSurface->center());
 }
 
 // Destructor:
 Trk::PseudoMeasurementOnTrack::~PseudoMeasurementOnTrack()
 {
-  if (m_associatedSurface && m_associatedSurface->isFree())
+  if (m_associatedSurface && m_associatedSurface->isFree()){
     delete m_associatedSurface; // Don't delete surfaces belonging to DEs!
+  }
   delete m_globalPosition;
 }
 
 // default constructor:
 Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack() :
   Trk::MeasurementBase(),
-  m_associatedSurface(0),
-  m_globalPosition(0)
+  m_associatedSurface(nullptr),
+  m_globalPosition(nullptr)
 {}
 
 // copy constructor:
 Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack( const Trk::PseudoMeasurementOnTrack& pmot) :
   Trk::MeasurementBase(pmot),
-  m_associatedSurface( pmot.m_associatedSurface? (pmot.m_associatedSurface->isFree() ? pmot.m_associatedSurface->clone():pmot.m_associatedSurface) : 0),
-  m_globalPosition(0)
-{}
+  m_associatedSurface( pmot.m_associatedSurface? (pmot.m_associatedSurface->isFree() ? pmot.m_associatedSurface->clone():pmot.m_associatedSurface) : nullptr),
+  m_globalPosition(nullptr)
+{
+  if(pmot.m_globalPosition){
+    m_globalPosition = new Amg::Vector3D(*(pmot.m_globalPosition));
+  }
+}
 
 // move constructor:
 Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack(Trk::PseudoMeasurementOnTrack&& pmot) :
@@ -79,25 +89,33 @@ Trk::PseudoMeasurementOnTrack::PseudoMeasurementOnTrack(Trk::PseudoMeasurementOn
 Trk::PseudoMeasurementOnTrack& Trk::PseudoMeasurementOnTrack::operator=(const PseudoMeasurementOnTrack& pmot)
 {
   if ( &pmot != this) {
-    if (m_associatedSurface && m_associatedSurface->isFree())
+    if (m_associatedSurface && m_associatedSurface->isFree()){
       delete m_associatedSurface;
+    }
     delete m_globalPosition;
-    m_globalPosition = 0;
+    
     Trk::MeasurementBase::operator=(pmot);
+    
     m_associatedSurface  = 
-      ( pmot.m_associatedSurface? (pmot.m_associatedSurface->isFree() ? pmot.m_associatedSurface->clone():pmot.m_associatedSurface) : 0);
+      ( pmot.m_associatedSurface? (pmot.m_associatedSurface->isFree() ? pmot.m_associatedSurface->clone():pmot.m_associatedSurface) : nullptr);
+
+    m_globalPosition=nullptr;  
+    if(pmot.m_globalPosition){ 
+      m_globalPosition = new Amg::Vector3D(*(pmot.m_globalPosition));
+    }
+  
   }
   return *this;
 }
 
-
 // move assignment operator:
 Trk::PseudoMeasurementOnTrack& Trk::PseudoMeasurementOnTrack::operator=(PseudoMeasurementOnTrack&& pmot)
 {
   if ( &pmot != this) {
     Trk::MeasurementBase::operator=(pmot);
-    if (m_associatedSurface && m_associatedSurface->isFree())
+    if (m_associatedSurface && m_associatedSurface->isFree()){
       delete m_associatedSurface;
+    }
     m_associatedSurface = move_ptr(pmot.m_associatedSurface);
     delete m_globalPosition;
     m_globalPosition    = move_ptr(pmot.m_globalPosition);
@@ -105,11 +123,12 @@ Trk::PseudoMeasurementOnTrack& Trk::PseudoMeasurementOnTrack::operator=(PseudoMe
   return *this;
 }
 
-
 const Amg::Vector3D& Trk::PseudoMeasurementOnTrack::globalPosition() const
 {
-  if (m_globalPosition == 0) {m_globalPosition = new Amg::Vector3D(m_associatedSurface->center());}
-  return *m_globalPosition;
+  if (m_globalPosition!=nullptr) {  
+    return *m_globalPosition;
+  }
+  return INVALID_VECTOR3D;
 }
 
 MsgStream& Trk::PseudoMeasurementOnTrack::dump( MsgStream& sl ) const
diff --git a/Tracking/TrkEvent/TrkSegment/TrkSegment/ATLAS_CHECK_THREAD_SAFETY b/Tracking/TrkEvent/TrkSegment/TrkSegment/ATLAS_CHECK_THREAD_SAFETY
new file mode 100644
index 00000000000..d0cb841b83e
--- /dev/null
+++ b/Tracking/TrkEvent/TrkSegment/TrkSegment/ATLAS_CHECK_THREAD_SAFETY
@@ -0,0 +1 @@
+Tracking/TrkEvent/TrkSegment
diff --git a/Tracking/TrkEvent/TrkSegment/TrkSegment/Segment.h b/Tracking/TrkEvent/TrkSegment/TrkSegment/Segment.h
index 3547003c8cd..c6d6627f5c0 100755
--- a/Tracking/TrkEvent/TrkSegment/TrkSegment/Segment.h
+++ b/Tracking/TrkEvent/TrkSegment/TrkSegment/Segment.h
@@ -19,7 +19,7 @@
 
 #include "AthContainers/DataVector.h"
 #include "TrkMeasurementBase/MeasurementBase.h"
-
+#include <atomic>
 class MsgStream;
 class SegmentCnv_p1;
 
@@ -98,9 +98,9 @@ class FitQuality;
       virtual Segment* clone() const override = 0;
       
       /** Extended method checking the type*/
-       virtual bool type(MeasurementBaseType::Type type) const override {
-         return (type==MeasurementBaseType::Segment);
-       }
+      virtual bool type(MeasurementBaseType::Type type) const override {
+        return (type==MeasurementBaseType::Segment);
+      }
 
       /** returns the vector of Trk::MeasurementBase objects 
         - specific for this TrackSegment: Trk::MeasurementBase (generic)
@@ -139,7 +139,7 @@ class FitQuality;
       DataVector<const MeasurementBase>* m_containedMeasBases;   
   
       /** number of objects of this type in memory */
-      static unsigned int  s_numberOfInstantiations;
+      static std::atomic<unsigned int>  s_numberOfInstantiations;
 
       /** segment author */
       Author m_author;
diff --git a/Tracking/TrkEvent/TrkSegment/TrkSegment/TrackSegment.h b/Tracking/TrkEvent/TrkSegment/TrkSegment/TrackSegment.h
index b778595f719..ea3a01131d6 100755
--- a/Tracking/TrkEvent/TrkSegment/TrkSegment/TrackSegment.h
+++ b/Tracking/TrkEvent/TrkSegment/TrkSegment/TrackSegment.h
@@ -67,37 +67,37 @@ class FitQuality;
                     const Surface* sf,
                     DataVector<const MeasurementBase>* crots,
                     FitQuality* fqual,
-		    Segment::Author author = Segment::AuthorUnknown
-                    ); 
+                    Segment::Author author = Segment::AuthorUnknown
+                  ); 
   
      /** Destructor */
      virtual ~TrackSegment();
   
      /** needed to avoid excessive RTTI*/
-     TrackSegment* clone() const;
+     virtual TrackSegment* clone() const override final;
      
      /** returns the surface for the local to global transformation 
          - interface from MeasurementBase */
-     const Surface& associatedSurface() const;
+     const Surface& associatedSurface() const override final;
      
      /**Interface method to get the global Position
         - interface from MeasurementBase */
-     const Amg::Vector3D&  globalPosition() const;
+     const Amg::Vector3D&  globalPosition() const override final;
            
      /**returns some information about this MeasurementBase/TrackSegment. 
      It should be overloaded by any child classes*/
-     MsgStream&    dump( MsgStream& out ) const;  
+     MsgStream&    dump( MsgStream& out ) const override final;  
      /**returns some information about this MeasurementBase/TrackSegment.
      It should be overloaded by any child classes*/
-     std::ostream& dump( std::ostream& out ) const;
+     std::ostream& dump( std::ostream& out ) const override final;
     
   private:
     friend class ::TrackSegmentCnv_p1;
 
     /** The surface to which the segment parameters are expressed to */
-    mutable const Surface*                  m_associatedSurface;
+    const Surface*                  m_associatedSurface;
     /** The surface to which the segment parameters are expressed to */
-    mutable const Amg::Vector3D*            m_globalPosition;
+    const Amg::Vector3D*            m_globalPosition;
 
 };
 
diff --git a/Tracking/TrkEvent/TrkSegment/src/Segment.cxx b/Tracking/TrkEvent/TrkSegment/src/Segment.cxx
index 9ad904a4813..70b669af866 100755
--- a/Tracking/TrkEvent/TrkSegment/src/Segment.cxx
+++ b/Tracking/TrkEvent/TrkSegment/src/Segment.cxx
@@ -9,7 +9,7 @@
 #include "TrkSegment/Segment.h"
 #include "TrkEventPrimitives/FitQuality.h"
 
-unsigned int Trk::Segment::s_numberOfInstantiations=0;
+std::atomic<unsigned int> Trk::Segment::s_numberOfInstantiations{0};
 
 // default constructor
 Trk::Segment::Segment()
diff --git a/Tracking/TrkEvent/TrkSegment/src/TrackSegment.cxx b/Tracking/TrkEvent/TrkSegment/src/TrackSegment.cxx
index 0fc9735461d..98cec8fdb24 100755
--- a/Tracking/TrkEvent/TrkSegment/src/TrackSegment.cxx
+++ b/Tracking/TrkEvent/TrkSegment/src/TrackSegment.cxx
@@ -12,44 +12,50 @@
 #include "GaudiKernel/MsgStream.h"
 #include "AthContainers/DataVector.h"
 
+namespace {
+const double NaN(std::numeric_limits<double>::quiet_NaN());
+alignas(16) const Amg::Vector3D INVALID_VECTOR3D(NaN, NaN, NaN);
+}
+
 
 // default constructor
 Trk::TrackSegment::TrackSegment()
     :
     Trk::Segment(),
-    m_associatedSurface(0),
-    m_globalPosition(0)
+    m_associatedSurface(nullptr),
+    m_globalPosition(nullptr)
 {
 }
 
-// copy constructor
-Trk::TrackSegment::TrackSegment(const Trk::TrackSegment& tseg) 
-    :
-    Trk::Segment(tseg),
-    m_associatedSurface(tseg.m_associatedSurface ? tseg.m_associatedSurface->clone() : 0),
-    m_globalPosition(tseg.m_globalPosition ? new Amg::Vector3D(*tseg.m_globalPosition) : 0)
-{
-}
-
-
 // explicit constructor
 Trk::TrackSegment::TrackSegment(const Trk::LocalParameters& locpars,
                                 const Amg::MatrixX& locerr,
                                 const Trk::Surface*  sf,
                                 DataVector<const MeasurementBase>* crots,
                                 FitQuality* fqual,
-				Segment::Author author)
+                                Segment::Author author):
+      Trk::Segment(locpars, locerr, crots, fqual,author),
+      m_associatedSurface(sf),
+      m_globalPosition(nullptr)
+{
+  if(m_associatedSurface){
+    m_globalPosition = m_associatedSurface->localToGlobal(localParameters()); 
+  }
+}
+
+// copy constructor
+Trk::TrackSegment::TrackSegment(const Trk::TrackSegment& tseg) 
     :
-Trk::Segment(locpars, locerr, crots, fqual,author),
-    m_associatedSurface(sf),
-    m_globalPosition(0)
+    Trk::Segment(tseg),
+    m_associatedSurface(tseg.m_associatedSurface ? tseg.m_associatedSurface->clone() : nullptr),
+    m_globalPosition(tseg.m_globalPosition ? new Amg::Vector3D(*tseg.m_globalPosition) : nullptr)
 {
 }
 
 
 // move constructor
 Trk::TrackSegment::TrackSegment(Trk::TrackSegment&& tseg)
-     : Trk::Segment(tseg)
+: Trk::Segment(tseg)
 {
      m_associatedSurface = tseg.m_associatedSurface;
      tseg.m_associatedSurface = nullptr;
@@ -58,6 +64,30 @@ Trk::TrackSegment::TrackSegment(Trk::TrackSegment&& tseg)
 }
 
 
+Trk::TrackSegment& Trk::TrackSegment::operator=(const Trk::TrackSegment& tseg)
+{
+   if (this!=&tseg){
+
+     if (m_associatedSurface && !m_associatedSurface->associatedDetectorElement()) {
+       delete m_associatedSurface;
+     }
+     delete m_globalPosition;
+ 
+     // assingment operator of base class
+     Trk::Segment::operator=(tseg); 
+    
+     if (tseg.m_associatedSurface){
+       // copy only if surface is not one owned by a detector Element
+       m_associatedSurface = (!tseg.m_associatedSurface->associatedDetectorElement()) ? tseg.m_associatedSurface->clone() : tseg.m_associatedSurface;
+     } else { 
+       m_associatedSurface = nullptr; 
+     }
+     m_globalPosition = tseg.m_globalPosition ? new Amg::Vector3D(*tseg.m_globalPosition) : nullptr; 
+   }
+  return (*this);
+}
+
+
 // move assignment operator
 Trk::TrackSegment& Trk::TrackSegment::operator=(Trk::TrackSegment&& tseg) {
    if (this!=&tseg){
@@ -72,35 +102,20 @@ Trk::TrackSegment& Trk::TrackSegment::operator=(Trk::TrackSegment&& tseg) {
    return (*this);
  }
 
-
-
-Trk::TrackSegment& Trk::TrackSegment::operator=(const Trk::TrackSegment& tseg)
-{
-   if (this!=&tseg){
-    // assingment operator of base class
-    Trk::Segment::operator=(tseg); 
-    if (m_associatedSurface && !m_associatedSurface->associatedDetectorElement()) delete m_associatedSurface;
-    delete m_globalPosition;
-    
-    if (tseg.m_associatedSurface){
-       // copy only if surface is not one owned by a detector Element
-       m_associatedSurface = (!tseg.m_associatedSurface->associatedDetectorElement()) ? tseg.m_associatedSurface->clone() : tseg.m_associatedSurface;
-    } else { m_associatedSurface = 0; }
-    m_globalPosition = tseg.m_globalPosition ? new Amg::Vector3D(*tseg.m_globalPosition) : 0; 
-  }
-  return (*this);
-}
-
 Trk::TrackSegment::~TrackSegment()
 {
-  if (m_associatedSurface && !m_associatedSurface->associatedDetectorElement()) {delete m_associatedSurface;}
+  if (m_associatedSurface && !m_associatedSurface->associatedDetectorElement()) {
+    delete m_associatedSurface;
+  }
   delete m_globalPosition;
 }
 
 const Amg::Vector3D& Trk::TrackSegment::globalPosition() const
-{ if (m_globalPosition) return (*m_globalPosition);
-   m_globalPosition = m_associatedSurface->localToGlobal(localParameters()); 
-   return (*m_globalPosition);  
+{ 
+  if (m_globalPosition) {
+    return (*m_globalPosition);
+  }
+   return INVALID_VECTOR3D;  
 } 
 
 MsgStream& Trk::TrackSegment::dump( MsgStream& out ) const
-- 
GitLab