From bf56eb6f64ca45afd8883c73767a23e5e0c44c81 Mon Sep 17 00:00:00 2001
From: Takahiro Shiomi <takahiro.shiomi@cern.ch>
Date: Fri, 9 Oct 2020 08:35:47 +0200
Subject: [PATCH] GoodMF flag implementation, Make class TGCGoodMF

---
 .../TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h  |   9 ++
 .../TrigT1/TrigT1TGC/TrigT1TGC/TGCGoodMF.h    |  57 ++++++++
 .../TrigT1/TrigT1TGC/TrigT1TGC/TGCSector.h    |   2 +
 .../TrigT1TGC/TrigT1TGC/TGCSectorLogic.h      |   4 +
 .../TrigT1TGC/src/TGCDatabaseManager.cxx      |   6 +-
 Trigger/TrigT1/TrigT1TGC/src/TGCGoodMF.cxx    | 125 ++++++++++++++++++
 .../src/TGCRPhiCoincidenceMatrix.cxx          |   3 -
 Trigger/TrigT1/TrigT1TGC/src/TGCSector.cxx    |   8 +-
 .../TrigT1/TrigT1TGC/src/TGCSectorLogic.cxx   |  19 +++
 9 files changed, 228 insertions(+), 5 deletions(-)
 create mode 100644 Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCGoodMF.h
 create mode 100644 Trigger/TrigT1/TrigT1TGC/src/TGCGoodMF.cxx

diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
index 1bc630385bc2..caaf9db446a9 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCDatabaseManager.h
@@ -28,6 +28,7 @@ class TGCRPhiCoincidenceMap;
 class TGCEIFICoincidenceMap;
 class TGCTileMuCoincidenceMap;
 class TGCNSWCoincidenceMap;
