From 1d7b455b90f1da7faf1ae5364f30d5cd88bc8137 Mon Sep 17 00:00:00 2001
From: scott snyder <sss@karma>
Date: Fri, 9 Oct 2020 10:33:09 -0400
Subject: [PATCH] LArByteStream: Change from ICaloNoiseTool to CaloNoise
 conditions data.

Change from ICaloNoiseTool to CaloNoise conditions data.
---
 .../LArCnv/LArByteStream/CMakeLists.txt       |  2 +-
 .../LArRawDataContByteStreamTool.h            | 15 +++++-----
 .../LArByteStream/LArRodEncoder.h             |  6 ++--
 .../python/LArByteStreamConfig.py             | 24 ++++++++++-----
 .../share/jobOptions_WriteLArROD_G4.py        |  4 +--
 .../src/LArRawDataContByteStreamTool.cxx      | 29 +++++++++++--------
 .../LArByteStream/src/LArRodEncoder.cxx       |  9 +++---
 7 files changed, 50 insertions(+), 39 deletions(-)

diff --git a/LArCalorimeter/LArCnv/LArByteStream/CMakeLists.txt b/LArCalorimeter/LArCnv/LArByteStream/CMakeLists.txt
index c8923e22a51..c50e9142fd0 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/CMakeLists.txt
+++ b/LArCalorimeter/LArCnv/LArByteStream/CMakeLists.txt
@@ -15,7 +15,7 @@ atlas_add_library( LArByteStreamLib
    AthenaKernel xAODEventInfo ByteStreamData GaudiKernel LArIdentifier LArRawEvent
    LArRecConditions LArRecEvent CaloDetDescrLib CaloUtilsLib StoreGateLib
    ByteStreamCnvSvcBaseLib LArRawUtilsLib LArRecUtilsLib LArCablingLib
-   ByteStreamCnvSvcLib
+   ByteStreamCnvSvcLib CaloConditions
    PRIVATE_LINK_LIBRARIES AtlasDetDescr CaloInterfaceLib Identifier LArRawConditions )
 
 atlas_add_component( LArByteStream
diff --git a/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRawDataContByteStreamTool.h b/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRawDataContByteStreamTool.h
index a892e909477..7dbe8efe82d 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRawDataContByteStreamTool.h
+++ b/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRawDataContByteStreamTool.h
@@ -1,7 +1,7 @@
 //Dear emacs, this is -*- c++ -*-
 
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 /**
@@ -23,6 +23,8 @@
 #include "LArByteStream/LArRodDecoder.h"
 
 #include "CaloIdentifier/CaloGain.h"
+#include "CaloConditions/CaloNoise.h"
+#include "StoreGate/ReadCondHandleKey.h"
 
 #include "LArRawEvent/LArFebHeaderContainer.h"
 // Map of ROBs need this
@@ -31,7 +33,6 @@
 class LArRawChannelContainer; 
 class LArDigitContainer;
 class IByteStreamEventAccess;
-class ICaloNoiseTool;
 
 /** 
  *@class LArRawDataContByteStreamTool
@@ -141,7 +142,6 @@ private:
   */
   bool m_initializeForWriting;
   uint16_t m_subDetId;
-  ToolHandle<ICaloNoiseTool> m_noisetool;
   double m_nfebsigma;
   // map with ROB group (LAr) to rob addresses
   std::map<eformat::SubDetectorGroup, std::vector<const uint32_t*> > m_robIndex;
@@ -151,13 +151,12 @@ private:
   bool m_includeDigits;
   // Name of Digit container to retrieve
   std::string m_DigitContName;
-  
+
+  SG::ReadCondHandleKey<CaloNoise> m_caloNoiseKey
+  { this, "CaloNoiseKey", "totalNoise", "" };
 };
 
 
-#endif
 #include "LArByteStream/LArRawDataContByteStreamTool.icc" 
 
-
-
-
+#endif
diff --git a/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRodEncoder.h b/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRodEncoder.h
index 0b3399e9eec..b22035d14fe 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRodEncoder.h
+++ b/LArCalorimeter/LArCnv/LArByteStream/LArByteStream/LArRodEncoder.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 #ifndef LARBYTESTREAM_LARROD_ENCODER_H
@@ -49,8 +49,8 @@
 #include "ByteStreamData/RawEvent.h" 
 #include "CaloDetDescr/CaloDetDescrManager.h"
 #include "LArIdentifier/LArOnlineID.h"
+#include "CaloConditions/CaloNoise.h"
 
