From a571f58e6316d72c3e9474b215ce78fbcbf6e99a Mon Sep 17 00:00:00 2001
From: Siarhei Harkusha <Siarhei.Harkusha@cern.ch>
Date: Tue, 6 Nov 2018 23:53:59 +0100
Subject: [PATCH] Decrease dependency on TileInfo

TileInfo is used to return any detector-dependent
constants or information regarding the Tile Calorimeter,
including conditions data via different conditions tools.
It is not good for AthenaMT.

So functionality of TileInfo which is provided via tools
will be removed and clients will be migrated to use tools directly.
It is supposed that TileInfo will keep and provide only constants
which will be initialized during initialization of job.

Migration of clients has been started.
---
 .../CaloTools/CaloTools/CaloNoiseTool.h       | 17 +++++
 Calorimeter/CaloTools/src/CaloNoiseTool.cxx   | 71 +++++++++++++------
 .../TileConditions/TileConditions/TileInfo.h  | 13 +++-
 .../TileSimAlgs/TileHitToRawChannel.h         |  5 ++
 .../TileSimAlgs/src/TileHitToRawChannel.cxx   | 23 +++---
 5 files changed, 96 insertions(+), 33 deletions(-)

diff --git a/Calorimeter/CaloTools/CaloTools/CaloNoiseTool.h b/Calorimeter/CaloTools/CaloTools/CaloNoiseTool.h
index 87e82fa2455..eec04509dc7 100644
--- a/Calorimeter/CaloTools/CaloTools/CaloNoiseTool.h
+++ b/Calorimeter/CaloTools/CaloTools/CaloNoiseTool.h
@@ -39,6 +39,10 @@ class StoreGateSvc;
 #include "LArElecCalib/ILArADC2MeVTool.h" 
 
 #include "TileConditions/TileInfo.h"
+#include "TileConditions/ITileCellNoiseTool.h"
+#include "TileConditions/TileCondToolNoiseSample.h"
+#include "TileConditions/TileCondToolEmscale.h"
+#include "TileConditions/TileCondIdTransforms.h"
 
 #include "CaloEvent/CaloCell.h"
 #include "LArRecEvent/LArCell.h"
@@ -136,6 +140,19 @@ private:
   std::string     m_tileInfoName;
   const TileInfo* m_tileInfo;
 
+  ToolHandle<ITileCellNoiseTool> m_tileCellNoise{this,
+      "TileCellNoiseTool", "TileCellNoiseTool", "Tile Cell noise tool"};
+
+  ToolHandle<TileCondToolEmscale> m_tileToolEmscale{this,
+      "TileCondToolEmscale", "TileCondToolEmscale", "Tile EM scale calibration tool"};
+
+  ToolHandle<TileCondToolNoiseSample> m_tileToolNoiseSample{this,
+      "TileCondToolNoiseSample", "TileCondToolNoiseSample", "Tile sample noise tool"};
+
+  ToolHandle<TileCondIdTransforms> m_tileIdTransforms{this,
+      "TileCondIdTransforms", "TileCondIdTransforms",
+      "Tile tool to tranlate hardware identifiers to the drawerIdx, channel, and adc"};
+
 //Database  
 
   bool m_retrieve[15];
diff --git a/Calorimeter/CaloTools/src/CaloNoiseTool.cxx b/Calorimeter/CaloTools/src/CaloNoiseTool.cxx
index 936428fc5cb..a8836332d70 100644
--- a/Calorimeter/CaloTools/src/CaloNoiseTool.cxx
+++ b/Calorimeter/CaloTools/src/CaloNoiseTool.cxx
@@ -356,9 +356,17 @@ CaloNoiseTool::initialize()
 
   }//UseLAr
 