+class TGCGoodMF;
 
 class TGCDatabaseManager : public AthMessaging
 {
@@ -50,6 +51,7 @@ class TGCDatabaseManager : public AthMessaging
   const TGCEIFICoincidenceMap* getEIFICoincidenceMap(int sideId) const;
   const TGCTileMuCoincidenceMap* getTileMuCoincidenceMap() const;
   std::shared_ptr<TGCNSWCoincidenceMap> getNSWCoincidenceMap(int sideId, int octantId, int moduleId) const;
+  TGCGoodMF* getGoodMFMap() const;
 
   TGCConnectionInPP* getConnectionInPP(TGCPatchPanel* patchPanel) const;
   void addConnectionInPP(const TGCPatchPanel* patchPanel, const TGCConnectionInPP* connectionInPP);
@@ -71,6 +73,7 @@ class TGCDatabaseManager : public AthMessaging
   std::array<std::array<std::array<std::shared_ptr<TGCNSWCoincidenceMap>, NumberOfModuleInBW>, NumberOfOctant>, NumberOfSide> m_mapNSW;
   TGCConnectionPPToSL* m_PPToSL[NumberOfRegionType];
   TGCConnectionASDToPP* m_ASDToPP[NumberOfRegionType][NumberOfPatchPanelType][TotalNumForwardBackwardType];
+  TGCGoodMF* m_mapGoodMF;
 
   std::map<PatchPanelIDs, std::pair<const TGCConnectionInPP, PatchPanelPointers> > m_patchPanelToConnectionInPP;
   
@@ -101,6 +104,12 @@ std::shared_ptr<TGCNSWCoincidenceMap> TGCDatabaseManager::getNSWCoincidenceMap(i
   return m_mapNSW[sideId][octantId][moduleId];
 }
 
+inline
+TGCGoodMF* TGCDatabaseManager::getGoodMFMap() const
+{
+      return m_mapGoodMF;
+}
+
 inline 
  const TGCConnectionPPToSL* TGCDatabaseManager::getConnectionPPToSL(TGCRegionType type) const
 {
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCGoodMF.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCGoodMF.h
new file mode 100644
index 000000000000..22a89103a88e
--- /dev/null
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCGoodMF.h
@@ -0,0 +1,57 @@
+/*
+  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
+*/
+
+// ====================================================================
+/*
+        TGCGoodMF.h
+*/
+// ====================================================================
+#ifndef TGCGoodMF_h
+#define TGCGoodMF_h
+
+#include "GaudiKernel/ToolHandle.h"
+#include "StoreGate/ReadCondHandleKey.h"
+
+#include "AthenaKernel/MsgStreamMember.h"
+#include "GaudiKernel/MsgStream.h"
+
+#include <map>
+
+namespace LVL1TGCTrigger {
+
+class TGCArguments;
+
+// ====================================================================
+//
+// class definition
+//
+// ====================================================================
+
+class TGCGoodMF {
+public:
+  TGCGoodMF(TGCArguments*);
+  ~TGCGoodMF();
+
+  TGCGoodMF(const TGCGoodMF& right);
+  const TGCGoodMF& operator=(const TGCGoodMF& right);
+
+  bool test_GoodMF(int moduleId, int sscId, int subsector) const;
+  bool readBadMFList();
+
+  TGCArguments* tgcArgs() const;
+
+private:
+  std::map<int, std::map<int,int> > m_mapisgoodMF; //Number of moduleId
+  TGCArguments* m_tgcArgs;
+
+};
+
+inline 
+TGCArguments* TGCGoodMF::tgcArgs() const {
+  return m_tgcArgs;
+}
+
+} //end of namespace bracket
+
+#endif // TGCGoodMF_h
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSector.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSector.h
index c70d46e97670..cf31717118ec 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSector.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSector.h
@@ -31,6 +31,7 @@
 #include "TrigT1TGC/TGCSectorLogic.h"
 #include "TrigT1TGC/TGCTMDB.h"
 #include "TrigT1TGC/TGCNSW.h"
+#include "TrigT1TGC/TGCGoodMF.h"
 
 namespace LVL1TGCTrigger {
 
@@ -94,6 +95,7 @@ private:
 		  const TGCEIFICoincidenceMap* mapI=0);
   void setTileMuMap(const TGCTileMuCoincidenceMap* map);
   void setNSWMap(std::shared_ptr<const TGCNSWCoincidenceMap> mapNSW);
+  void setGoodMFMap(const TGCGoodMF* mapGoodMF);
   void connectPPToSB(const TGCConnectionPPToSB* connection);
   void connectSBToHPB(const TGCConnectionSBToHPB* connection);
   void connectHPBToSL(const TGCConnectionHPBToSL* connection);
diff --git a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSectorLogic.h b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSectorLogic.h
index 54a8d15c3a26..c919c9c0ffd2 100644
--- a/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSectorLogic.h
+++ b/Trigger/TrigT1/TrigT1TGC/TrigT1TGC/TGCSectorLogic.h
@@ -38,6 +38,7 @@ class  TGCHighPtChipOut;
 class  TGCTMDB;
 class  TGCNSW;
 class  TGCNSWCoincidenceMap;
+class  TGCGoodMF;
 
 //for Run3
 class TGCTrackSelectorOut;
@@ -78,6 +79,7 @@ public:
 		    const TGCTileMuCoincidenceMap* mapTM);
   void setNSWMap(std::shared_ptr<const TGCNSW> nsw,
 		 std::shared_ptr<const TGCNSWCoincidenceMap> mapNSW);
+  void setGoodMFMap(const TGCGoodMF* mapGoodMF);
   void showResult(TGCSLSelectorOut* out);
  
   TGCSectorLogic(TGCArguments*, TGCRegionType regionIn, int id);
@@ -118,6 +120,7 @@ private:
   int m_sideId, m_octantId;
   TGCRegionType m_region;
   int  m_NumberOfWireHighPtBoard;
+  bool m_useGoodMF;
 
   TGCSSCController m_SSCController;
   TGCRPhiCoincidenceMatrix m_matrix;
@@ -126,6 +129,7 @@ private:
   const TGCTMDB*            m_pTMDB;
   std::shared_ptr<const TGCNSW>             m_nsw;
   std::shared_ptr<const TGCNSWCoincidenceMap> m_mapNSW;
+  const TGCGoodMF*            m_mapGoodMF;
 
   // for Run2
   TGCSLPreSelector m_preSelector; 
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
index 753eabd811cf..1f773af7e557 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCDatabaseManager.cxx
@@ -8,6 +8,7 @@
 #include "TrigT1TGC/TGCEIFICoincidenceMap.h"
 #include "TrigT1TGC/TGCTileMuCoincidenceMap.h"
 #include "TrigT1TGC/TGCNSWCoincidenceMap.h"
+#include "TrigT1TGC/TGCGoodMF.h"
 #include "TrigT1TGC/TGCConnectionASDToPP.h"
 #include "TrigT1TGC/TGCConnectionInPP.h"
 #include "TrigT1TGC/TGCPatchPanel.h"
@@ -167,7 +168,10 @@ TGCDatabaseManager::TGCDatabaseManager(TGCArguments* tgcargs,
     }
   }
 
