From 91da1d76efdcc7683aa9814eb65c3c1430b501a7 Mon Sep 17 00:00:00 2001
From: Christian Grefe <christian.grefe@cern.ch>
Date: Tue, 26 Feb 2019 15:11:13 +0000
Subject: [PATCH] TRT HT and ToT corrections for Ar

---
 .../share/ConfiguredInDetPreProcessingTRT.py  |  16 ++
 .../src/TRT_DriftCircleTool.cxx               |   8 +-
 .../ITRT_DriftFunctionTool.h                  |   4 +-
 .../TRT_DriftFunctionTool.h                   |  17 +-
 .../src/TRT_DriftFunctionTool.cxx             | 147 ++++++------------
 5 files changed, 78 insertions(+), 114 deletions(-)

diff --git a/InnerDetector/InDetExample/InDetRecExample/share/ConfiguredInDetPreProcessingTRT.py b/InnerDetector/InDetExample/InDetRecExample/share/ConfiguredInDetPreProcessingTRT.py
index 48146a3c538..798cb07f164 100644
--- a/InnerDetector/InDetExample/InDetRecExample/share/ConfiguredInDetPreProcessingTRT.py
+++ b/InnerDetector/InDetExample/InDetRecExample/share/ConfiguredInDetPreProcessingTRT.py
@@ -60,6 +60,22 @@ class ConfiguredInDetPreProcessingTRT:
          if usePhase and jobproperties.Beam.beamType()=='cosmics' and globalflags.DataSource == "geant4":
             InDetTRT_DriftFunctionTool.AllowDigiVersionOverride = True
             InDetTRT_DriftFunctionTool.ForcedDigiVersion        = 9
+            
+         # --- set HT corrections
+         InDetTRT_DriftFunctionTool.HTCorrectionBarrelXe = 1.5205
+         InDetTRT_DriftFunctionTool.HTCorrectionEndcapXe = 1.2712
+         InDetTRT_DriftFunctionTool.HTCorrectionBarrelAr = 1.5205
+         InDetTRT_DriftFunctionTool.HTCorrectionEndcapAr = 1.2712
+         
+         # --- set ToT corrections
+         InDetTRT_DriftFunctionTool.ToTCorrectionsBarrelXe = [0., 4.358121, 3.032195, 1.631892, 0.7408397, -0.004113, -0.613288, -0.73758, -0.623346, -0.561229,
+                                                              -0.29828, -0.21344, -0.322892, -0.386718, -0.534751, -0.874178, -1.231799, -1.503689, -1.896464, -2.385958]
+         InDetTRT_DriftFunctionTool.ToTCorrectionsEndcapXe = [0., 5.514777, 3.342712, 2.056626, 1.08293693, 0.3907979, -0.082819, -0.457485, -0.599706, -0.427493, 
+                                                              -0.328962, -0.403399, -0.663656, -1.029428, -1.46008, -1.919092, -2.151582, -2.285481, -2.036822, -2.15805]
+         InDetTRT_DriftFunctionTool.ToTCorrectionsBarrelAr = [0., 4.358121, 3.032195, 1.631892, 0.7408397, -0.004113, -0.613288, -0.73758, -0.623346, -0.561229,
+                                                              -0.29828, -0.21344, -0.322892, -0.386718, -0.534751, -0.874178, -1.231799, -1.503689, -1.896464, -2.385958]
+         InDetTRT_DriftFunctionTool.ToTCorrectionsEndcapAr = [0., 5.514777, 3.342712, 2.056626, 1.08293693, 0.3907979, -0.082819, -0.457485, -0.599706, -0.427493, 
+                                                              -0.328962, -0.403399, -0.663656, -1.029428, -1.46008, -1.919092, -2.151582, -2.285481, -2.036822, -2.15805]
 
          ToolSvc += InDetTRT_DriftFunctionTool
          if (InDetFlags.doPrintConfigurables()):
