From 0a99fd66812e3be12aaba342e7f5a1bd34631e8b Mon Sep 17 00:00:00 2001
From: christos <christos@cern.ch>
Date: Fri, 20 Nov 2020 01:18:58 +0100
Subject: [PATCH] Make TRT_ElectronPidTools EventContext aware

---
 .../TRT_ElectronPidToolRun2.h                 | 23 ++++++--
 .../src/TRT_ElectronPidToolRun2.cxx           | 59 +++++++++++++------
 .../TrkToolInterfaces/ITRT_ElectronPidTool.h  | 52 ++++++++++++++--
 .../src/TrackSummaryTool.cxx                  | 14 ++---
 4 files changed, 114 insertions(+), 34 deletions(-)

diff --git a/InnerDetector/InDetRecTools/TRT_ElectronPidTools/TRT_ElectronPidTools/TRT_ElectronPidToolRun2.h b/InnerDetector/InDetRecTools/TRT_ElectronPidTools/TRT_ElectronPidTools/TRT_ElectronPidToolRun2.h
index 7e148197811..aac420af678 100644
--- a/InnerDetector/InDetRecTools/TRT_ElectronPidTools/TRT_ElectronPidTools/TRT_ElectronPidToolRun2.h
+++ b/InnerDetector/InDetRecTools/TRT_ElectronPidTools/TRT_ElectronPidTools/TRT_ElectronPidToolRun2.h
@@ -100,16 +100,31 @@ namespace InDet
     virtual StatusCode initialize() override;
 
     /** standard Athena-Algorithm method */
-    virtual StatusCode finalize  () override;
+    virtual StatusCode finalize() override;
 
     /** Electron probabilities to be returned */
-    virtual std::vector<float> electronProbability(const Trk::Track& track) const override;
+    virtual std::vector<float> electronProbability(
+      const EventContext& ctx,
+      const Trk::Track& track) const override final;
 
     /** Electron probabilities to be returned */
     std::vector<float> electronProbability_old(const Trk::Track& track);
 
-    virtual double probHT( const double pTrk, const Trk::ParticleHypothesis hypothesis, const int HitPart, const int Layer, const int Strawlayer) const override;
-    virtual double probHTRun2( float pTrk, Trk::ParticleHypothesis hypothesis, int TrtPart, int GasType, int StrawLayer, float ZR, float rTrkWire, float Occupancy ) const override;
+    virtual double probHT(const double pTrk,
+                          const Trk::ParticleHypothesis hypothesis,
+                          const int HitPart,
+                          const int Layer,
+                          const int Strawlayer) const override final;
+
+    virtual double probHTRun2(const EventContext& ctx,
+                              float pTrk,
+                              Trk::ParticleHypothesis hypothesis,
+                              int TrtPart,
+                              int GasType,
+                              int StrawLayer,
+                              float ZR,
+                              float rTrkWire,
+                              float Occupancy) const override final;
 
   private:
 