-  }  
+  }
+  if(tgcArgs()->useRun3Config()){
+    m_mapGoodMF = new TGCGoodMF(tgcArgs());
+  }
 }
 
 void TGCDatabaseManager::deleteConnectionPPToSL()
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCGoodMF.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCGoodMF.cxx
new file mode 100644
index 000000000000..145e646fef8f
--- /dev/null
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCGoodMF.cxx
@@ -0,0 +1,125 @@
+/*
+   Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration 
+*/
+
+// ====================================================================
+/*
+        TGCGoodMF.cc            
+*/
+// ====================================================================
+
+#include "TrigT1TGC/TGCGoodMF.h"
+#include "PathResolver/PathResolver.h"
+
+#include "MuonCondInterface/ITGCTriggerDbTool.h"
+
+#include "GaudiKernel/ISvcLocator.h"
+#include "GaudiKernel/Bootstrap.h"
+#include "GaudiKernel/MsgStream.h"
+#include "GaudiKernel/IMessageSvc.h"
+
+#include "TrigT1TGC/TGCArguments.h"
+
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <iomanip>
+
+namespace LVL1TGCTrigger {
+
+// ====================================================================
+//
+// class description
+//
+// ====================================================================
+
+TGCGoodMF::TGCGoodMF(TGCArguments* tgcargs)
+  :m_tgcArgs(tgcargs)
+{
+  if(tgcArgs()->useRun3Config()){
+    this->readBadMFList();
+  }
+}
+
+TGCGoodMF::~TGCGoodMF()
+{
+}
+
+TGCGoodMF::TGCGoodMF(const TGCGoodMF& right)
+{
+  m_tgcArgs=right.m_tgcArgs;
+  if(tgcArgs()->useRun3Config()){
+    this->readBadMFList();
+  }
+}
+
+const TGCGoodMF& TGCGoodMF::operator=(const TGCGoodMF& right)
+{
+  if(this != &right){
+    m_tgcArgs=right.m_tgcArgs;
+    if(tgcArgs()->useRun3Config()){
+      this->readBadMFList();
+    }
+  }
+
+  return *this;
+}
+
+bool TGCGoodMF::readBadMFList()
+{
+  //initialize
+  std::string buf;
+  std::string fullName, tag;    char delimiter = '\n';
+  std::string fn = "HotRoI.v1.db";
+
+  fullName = PathResolver::find_file( fn.c_str(), "DATAPATH" );
+  int mod,maxssc;
+  std::ifstream file(fullName.c_str(),std::ios::in); 
+  while(getline(file,buf,delimiter)) {
+    std::istringstream header(buf);
+    header>>tag;
+    if(tag == "#"){ // read header part.
+      header>>mod>>maxssc;
+
+      // get BadMF list
+      getline(file,buf,delimiter);
+      std::istringstream cont(buf);
+      std::map<int,int> mapssc; // <SSCId,badMFId>
+      int badMFId;
+      for(int i=0; i<=maxssc; i++) {
+        cont>>badMFId;
+        if(badMFId < 0) continue;
+        mapssc[i] = badMFId;
+      }
+      m_mapisgoodMF[mod] = mapssc; // <ModuleId,<~>>
+    }
+  }
+  return true;
+}
+
+bool TGCGoodMF::test_GoodMF(int moduleId,int sscId,int RoI) const
+{
+  std::map<int, std::map<int, int> >::const_iterator itModule=m_mapisgoodMF.find(moduleId);
+  if (itModule==m_mapisgoodMF.end()) return true;
+
+  std::map<int, int> mapssc = itModule->second;
+  std::map<int, int>::const_iterator itSSC=mapssc.find(sscId);
+  if (itSSC==mapssc.end()) return true;
+
+  int badMFId = itSSC->second;
+  if (badMFId==0){ return true; }
+
+  enum {N_RoIofSSC=8};  
+  std::bitset<N_RoIofSSC> bs(badMFId);
+
+  int RoIId;
+  if(RoI%N_RoIofSSC >= 4){ RoIId = RoI%N_RoIofSSC-4; }
+  else{ RoIId = RoI%N_RoIofSSC+4; }
+  
+  if(bs[RoIId]){return false;}
+  else return true;
+}
+
+
+} //end of namespace bracket
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMatrix.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMatrix.cxx
index eacfe21ae7dc..b52714bd86b9 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMatrix.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCRPhiCoincidenceMatrix.cxx
@@ -73,7 +73,6 @@ TGCRPhiCoincidenceOut* TGCRPhiCoincidenceMatrix::doCoincidence()
     int ptOut = -99;
     int chargeOut = 2;
     int CoincidenceTypeOut=-1;