diff --git a/InnerDetector/InDetRecTools/TRT_DriftCircleTool/src/TRT_DriftCircleTool.cxx b/InnerDetector/InDetRecTools/TRT_DriftCircleTool/src/TRT_DriftCircleTool.cxx
index c0926bff724..54d32174919 100755
--- a/InnerDetector/InDetRecTools/TRT_DriftCircleTool/src/TRT_DriftCircleTool.cxx
+++ b/InnerDetector/InDetRecTools/TRT_DriftCircleTool/src/TRT_DriftCircleTool.cxx
@@ -247,14 +247,14 @@ InDet::TRT_DriftCircleCollection* InDet::TRT_DriftCircleTool::convert(int Mode,c
 
 
       // ToT and HT Corrections            
-      if (m_useToTCorrection && !isArgonStraw) {
-        rawTime -= m_driftFunctionTool->driftTimeToTCorrection((*r)->timeOverThreshold(), id);     
+      if (m_useToTCorrection) {
+        rawTime -= m_driftFunctionTool->driftTimeToTCorrection((*r)->timeOverThreshold(), id, isArgonStraw);     
       }  
-      if (m_useHTCorrection && !isArgonStraw &&
+      if (m_useHTCorrection &&
           ((!m_mask_first_HT_bit &&  (word & 0x04000000)) ||
            (!m_mask_middle_HT_bit && (word & 0x00020000)) ||
            (!m_mask_last_HT_bit &&   (word & 0x00000100)))) {
-         rawTime += m_driftFunctionTool->driftTimeHTCorrection(id);           
+         rawTime += m_driftFunctionTool->driftTimeHTCorrection(id, isArgonStraw);           
       }
 
       // calibrated data
diff --git a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/ITRT_DriftFunctionTool.h b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/ITRT_DriftFunctionTool.h
index 00a8e0f702f..5c72a06c426 100755
--- a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/ITRT_DriftFunctionTool.h
+++ b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/ITRT_DriftFunctionTool.h
@@ -37,9 +37,9 @@ class ITRT_DriftFunctionTool : virtual public IAlgTool {
 
   virtual double errorOfDriftRadius(double drifttime, Identifier id, float mu = -10, unsigned int word=0) const = 0;  
 
-  virtual double driftTimeToTCorrection(double tot, Identifier id) = 0;
+  virtual double driftTimeToTCorrection(double tot, Identifier id, bool isArgonStraw=false) = 0;
 
-  virtual double driftTimeHTCorrection(Identifier id) = 0;
+  virtual double driftTimeHTCorrection(Identifier id, bool isArgonStraw=false) = 0;
 
 
 };
diff --git a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/TRT_DriftFunctionTool.h b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/TRT_DriftFunctionTool.h
index 3bc79eb26fb..b625f1611a5 100755
--- a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/TRT_DriftFunctionTool.h
+++ b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/TRT_DriftFunctionTool/TRT_DriftFunctionTool.h
@@ -85,10 +85,10 @@ public:
   double errorOfDriftRadius(double drifttime, Identifier id, float mu = -10, unsigned int word=0) const;  
 
   /** Returns time over threshold correction to the drift time (ns) */
-  double driftTimeToTCorrection(double tot, Identifier id);
+  double driftTimeToTCorrection(double tot, Identifier id, bool isArgonStraw=false);
   
   /** Returns high threshold correction to the drift time (ns) */
-  double driftTimeHTCorrection(Identifier id);
+  double driftTimeHTCorrection(Identifier id, bool isArgonStraw=false);
   
   /** Initialise Rt relation in data */
   void setupRtRelationData();
@@ -107,8 +107,6 @@ private:
   const InDetDD::TRT_DetectorManager* m_manager{};
   const TRT_ID* m_trtid{};
 
-  bool m_setupToT;                     //!< true at first call
-  bool m_setupHT;                      //!< true at first call
   //  ReadHandle  keys
   SG::ReadCondHandleKey<CondAttrListCollection> m_ToTkey{this,"ToTKeyName","/TRT/Calib/ToTCalib","ToTCalib in-key"};
   SG::ReadCondHandleKey<CondAttrListCollection> m_HTkey{this,"HTKeyName","/TRT/Calib/HTCalib","HTCalib in-key"};
@@ -146,9 +144,14 @@ private:
   double m_t0_barrel[3];               //!< t0 for the 3 barrel rings
   double m_t0_endcap[18];              //!< t0 for the 14(18) endcap wheels
   double m_t0_shift;                   //!< digiversion dependent t0 shift
-  float m_tot_corrections[2][20];      //!< ToT corrections for 20 ToT bins in barrel and endcap
-  double m_ht_corrections[2];  	       //!< HT corrections for barrel and endcap
-  mutable std::mutex m_cacheMutex;
+  double m_ht_correction_barrel_Xe;    //!< HT correction for Xe straws in barrel
+  double m_ht_correction_endcap_Xe;    //!< HT correction for Xe straws in barrel
+  double m_ht_correction_barrel_Ar;    //!< HT correction for Ar straws in barrel
+  double m_ht_correction_endcap_Ar;    //!< HT correction for Ar straws in barrel
+  std::vector<double> m_tot_corrections_barrel_Xe; //!< ToT corrections for 20 ToT bins in Xe barrel straws
+  std::vector<double> m_tot_corrections_endcap_Xe; //!< ToT corrections for 20 ToT bins in Xe endcap straws
+  std::vector<double> m_tot_corrections_barrel_Ar; //!< ToT corrections for 20 ToT bins in Ar barrel straws
+  std::vector<double> m_tot_corrections_endcap_Ar; //!< ToT corrections for 20 ToT bins in Ar endcap straws
 };
 
 inline bool TRT_DriftFunctionTool::isValidTime(double drifttime) const
diff --git a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/src/TRT_DriftFunctionTool.cxx b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/src/TRT_DriftFunctionTool.cxx
index 939d0bb1b50..8ac2c340e2d 100755
--- a/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/src/TRT_DriftFunctionTool.cxx
+++ b/InnerDetector/InDetRecTools/TRT_DriftFunctionTool/src/TRT_DriftFunctionTool.cxx
@@ -40,10 +40,6 @@ TRT_DriftFunctionTool::TRT_DriftFunctionTool(const std::string& type,
   : AthAlgTool(type, name, parent),
     m_TRTCalDbSvc("TRT_CalDbSvc",name),
     m_TRTCalDbSvc2("",name),
-    m_setupToT(true),
-    m_setupHT(true),
-    m_ToTkey("/TRT/Calib/ToTCalib"),
-    m_HTkey("/TRT/Calib/HTCalib"),
     m_drifttimeperbin(3.125 * CLHEP::ns),
     m_error(0.17),
     m_drifttimeperhalfbin(0.), // set later
@@ -62,8 +58,15 @@ TRT_DriftFunctionTool::TRT_DriftFunctionTool(const std::string& type,
     m_uni_error(0.136),
     m_inputfile(""),
     m_key(""),
-    m_trt_mgr_location("TRT")
-
+    m_trt_mgr_location("TRT"),
+    m_ht_correction_barrel_Xe(0.0), // initialised from python
+    m_ht_correction_endcap_Xe(0.0), // initialised from python
+    m_ht_correction_barrel_Ar(0.0), // initialised from python
+    m_ht_correction_endcap_Ar(0.0), // initialised from python
+    m_tot_corrections_barrel_Xe(20, 0.), // initialised from python
+    m_tot_corrections_endcap_Xe(20, 0.), // initialised from python
+    m_tot_corrections_barrel_Ar(20, 0.), // initialised from python
+    m_tot_corrections_endcap_Ar(20, 0.) // initialised from python
 {
   declareInterface<ITRT_DriftFunctionTool>(this);
   m_drifttimeperhalfbin = m_drifttimeperbin/2.;
@@ -82,6 +85,14 @@ TRT_DriftFunctionTool::TRT_DriftFunctionTool(const std::string& type,
   declareProperty("TRTCalDbTool2", m_TRTCalDbSvc2);
   declareProperty("DriftFunctionFile", m_inputfile);
   declareProperty("TrtDescrManageLocation",m_trt_mgr_location);
+  declareProperty("ToTCorrectionsBarrelXe",m_tot_corrections_barrel_Xe);
+  declareProperty("ToTCorrectionsEndcapXe",m_tot_corrections_endcap_Xe);
+  declareProperty("ToTCorrectionsBarrelAr",m_tot_corrections_barrel_Xe);
+  declareProperty("ToTCorrectionsEndcapAr",m_tot_corrections_endcap_Xe);
+  declareProperty("HTCorrectionBarrelXe",m_ht_correction_barrel_Xe);
+  declareProperty("HTCorrectionEndcapXe",m_ht_correction_endcap_Xe);
+  declareProperty("HTCorrectionBarrelAr",m_ht_correction_barrel_Ar);
+  declareProperty("HTCorrectionEndcapAr",m_ht_correction_endcap_Ar);
 
   // make sure all arrays are initialized - use DC3version2 as default
   for (int i=0; i<3; i++) m_t0_barrel[i] = 15.625;
@@ -111,32 +122,6 @@ TRT_DriftFunctionTool::TRT_DriftFunctionTool(const std::string& type,
   m_radius[17] = 1.950;  m_errors[17] = 0.20;
   m_radius[18] = 1.955;  m_errors[18] = 0.20;
 
-  //ToT drift time corrections  --  Barrel (0) --  Endcap(1)
-  m_tot_corrections[0][0] = 0.;           m_tot_corrections[1][0] = 0.;
-  m_tot_corrections[0][1] = 4.358121;     m_tot_corrections[1][1] = 5.514777;
-  m_tot_corrections[0][2] = 3.032195;     m_tot_corrections[1][2] = 3.342712;
-  m_tot_corrections[0][3] = 1.631892;     m_tot_corrections[1][3] = 2.056626;
-  m_tot_corrections[0][4] = 0.7408397;    m_tot_corrections[1][4] = 1.08293693;
-  m_tot_corrections[0][5] = -0.004113;    m_tot_corrections[1][5] = 0.3907979;
-  m_tot_corrections[0][6] = -0.613288;    m_tot_corrections[1][6] = -0.082819;
-  m_tot_corrections[0][7] = -0.73758;     m_tot_corrections[1][7] = -0.457485;
-  m_tot_corrections[0][8] = -0.623346;    m_tot_corrections[1][8] = -0.599706; 
-  m_tot_corrections[0][9] = -0.561229;    m_tot_corrections[1][9] = -0.427493;
-  m_tot_corrections[0][10] = -0.29828;    m_tot_corrections[1][10] = -0.328962;
-  m_tot_corrections[0][11] = -0.21344;    m_tot_corrections[1][11] = -0.403399;
-  m_tot_corrections[0][12] = -0.322892;   m_tot_corrections[1][12] = -0.663656;
-  m_tot_corrections[0][13] = -0.386718;   m_tot_corrections[1][13] = -1.029428;
-  m_tot_corrections[0][14] = -0.534751;   m_tot_corrections[1][14] = -1.46008;
-  m_tot_corrections[0][15] = -0.874178;   m_tot_corrections[1][15] = -1.919092;
-  m_tot_corrections[0][16] = -1.231799;   m_tot_corrections[1][16] = -2.151582;
-  m_tot_corrections[0][17] = -1.503689;   m_tot_corrections[1][17] = -2.285481;
-  m_tot_corrections[0][18] = -1.896464;   m_tot_corrections[1][18] = -2.036822;
-  m_tot_corrections[0][19] = -2.385958;   m_tot_corrections[1][19] = -2.15805;
-  
-  //HT drift time correction
-  m_ht_corrections[0] = 1.5205;  //barrel
-  m_ht_corrections[1] = 1.2712;  //endcap
-  
 }
 
 //
@@ -179,6 +164,24 @@ StatusCode TRT_DriftFunctionTool::initialize()
     return sc;
   }
 
+  // Check that ToT corrections have the correct length
+  if (m_tot_corrections_barrel_Xe.size() != 20) {
+    ATH_MSG_FATAL( "Length of ToTCorrectionsBarrelXe is not 20." );
+    return sc;
+  }
+  if (m_tot_corrections_endcap_Xe.size() != 20) {
+    ATH_MSG_FATAL( "Length of ToTCorrectionsEndcapXe is not 20." );
+    return sc;
+  }
+  if (m_tot_corrections_barrel_Ar.size() != 20) {
+    ATH_MSG_FATAL( "Length of ToTCorrectionsBarrelAr is not 20." );
+    return sc;
+  }
+  if (m_tot_corrections_endcap_Ar.size() != 20) {
+    ATH_MSG_FATAL( "Length of ToTCorrectionsEndcapAr is not 20." );
+    return sc;
+  }
+
   if(m_allow_data_mc_override)
     {
       ATH_MSG_DEBUG(" Constants from conddb, code or file allowed "); 
@@ -196,9 +199,6 @@ StatusCode TRT_DriftFunctionTool::initialize()
   DecodeVersionKey versionKey(geomodel,"TRT");
   m_key=versionKey.tag();
 
-  ATH_CHECK( m_ToTkey.initialize() );
-  ATH_CHECK( m_HTkey.initialize() );
-
   int numB = m_manager->getNumerology()->getNBarrelPhi();
   ATH_MSG_DEBUG(" Number of Barrel elements "<< numB);      
       
@@ -426,84 +426,29 @@ double TRT_DriftFunctionTool::errorOfDriftRadius(double drifttime, Identifier id
 
 //
 // returns the time over threshold correction in ns
-double TRT_DriftFunctionTool::driftTimeToTCorrection(double tot, Identifier id)
+double TRT_DriftFunctionTool::driftTimeToTCorrection(double tot, Identifier id, bool isArgonStraw)
 {
-
-  if(m_setupToT) {
-    std::lock_guard<std::mutex> lock(m_cacheMutex);
-    const CondAttrListCollection* atrlistcol;
-    SG::ReadCondHandle<CondAttrListCollection> rch(m_ToTkey);
-    atrlistcol=*rch;
-    if(!atrlistcol) {
-      ATH_MSG_ERROR ("Problem reading condDB ToT correction constants.");
-    } else {
-      int channel;
-      std::ostringstream var_name;
-      for (CondAttrListCollection::const_iterator citr=atrlistcol->begin();
-            citr!=atrlistcol->end();++citr) {
-
-        //get Barrel (1) or Endcap (2)
-        channel = citr->first;
-
-        if ((channel == 1) || (channel == 2)) {
-          const coral::AttributeList& atrlist = citr->second;
-
-          for (int i = 0; i < 20; ++i ) {
-            var_name << "TRT_ToT_" << std::dec << i;
-            m_tot_corrections[channel-1][i] = atrlist[var_name.str()].data<float>();
-            var_name.str("");
-          }
-        }
-      }
-    }
-    m_setupToT=false;
-  }
-
-
-  int tot_index = tot/3.125;
+  int tot_index = tot/m_drifttimeperbin;
   if (tot_index < 0) tot_index = 0;
   if (tot_index > 19) tot_index = 19;
 
   int bec_index = abs(m_trtid->barrel_ec(id)) - 1;
 
-  return m_tot_corrections[bec_index][tot_index];
+  if (isArgonStraw) {
+    return (bec_index) ? m_tot_corrections_endcap_Ar[tot_index] : m_tot_corrections_barrel_Ar[tot_index];
+  }
+  return (bec_index) ? m_tot_corrections_endcap_Xe[tot_index] : m_tot_corrections_barrel_Xe[tot_index];
 }
 
 // Returns high threshold correction to the drift time (ns)
-double TRT_DriftFunctionTool::driftTimeHTCorrection(Identifier id)
+double TRT_DriftFunctionTool::driftTimeHTCorrection(Identifier id, bool isArgonStraw)
 {
-
-if(m_setupHT) {
-    std::lock_guard<std::mutex> lock(m_cacheMutex);
-    const CondAttrListCollection* atrlistcol;
-    SG::ReadCondHandle<CondAttrListCollection> rch(m_HTkey);
-    atrlistcol=*rch;
-    if (!atrlistcol) {
-      ATH_MSG_ERROR ("Problem reading condDB HT correction constants.");
-    } else {
-      int channel;
-      std::ostringstream var_name;
-      for (CondAttrListCollection::const_iterator citr=atrlistcol->begin();
-           citr!=atrlistcol->end();++citr) {
-
-        channel = citr->first;
-        if (channel == 1) {
-          const coral::AttributeList& atrlist = citr->second;
-
-          for (int i = 0; i < 2; ++i ) {
-            var_name << "TRT_HT_" << std::dec << i;
-            m_ht_corrections[i] = atrlist[var_name.str()].data<float>();
-            var_name.str("");
-          }
-        }
-      }
-    }
-    m_setupHT=false;
-  }
-
   int bec_index = abs(m_trtid->barrel_ec(id)) - 1;
 
-  return m_ht_corrections[bec_index];
+  if (isArgonStraw) {
+    return (bec_index) ? m_ht_correction_endcap_Ar : m_ht_correction_barrel_Ar;
+  }
+  return (bec_index) ? m_ht_correction_endcap_Xe : m_ht_correction_barrel_Xe;
 }
 
 //
-- 
GitLab