diff --git a/InnerDetector/InDetRecTools/TRT_ElectronPidTools/src/TRT_ElectronPidToolRun2.cxx b/InnerDetector/InDetRecTools/TRT_ElectronPidTools/src/TRT_ElectronPidToolRun2.cxx
index fed2451d559..70b0a28e732 100644
--- a/InnerDetector/InDetRecTools/TRT_ElectronPidTools/src/TRT_ElectronPidToolRun2.cxx
+++ b/InnerDetector/InDetRecTools/TRT_ElectronPidTools/src/TRT_ElectronPidToolRun2.cxx
@@ -148,10 +148,12 @@ std::vector<float> InDet::TRT_ElectronPidToolRun2::electronProbability_old(const
 \*****************************************************************************/
 
 std::vector<float>
-InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) const {
+InDet::TRT_ElectronPidToolRun2::electronProbability(
+  const EventContext& ctx,
+  const Trk::Track& track) const
+{
 
   // Get the probability calculator
-  const EventContext& ctx = Gaudi::Hive::currentContext();
   SG::ReadCondHandle<HTcalculator> readHandle{m_HTReadKey,ctx};
   const HTcalculator* HTcalc = (*readHandle);
   // make sure some calibration is available
@@ -203,7 +205,8 @@ InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) con
   PIDvalues[Trk::TRTTrackOccupancy] = m_LocalOccTool->LocalOccupancy(ctx,track);
 
   if (PIDvalues[Trk::TRTTrackOccupancy] > 1.0  || PIDvalues[Trk::TRTTrackOccupancy]  < 0.0) {
-    ATH_MSG_WARNING("  Occupancy was outside allowed range! Returning default Pid values. Occupancy = " << PIDvalues[Trk::TRTTrackOccupancy] );
+    ATH_MSG_WARNING("  Occupancy was outside allowed range! Returning default Pid values. Occupancy = "
+                    << PIDvalues[Trk::TRTTrackOccupancy] );
     return PIDvalues;
   }
 
@@ -289,13 +292,22 @@ InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) con
     int StrawLayer = 0;
     if (TrtPart == 0) {
       // Barrel:
-      if      (m_trtId->layer_or_wheel(DCid) == 0) StrawLayer = m_trtId->straw_layer(DCid);
-      else if (m_trtId->layer_or_wheel(DCid) == 1) StrawLayer = 19 + m_trtId->straw_layer(DCid);
-      else                                         StrawLayer = 19 + 24 + m_trtId->straw_layer(DCid);
+      if (m_trtId->layer_or_wheel(DCid) == 0) {
+        StrawLayer = m_trtId->straw_layer(DCid);
+      } else if (m_trtId->layer_or_wheel(DCid) == 1) {
+        StrawLayer = 19 + m_trtId->straw_layer(DCid);
+      } else {
+        StrawLayer = 19 + 24 + m_trtId->straw_layer(DCid);
+      }
     } else {
       // Endcap:
-      if (m_trtId->layer_or_wheel(DCid) < 6) StrawLayer = 16*m_trtId->layer_or_wheel(DCid) + m_trtId->straw_layer(DCid);
-      else                                   StrawLayer = 8*(m_trtId->layer_or_wheel(DCid)-6) + m_trtId->straw_layer(DCid);
+      if (m_trtId->layer_or_wheel(DCid) < 6) {
+        StrawLayer =
+          16 * m_trtId->layer_or_wheel(DCid) + m_trtId->straw_layer(DCid);
+      } else {
+        StrawLayer =
+          8 * (m_trtId->layer_or_wheel(DCid) - 6) + m_trtId->straw_layer(DCid);
+      }
     }
 
     // Get Z (Barrel) or R (Endcap) location of the hit, and distance from track to wire (i.e. anode) in straw:
@@ -362,10 +374,16 @@ InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) con
       else if  ( stat==1 || stat==4 ) { GasType = 1; } // Ar
       else if  ( stat==5 )            { GasType = 1; } // Kr -- ESTIMATED AS AR UNTIL PID IS TUNED TO HANDLE KR
       else if  ( stat==6 )            { GasType = 1; } // Emulated Ar
-      else if  ( stat==7 )            { GasType = 1; } // Emulated Kr -- ESTIMATED AS AR UNTIL PID IS TUNED TO HANDLE KR
-      else { ATH_MSG_FATAL ("getStatusHT = " << stat << ", must be 'Good(2)||Xenon(3)' or 'Dead(1)||Argon(4)' or 'Krypton(5)' or 'EmulatedArgon(6)' or 'EmulatedKr(7)'!");
-             throw std::exception();
-           }
+      else if  ( stat==7 )            { GasType = 1;
+      } // Emulated Kr -- ESTIMATED AS AR UNTIL PID IS TUNED TO HANDLE KR
+      else {
+        ATH_MSG_FATAL(
+          "getStatusHT = "
+          << stat
+          << ", must be 'Good(2)||Xenon(3)' or 'Dead(1)||Argon(4)' or "
+             "'Krypton(5)' or 'EmulatedArgon(6)' or 'EmulatedKr(7)'!");
+        throw std::exception();
+      }
     }
 
     ATH_MSG_DEBUG("check Hit: "
@@ -456,14 +474,17 @@ InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) con
   PIDvalues[Trk::eProbabilityNumberOfTRTHitsUsedFordEdx] = m_TRTdEdxTool->usedHits(ctx,&track);
   double dEdx_noHTHits = m_TRTdEdxTool->dEdx(ctx,&track, false); // Divide by L, exclude HT hits
   double dEdx_usedHits_noHTHits = m_TRTdEdxTool->usedHits(ctx,&track, false);
-  PIDvalues[Trk::eProbabilityToT] = m_TRTdEdxTool->getTest(ctx, dEdx_noHTHits, pTrk, Trk::electron, Trk::pion, dEdx_usedHits_noHTHits);
+  PIDvalues[Trk::eProbabilityToT] = m_TRTdEdxTool->getTest(
+    ctx, dEdx_noHTHits, pTrk, Trk::electron, Trk::pion, dEdx_usedHits_noHTHits);
 
   // Limit the probability values the upper and lower limits that are given/trusted for each part:
   double limProbHT = HTcalc->Limit(PIDvalues[Trk::eProbabilityHT]);
   double limProbToT = HTcalc->Limit(PIDvalues[Trk::eProbabilityToT]);
 
   // Calculate the combined probability, assuming no correlations (none are expected).
