From a84d6d45530d14b7028713dc2ca009419e81352a Mon Sep 17 00:00:00 2001
From: Christos Anastopoulos <christos.anastopoulos@cern.ch>
Date: Thu, 17 Dec 2020 17:33:26 +0000
Subject: [PATCH] Make CaloBanChanTool EventContext aware to avoid lookups

---
 .../CaloBadChannelTool/CaloBadChanTool.h      | 22 ++++---
 .../src/CaloBadChanTool.cxx                   | 57 +++++++++----------
 .../src/CaloClusterBadChannelList.cxx         | 11 ++--
 .../CaloConditions/ICaloBadChanTool.h         | 13 ++++-
 .../TileConditions/TileBadChanTool.h          |  4 +-
 .../TileConditions/src/TileBadChanTool.cxx    |  6 +-
 6 files changed, 67 insertions(+), 46 deletions(-)

diff --git a/Calorimeter/CaloBadChannelTool/CaloBadChannelTool/CaloBadChanTool.h b/Calorimeter/CaloBadChannelTool/CaloBadChannelTool/CaloBadChanTool.h
index db0b28852b3..38c44b229ae 100644
--- a/Calorimeter/CaloBadChannelTool/CaloBadChannelTool/CaloBadChanTool.h
+++ b/Calorimeter/CaloBadChannelTool/CaloBadChannelTool/CaloBadChanTool.h
@@ -22,17 +22,25 @@ public:
   CaloBadChanTool(const std::string& type, const std::string& name, 
 		  const IInterface* parent);
 
-  virtual ~CaloBadChanTool();
+  virtual ~CaloBadChanTool() override;
 					  
-  virtual StatusCode initialize();
-
-  virtual CaloBadChannel caloStatus( Identifier id) const;
+  virtual StatusCode initialize() override;
 
+  using ICaloBadChanTool::caloStatus;
+  virtual CaloBadChannel caloStatus(const EventContext& ctx,
+                                    Identifier id) const override;
 
 private:
-
-  SG::ReadCondHandleKey<LArBadChannelCont> m_larBCKey{this, "LArBadChanKey", "LArBadChannel", "LAr bad channel SG key"};
-  ToolHandle<ICaloBadChanTool> m_tileBCT{this, "TileBadChanTool", "TileBadChanTool", "Tile bad channel tool"};
+  SG::ReadCondHandleKey<LArBadChannelCont> m_larBCKey{
+    this,
+    "LArBadChanKey",
+    "LArBadChannel",
+    "LAr bad channel SG key"
+  };
+  ToolHandle<ICaloBadChanTool> m_tileBCT{ this,
+                                          "TileBadChanTool",
+                                          "TileBadChanTool",
+                                          "Tile bad channel tool" };
   const CaloCell_ID* m_caloID;
 };
 
diff --git a/Calorimeter/CaloBadChannelTool/src/CaloBadChanTool.cxx b/Calorimeter/CaloBadChannelTool/src/CaloBadChanTool.cxx
index 9db108c108c..70d49eb9916 100644
--- a/Calorimeter/CaloBadChannelTool/src/CaloBadChanTool.cxx
+++ b/Calorimeter/CaloBadChannelTool/src/CaloBadChanTool.cxx
@@ -6,7 +6,7 @@
 
 //#include "GaudiKernel/MsgStream.h"
 
-CaloBadChanTool::CaloBadChanTool(const std::string& type, const std::string& name, 
+CaloBadChanTool::CaloBadChanTool(const std::string& type, const std::string& name,
 				 const IInterface* parent) :
   AthAlgTool( type, name, parent),
   m_caloID(nullptr)
@@ -26,7 +26,7 @@ StatusCode CaloBadChanTool::initialize()
   if (sc.isFailure()) {
     ATH_MSG_WARNING ( "Unable to initialize LAr bad channels key: no LAr bad channel info will be provided " );
   }
-  else 
+  else
     ATH_MSG_DEBUG ( "LAr bad channels key initialized" );
 
 
@@ -34,41 +34,40 @@ StatusCode CaloBadChanTool::initialize()
   if (sc.isFailure()) {
     ATH_MSG_WARNING ( "Unable to get TileBadChannelTool: no Tile bad channel info will be provided " );
   }
-  else 
+  else
     ATH_MSG_DEBUG ( "TileBadChannelTool retrieved" );
 
   ATH_CHECK( detStore()->retrieve(m_caloID, "CaloCell_ID") );
   return StatusCode::SUCCESS;
 }
 