-class ICaloNoiseTool;
 class LArRodEncoder
 {
 public: 
@@ -77,7 +77,7 @@ public:
 
   // convert all LArRawChannels in the current list to a vector of 32bit words
 //  void fillROD(std::vector<uint32_t>& v, MsgStream& logstr) ; 
-  void fillROD(std::vector<uint32_t>& v, MsgStream& logstr, ICaloNoiseTool* calonoisetool, double nsigma) ;
+  void fillROD(std::vector<uint32_t>& v, MsgStream& logstr, const CaloNoise& noise, double nsigma) ;
 
   static void setRodBlockStructure(LArRodBlockStructure* BlStructPtr);
 
diff --git a/LArCalorimeter/LArCnv/LArByteStream/python/LArByteStreamConfig.py b/LArCalorimeter/LArCnv/LArByteStream/python/LArByteStreamConfig.py
index c9ac5a5be4d..cb948920437 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/python/LArByteStreamConfig.py
+++ b/LArCalorimeter/LArCnv/LArByteStream/python/LArByteStreamConfig.py
@@ -1,4 +1,4 @@
-# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 
 
 from LArByteStream.LArByteStreamConf import LArRawDataContByteStreamTool
@@ -7,12 +7,20 @@ from LArByteStream.LArByteStreamConf import LArRawDataContByteStreamTool
 
 class LArRawDataContByteStreamToolConfig(LArRawDataContByteStreamTool):
    __slots__ = []
-   def __init__(self, name="LArRawDataContByteStreamTool", **kwargs):
+   def __init__(self, name="LArRawDataContByteStreamTool",
+                InitializeForWriting = False,
+                stream=None,
+                **kwargs):
       super( LArRawDataContByteStreamToolConfig, self).__init__(name)
-      from CaloTools.CaloNoiseToolDefault import CaloNoiseToolDefault
-      theCaloNoiseTool=CaloNoiseToolDefault()
-      from AthenaCommon.AppMgr import ToolSvc
-      ToolSvc+=theCaloNoiseTool
-      self.NoiseTool = theCaloNoiseTool
+      if InitializeForWriting:
+         from CaloTools.CaloNoiseCondAlg import CaloNoiseCondAlg
+         noisealg = CaloNoiseCondAlg ('totalNoise')
+         if stream:
+            key = noisealg.OutputKey
+            if key.find ('+') < 0:
+               key = 'ConditionsStore+' + key
+            stream.ExtraInputs += [('CaloNoise', key)]
       for n,v in kwargs.items():
-       setattr(self, n, v)
+         setattr(self, n, v)
+      self.InitializeForWriting = InitializeForWriting
+      return
diff --git a/LArCalorimeter/LArCnv/LArByteStream/share/jobOptions_WriteLArROD_G4.py b/LArCalorimeter/LArCnv/LArByteStream/share/jobOptions_WriteLArROD_G4.py
index 1f9466e2d81..23e37e0f849 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/share/jobOptions_WriteLArROD_G4.py
+++ b/LArCalorimeter/LArCnv/LArByteStream/share/jobOptions_WriteLArROD_G4.py
@@ -110,8 +110,8 @@ theApp.EvtMax = 50
 ByteStreamEventStorageOutputSvc.FileTag = "csc12"
 from LArByteStream.LArByteStreamConfig import LArRawDataContByteStreamToolConfig
 
-svcMgr.ToolSvc += LArRawDataContByteStreamToolConfig()
-svcMgr.ToolSvc.LArRawDataContByteStreamTool.InitializeForWriting = True
+svcMgr.ToolSvc += LArRawDataContByteStreamToolConfig(InitializeForWriting = True,
+                                                     stream = StreamBS)
 
 
 from AthenaCommon.AppMgr import theAuditorSvc
diff --git a/LArCalorimeter/LArCnv/LArByteStream/src/LArRawDataContByteStreamTool.cxx b/LArCalorimeter/LArCnv/LArByteStream/src/LArRawDataContByteStreamTool.cxx
index c47885e38b9..c64b071b6b3 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/src/LArRawDataContByteStreamTool.cxx
+++ b/LArCalorimeter/LArCnv/LArByteStream/src/LArRawDataContByteStreamTool.cxx
@@ -21,7 +21,8 @@
 #include "LArByteStream/LArRodBlockPhysicsV5.h"
 #include "LArByteStream/LArRodBlockPhysicsV6.h"
 #include "GaudiKernel/MsgStream.h"
-#include "CaloInterface/ICaloNoiseTool.h"
+
+#include "StoreGate/ReadCondHandle.h"
 
 // STL stuff
 #include <map> 
@@ -38,14 +39,12 @@ const InterfaceID& LArRawDataContByteStreamTool::interfaceID( )
  
 LArRawDataContByteStreamTool::LArRawDataContByteStreamTool
 ( const std::string& type, const std::string& name,const IInterface* parent )
-  :  AthAlgTool(type,name,parent),
-     m_noisetool("CaloNoiseTool/CaloNoiseToolDefault")
+  :  AthAlgTool(type,name,parent)
 { declareInterface< LArRawDataContByteStreamTool  >( this );
   declareProperty("DSPRunMode",m_DSPRunMode=4);
   declareProperty("RodBlockVersion",m_RodBlockVersion=0);
   declareProperty("FebNoiseCut",m_nfebsigma=2);
   declareProperty("InitializeForWriting",m_initializeForWriting=false);
-  declareProperty("NoiseTool",m_noisetool);
   declareProperty("SubDetectorId",m_subDetId=0);
   declareProperty("IncludeDigits",m_includeDigits=false);
   declareProperty("DigitsContainer",m_DigitContName="LArDigitContainer_MC_Thinned");
@@ -62,15 +61,12 @@ LArRawDataContByteStreamTool::initialize()
 {
   ATH_CHECK( AthAlgTool::initialize() );
   ATH_MSG_DEBUG ( "Initializing LArRawDataContByteStream" );
- 
+
   ATH_CHECK( toolSvc()->retrieveTool("LArRodDecoder",m_decoder) );
 
   if (m_initializeForWriting) {
    ATH_CHECK( m_hid2re.initialize() );
 
-   ATH_CHECK( m_noisetool.retrieve() );
-   ATH_MSG_DEBUG ( "Successfully retrieved CaloNoiseTool " );
-   
    //Set LArRodBlockStructure according to jobOpts.
    switch(m_DSPRunMode)
      {case 0:  //Obsolete mode 
@@ -111,11 +107,14 @@ LArRawDataContByteStreamTool::initialize()
    // Set chosen RodBlockType
    LArRodEncoder::setRodBlockStructure(m_RodBlockStructure);
    ATH_MSG_INFO ( "Initialization done for reading and writing" );
+
  }
   else {
-    m_noisetool.disable();
     ATH_MSG_INFO ( "Initialization done for reading only" );
   }
+
+  ATH_CHECK( m_caloNoiseKey.initialize (m_initializeForWriting) );
+
   return StatusCode::SUCCESS;  
 }
 
@@ -190,6 +189,8 @@ StatusCode LArRawDataContByteStreamTool::WriteLArDigits(const LArDigitContainer*
       ATH_MSG_VERBOSE(" number of channels added to framgent: "<<n );
      }//  end else-if(can set Raw data)
 
+ SG::ReadCondHandle<CaloNoise> noise (m_caloNoiseKey);
+
  // Now loop over map and fill all ROD Data Blocks
  std::map<uint32_t,LArRodEncoder>::iterator it  =mapEncoder.begin(); 
  std::map<uint32_t,LArRodEncoder>::iterator it_end=mapEncoder.end();
@@ -197,7 +198,7 @@ StatusCode LArRawDataContByteStreamTool::WriteLArDigits(const LArDigitContainer*
  // ROD block data.
  for(; it!=it_end;++it) {
    theROD  = fea->getRodData( (*it).first );
-   ((*it).second).fillROD(*theROD,msg(), &(*m_noisetool), m_nfebsigma ) ; 
+   ((*it).second).fillROD(*theROD,msg(), **noise, m_nfebsigma ) ; 
  } 
  ATH_MSG_DEBUG ( "Filled " << mapEncoder.size() << " Rod Blocks" );
  // Finally, fill full event
@@ -257,6 +258,8 @@ StatusCode LArRawDataContByteStreamTool::WriteLArCalibDigits(const LArCalibDigit
  ATH_MSG_VERBOSE(" number of channels in the LArCalibDigitContainer for gain " 
                  << fixgain << ": "<<n );
 
+ SG::ReadCondHandle<CaloNoise> noise (m_caloNoiseKey);
+
  // Now loop over map and fill all ROD Data Blocks
  std::map<uint32_t,LArRodEncoder>::iterator it  =mapEncoder.begin(); 
  std::map<uint32_t,LArRodEncoder>::iterator it_end=mapEncoder.end();
@@ -264,7 +267,7 @@ StatusCode LArRawDataContByteStreamTool::WriteLArCalibDigits(const LArCalibDigit
  // ROD block data.
  for(; it!=it_end;++it) {
    theROD  = fea->getRodData( (*it).first ); 
-   ((*it).second).fillROD( *theROD,msg(), &(*m_noisetool), m_nfebsigma ) ; 
+   ((*it).second).fillROD( *theROD,msg(), **noise, m_nfebsigma ) ; 
  } 
  ATH_MSG_DEBUG ( "Filled " << mapEncoder.size() << " Rod Blocks" );
  return StatusCode::SUCCESS;
@@ -341,6 +344,8 @@ StatusCode LArRawDataContByteStreamTool::WriteLArRawChannels(const LArRawChannel
 	  } // End of check whether format allows to include RawData
 	} // Finish checking for Digit container in SG
    } // End of check for digits inclusion
+
+ SG::ReadCondHandle<CaloNoise> noise (m_caloNoiseKey);
       
  // Now loop over map and fill all ROD Data Blocks
  std::map<uint32_t,LArRodEncoder>::iterator it_m  =mapEncoder.begin(); 
@@ -349,7 +354,7 @@ StatusCode LArRawDataContByteStreamTool::WriteLArRawChannels(const LArRawChannel
  // ROD block data.
  for(; it_m!=it_m_e;++it_m) {
    theROD  = fea->getRodData( (*it_m).first ); 
-   ((*it_m).second).fillROD( *theROD,msg(), &(*m_noisetool), m_nfebsigma ) ; 
+   ((*it_m).second).fillROD( *theROD,msg(), **noise, m_nfebsigma ) ; 
    // delete ((*it_m).second);
    // ((*it_m).second)=NULL;
  } 
diff --git a/LArCalorimeter/LArCnv/LArByteStream/src/LArRodEncoder.cxx b/LArCalorimeter/LArCnv/LArByteStream/src/LArRodEncoder.cxx
index f2e3716f65e..08c3ab0d1ed 100644
--- a/LArCalorimeter/LArCnv/LArByteStream/src/LArRodEncoder.cxx
+++ b/LArCalorimeter/LArCnv/LArByteStream/src/LArRodEncoder.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
 */
 
 // Implementation of LArRodEncoder class
@@ -121,7 +121,7 @@ void LArRodEncoder::clear()
 
 // convert all LArRawChannels and LArDigits
 // in the current list to a vector of 32bit words
-void LArRodEncoder::fillROD(std::vector<uint32_t>& v, MsgStream& logstr, ICaloNoiseTool* calonoisetool, double nsigma)
+void LArRodEncoder::fillROD(std::vector<uint32_t>& v, MsgStream& logstr, const CaloNoise& noise, double nsigma)
 {
  if (!m_BlStruct)
   {logstr << MSG::ERROR << "No LArRodBlockStructure defined! Can't encode fragment!" << endmsg; 
@@ -153,7 +153,6 @@ void LArRodEncoder::fillROD(std::vector<uint32_t>& v, MsgStream& logstr, ICaloNo
 	double Ez=0;
 	double SumE=0;
 	Identifier myofflineID;
-	const CaloDetDescrElement* caloDDE;
 
 	for (;chit!=chit_e;chit++) {
 	  if ( ((LArRawChannel*)(*chit))!=0 ){
@@ -168,9 +167,9 @@ void LArRodEncoder::fillROD(std::vector<uint32_t>& v, MsgStream& logstr, ICaloNo
 	  // you convert from hardwareID to offline channle ID hash (???)
 	  myofflineID = m_cablingSvc->cnvToIdentifier(theChannel->hardwareID()) ;
 	  //std::cout << "Got Offile id 0x" << std::hex << myofflineID.get_compact() << std::dec << std::endl;
-	  caloDDE = m_CaloDetDescrManager->get_element(myofflineID);
+          const CaloDetDescrElement* caloDDE = m_CaloDetDescrManager->get_element(myofflineID);
 	  // This is probably NOT what one wants. You want the cell gain!
-          double cellnoise = calonoisetool->totalNoiseRMS(caloDDE,theChannel->gain());
+          double cellnoise = noise.getNoise(myofflineID,theChannel->gain());
           if( e > (nsigma*cellnoise) && (quality != 65535 ) ){
 	    double aux=caloDDE->sinTh();
 	    double aux_z=tanh(caloDDE->eta());
-- 
GitLab