-  PIDvalues[Trk::eProbabilityComb] = (limProbHT * limProbToT ) / ( (limProbHT * limProbToT) + ( (1.0-limProbHT) * (1.0-limProbToT)) );
+  PIDvalues[Trk::eProbabilityComb] =
+    (limProbHT * limProbToT) /
+    ((limProbHT * limProbToT) + ((1.0 - limProbHT) * (1.0 - limProbToT)));
 
   // Troels: VERY NASTY NAMING, BUT AGREED UPON FOR NOW (for debugging, 27. NOV. 2014):
   PIDvalues[Trk::eProbabilityBrem] = pHTel_prod; // decorates electron LH to el brem for now... (still used?)
@@ -524,7 +545,6 @@ InDet::TRT_ElectronPidToolRun2::electronProbability(const Trk::Track& track) con
   return PIDvalues;
 }
 
-
 /*****************************************************************************\
 |*%%%  TRT straw address check, done once per hit.  %%%%%%%%%%%%%%%%%%%%%%%%%*|
 |*%%%  Nowhere else are these numbers checked. If this is deemed  %%%%%%%%%%%*|
@@ -564,7 +584,9 @@ bool InDet::TRT_ElectronPidToolRun2::CheckGeometry(int BEC, int Layer, int Straw
                                         {16,16,16,16,16,16,8,8,8,8,8,8,8,8}};
 
   if(not(StrawLayer < strawsPerBEC[part][Layer])){
-    ATH_MSG_ERROR("TRT part "<<BEC<<" Layer "<<Layer<<" only has "<<strawsPerBEC[part][Layer]<<" straws. Found index "<<StrawLayer);
+    ATH_MSG_ERROR("TRT part " << BEC << " Layer " << Layer << " only has "
+                              << strawsPerBEC[part][Layer]
+                              << " straws. Found index " << StrawLayer);
     return false;
   }
 
@@ -593,7 +615,8 @@ InDet::TRT_ElectronPidToolRun2::probHT(
 }
 
 double
-InDet::TRT_ElectronPidToolRun2::probHTRun2(float pTrk,
+InDet::TRT_ElectronPidToolRun2::probHTRun2(const EventContext& ctx,
+                                           float pTrk,
                                            Trk::ParticleHypothesis hypothesis,
                                            int TrtPart,
                                            int GasType,
@@ -602,7 +625,7 @@ InDet::TRT_ElectronPidToolRun2::probHTRun2(float pTrk,
                                            float rTrkWire,
                                            float Occupancy) const
 {
-  SG::ReadCondHandle<HTcalculator> readHandle{ m_HTReadKey };
+  SG::ReadCondHandle<HTcalculator> readHandle{ m_HTReadKey, ctx };
   bool hasTrackPar = true;
   return (*readHandle)
     ->getProbHT(pTrk,
diff --git a/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/ITRT_ElectronPidTool.h b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/ITRT_ElectronPidTool.h
index 7050bd36525..8f9582db573 100644
--- a/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/ITRT_ElectronPidTool.h
+++ b/Tracking/TrkTools/TrkToolInterfaces/TrkToolInterfaces/ITRT_ElectronPidTool.h
@@ -9,6 +9,8 @@
 #define TRK_ITRT_ELECTRONPIDTOOL_H
 
 #include "GaudiKernel/IAlgTool.h"
+#include "GaudiKernel/EventContext.h"
+#include "GaudiKernel/ThreadLocalContext.h"
 #include "TrkEventPrimitives/ParticleHypothesis.h"
 #include "TrkTrackSummary/TrackSummary.h"
 #include <vector>
@@ -37,14 +39,54 @@ namespace Trk {
        @param[in] track the track to be identified
        @returns   vector of probabilities
      */
-    virtual std::vector<float> electronProbability(const Trk::Track& track) const = 0;
-    
-    /** @brief return high threshold probability 
+    virtual std::vector<float> electronProbability(
+      const EventContext& ctx,
+      const Trk::Track& track) const = 0;
+
+    std::vector<float> electronProbability(
+      const Trk::Track& track) const {
+      return  electronProbability(Gaudi::Hive::currentContext(),track);
+    }
+
+
+    /** @brief return high threshold probability
         @returns double of probability
     */