-CaloBadChannel CaloBadChanTool::caloStatus(Identifier id) const{
+CaloBadChannel
+CaloBadChanTool::caloStatus(const EventContext& ctx, Identifier id) const
+{
   if (m_tileBCT && m_caloID->is_tile(id)) {
-    return m_tileBCT->caloStatus(id);
-  }
-  else if(m_caloID->is_lar(id)) {
-     SG::ReadCondHandle<LArBadChannelCont> bch{m_larBCKey};
-     const LArBadChannelCont* bcCont{*bch};
-     if(bcCont) {
-        CaloBadChannel::BitWord res = 0;
-        LArBadChannel lbc = bcCont->offlineStatus(id);
-        
-        if ( lbc.reallyNoisy() || lbc.sporadicBurstNoise()) {
-          CaloBadChannel::setBit( CaloBadChannel::noisyBit, res);
-        }
-        if (lbc.deadReadout() || lbc.deadPhys()) {
-          CaloBadChannel::setBit( CaloBadChannel::deadBit, res);
-        }
-        else if ( ! lbc.good()) {
-          CaloBadChannel::setBit( CaloBadChannel::affectedBit, res);
-        }
-        return CaloBadChannel(res);
-     } else {
-        CaloBadChannel empty;
-        return empty;
-     }
-  }
-  else {
+    return m_tileBCT->caloStatus(ctx,id);
+  } else if (m_caloID->is_lar(id)) {
+    SG::ReadCondHandle<LArBadChannelCont> bch{ m_larBCKey, ctx };
+    const LArBadChannelCont* bcCont{ *bch };
+    if (bcCont) {
+      CaloBadChannel::BitWord res = 0;
+      LArBadChannel lbc = bcCont->offlineStatus(id);
+
+      if (lbc.reallyNoisy() || lbc.sporadicBurstNoise()) {
+        CaloBadChannel::setBit(CaloBadChannel::noisyBit, res);
+      }
+      if (lbc.deadReadout() || lbc.deadPhys()) {
+        CaloBadChannel::setBit(CaloBadChannel::deadBit, res);
+      } else if (!lbc.good()) {
+        CaloBadChannel::setBit(CaloBadChannel::affectedBit, res);
+      }
+      return CaloBadChannel(res);
+    } else {
       CaloBadChannel empty;
       return empty;
+    }
+  } else {
+    CaloBadChannel empty;
+    return empty;
   }
 }
diff --git a/Calorimeter/CaloClusterCorrection/src/CaloClusterBadChannelList.cxx b/Calorimeter/CaloClusterCorrection/src/CaloClusterBadChannelList.cxx
index c5bda73dddc..920085af0c4 100755
--- a/Calorimeter/CaloClusterCorrection/src/CaloClusterBadChannelList.cxx
+++ b/Calorimeter/CaloClusterCorrection/src/CaloClusterBadChannelList.cxx
@@ -35,7 +35,7 @@ StatusCode CaloClusterBadChannelList::initialize()
 }
 
 
