From eb65694ec8cd2003e2bdf140ad98ee77a1e39e5a Mon Sep 17 00:00:00 2001
From: christos <christos@cern.ch>
Date: Sat, 28 Nov 2020 19:02:01 +0100
Subject: [PATCH] Make PixelToTPIDTool aware of the EventContext. Allow to pass
 it as argument to avoid lookups

---
 .../PixelToTPIDTool/PixelToTPIDTool.h         |  19 ++-
 .../PixelToTPIDTool/src/PixelToTPIDTool.cxx   | 134 +++++++++++-------
 .../TrkToolInterfaces/IPixelToTPIDTool.h      |  28 +++-
 .../TrkToolInterfaces/IPixelToTPIDTool.icc    |  28 ++++
 .../src/TrackSummaryTool.cxx                  |   2 +-
 5 files changed, 153 insertions(+), 58 deletions(-)
 create mode 100644 Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.icc

diff --git a/InnerDetector/InDetRecTools/PixelToTPIDTool/PixelToTPIDTool/PixelToTPIDTool.h b/InnerDetector/InDetRecTools/PixelToTPIDTool/PixelToTPIDTool/PixelToTPIDTool.h
index 66a8b8da39b..bd5dae5a11b 100644
--- a/InnerDetector/InDetRecTools/PixelToTPIDTool/PixelToTPIDTool/PixelToTPIDTool.h
+++ b/InnerDetector/InDetRecTools/PixelToTPIDTool/PixelToTPIDTool/PixelToTPIDTool.h
@@ -37,12 +37,23 @@ namespace InDet {
 
       virtual ~PixelToTPIDTool ();
       virtual StatusCode initialize() override;
-      virtual StatusCode finalize  () override; 
+      virtual StatusCode finalize  () override;
 
-      virtual float dEdx(const Trk::Track& track, int& nUsedHits, int& nUsedIBLOverflowHits) const override;
+      virtual float dEdx(const EventContext& ctx,
+                         const Trk::Track& track,
+                         int& nUsedHits,
+                         int& nUsedIBLOverflowHits) const override final;
 
-      virtual std::vector<float> getLikelihoods(double dedx, double p, int nGoodPixels) const override;
-      virtual float getMass(double dedx, double p, int nGoodPixels) const override;
+      virtual std::vector<float> getLikelihoods(
+        const EventContext& ctx,
+        double dedx,
+        double p,
+        int nGoodPixels) const override final;
+
+      virtual float getMass(const EventContext& ctx,
+                            double dedx,
+                            double p,
+                            int nGoodPixels) const override final;
 
     private:
       ServiceHandle<IBLParameterSvc> m_IBLParameterSvc;
diff --git a/InnerDetector/InDetRecTools/PixelToTPIDTool/src/PixelToTPIDTool.cxx b/InnerDetector/InDetRecTools/PixelToTPIDTool/src/PixelToTPIDTool.cxx
index 43d2f58d9a8..38eda409b24 100644
--- a/InnerDetector/InDetRecTools/PixelToTPIDTool/src/PixelToTPIDTool.cxx
+++ b/InnerDetector/InDetRecTools/PixelToTPIDTool/src/PixelToTPIDTool.cxx
@@ -20,7 +20,7 @@
 // Pixels:
 #include "InDetRIO_OnTrack/PixelClusterOnTrack.h"
 #include "InDetIdentifier/PixelID.h"
-#include "PixelGeoModel/IBLParameterSvc.h" 
+#include "PixelGeoModel/IBLParameterSvc.h"
 
 // CLHEP:
 #include "CLHEP/Matrix/Vector.h"
@@ -41,10 +41,10 @@ InDet::PixelToTPIDTool::PixelToTPIDTool(const std::string& t, const std::string&
 
   float energyPair = 3.68e-6; // Energy in MeV to create an electron-hole pair in silicon
   float sidensity = 2.329; // silicon density in g cm^-3
- 
+
   //conversion Factor
   //{.025,.023,.020}; //{Old Planars,IBL_3Ds,IBL_Planars} the sensors thickness will be take into account in dEdx calculation
-  
+
   m_conversionfactor=energyPair/sidensity;
 
 }
@@ -57,11 +57,11 @@ StatusCode InDet::PixelToTPIDTool::initialize() {
 
   ATH_CHECK(detStore()->retrieve(m_pixelid,"PixelID"));
 
-  if (m_IBLParameterSvc.retrieve().isFailure()) { 
-    ATH_MSG_FATAL("Could not retrieve IBLParameterSvc"); 
-    return StatusCode::FAILURE; 
-  } else  
-    ATH_MSG_INFO("Retrieved service " << m_IBLParameterSvc); 
+  if (m_IBLParameterSvc.retrieve().isFailure()) {
+    ATH_MSG_FATAL("Could not retrieve IBLParameterSvc");
+    return StatusCode::FAILURE;
+  } else
+    ATH_MSG_INFO("Retrieved service " << m_IBLParameterSvc);
 
   ATH_CHECK(m_moduleDataKey.initialize());
 
@@ -82,19 +82,25 @@ StatusCode InDet::PixelToTPIDTool::finalize()
 
 //============================================================================================
 
-float InDet::PixelToTPIDTool::dEdx(const Trk::Track& track, int& nUsedHits, int& nUsedIBLOverflowHits) const {
- 
+float
+InDet::PixelToTPIDTool::dEdx(const EventContext& ctx,
+                             const Trk::Track& track,
+                             int& nUsedHits,
+                             int& nUsedIBLOverflowHits) const
+{
+
   unsigned int pixelhits = 0;
   nUsedHits=0;
   nUsedIBLOverflowHits=0;
   float Pixel_sensorthickness=.025; //250 microns Pixel Planars
-  float IBL_3D_sensorthickness=.023; //230 microns IBL 3D 
+  float IBL_3D_sensorthickness=.023; //230 microns IBL 3D
   float IBL_PLANAR_sensorthickness=.020;// 200 microns IBL Planars
   float dEdxValue=0;
-  
-  //std::multimap<float,int> chargesMap; //second value keeps track if the cluster is in IBL and has at least an overflow hit
+
+  //std::multimap<float,int> chargesMap;
+  ////second value keeps track if the cluster is in IBL and has at least an overflow hit
   std::multimap<float,int> dEdxMap;
-  
+
   // Check for track states:
   const DataVector<const Trk::TrackStateOnSurface>* recoTrackStates = track.trackStateOnSurfaces();
   if (recoTrackStates) {
@@ -125,22 +131,26 @@ float InDet::PixelToTPIDTool::dEdx(const Trk::Track& track, int& nUsedHits, int&
           int bec=m_pixelid->barrel_ec(pixclus->identify());
           int layer=m_pixelid->layer_disk(pixclus->identify());
           int eta_module=m_pixelid->eta_module(pixclus->identify());//check eta module to select thickness
-   
-          float dotProd = (*tsosIter)->trackParameters()->momentum().dot((*tsosIter)->trackParameters()->associatedSurface().normal());
-          float cosalpha=fabs(dotProd/(*tsosIter)->trackParameters()->momentum().mag());
-          
+
+          float dotProd = (*tsosIter)->trackParameters()->momentum().dot(
+            (*tsosIter)->trackParameters()->associatedSurface().normal());
+          float cosalpha =
+            fabs(dotProd / (*tsosIter)->trackParameters()->momentum().mag());
+
           if (std::abs(cosalpha)<0.16) { continue; }
-   
+
           float charge=pixclus->prepRawData()->totalCharge()*cosalpha;
-   
+
           //keep track if this is an ibl cluster with overflow
           int iblOverflow=0;
-          if ((m_IBLParameterSvc->containsIBL()) and (bec==0) and (layer==0)) { // check if IBL 
-   
+          if ((m_IBLParameterSvc->containsIBL()) and (bec==0) and (layer==0)) { // check if IBL
+
             //loop over ToT and check if anyone is overflow (ToT==14) check for IBL cluster overflow
-            int overflowIBLToT = SG::ReadCondHandle<PixelModuleData>(m_moduleDataKey)->getFEI4OverflowToT(0,0);
+            int overflowIBLToT =
+              SG::ReadCondHandle<PixelModuleData>(m_moduleDataKey, ctx)
+                ->getFEI4OverflowToT(0, 0);
             const std::vector<int>& ToTs = pixclus->prepRawData()->totList();
-   
+
             for (int pixToT : ToTs) {
               if (pixToT >= overflowIBLToT) {
                 //overflow pixel hit -- flag cluster
@@ -149,23 +159,35 @@ float InDet::PixelToTPIDTool::dEdx(const Trk::Track& track, int& nUsedHits, int&
               }
             }// end
 
-            //this is IBL layer -- @todo: check using proper service (safe against geometries)
-            if (((eta_module>=-10 && eta_module<=-7)||(eta_module>=6 && eta_module<=9)) && (fabs(locy)<10. && (locx>-8.33 && locx<8.3))) { //check if IBL 3D and good cluster selection
-              dEdxValue=charge*m_conversionfactor/IBL_3D_sensorthickness;
-              dEdxMap.insert(std::pair<float,int>(dEdxValue, iblOverflow));
+            // this is IBL layer -- @todo: check using proper service (safe
+            // against geometries)
+            if (((eta_module >= -10 && eta_module <= -7) ||
+                 (eta_module >= 6 && eta_module <= 9)) &&
+                (fabs(locy) < 10. &&
+                 (locx > -8.33 &&
+                  locx < 8.3))) { // check if IBL 3D and good cluster selection
+              dEdxValue = charge * m_conversionfactor / IBL_3D_sensorthickness;
+              dEdxMap.insert(std::pair<float, int>(dEdxValue, iblOverflow));
               pixelhits++;
-              if (iblOverflow==1) { nUsedIBLOverflowHits++; }
-            }
-            else if((eta_module>=-6 && eta_module<=5) && (fabs(locy)<20. &&(locx>-8.33 && locx<8.3))) { //check if IBL planar and good cluster selection
-              dEdxValue=charge*m_conversionfactor/IBL_PLANAR_sensorthickness; 
-              dEdxMap.insert(std::pair<float,int>(dEdxValue, iblOverflow));
+              if (iblOverflow == 1) {
+                nUsedIBLOverflowHits++;
+              }
+            } else if ((eta_module >= -6 && eta_module <= 5) &&
+                       (fabs(locy) < 20. &&
+                        (locx > -8.33 &&
+                         locx < 8.3))) { // check if IBL planar and good cluster
+                                         // selection
+              dEdxValue =
+                charge * m_conversionfactor / IBL_PLANAR_sensorthickness;
+              dEdxMap.insert(std::pair<float, int>(dEdxValue, iblOverflow));
               pixelhits++;
-              if (iblOverflow==1) { nUsedIBLOverflowHits++; }
-            }
-            else{
+              if (iblOverflow == 1) {
+                nUsedIBLOverflowHits++;
+              }
+            } else {
               dEdxValue=-1;
-            } //end check which IBL Module
-     
+            } // end check which IBL Module
+
           }
           //PIXEL layer and ENDCAP
           else if(bec==0 && fabs(locy)<30. &&  ((locx>-8.20 && locx<-0.60) || (locx>0.50 && locx<8.10))) {
@@ -187,7 +209,7 @@ float InDet::PixelToTPIDTool::dEdx(const Trk::Track& track, int& nUsedHits, int&
   float averagedEdx=0.;
   nUsedHits=0;
   int IBLOverflow=0;
-  
+
   for (std::pair<float,int> itdEdx : dEdxMap) {
     if (itdEdx.second==0) {
       averagedEdx += itdEdx.first;
@@ -197,35 +219,47 @@ float InDet::PixelToTPIDTool::dEdx(const Trk::Track& track, int& nUsedHits, int&
 
     //break, skipping last or the two last elements depending on total measurements
     if (((int)pixelhits>=5) and ((int)nUsedHits>=(int)pixelhits-2)) { break; }
-    
-    //break, IBL Overflow case pixelhits==3 and 4 
+
+    //break, IBL Overflow case pixelhits==3 and 4
     if ((int)IBLOverflow>0 and ((int)pixelhits==3) and (int)nUsedHits==1) { break; }
     if ((int)IBLOverflow>0 and ((int)pixelhits==4) and (int)nUsedHits==2) { break; }
-    
+
     if (((int)pixelhits > 1) and ((int)nUsedHits >=(int)pixelhits-1)) { break; }
-    
+
     if ((int)IBLOverflow>0 and (int)pixelhits==1) { //only IBL in overflow
       averagedEdx=itdEdx.first;
       break;
     }
   }
-  
+
   if (nUsedHits>0 or (nUsedHits==0 and(int)IBLOverflow>0 and (int)pixelhits==1)) {
     if (nUsedHits>0) { averagedEdx=averagedEdx/nUsedHits; }
-    
+
     ATH_MSG_DEBUG("NEW dEdx = " << averagedEdx);
     ATH_MSG_DEBUG("Used hits: " << nUsedHits << ", IBL overflows: " << IBLOverflow );
     ATH_MSG_DEBUG("Original number of measurements = " << pixelhits << "( map size = " << dEdxMap.size() << ")");
-    return averagedEdx;  
+    return averagedEdx;
   }
   return -1;
 }
 
-std::vector<float> InDet::PixelToTPIDTool::getLikelihoods(double dedx2, double p2, int nGoodPixels) const {
-  return SG::ReadCondHandle<PixeldEdxData>(m_dedxKey)->getLikelihoods(dedx2,p2,nGoodPixels);
+std::vector<float>
+InDet::PixelToTPIDTool::getLikelihoods(const EventContext& ctx,
+                                       double dedx2,
+                                       double p2,
+                                       int nGoodPixels) const
+{
+  return SG::ReadCondHandle<PixeldEdxData>(m_dedxKey, ctx)
+    ->getLikelihoods(dedx2, p2, nGoodPixels);
 }
 
-float InDet::PixelToTPIDTool::getMass(double dedx, double p, int nGoodPixels) const {
-  return SG::ReadCondHandle<PixeldEdxData>(m_dedxKey)->getMass(dedx,p/1000,nGoodPixels); 
+float
+InDet::PixelToTPIDTool::getMass(const EventContext& ctx,
+                                double dedx,
+                                double p,
+                                int nGoodPixels) const
+{
+  return SG::ReadCondHandle<PixeldEdxData>(m_dedxKey, ctx)
+    ->getMass(dedx, p / 1000, nGoodPixels);
 }
 
diff --git a/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.h b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.h
index 3ff5cec8d47..3c680da4bf2 100644
--- a/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.h
+++ b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.h
@@ -8,6 +8,8 @@
 #ifndef TRK_IPIXELTOTPIDTOOL_H
 #define TRK_IPIXELTOTPIDTOOL_H
 
+#include "GaudiKernel/EventContext.h"
+#include "GaudiKernel/ThreadLocalContext.h"
 #include "GaudiKernel/IAlgTool.h"
 #include <vector>
 
@@ -29,11 +31,30 @@ namespace Trk {
        @param[in] track the track to be identified
        @returns   probability
      */
-    virtual float dEdx(const Trk::Track& track,
+    virtual float dEdx(const EventContext& ctx,
+                       const Trk::Track& track,
                        int& nUsedHits,
                        int& nUsedIBLOverflowHits) const = 0;
-    virtual std::vector<float> getLikelihoods(double dedx, double p, int nGoodPixels) const = 0;
-    virtual float getMass(double dedx, double p, int nGoodPixels) const = 0;
+
+    float dEdx(const Trk::Track& track,
+               int& nUsedHits,
+               int& nUsedIBLOverflowHits) const;
+
+    virtual std::vector<float> getLikelihoods(const EventContext& ctx,
+                                              double dedx,
+                                              double p,
+                                              int nGoodPixels) const = 0;
+
+    std::vector<float> getLikelihoods(double dedx,
+                                      double p,
+                                      int nGoodPixels) const;
+
+    virtual float getMass(const EventContext& ctx,
+                          double dedx,
+                          double p,
+                          int nGoodPixels) const = 0;
+
+    float getMass(double dedx, double p, int nGoodPixels) const;
   };
 
   inline const InterfaceID& Trk::IPixelToTPIDTool::interfaceID()
@@ -42,5 +63,6 @@ namespace Trk {
     }
 
 } // end of namespace
+#include "TrkToolInterfaces/IPixelToTPIDTool.icc"
 
 #endif 
diff --git a/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.icc b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.icc
new file mode 100644
index 00000000000..9a85a7c6c46
--- /dev/null
+++ b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/IPixelToTPIDTool.icc
@@ -0,0 +1,28 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+namespace Trk {
+
+inline float
+IPixelToTPIDTool::dEdx(const Trk::Track& track,
+                       int& nUsedHits,
+                       int& nUsedIBLOverflowHits) const
+{
+
+  return dEdx(
+    Gaudi::Hive::currentContext(), track, nUsedHits, nUsedIBLOverflowHits);
+}
+
+inline std::vector<float>
+IPixelToTPIDTool::getLikelihoods(double dedx, double p, int nGoodPixels) const
+{
+
+  return getLikelihoods(Gaudi::Hive::currentContext(), dedx, p, nGoodPixels);
+}
+
+inline float
+IPixelToTPIDTool::getMass(double dedx, double p, int nGoodPixels) const
+{
+  return getMass(Gaudi::Hive::currentContext(), dedx, p, nGoodPixels);
+}
+}
diff --git a/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx b/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
index 670208cdd27..10719a22b0d 100755
--- a/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
+++ b/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
@@ -198,7 +198,7 @@ Trk::TrackSummaryTool::fillSummary(const EventContext& ctx,
       };
       setTheseElements(information, atPixelIndices, toZero);
       if (track.info().trackFitter() != TrackInfo::Unknown && !m_dedxtool.empty()) {
-        dedx = m_dedxtool->dEdx(track, nHitsUsed_dEdx, nOverflowHits_dEdx);
+        dedx = m_dedxtool->dEdx(ctx,track, nHitsUsed_dEdx, nOverflowHits_dEdx);
       }
       information[Trk::numberOfDBMHits] = 0;
     }
-- 
GitLab