-    virtual double probHT( const double p, const Trk::ParticleHypothesis hypothesis, const int HitPart, const int Layer, const int Strawlayer) const = 0;
-    virtual double probHTRun2( float pTrk, Trk::ParticleHypothesis hypothesis, int TrtPart, int GasType, int StrawLayer, float ZR, float rTrkWire, float Occupancy ) const = 0;
+    virtual double probHT(const double p,
+                          const Trk::ParticleHypothesis hypothesis,
+                          const int HitPart,
+                          const int Layer,
+                          const int Strawlayer) const = 0;
+ 
+    virtual double probHTRun2(const EventContext& ctx,
+                              float pTrk,
+                              Trk::ParticleHypothesis hypothesis,
+                              int TrtPart,
+                              int GasType,
+                              int StrawLayer,
+                              float ZR,
+                              float rTrkWire,
+                              float Occupancy) const = 0;
 
+    double probHTRun2(float pTrk,
+                      Trk::ParticleHypothesis hypothesis,
+                      int TrtPart,
+                      int GasType,
+                      int StrawLayer,
+                      float ZR,
+                      float rTrkWire,
+                      float Occupancy) const
+    {
+      return probHTRun2(Gaudi::Hive::currentContext(),
+                        pTrk,
+                        hypothesis,
+                        TrtPart,
+                        GasType,
+                        StrawLayer,
+                        ZR,
+                        rTrkWire,
+                        Occupancy);
+    }
   };
 
   inline const InterfaceID& Trk::ITRT_ElectronPidTool::interfaceID()
diff --git a/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx b/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
index cc47250394d..670208cdd27 100755
--- a/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
+++ b/Tracking/TrkTools/TrkTrackSummaryTool/src/TrackSummaryTool.cxx
@@ -183,11 +183,11 @@ Trk::TrackSummaryTool::fillSummary(const EventContext& ctx,
   int nHitsUsed_dEdx = -1;
   int nOverflowHits_dEdx = -1;
   // Now set values to 0 for the ones we evaluate
-  const int toZero{0};
+  constexpr int toZero{0};
   if (!m_idTool.empty()) {
     if (m_pixelExists) {
       constexpr size_t numberOfPixelCounters{14};
-      const std::array<size_t, numberOfPixelCounters> atPixelIndices{
+      constexpr std::array<size_t, numberOfPixelCounters> atPixelIndices{
         numberOfContribPixelLayers,
         numberOfInnermostPixelLayerHits, numberOfInnermostPixelLayerOutliers,
         numberOfNextToInnermostPixelLayerHits, numberOfNextToInnermostPixelLayerOutliers,
@@ -210,7 +210,7 @@ Trk::TrackSummaryTool::fillSummary(const EventContext& ctx,
     };
     setTheseElements(information, atSctOrTrtIndices, toZero);
     if (!m_eProbabilityTool.empty()) {
-      eProbability = m_eProbabilityTool->electronProbability(track);
+      eProbability = m_eProbabilityTool->electronProbability(ctx,track);
       information[Trk::numberOfTRTHitsUsedFordEdx] = static_cast<int>(
         eProbability[Trk::eProbabilityNumberOfTRTHitsUsedFordEdx]);
     }
@@ -496,8 +496,8 @@ Trk::TrackSummaryTool::searchHolesStepWise(const Trk::Track& track,
   // -------- obtain hits in Pixel and SCT only
   if (track.trackStateOnSurfaces()==nullptr){
     ATH_MSG_DEBUG ("No trackStatesOnSurface!!!!");
-    const int toMinusOne{-1};
-    const std::array<size_t, 16> atIndices{
+    constexpr int toMinusOne{-1};
+    constexpr std::array<size_t, 16> atIndices{
       numberOfPixelHoles, numberOfPixelDeadSensors,
       numberOfSCTHoles, numberOfSCTDoubleHoles, numberOfSCTDeadSensors,
       numberOfTRTHoles, numberOfTRTDeadStraws,
@@ -512,7 +512,7 @@ Trk::TrackSummaryTool::searchHolesStepWise(const Trk::Track& track,
     return;
   }
 
-  const int toZero{0};
+  constexpr int toZero{0};
   if (doHolesInDet){
     // -------- perform the InDet hole search
     if (m_pixelExists) {
@@ -527,7 +527,7 @@ Trk::TrackSummaryTool::searchHolesStepWise(const Trk::Track& track,
     // so we need to make this all a bit more general
     // and probably more efficient. But this hopefully works for now! EJWM
     constexpr size_t numberOfRelatedMuonCounters{10};
-    const std::array<size_t, numberOfRelatedMuonCounters> atMuonIndices{
+    constexpr std::array<size_t, numberOfRelatedMuonCounters> atMuonIndices{
       numberOfMdtHoles,
       numberOfCscEtaHoles, numberOfCscPhiHoles,
       numberOfRpcEtaHoles, numberOfRpcPhiHoles,
-- 
GitLab