From d655b792fb89ba92f4f174b643be2f8b7108a61c Mon Sep 17 00:00:00 2001
From: Hiroaki Hibi <hiroaki.hibi@cern.ch>
Date: Fri, 23 Oct 2020 09:00:27 +0000
Subject: [PATCH] read TGCRPhiCoincidenceMap from SQL

---
 .../TrigT1/TGCTriggerCondSvc/CMakeLists.txt   |  26 +++
 .../TGCTriggerCondSvc/TGCTriggerBWCWReader.h  |  30 +++
 .../TGCTriggerCondSvc/TGCTriggerCondAlg.h     |  42 ++++
 .../TGCTriggerCondSvc/TGCTriggerLUTReader.h   |  39 ++++
 .../TGCTriggerCondSvc/TGCTriggerLUTs.h        |  61 ++++++
 .../src/TGCTriggerBWCWReader.cxx              | 179 ++++++++++++++++++
 .../src/TGCTriggerCondAlg.cxx                 | 120 ++++++++++++
 .../src/TGCTriggerLUTReader.cxx               |  44 +++++
 .../TGCTriggerCondSvc/src/TGCTriggerLUTs.cxx  |  48 +++++
 .../src/components/TGCCondSvc_entries.cxx     |   5 +
 Trigger/TrigT1/TrigT1TGC/CMakeLists.txt       |   2 +-
 .../TrigT1TGC/TrigT1TGC/LVL1TGCTrigger.h      |   3 +
 .../TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h  |   2 +
 .../TrigT1TGC/TGCRPhiCoincidenceMap.h         |   5 +
 .../TrigT1TGC/python/TrigT1TGCConfig.py       |   7 +
 .../TrigT1/TrigT1TGC/src/LVL1TGCTrigger.cxx   |  16 +-
 .../TrigT1TGC/src/TGCDatabaseManager.cxx      |   3 +-
 .../TrigT1TGC/src/TGCRPhiCoincidenceMap.cxx   |  24 ++-
 .../python/Lvl1SimulationConfig.py            |   8 +-
 19 files changed, 654 insertions(+), 10 deletions(-)
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/CMakeLists.txt
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerBWCWReader.h
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerCondAlg.h
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTReader.h
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTs.h
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerBWCWReader.cxx
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerCondAlg.cxx
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTReader.cxx
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTs.cxx
 create mode 100644 Trigger/TrigT1/TGCTriggerCondSvc/src/components/TGCCondSvc_entries.cxx

diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/CMakeLists.txt b/Trigger/TrigT1/TGCTriggerCondSvc/CMakeLists.txt
new file mode 100644
index 00000000000..4d921b4add8
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/CMakeLists.txt
@@ -0,0 +1,26 @@
+################################################################################
+# Package: TGCTriggerCondSvc
+################################################################################
+
+# Declare the package name:
+atlas_subdir( TGCTriggerCondSvc )
+
+# External dependencies:
+find_package( CORAL COMPONENTS CoralBase CoralKernel RelationalAccess )
+find_package( ROOT COMPONENTS Core Tree MathCore Hist RIO pthread )
+
+# Component(s) in the package:
+atlas_add_library( TGCTriggerCondSvcLib
+                   src/*.cxx
+                   PUBLIC_HEADERS TGCTriggerCondSvc
+                   INCLUDE_DIRS ${CORAL_INCLUDE_DIRS}
+                   PRIVATE_INCLUDE_DIRS ${ROOT_INCLUDE_DIRS}
+                   LINK_LIBRARIES ${CORAL_LIBRARIES} AthenaBaseComps AthenaKernel AthenaPoolUtilities Identifier GaudiKernel StoreGateLib SGtests
+                   PRIVATE_LINK_LIBRARIES ${ROOT_LIBRARIES} )
+
+atlas_add_component( TGCTriggerCondSvc
+                     src/components/*.cxx
+                     INCLUDE_DIRS ${ROOT_INCLUDE_DIRS} ${CORAL_INCLUDE_DIRS}
+                     LINK_LIBRARIES ${ROOT_LIBRARIES} ${CORAL_LIBRARIES} AthenaBaseComps AthenaKernel StoreGateLib SGtests AthenaPoolUtilities Identifier GaudiKernel TGCTriggerCondSvcLib )
+
+
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerBWCWReader.h b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerBWCWReader.h
new file mode 100644
index 00000000000..c1fe6427540
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerBWCWReader.h
@@ -0,0 +1,30 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#ifndef TGCTRIGGERBWCWREADER_H
+#define TGCTRIGGERBWCWREADER_H
+
+#include "TGCTriggerCondSvc/TGCTriggerLUTReader.h"
+
+class TGCTriggerLUTs;
+class CondAttrListCollection;
+
+class TGCTriggerBWCWReader: public TGCTriggerLUTReader{
+
+ public:
+  TGCTriggerBWCWReader(int lutType);
+  ~TGCTriggerBWCWReader() = default;
+ 
+  bool readLUT(TGCTriggerLUTs* writeCdo) override;
+  bool loadParameters(TGCTriggerLUTs* writeCdo,
+                      const CondAttrListCollection* readKey) override;
+ private:
+  TGCTriggerBWCWReader() = default;
+  enum {TMap_HH=0, TMap_HL, TMap_LH, TMap_LL, N_TMap};
+  int getType(int lDR, int hDR, int lDPhi, int hDPhi) const;
+  int subSectorAdd(int ssid, int modid, int phimod2, int type) const;
+};
+
+
+#endif
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerCondAlg.h b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerCondAlg.h
new file mode 100644
index 00000000000..8de28ff8e48
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerCondAlg.h
@@ -0,0 +1,42 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#ifndef TGCTRIGGERCONDALG_H
+#define TGCTRIGGERCONDALG_H
+
+#include "AthenaBaseComps/AthAlgorithm.h"
+#include "StoreGate/ReadCondHandleKey.h"
+#include "StoreGate/WriteCondHandleKey.h"
+#include "GaudiKernel/ICondSvc.h"
+#include "AthenaPoolUtilities/CondAttrListCollection.h"
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
+#include "TGCTriggerCondSvc/TGCTriggerBWCWReader.h"
+
+  class TGCTriggerCondAlg: public AthAlgorithm {
+
+  public:
+
+    TGCTriggerCondAlg (const std::string& name, ISvcLocator* pSvcLocator);
+    virtual ~TGCTriggerCondAlg() = default;
+    virtual StatusCode initialize() override;
+    virtual StatusCode execute() override;
+    virtual StatusCode finalize() override;
+ 
+
+  private:
+
+    SG::ReadCondHandleKey<CondAttrListCollection> m_readKey_bw;
+    SG::WriteCondHandleKey<TGCTriggerLUTs> m_writeKey;    
+    ServiceHandle<ICondSvc> m_condSvc;
+
+
+    // LUT Readers 
+    TGCTriggerBWCWReader m_bwCWReader;
+
+  };
+
+
+#endif
+
+
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTReader.h b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTReader.h
new file mode 100644
index 00000000000..157675ccf18
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTReader.h
@@ -0,0 +1,39 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#ifndef TGCTRIGGERLUTREADER_H
+#define TGCTRIGGERLUTREADER_H
+
+#include "AthenaBaseComps/AthAlgorithm.h"
+#include "GaudiKernel/ISvcLocator.h"
+#include "GaudiKernel/Bootstrap.h"
+#include "GaudiKernel/MsgStream.h"
+#include "GaudiKernel/IMessageSvc.h"
+
+class TGCTriggerLUTs;
+class CondAttrListCollection;
+
+
+class TGCTriggerLUTReader{
+
+ public:
+  TGCTriggerLUTReader(int lutType);
+  ~TGCTriggerLUTReader() = default;
+
+  virtual bool readLUT(TGCTriggerLUTs* );
+  virtual bool loadParameters(TGCTriggerLUTs* ,
+                              const CondAttrListCollection* );
+
+ protected:
+  const int m_lutType;
+  IMessageSvc* m_msgSvc;
+  ISvcLocator* m_svcLocator;
+
+ private:
+  TGCTriggerLUTReader() = default;
+};
+
+
+
+#endif
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTs.h b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTs.h
new file mode 100644
index 00000000000..a37c7da4f75
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/TGCTriggerCondSvc/TGCTriggerLUTs.h
@@ -0,0 +1,61 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#ifndef TGCTRIGGERLUTS_H
+#define TGCTRIGGERLUTS_H
+
+#include "AthenaKernel/BaseInfo.h"
+#include "AthenaKernel/CLASS_DEF.h"
+
+#include <string>
+#include <map>
+#include <fstream>
+
+
+class TGCTriggerLUTs {
+
+  friend class TGCTriggerCondAlg;
+  friend class TGCTriggerBWCWReader;
+
+ public:
+
+  enum {CW_BW=0, CW_EIFI=1, CW_TILE=2, CW_NSW=3, CW_BIS78=4, WHICH_INNER=5, HOTRoI=6, LUT_NUM}; 
+  enum {N_PT_THRESH=15,
+        N_SIDE=2,
+        N_OCTANT=8,
+        N_MODULETYPE=12};
+  enum {DR_offset=-15,
+        DPhi_offset=-7};
+
+
+  TGCTriggerLUTs();
+  virtual ~TGCTriggerLUTs();
+
+  std::string getData(int cwtype, std::string file) const;
+  std::string getData(int cwtype, int channel) const;
+  std::string getFile(int cwtype, int channel) const;
+  std::string getVersion(int cwtype, int channel = 0) const;
+  std::string getType(int cwtype, int channel = 0) const;
+
+  std::map<int, std::map<int, std::map<int, char> > > getReadMapBw(int side, int octantId) const;
+
+  private:
+
+    std::map<std::string, std::string> m_datamap[LUT_NUM];
+    std::vector<std::string> m_data[LUT_NUM];
+    std::vector<std::string> m_file[LUT_NUM];
+    std::vector<std::string> m_version[LUT_NUM];
+    std::vector<std::string> m_type[LUT_NUM];
+  
+    std::map<int, std::map<int, std::map<int, char> > > m_mapDB_bw[N_SIDE][N_OCTANT]; //for Run3 <RoI(&type),<R,<Phi,pT(char)> > >
+
+};
+
+
+CLASS_DEF(TGCTriggerLUTs, 206388330, 1)
+
+#include "AthenaKernel/CondCont.h"
+CLASS_DEF(CondCont<TGCTriggerLUTs>, 123453230, 1)
+
+#endif // TGCTRIGGERDATA_H
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerBWCWReader.cxx b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerBWCWReader.cxx
new file mode 100644
index 00000000000..3c0d801f7da
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerBWCWReader.cxx
@@ -0,0 +1,179 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#include "TGCTriggerCondSvc/TGCTriggerBWCWReader.h"
+
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
+#include "AthenaPoolUtilities/CondAttrListCollection.h"
+
+TGCTriggerBWCWReader::TGCTriggerBWCWReader(int lutType):
+  TGCTriggerLUTReader(lutType)
+{
+}
+
+
+
+bool TGCTriggerBWCWReader::loadParameters(TGCTriggerLUTs* writeCdo,
+                                          const CondAttrListCollection* readCdo) {
+
+  MsgStream log(m_msgSvc, "TGCTriggerBWCWReader::loadParameters");
+  if(m_svcLocator->service("MessageSvc", m_msgSvc) == StatusCode::FAILURE){
+    return false;
+  }
+
+  CondAttrListCollection::const_iterator itr = readCdo->begin();
+  CondAttrListCollection::const_iterator itr_e = readCdo->end();
+
+  for(; itr!=itr_e; ++itr) {
+    const unsigned int channel = (*itr).first;
+    const coral::AttributeList& atr = (*itr).second;
+
+    log << MSG::DEBUG<<"channel: " << channel<<endmsg;
+    std::string file    = *(static_cast<const std::string*>((atr["file"]).addressOfData()));
+    std::string data    = *(static_cast<const std::string*>((atr["data"]).addressOfData()));
+    std::string version = *(static_cast<const std::string*>((atr["version"]).addressOfData()));
+    std::string type    = *(static_cast<const std::string*>((atr["type"]).addressOfData()));
+
+    writeCdo->m_datamap[m_lutType][file] = data;
+    writeCdo->m_data[m_lutType].push_back(data);
+    writeCdo->m_file[m_lutType].push_back(file);
+    writeCdo->m_version[m_lutType].push_back(version);
+    writeCdo->m_type[m_lutType].push_back(type);
+
+    log << MSG::INFO<<"file: " <<file<<endmsg;
+    log << MSG::DEBUG<<"data: " << data<<endmsg;
+
+  }
+  return true;
+}
+
+
+
+
+
+
+
+
+bool TGCTriggerBWCWReader::readLUT(TGCTriggerLUTs* writeCdo){
+
+  MsgStream log(m_msgSvc, "TGCTriggerBWCWReader::readLUT");
+  if(m_svcLocator->service("MessageSvc", m_msgSvc) == StatusCode::FAILURE){
+    return false;
+  }
+
+  const int numberOfCoincidenceType = 4; 
+
+  const int moduleNumber[TGCTriggerLUTs::N_MODULETYPE]  =
+    {  0,  1,   2,   2,  3,  4,   5,   5,  6,  7,   8,  8 };
+  const std::string moduleName[TGCTriggerLUTs::N_MODULETYPE]=
+    {"0a","1a","2a","2b","3a","4a","5a","5b","6a","7a","8a","8b"};
+  const std::string sideName[TGCTriggerLUTs::N_SIDE] = {"a","c"};
+  const std::string octantName[TGCTriggerLUTs::N_OCTANT] =
+    {  "0", "1", "2", "3", "4", "5", "6", "7"};
+  const std::string coincidenceTypeName[numberOfCoincidenceType] = {"HH","HL","LH","LL"};
+
+
+  // initialize
+  bool fullCW = (writeCdo->getType(TGCTriggerLUTs::CW_BW) == "full" );
+  // loop over all files...
+  for(int iSide = 0; iSide<TGCTriggerLUTs::N_SIDE; iSide++){
+    for(int iOctant = 0; iOctant < TGCTriggerLUTs::N_OCTANT; iOctant++) {
+      for(int iModule=0; iModule<TGCTriggerLUTs::N_MODULETYPE; iModule+=1) {
+        for(int iCoinType=0; iCoinType!=numberOfCoincidenceType; iCoinType++){
+
+          int phimod2=moduleName[iModule].find("b")!=std::string::npos ? 1 : 0;
+          std::string fn;
+          if (fullCW) {
+            fn = "RPhiCoincidenceMapRun3_"
+              +sideName[iSide]
+              +octantName[iOctant]
+              +moduleName[iModule]
+              +coincidenceTypeName[iCoinType]+".db";
+          }
+          else{
+            fn = "RPhiCoincidenceMapRun3_"
+              +moduleName[iModule]
+              +coincidenceTypeName[iCoinType]+".db";
+          }
+
+          bool Forward_type1=(moduleName[iModule]=="2b"||moduleName[iModule]=="5a"||moduleName[iModule]=="8b");
+          bool Forward_type2=(moduleName[iModule]=="2a"||moduleName[iModule]=="5b"||moduleName[iModule]=="8a");
+
+          if(iOctant%2==0 && Forward_type1){continue;}
+          if(iOctant%2==1 && Forward_type2){continue;}
+          int type = -1;
+          int lDR, hDR, lDPhi, hDPhi;
+
+          std::string data = writeCdo->getData(TGCTriggerLUTs::CW_BW, fn);
+          std::istringstream stream(data);
+
+          std::string buf,tag;
+          char delimiter = '\n';
+          int ssId;
+
+          while(getline(stream,buf,delimiter)){
+            std::istringstream header(buf);
+            header>>tag;
+            if(tag=="#") { // read header part.     
+              header>>ssId>>lDR>>hDR>>lDPhi>>hDPhi;
+              type = getType( lDR, hDR, lDPhi, hDPhi );
+              if( type<0 ) {
+                break;
+              }
+
+              // get window data
+              std::map<int, std::map<int, char> >  bWindow;//<R,<~>>
+              char pT;
+              for(int ir=0; ir<=hDR-lDR; ir++) {
+                getline(stream,buf,delimiter);
+                std::map<int, char> aWindow;//<Phi,pT>
+                for(int iphi=0; iphi<=hDPhi-lDPhi; iphi++){
+                  pT = buf[iphi];
+                  if (pT=='X'){continue;} // none of window is opened in this dR
+                  aWindow[iphi+TGCTriggerLUTs::DPhi_offset] = pT;
+                }
+                // Warning : no window 
+                if (aWindow.size()!=0){
+                  bWindow[ir+TGCTriggerLUTs::DR_offset]=aWindow;
+                }
+              }
+
+              int addr = subSectorAdd(ssId,moduleNumber[iModule],phimod2,type);
+              if (writeCdo->m_mapDB_bw[iSide][iOctant].find(addr)!=writeCdo->m_mapDB_bw[iSide][iOctant].end()) {
+               log << MSG::DEBUG<<"This subsector was already reserved."<<endmsg;
+              } else {
+                writeCdo->m_mapDB_bw[iSide][iOctant][addr]=bWindow;
+              }
+            }
+          }
+
+
+        }// coincidence type
+      }// module
+    }// octant
+  }// side
+
+
+  return true;
+
+}
+
+
+
+
+
+
+int TGCTriggerBWCWReader::getType(int lDR, int hDR, int lDPhi, int hDPhi ) const {
+  int type = -1;
+  if ( (lDR==-15) && (hDR==15) && (lDPhi==-7) && (hDPhi==7))      type = TGCTriggerBWCWReader::TMap_HH;
+  else if ( (lDR==-15) && (hDR==15) && (lDPhi==-3) && (hDPhi==3)) type = TGCTriggerBWCWReader::TMap_HL;
+  else if ( (lDR==-7) && (hDR==7) && (lDPhi==-7) && (hDPhi==7))   type = TGCTriggerBWCWReader::TMap_LH;
+  else if ( (lDR==-7) && (hDR==7) && (lDPhi==-3) && (hDPhi==3))   type = TGCTriggerBWCWReader::TMap_LL;
+  return type;
+}
+
+int TGCTriggerBWCWReader::subSectorAdd(int ssid, int modid, int phimod2, int type) const { 
+  return (ssid+(modid<<8)+(phimod2<<12) + (type<<16) ); 
+}
+
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerCondAlg.cxx b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerCondAlg.cxx
new file mode 100644
index 00000000000..e4509f0073b
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerCondAlg.cxx
@@ -0,0 +1,120 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#include "TGCTriggerCondSvc/TGCTriggerCondAlg.h"
+#include "StoreGate/ReadCondHandle.h"
+#include "StoreGate/WriteCondHandle.h"
+
+
+TGCTriggerCondAlg::TGCTriggerCondAlg(const std::string& name, ISvcLocator* pSvcLocator) :
+  AthAlgorithm(name, pSvcLocator),
+  m_readKey_bw("/TGC/TRIGGER/CW_BW_RUN3"),
+  m_writeKey("TGCTriggerLUTs"),
+  m_condSvc("CondSvc", name),
+  m_bwCWReader(0)
+{
+  declareProperty("ReadKeyBw", m_readKey_bw);
+  declareProperty("WriteKey", m_writeKey);
+}
+
+StatusCode TGCTriggerCondAlg::initialize(){
+
+  ATH_MSG_INFO( "initialize " << name() );                
+
+  ATH_CHECK(m_condSvc.retrieve());
+  
+  ATH_CHECK(m_readKey_bw.initialize());
+
+  ATH_CHECK(m_writeKey.initialize());
+
+  if(m_condSvc->regHandle(this, m_writeKey).isFailure()) {
+    ATH_MSG_FATAL("unable to register WriteCondHandle " << m_writeKey.fullKey() << " with CondSvc");
+    return StatusCode::FAILURE;
+  }
+
+  return StatusCode::SUCCESS;
+}
+
+StatusCode TGCTriggerCondAlg::execute(){
+  
+  ATH_MSG_DEBUG( "start execute " << name() ); 
+    
+  SG::WriteCondHandle<TGCTriggerLUTs> writeHandle{m_writeKey};
+  if (writeHandle.isValid()) {
+    ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
+                  << ". In theory this should not be called, but may happen"
+                  << " if multiple concurrent events are being processed out of order.");
+    return StatusCode::SUCCESS; 
+  }
+
+  auto writeCdo = std::make_unique<TGCTriggerLUTs>();
+
+  // Big wheel for Run3
+  SG::ReadCondHandle<CondAttrListCollection> readHandle_bw(m_readKey_bw);
+  const CondAttrListCollection* readCdo_bw(*readHandle_bw);
+
+  
+  if (readCdo_bw == 0) {
+    ATH_MSG_ERROR("Null pointer to the read conditions object");
+    return StatusCode::FAILURE; 
+  } 
+
+  ATH_MSG_INFO("Size of CondAttrListCollection " << readHandle_bw.fullKey() << " readCdo->size()= " << readCdo_bw->size());
+ 
+  EventIDRange rangeW_bw;
+  if ( !readHandle_bw.range(rangeW_bw) ) {
+    ATH_MSG_ERROR("Failed to retrieve validity range for " << readHandle_bw.key());
+    return StatusCode::FAILURE;
+  } 
+  ATH_MSG_INFO("Range of input is " << rangeW_bw);
+
+  if(!m_bwCWReader.loadParameters(writeCdo.get(), readCdo_bw)){
+    ATH_MSG_ERROR("Could not get MessageSvc");
+  }
+
+  // fill maps 
+  if(!m_bwCWReader.readLUT(writeCdo.get())){
+    ATH_MSG_ERROR("Could not get MessageSvc");
+  }
+
+
+  /*
+  // other LUTs will be implemented (NSW,RPCBIS78,HotRoI,WichInner...)
+  // write condition object
+  EventIDRange rangeIntersection = EventIDRange::intersect(rangeW_bw);
+  if(rangeIntersection.start()>rangeIntersection.stop()) {
+    ATH_MSG_ERROR("Invalid intersection range: " << rangeIntersection);
+    return StatusCode::FAILURE;
+  }
+
+  if (writeHandle.record(rangeIntersection, std::move(writeCdo)).isFailure()) {
+    ATH_MSG_FATAL("Could not record TGCTriggerLUTs " << writeHandle.key() 
+                  << " with EventRange " << rangeIntersection
+                  << " into Conditions Store");
+    return StatusCode::FAILURE;
+  }                  
+  ATH_MSG_INFO("recorded new " << writeHandle.key() << " with range " << rangeIntersection << " into Conditions Store");
+  */
+
+
+
+  if (writeHandle.record(rangeW_bw, std::move(writeCdo)).isFailure()) {
+    ATH_MSG_FATAL("Could not record TGCTriggerLUTs " << writeHandle.key() 
+                  << " with EventRange " << rangeW_bw
+                  << " into Conditions Store");
+    return StatusCode::FAILURE;
+  }        
+
+
+
+   
+  return StatusCode::SUCCESS;
+}
+
+
+StatusCode TGCTriggerCondAlg::finalize(){
+  
+  ATH_MSG_INFO( "finalize " << name() );
+  return StatusCode::SUCCESS;
+}
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTReader.cxx b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTReader.cxx
new file mode 100644
index 00000000000..629904a6599
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTReader.cxx
@@ -0,0 +1,44 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#include "TGCTriggerCondSvc/TGCTriggerLUTReader.h"
+
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
+#include "AthenaPoolUtilities/CondAttrListCollection.h"
+#include "AthenaKernel/errorcheck.h"
+
+TGCTriggerLUTReader::TGCTriggerLUTReader(int lutType):
+  m_lutType(lutType),
+  m_msgSvc(nullptr)
+{
+  m_svcLocator = Gaudi::svcLocator();
+}
+
+
+
+bool TGCTriggerLUTReader::readLUT(TGCTriggerLUTs* ){
+
+  MsgStream log(m_msgSvc, "TGCTriggerLUTReader::readLUT");
+  if(m_svcLocator->service("MessageSvc", m_msgSvc) == StatusCode::FAILURE){
+    return false;
+  }
+
+  log<<MSG::ERROR<<" Please implement readLUT"<<endmsg;
+
+  return true;
+}
+
+bool TGCTriggerLUTReader::loadParameters(TGCTriggerLUTs* ,
+                                         const CondAttrListCollection* 
+                                         ){
+
+  MsgStream log(m_msgSvc, "TGCTriggerLUTReader::loadParameters");
+  if(m_svcLocator->service("MessageSvc", m_msgSvc) == StatusCode::FAILURE){
+    return false;
+  }
+  log<<MSG::ERROR<<" Please implement loadParameters"<<endmsg;
+
+  return true;
+
+}
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTs.cxx b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTs.cxx
new file mode 100644
index 00000000000..c28d3a79ab8
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/src/TGCTriggerLUTs.cxx
@@ -0,0 +1,48 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
+#include "AthenaBaseComps/AthMessaging.h"
+
+TGCTriggerLUTs::TGCTriggerLUTs()
+{  
+}
+
+
+TGCTriggerLUTs::~TGCTriggerLUTs()
+{  
+}
+
+
+std::string TGCTriggerLUTs::getData(int cwtype, std::string file) const {
+  auto itr = m_datamap[cwtype].find(file);
+  if (itr == m_datamap[cwtype].end()) {
+    return "";
+  }
+ 
+  return itr->second;
+}
+
+
+std::string TGCTriggerLUTs::getData(int cwtype, int channel) const {
+  return m_data[cwtype][channel];
+}
+
+
+std::string TGCTriggerLUTs::getFile(int cwtype, int channel) const {
+  return m_file[cwtype][channel];
+}
+
+std::string TGCTriggerLUTs::getVersion(int cwtype, int channel) const {
+  return m_version[cwtype][channel];
+}
+
+std::string TGCTriggerLUTs::getType(int cwtype, int channel) const {
+  return m_type[cwtype][channel];
+}
+
+
+std::map<int, std::map<int, std::map<int, char> > > TGCTriggerLUTs::getReadMapBw(int side, int octantId) const {
+  return m_mapDB_bw[side][octantId];
+}
diff --git a/Trigger/TrigT1/TGCTriggerCondSvc/src/components/TGCCondSvc_entries.cxx b/Trigger/TrigT1/TGCTriggerCondSvc/src/components/TGCCondSvc_entries.cxx
new file mode 100644
index 00000000000..efc4b3eb9af
--- /dev/null
+++ b/Trigger/TrigT1/TGCTriggerCondSvc/src/components/TGCCondSvc_entries.cxx
@@ -0,0 +1,5 @@
+#include "TGCTriggerCondSvc/TGCTriggerCondAlg.h"
+
+DECLARE_COMPONENT( TGCTriggerCondAlg )
+
+
diff --git a/Trigger/TrigT1/TrigT1TGC/CMakeLists.txt b/Trigger/TrigT1/TrigT1TGC/CMakeLists.txt
index 998b0b26fda..3e5881b358b 100644
--- a/Trigger/TrigT1/TrigT1TGC/CMakeLists.txt
+++ b/Trigger/TrigT1/TrigT1TGC/CMakeLists.txt
@@ -7,7 +7,7 @@ atlas_subdir( TrigT1TGC )
 atlas_add_library( TrigT1TGCLib
                    src/*.cxx
                    PUBLIC_HEADERS TrigT1TGC
-                   LINK_LIBRARIES AthenaBaseComps AthenaKernel GaudiKernel Identifier MuonCondSvcLib MuonDigitContainer MuonRDO StoreGateLib TileEvent TrigT1Interfaces
+                   LINK_LIBRARIES AthenaBaseComps AthenaKernel GaudiKernel Identifier MuonCondSvcLib MuonDigitContainer MuonRDO StoreGateLib TileEvent TrigT1Interfaces TGCTriggerCondSvcLib
                    PRIVATE_LINK_LIBRARIES AthenaPoolUtilities MuonCondInterface PathResolver TGCcablingInterfaceLib TrigConfL1Data )
 
 atlas_add_component( TrigT1TGC
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/LVL1TGCTrigger.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/LVL1TGCTrigger.h
index 3d6c2c24c50..d1ea6f7d076 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/LVL1TGCTrigger.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/LVL1TGCTrigger.h
@@ -26,6 +26,8 @@
 
 #include "StoreGate/ReadCondHandle.h"
 #include "MuonCondSvc/TGCTriggerData.h"
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
+
 
 #include "StoreGate/ReadHandleKey.h"
 #include "StoreGate/WriteHandleKey.h"
@@ -182,6 +184,7 @@ namespace LVL1TGCTrigger {
     SG::ReadHandleKey<TileMuonReceiverContainer> m_keyTileMu{this,"TileMuRcv_Input","TileMuRcvCnt","Location of TileMuonReceiverContainer"};
     SG::ReadHandleKey<Muon::NSW_TrigRawDataContainer> m_keyNSWTrigOut{this,"NSWTrigger_Input","NSWTRGRDO","Location of NSW_TrigRawDataContainer"};
     SG::ReadCondHandleKey<TGCTriggerData> m_readCondKey{this,"ReadCondKey","TGCTriggerData"};
+    SG::ReadCondHandleKey<TGCTriggerLUTs> m_readLUTs_CondKey{this,"ReadLUTCondKey","TGCTriggerLUTs"};
     SG::WriteHandleKey<LVL1MUONIF::Lvl1MuCTPIInput> m_muctpiKey{this, "MuctpiLocationTGC", "L1MuctpiStoreTGC", "Location of muctpi for Tgc"};
     SG::WriteHandleKey<LVL1MUONIF::Lvl1MuCTPIInputPhase1> m_muctpiPhase1Key{this, "MuctpiPhase1LocationTGC", "L1MuctpiStoreTGC", "Location of muctpiPhase1 for Tgc"};
 
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
index b98575446ba..9f529937444 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
@@ -11,6 +11,7 @@
 #include "TrigT1TGC/TGCConnectionPPToSB.h"
 
 #include "MuonCondSvc/TGCTriggerData.h"
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
 #include "StoreGate/ReadCondHandleKey.h"
 #include "AthenaBaseComps/AthMessaging.h"
 
@@ -39,6 +40,7 @@ class TGCDatabaseManager : public AthMessaging
   TGCDatabaseManager(TGCArguments*);
   TGCDatabaseManager(TGCArguments*,
                      const SG::ReadCondHandleKey<TGCTriggerData>& readCondKey,
+		     const SG::ReadCondHandleKey<TGCTriggerLUTs>& readLUTsCondKey,
                      const std::string& version, bool flag=false);
   virtual ~TGCDatabaseManager();
 
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCRPhiCoincidenceMap.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCRPhiCoincidenceMap.h
index 9f90891a7bc..578346e07b5 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCRPhiCoincidenceMap.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCRPhiCoincidenceMap.h
@@ -12,6 +12,7 @@
 #include "GaudiKernel/ToolHandle.h"
 #include "StoreGate/ReadCondHandleKey.h"
 #include "MuonCondSvc/TGCTriggerData.h"
+#include "TGCTriggerCondSvc/TGCTriggerLUTs.h"
 #include "TrigT1TGC/TGCArguments.h"
 
 namespace LVL1TGCTrigger {
@@ -35,6 +36,7 @@ public:
 
   TGCRPhiCoincidenceMap(TGCArguments*,
                         const SG::ReadCondHandleKey<TGCTriggerData>& readCondKey,
+			const SG::ReadCondHandleKey<TGCTriggerLUTs>& readLUTsCondKey,
                         const std::string& version,
                         int   sideId=0, int octantId=0);
 
@@ -88,6 +90,9 @@ private:
   TGCArguments* m_tgcArgs;
 
   const SG::ReadCondHandleKey<TGCTriggerData>& m_readCondKey;
+  const SG::ReadCondHandleKey<TGCTriggerLUTs>& m_readLUTsCondKey;
+
+
 };
 
 inline 
diff --git a/Trigger/TrigT1/TrigT1TGC/python/TrigT1TGCConfig.py b/Trigger/TrigT1/TrigT1TGC/python/TrigT1TGCConfig.py
index 0cde794b04b..75cbdd919d9 100755
--- a/Trigger/TrigT1/TrigT1TGC/python/TrigT1TGCConfig.py
+++ b/Trigger/TrigT1/TrigT1TGC/python/TrigT1TGCConfig.py
@@ -6,6 +6,8 @@ from AthenaCommon.GlobalFlags import globalflags
 from IOVDbSvc.CondDB import conddb
 from AthenaCommon.AlgSequence import AthSequencer
 from MuonCondSvc.MuonCondSvcConf import TGCTriggerDbAlg
+from TGCTriggerCondSvc.TGCTriggerCondSvcConf import TGCTriggerCondAlg
+from PathResolver import PathResolver
 
 class TrigT1TGCConfig (LVL1TGCTrigger__LVL1TGCTrigger):
     
@@ -47,5 +49,10 @@ if globalflags.DataSource() == 'geant4':
     conddb.addFolder("TGC_OFL","/TGC/TRIGGER/CW_BW",className='CondAttrListCollection')
     conddb.addFolder("TGC_OFL","/TGC/TRIGGER/CW_TILE",className='CondAttrListCollection')
 
+    # DB files will be put in COOOLDB
+    bwCW_Run3_filePath=PathResolver.FindCalibFile("TrigT1TGC_CW/BW/CW_BW_Run3.v01.db")
+    conddb.addFolder(bwCW_Run3_filePath,"/TGC/TRIGGER/CW_BW_RUN3 <tag>TgcTriggerCwBwRun3-01</tag>", className='CondAttrListCollection')
+
     condSequence = AthSequencer("AthCondSeq")
     condSequence += TGCTriggerDbAlg("TGCTriggerDbAlg")
+    condSequence += TGCTriggerCondAlg("TGCTriggerCondAlg")
diff --git a/Trigger/TrigT1/TrigT1TGC/src/LVL1TGCTrigger.cxx b/Trigger/TrigT1/TrigT1TGC/src/LVL1TGCTrigger.cxx
index 7c097722af7..19ed1d4826d 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/LVL1TGCTrigger.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/LVL1TGCTrigger.cxx
@@ -105,10 +105,22 @@ namespace LVL1TGCTrigger {
     m_tgcArgs.set_useRun3Config( m_useRun3Config.value() );
 
     // initialize to read condition DB key of TGCTriggerData
+    // initialize TGCDataBase
+    /*
+    if(!m_useRun3Config.value()){
+      ATH_CHECK( m_readCondKey.initialize() );
+      m_db = new TGCDatabaseManager(&m_tgcArgs, m_readLUTs_CondKey, m_VerCW);
+    }
+    else{
+      ATH_CHECK( m_readLUTs_CondKey.initialize() );
+      m_db = new TGCDatabaseManager(&m_tgcArgs, m_readLUTs_CondKey, m_VerCW);
+    }
+    */
+
     ATH_CHECK( m_readCondKey.initialize() );
-    
+    ATH_CHECK( m_readLUTs_CondKey.initialize() );
     // initialize TGCDataBase
-    m_db = new TGCDatabaseManager(&m_tgcArgs, m_readCondKey, m_VerCW);
+    m_db = new TGCDatabaseManager(&m_tgcArgs, m_readCondKey, m_readLUTs_CondKey, m_VerCW);
 
     // initialize the TGCcabling
     ATH_CHECK(getCabling());
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
index 84c16d76221..f407d9a4344 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
@@ -99,6 +99,7 @@ TGCDatabaseManager::TGCDatabaseManager(TGCArguments* tgcargs)
 
 TGCDatabaseManager::TGCDatabaseManager(TGCArguments* tgcargs,
 				       const SG::ReadCondHandleKey<TGCTriggerData>& readCondKey,
+				       const SG::ReadCondHandleKey<TGCTriggerLUTs>& readLUTsCondKey,
 				       const std::string& ver, bool )
  : AthMessaging(Athena::getMessageSvc(), "LVL1TGC::TGCDatabaseManager"),
    m_tgcArgs(tgcargs)
@@ -147,7 +148,7 @@ TGCDatabaseManager::TGCDatabaseManager(TGCArguments* tgcargs,
   // RPhi Coincidence Map
   for (int side=0; side<NumberOfSide; side +=1) {
     for (int oct=0; oct<NumberOfOctant; oct++) {
-      m_mapRphi[side][oct] = new TGCRPhiCoincidenceMap(tgcArgs(),readCondKey, ver_BW, side, oct);
+      m_mapRphi[side][oct] = new TGCRPhiCoincidenceMap(tgcArgs(),readCondKey,readLUTsCondKey, ver_BW, side, oct);
     }
   }
 
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMap.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMap.cxx
index 2f0c42930c0..d6835b704ae 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMap.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMap.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
 */
 
 #include <iostream>
@@ -73,8 +73,19 @@ int TGCRPhiCoincidenceMap::test_Run3(int octantId, int moduleId, int subsector,
   int phimod2 = (moduleId==2||moduleId==5||moduleId==8)&&(sector%2==1) ? 1 : 0;
   int addr=SUBSECTORADD(subsector, moduleId, phimod2,type);
 
-  std::map<int, std::map<int, std::map<int, char> > >::const_iterator it=m_mapDB_Run3.find(addr);
-  if (it==m_mapDB_Run3.end()) return false;
+  SG::ReadCondHandle<TGCTriggerLUTs> readHandle{m_readLUTsCondKey};
+  const TGCTriggerLUTs* readCdo{*readHandle};
+
+  std::map<int, std::map<int, std::map<int, char> > > readMap;
+
+  if (tgcArgs()->USE_CONDDB()){
+    readMap = readCdo->getReadMapBw(m_side, m_octant);
+  } else {
+    readMap = m_mapDB_Run3;
+  }
+
+  std::map<int, std::map<int, std::map<int, char> > >::const_iterator it=readMap.find(addr);
+  if (it==readMap.end()) return false;
 
   std::map<int, std::map<int, char> > mapR = it->second;
   std::map<int, std::map<int, char> >::const_iterator itR=mapR.find(dr);
@@ -96,6 +107,8 @@ int TGCRPhiCoincidenceMap::test_Run3(int octantId, int moduleId, int subsector,
 
 TGCRPhiCoincidenceMap::TGCRPhiCoincidenceMap(TGCArguments* tgcargs,
 					     const SG::ReadCondHandleKey<TGCTriggerData>& readCondKey,
+					     const SG::ReadCondHandleKey<TGCTriggerLUTs>& readLUTsCondKey,
+
                                              const std::string& version,
 					     int   sideId, int octantId)
   :m_numberOfDR(0), m_numberOfDPhi(0),
@@ -104,7 +117,8 @@ TGCRPhiCoincidenceMap::TGCRPhiCoincidenceMap(TGCArguments* tgcargs,
    m_octant(octantId),
    m_fullCW(false),
    m_tgcArgs(tgcargs),
-   m_readCondKey(readCondKey)
+   m_readCondKey(readCondKey),
+   m_readLUTsCondKey(readLUTsCondKey)
 {
   if (!tgcArgs()->USE_CONDDB()) {
     if(!tgcArgs()->useRun3Config()){
@@ -117,7 +131,6 @@ TGCRPhiCoincidenceMap::TGCRPhiCoincidenceMap(TGCArguments* tgcargs,
     else{
       this -> readMap_Run3();// read Coincidence Map for Run3 (15 thresholds)
     }
-
   } 
 }
 
@@ -195,6 +208,7 @@ TGCRPhiCoincidenceMap::~TGCRPhiCoincidenceMap()
 
 TGCRPhiCoincidenceMap::TGCRPhiCoincidenceMap(const TGCRPhiCoincidenceMap& right)
   : m_readCondKey(right.m_readCondKey)
+  , m_readLUTsCondKey(right.m_readLUTsCondKey)
 {
   m_numberOfDR=right.m_numberOfDR;
   m_numberOfDPhi=right.m_numberOfDPhi;
diff --git a/Trigger/TriggerCommon/TriggerJobOpts/python/Lvl1SimulationConfig.py b/Trigger/TriggerCommon/TriggerJobOpts/python/Lvl1SimulationConfig.py
index 87f82bf65fc..fcbf85cb1c4 100644
--- a/Trigger/TriggerCommon/TriggerJobOpts/python/Lvl1SimulationConfig.py
+++ b/Trigger/TriggerCommon/TriggerJobOpts/python/Lvl1SimulationConfig.py
@@ -168,10 +168,16 @@ def Lvl1SimulationSequence( flags = None ):
     conddb.addFolder("TGC_OFL", "/TGC/TRIGGER/CW_EIFI", className="CondAttrListCollection")
     conddb.addFolder("TGC_OFL", "/TGC/TRIGGER/CW_BW", className="CondAttrListCollection")
     conddb.addFolder("TGC_OFL", "/TGC/TRIGGER/CW_TILE", className="CondAttrListCollection")
+    #COOL DB will be used.
+    from PathResolver import PathResolver
+    bwCW_Run3_filePath=PathResolver.FindCalibFile("TrigT1TGC_CW/BW/CW_BW_Run3.v01.db")
+    conddb.addFolder(bwCW_Run3_filePath,"/TGC/TRIGGER/CW_BW_RUN3 <tag>TgcTriggerCwBwRun3-01</tag>", className='CondAttrListCollection')
+
     condSeq = AthSequencer("AthCondSeq")
     from MuonCondSvc.MuonCondSvcConf import TGCTriggerDbAlg
     condSeq += TGCTriggerDbAlg()
-
+    from TGCTriggerCondSvc.TGCTriggerCondSvcConf import TGCTriggerCondAlg
+    condSeq += TGCTriggerCondAlg()
     ##################################################
     # Topo
     ##################################################
-- 
GitLab