-void CaloClusterBadChannelList::makeCorrection (const Context& /*myctx*/,
+void CaloClusterBadChannelList::makeCorrection (const Context& myctx,
                                                 CaloCluster* cluster) const
 {
   xAOD::CaloClusterBadChannelList badChanList;
@@ -44,8 +44,8 @@ void CaloClusterBadChannelList::makeCorrection (const Context& /*myctx*/,
   CaloCluster::cell_iterator cellIterEnd = cluster->cell_end();
   for( ;cellIter!=cellIterEnd;cellIter++) {
       const CaloCell* cell = (*cellIter);
-      const Identifier id = cell->ID(); 
-      CaloBadChannel status = m_badChannelTool->caloStatus(id);
+      const Identifier id = cell->ID();
+      CaloBadChannel status = m_badChannelTool->caloStatus(myctx.ctx(),id);
       bool isBad = cell->badcell();
       if (status.dead() || status.noisy() || isBad )   {
          const float eta = cell->eta();
@@ -56,8 +56,9 @@ void CaloClusterBadChannelList::makeCorrection (const Context& /*myctx*/,
          if (isBad && !status.dead()) {
             CaloBadChannel::setBit(CaloBadChannel::deadBit,myword,true);
          }
-         ATH_MSG_DEBUG(" bad channel found eta,phi,layer,status " << eta << " " << phi << " " << layer << " " << myword);
-	 badChanList.emplace_back(eta,phi,layer,myword);
+         ATH_MSG_DEBUG(" bad channel found eta,phi,layer,status "
+                       << eta << " " << phi << " " << layer << " " << myword);
+         badChanList.emplace_back(eta, phi, layer, myword);
       }
   }  // end loop over cells
   cluster->setBadChannelList(badChanList);
diff --git a/Calorimeter/CaloConditions/CaloConditions/ICaloBadChanTool.h b/Calorimeter/CaloConditions/CaloConditions/ICaloBadChanTool.h
index 4aa9304052a..11d50456c9c 100644
--- a/Calorimeter/CaloConditions/CaloConditions/ICaloBadChanTool.h
+++ b/Calorimeter/CaloConditions/CaloConditions/ICaloBadChanTool.h
@@ -8,13 +8,22 @@
 #include "Identifier/Identifier.h"
 #include "CaloConditions/CaloBadChannel.h"
 #include "GaudiKernel/IAlgTool.h"
+#include "GaudiKernel/EventContext.h"
+#include "GaudiKernel/ThreadLocalContext.h"
+
 
 class ICaloBadChanTool : public virtual IAlgTool {
 public:
 					  
   virtual ~ICaloBadChanTool() {}
-  
-  virtual CaloBadChannel caloStatus( Identifier id) const = 0;
+
+  virtual CaloBadChannel caloStatus(const EventContext& ctx,
+                                    Identifier id) const = 0;
+
+  virtual CaloBadChannel caloStatus(Identifier id) const
+  {
+    return caloStatus(Gaudi::Hive::currentContext(), id);
+  }
 
   static const InterfaceID& interfaceID() { 
     static const InterfaceID id("ICaloBadChanTool", 1 , 0);
diff --git a/TileCalorimeter/TileConditions/TileConditions/TileBadChanTool.h b/TileCalorimeter/TileConditions/TileConditions/TileBadChanTool.h
index f7af1ad527f..52c7df54697 100644
--- a/TileCalorimeter/TileConditions/TileConditions/TileBadChanTool.h
+++ b/TileCalorimeter/TileConditions/TileConditions/TileBadChanTool.h
@@ -51,7 +51,9 @@ class TileBadChanTool: public extends<AthAlgTool, ITileBadChanTool, ICaloBadChan
      * @brief ICaloBadChanTool interface.
      * @param cell_id Calo cell identifier
      */
-    virtual CaloBadChannel caloStatus(Identifier cell_id) const override;
+    using ICaloBadChanTool::caloStatus;
+    virtual CaloBadChannel caloStatus(const EventContext& ctx,
+                                      Identifier cell_id) const override;
 
     //===============================================================
     //=== ITileBadChanTool methods
diff --git a/TileCalorimeter/TileConditions/src/TileBadChanTool.cxx b/TileCalorimeter/TileConditions/src/TileBadChanTool.cxx
index 7417de19998..9b70a2572c6 100644
--- a/TileCalorimeter/TileConditions/src/TileBadChanTool.cxx
+++ b/TileCalorimeter/TileConditions/src/TileBadChanTool.cxx
@@ -81,7 +81,9 @@ StatusCode TileBadChanTool::finalize() {
 
 //
 //____________________________________________________________________
-CaloBadChannel TileBadChanTool::caloStatus(Identifier cell_id) const {
+CaloBadChannel
+TileBadChanTool::caloStatus(const EventContext& ctx, Identifier cell_id) const
+{
 
   CaloBadChannel::BitWord res = 0;
 
@@ -94,7 +96,7 @@ CaloBadChannel TileBadChanTool::caloStatus(Identifier cell_id) const {
     std::abort();
   }
 
-  SG::ReadCondHandle<TileBadChannels> badChannels(m_badChannelsKey);
+  SG::ReadCondHandle<TileBadChannels> badChannels(m_badChannelsKey,ctx);
 
   IdentifierHash hash1_id(elem->onl1());
   IdentifierHash hash2_id(elem->onl2());
-- 
GitLab