-  if(m_UseTile)
-  {
-   //currently no database for Tile
+  if(m_UseTile) {
+    //currently no database for Tile
+    ATH_CHECK( m_tileCellNoise.retrieve() );
+    ATH_CHECK( m_tileToolEmscale.retrieve() );
+    ATH_CHECK( m_tileToolNoiseSample.retrieve() );
+    ATH_CHECK( m_tileIdTransforms.retrieve() );
+  } else {
+    m_tileCellNoise.disable();
+    m_tileToolEmscale.disable();
+    m_tileToolNoiseSample.disable();
+    m_tileIdTransforms.disable();
   }
 
   m_CNoise    = 0.456E-3;
@@ -894,7 +902,10 @@ CaloNoiseTool::calculateElecNoiseForTILE(const IdentifierHash & idCaloHash)
   for(int igain=0;igain<CaloGain::LARNGAIN;++igain)
   {
     CaloGain::CaloGain gain = static_cast<CaloGain::CaloGain>(igain);
-    float sigma = m_tileInfo->CellNoiseSigma(id,gain);
+    float sigma = m_tileCellNoise->getCellNoise(id, gain);
+    // Conversion from ADC sigma noise to OF sigma noise
+    sigma *= m_tileInfo->getNoiseScaleFactor();
+
     //the LAr gain is internally (in CellNoiseSigma) converted into Tile gain
     sigmaVector[igain]= sigma;
     //::::::::::::::::::::::::::::::::::::::
@@ -1950,42 +1961,56 @@ CaloNoiseTool::estimatedTileGain(const CaloCell* caloCell,
   //double eneTot = tileCell->energy();
 
     // threshold (1023 counts) is the same for all channels
-  double thr = m_tileInfo->ADCmax(); 
+  double threshold = m_tileInfo->ADCmax();
 
   static const TileHWID * tileHWID = TileCablingService::getInstance()->getTileHWID();
   static const IdContext chContext = tileHWID->channel_context();
-  HWIdentifier hwid;
+  HWIdentifier hwid1;
   
-  tileHWID->get_id(caloDDE->onl1(), hwid, &chContext ); // pmt id
-  hwid = tileHWID->adc_id(hwid,TileHWID::HIGHGAIN); // high gain ADC id
+  tileHWID->get_id(caloDDE->onl1(), hwid1, &chContext ); // pmt id
+  hwid1 = tileHWID->adc_id(hwid1, TileHWID::HIGHGAIN); // high gain ADC id
   
+  unsigned int drawerIdx1(0), channel1(0), adc1(0);
+  m_tileIdTransforms->getIndices(hwid1, drawerIdx1, channel1, adc1);
+
   // first PMT, convert energy to ADC counts
-  double amp = tileCell->ene1();
-  amp /= m_tileInfo->ChannelCalib(hwid,TileRawChannelUnit::ADCcounts,TileRawChannelUnit::MegaElectronVolts);
-  double ped = m_tileInfo->DigitsPedLevel(hwid); 
+  double amplitude1 = tileCell->ene1();
+  amplitude1 /= m_tileToolEmscale->channelCalib(drawerIdx1, channel1, adc1, 1.0,
+                                          TileRawChannelUnit::ADCcounts,
+                                          TileRawChannelUnit::MegaElectronVolts);
+
+  double pedestal1 = m_tileToolNoiseSample->getPed(drawerIdx1, channel1, adc1);
 
   int igain1;
 
-  if (amp + ped < thr )
+  if (amplitude1 + pedestal1 < threshold ) {
     igain1 = TileID::HIGHGAIN;
-  else
+  } else {
     igain1 = TileID::LOWGAIN;
+  }
 
   // second PMT, if it exists
-  if (caloDDE->onl2() !=  TileID::NOT_VALID_HASH ) 
-  {
-    tileHWID->get_id(caloDDE->onl2(), hwid, &chContext ); // pmt id
-    hwid = tileHWID->adc_id(hwid,TileHWID::HIGHGAIN); // high gain ADC id
+  if (caloDDE->onl2() !=  TileID::NOT_VALID_HASH ) {
+
+    HWIdentifier hwid2;
+    tileHWID->get_id(caloDDE->onl2(), hwid2, &chContext ); // pmt id
+    hwid2 = tileHWID->adc_id(hwid2, TileHWID::HIGHGAIN); // high gain ADC id
+
+    unsigned int drawerIdx2(0), channel2(0), adc2(0);
+    m_tileIdTransforms->getIndices(hwid2, drawerIdx2, channel2, adc2);
 
-    amp = tileCell->ene2();
-    amp /= m_tileInfo->ChannelCalib(hwid,TileRawChannelUnit::ADCcounts,TileRawChannelUnit::MegaElectronVolts);
-    ped = m_tileInfo->DigitsPedLevel(hwid); 
+    // first PMT, convert energy to ADC counts
+    double amplitude2 = tileCell->ene2();
+    amplitude2 /= m_tileToolEmscale->channelCalib(drawerIdx2, channel2, adc2, 1.0,
+                                            TileRawChannelUnit::ADCcounts,
+                                            TileRawChannelUnit::MegaElectronVolts);
 
-    if (amp + ped < thr ) {
+    double pedestal2 = m_tileToolNoiseSample->getPed(drawerIdx2, channel2, adc2);
+
+    if (amplitude2 + pedestal2 < threshold) {
       // igain2 high
       return igain1 == TileID::LOWGAIN ? CaloGain::TILEHIGHLOW : CaloGain::TILEHIGHHIGH;
-    }
-    else {
+    } else {
       // igain2 low
       return igain1 == TileID::LOWGAIN ? CaloGain::TILELOWLOW : CaloGain::TILEHIGHLOW;
     }
diff --git a/TileCalorimeter/TileConditions/TileConditions/TileInfo.h b/TileCalorimeter/TileConditions/TileConditions/TileInfo.h
index 099a948f891..122dba935e6 100644
--- a/TileCalorimeter/TileConditions/TileConditions/TileInfo.h
+++ b/TileCalorimeter/TileConditions/TileConditions/TileInfo.h
@@ -397,7 +397,11 @@ class TileInfo : public DataObject {
   /** NGO Need description. Do we need this function? */
   double ttl1Phase(const int /* posneg */, const int /* ieta */, const int /* iphi */) const {return 0.;}
 
-
+  /**
+   * @brief Conversion from ADC sigma noise to OF sigma noise
+   * @return Conversion factor from ADC sigma noise to OF sigma noise
+   */
+  float getNoiseScaleFactor(void) const;
 
  private:
 
@@ -525,4 +529,11 @@ class TileInfo : public DataObject {
 
 };
 
+
+// inlines
+inline
+float TileInfo::getNoiseScaleFactor(void) const {
+  return m_noiseScaleFactor[m_noiseScaleIndex];
+}
+
 #endif  //TILECONDITIONS_TILEINFO_H
diff --git a/TileCalorimeter/TileSimAlgs/TileSimAlgs/TileHitToRawChannel.h b/TileCalorimeter/TileSimAlgs/TileSimAlgs/TileHitToRawChannel.h
index 609697f9fee..33d10122a1f 100644
--- a/TileCalorimeter/TileSimAlgs/TileSimAlgs/TileHitToRawChannel.h
+++ b/TileCalorimeter/TileSimAlgs/TileSimAlgs/TileHitToRawChannel.h
@@ -32,6 +32,7 @@
 #include "TileEvent/TileRawChannelContainer.h"
 #include "TileIdentifier/TileFragHash.h"
 #include "TileIdentifier/TileRawChannelUnit.h"
+#include "TileConditions/TileCondToolNoiseSample.h"
 
 // Atlas includes
 #include "AthenaBaseComps/AthAlgorithm.h"
@@ -114,6 +115,10 @@ class TileHitToRawChannel: public AthAlgorithm {
     CLHEP::HepRandomEngine * m_pHRengine;  //!< Random number service to use
 
     ToolHandle<TileCondToolEmscale> m_tileToolEmscale; //!< main Tile Calibration tool
+
+    ToolHandle<TileCondToolNoiseSample> m_tileToolNoiseSample{this,
+        "TileCondToolNoiseSample", "TileCondToolNoiseSample", "Tile sample noise tool"};
+
 };
 
 #endif // TILESIMALGS_TILEHITTORAWCHANNEL_H
diff --git a/TileCalorimeter/TileSimAlgs/src/TileHitToRawChannel.cxx b/TileCalorimeter/TileSimAlgs/src/TileHitToRawChannel.cxx
index 1e5cdca1fe7..ed8a673f666 100644
--- a/TileCalorimeter/TileSimAlgs/src/TileHitToRawChannel.cxx
+++ b/TileCalorimeter/TileSimAlgs/src/TileHitToRawChannel.cxx
@@ -91,16 +91,18 @@ StatusCode TileHitToRawChannel::initialize() {
 
   // retrieve TileID helper and TileInfo from det store
 
-  CHECK( detStore()->retrieve(m_tileID) );
+  ATH_CHECK( detStore()->retrieve(m_tileID) );
 
-  CHECK( detStore()->retrieve(m_tileTBID) );
+  ATH_CHECK( detStore()->retrieve(m_tileTBID) );
 
-  CHECK( detStore()->retrieve(m_tileHWID) );
+  ATH_CHECK( detStore()->retrieve(m_tileHWID) );
 
-  CHECK( detStore()->retrieve(m_tileInfo, m_infoName) );
+  ATH_CHECK( detStore()->retrieve(m_tileInfo, m_infoName) );
 
   //=== get TileCondToolEmscale
-  CHECK( m_tileToolEmscale.retrieve() );
+  ATH_CHECK( m_tileToolEmscale.retrieve() );
+
+  ATH_CHECK( m_tileToolNoiseSample.retrieve() );
 
   if (m_tileNoise) {
     static const bool CREATEIFNOTTHERE_RNDM(true);
@@ -230,7 +232,9 @@ StatusCode TileHitToRawChannel::execute() {
 
       for (ch = 0; ch < nChMax; ++ch) {
         adc_gain[ch] = TileID::HIGHGAIN;
-        adc_ampl[ch] = random[ch] * m_tileInfo->ChannelNoiseSigma(TileID::HIGHGAIN, ch, idhash);
+        adc_ampl[ch] = random[ch] * m_tileToolNoiseSample->getHfn(drawerIdx, ch, TileID::HIGHGAIN)
+                                  * m_tileInfo->getNoiseScaleFactor();
+
       }
     } else {
       memset(adc_gain, -1, sizeof(adc_gain)); /* TileID::INVALID */
@@ -283,8 +287,8 @@ StatusCode TileHitToRawChannel::execute() {
                                                              , TileRawChannelUnit::MegaElectronVolts);
       double noise;
       // If high saturates, convert adc_id to low-gain value and recalculate.
-      if (adc_ampl[ch] + amp_ch + m_tileInfo->DigitsPedLevel(TileID::HIGHGAIN, ch, idhash)
-          > m_ampMaxHi) {
+      if (adc_ampl[ch] + amp_ch + m_tileToolNoiseSample->getPed(drawerIdx, ch, gain) > m_ampMaxHi) {
+
         gain = TileID::LOWGAIN;
         amp_ch = e_ch / m_tileToolEmscale->channelCalib(drawerIdx, ch, gain, 1., m_rChUnit
                                                         , TileRawChannelUnit::MegaElectronVolts);
@@ -292,7 +296,8 @@ StatusCode TileHitToRawChannel::execute() {
         // If Noise is requested, 
         // recalculate noise using the SAME random number as for high.
         if (m_tileNoise) {
-          adc_ampl[ch] = random[ch] * m_tileInfo->ChannelNoiseSigma(TileID::LOWGAIN, ch, idhash);
+          adc_ampl[ch] = random[ch] * m_tileToolNoiseSample->getHfn(drawerIdx, ch, TileID::LOWGAIN)
+                                    * m_tileInfo->getNoiseScaleFactor();
         }
       }
 
-- 
GitLab