-    bool isgoodMFOut=false;
 
     if(m_sectorLogic->getRegion()==Endcap){
       subsector = 4*(2*m_SSCId+m_r-1)+m_phi[j];
@@ -90,7 +89,6 @@ TGCRPhiCoincidenceOut* TGCRPhiCoincidenceMatrix::doCoincidence()
                             subsector,type,m_dR,m_dPhi[j]); // this function will be implemented. 
       ptOut = std::abs(pt);
       chargeOut = pt<0 ? 0:1;
-      //isgoodMFOut : will be set.
       
       CoincidenceTypeOut=(type==0);
     }
@@ -119,7 +117,6 @@ TGCRPhiCoincidenceOut* TGCRPhiCoincidenceMatrix::doCoincidence()
       out->setRoI(subsector);
       out->setCharge(chargeOut);
       out->setCoincidenceType(CoincidenceTypeOut);
-      out->setGoodMFFlag(isgoodMFOut);
       j0 = j;
     }
   }
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCSector.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCSector.cxx
index f21a634ca83e..0cdeb71c77ba 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCSector.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCSector.cxx
@@ -147,6 +147,9 @@ TGCSector::TGCSector(TGCArguments* tgcargs)
     std::shared_ptr<const TGCNSWCoincidenceMap> mapNSW = db->getNSWCoincidenceMap(m_sideId, m_octantId,m_moduleId);
     setNSWMap(mapNSW);
 
+    const TGCGoodMF* mapGoodMF = db->getGoodMFMap();
+    setGoodMFMap(mapGoodMF);
+
   }
 
   // set connection between boards;
@@ -306,7 +309,10 @@ void TGCSector::setNSWMap(std::shared_ptr<const TGCNSWCoincidenceMap>  mapNSW)
   if (m_SL) m_SL->setNSWMap(m_NSW, mapNSW);
 }
 
-
+void TGCSector::setGoodMFMap(const TGCGoodMF* mapGoodMF)
+{
+  if (m_SL) m_SL->setGoodMFMap(mapGoodMF);
+}
 
 
 void TGCSector::connectPPToSB(const TGCConnectionPPToSB* connection)
diff --git a/Trigger/TrigT1/TrigT1TGC/src/TGCSectorLogic.cxx b/Trigger/TrigT1/TrigT1TGC/src/TGCSectorLogic.cxx
index 16655c5b6738..3528a552a216 100644
--- a/Trigger/TrigT1/TrigT1TGC/src/TGCSectorLogic.cxx
+++ b/Trigger/TrigT1/TrigT1TGC/src/TGCSectorLogic.cxx
@@ -13,6 +13,7 @@
 #include "TrigT1TGC/TGCNSW.h"
 #include "TrigT1TGC/NSWTrigOut.h"
 #include "TrigT1TGC/TGCNSWCoincidenceMap.h"
+#include "TrigT1TGC/TGCGoodMF.h"
 
 #include "StoreGate/ReadCondHandle.h"
 #include "MuonCondSvc/TGCTriggerData.h"
@@ -46,6 +47,7 @@ namespace LVL1TGCTrigger {
     m_stripHighPtChipOut(0),
     m_useEIFI(false),
     m_useTileMu(false),
+    m_useGoodMF(false),
     m_tgcArgs(tgcargs)
 {
   m_sideId = (idIn/NumberOfModule)/NumberOfOctant;
@@ -111,6 +113,12 @@ void TGCSectorLogic::setTileMuMap(const TGCTMDB* tmdb,
   if( m_nsw == 0 || m_mapNSW == 0){tgcArgs()->set_USE_NSW(false);}
 }
 
+void TGCSectorLogic::setGoodMFMap(const TGCGoodMF* mapGoodMF)
+{
+  m_mapGoodMF = mapGoodMF;
+  if (m_mapGoodMF !=0){m_useGoodMF = true;}
+  else{m_useGoodMF = false;}
+}
 
 void TGCSectorLogic::setWireHighPtBoard(int port, TGCHighPtBoard* highPtBoard)
 {
@@ -192,6 +200,17 @@ void TGCSectorLogic::clockIn(const SG::ReadCondHandleKey<TGCTriggerData> readCon
 	}
       }
     }
+
+    if(tgcArgs()->useRun3Config()){
+      if(coincidenceOut){
+        if (m_useGoodMF){
+          bool isgoodMF;
+          isgoodMF = m_mapGoodMF->test_GoodMF(m_moduleId,SSCid,coincidenceOut->getRoI());
+          coincidenceOut->setGoodMFFlag(isgoodMF);
+        }
+      }
+    }
+
     ////////////////////////////////////////////
     // do coincidence with Inner Tracklet of EIFI and/or TileMu
 
-- 
GitLab