From 148e7a7da2f2c9e8e61c7c0284f4983bf9f27561 Mon Sep 17 00:00:00 2001
From: Alexandru Bercuci <abercuci@niham.nipne.ro>
Date: Mon, 12 Jun 2023 15:39:51 +0300
Subject: [PATCH] re-introduce the equipment map, add channel mask info to
 internal parameters Update from MR 1198

---
 algo/detectors/trd2d/Trd2dReadoutConfig.cxx | 25 ++++++++++++++++-----
 algo/detectors/trd2d/Trd2dReadoutConfig.h   |  5 ++++-
 algo/detectors/trd2d/UnpackTrd2d.cxx        |  4 ++--
 algo/detectors/trd2d/UnpackTrd2d.h          |  2 +-
 4 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/algo/detectors/trd2d/Trd2dReadoutConfig.cxx b/algo/detectors/trd2d/Trd2dReadoutConfig.cxx
index 3a7d04e7fe..e5ffe41d97 100644
--- a/algo/detectors/trd2d/Trd2dReadoutConfig.cxx
+++ b/algo/detectors/trd2d/Trd2dReadoutConfig.cxx
@@ -62,11 +62,27 @@ namespace cbm::algo
   }
   // ------------------------------------------------------------------------------------
 
+  // ---  Initialise the component mapping structure   ----------------------------------
+  void Trd2dReadoutConfig::InitComponentMap(const std::map<uint32_t, uint16_t[NCROBMOD]>& map)
+  {
+    // Receive map (moduleId, crobId) -> (equipId)
+    // Invert to obtain component map (equipId) -> (module iq, crob id)
+    for (auto& entry : map) {
+      uint16_t mod_id = entry.first;
+      for (uint8_t crob_id = 0; crob_id < NCROBMOD; crob_id++) {
+        uint16_t eq_id     = entry.second[crob_id];
+        if (!eq_id) continue;
+        fReadoutMap[eq_id] = std::make_pair(mod_id, crob_id);
+      }
+    }
+  }
+  // ------------------------------------------------------------------------------------
+
   // ---  Initialise the mapping structure   --------------------------------------------
   void Trd2dReadoutConfig::InitChannelMap(
     const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, uint64_t>>>>& channelMap)
   {
-    // Constructing the map (equipId, asicId, chanId) -> (pad address, R pairing flag, daq offset)
+    // Constructing the map (equipId, asicId, chanId) -> (pad address, mask flag, daq offset)
     for (auto compMap : channelMap) {
       uint16_t equipmentId = compMap.first;
       uint16_t numAsics    = compMap.second.size();
@@ -76,7 +92,6 @@ namespace cbm::algo
         uint16_t asicId   = asicMap.first;
         uint16_t numChans = asicMap.second.size();
         fChannelMap[equipmentId][asicId].resize(numChans);
-
         for (auto chanMap : asicMap.second) {
           uint16_t chanId                              = chanMap.first;
           std::tuple<int32_t, bool, uint64_t> chanPars = chanMap.second;
@@ -88,7 +103,7 @@ namespace cbm::algo
   // ------------------------------------------------------------------------------------
 
 
-  // ---  Mapping (equimentId, asicId, channel) -> (pad address, R pairing flag, daq offset)  -----
+  // ---  Mapping (equimentId, asicId, channel) -> (pad address, mask flag, daq offset)  -----
   std::tuple<int32_t, bool, uint64_t> Trd2dReadoutConfig::ChanMap(uint16_t equipId, uint16_t asic, uint16_t chan)
   {
     std::tuple<int32_t, bool, uint64_t> result = std::make_tuple(-1, false, 0);
@@ -138,10 +153,10 @@ namespace cbm::algo
         for (size_t chanId = 0; chanId < numChans; chanId++) {
           auto entry         = asicMap.second.at(asicId).at(chanId);
           int32_t address    = std::get<0>(entry);
-          bool hasPairingR   = std::get<1>(entry);
+          bool isMasked      = std::get<1>(entry);
           uint64_t daqOffset = std::get<2>(entry);
           ss << "\n Equipment " << equipmentId << " AsicId " << asicId << " chanID " << chanId << " pad address "
-             << address << " pairingR " << hasPairingR << " daq offset " << daqOffset;
+             << address << " mask " << isMasked << " daq offset " << daqOffset;
         }
       }
     }
diff --git a/algo/detectors/trd2d/Trd2dReadoutConfig.h b/algo/detectors/trd2d/Trd2dReadoutConfig.h
index e449fabb58..4330bc3a9b 100644
--- a/algo/detectors/trd2d/Trd2dReadoutConfig.h
+++ b/algo/detectors/trd2d/Trd2dReadoutConfig.h
@@ -80,6 +80,9 @@ namespace cbm::algo
     /** @brief Debug output of readout map **/
     std::string PrintReadoutMap();
 
+    /** @brief Initialisation of readout map **/
+    void InitComponentMap(const std::map<uint32_t, uint16_t[NCROBMOD]>& crob_map);
+
     /** @brief Initialisation of channel map **/
     void InitChannelMap(
       const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, uint64_t>>>>& channelMap);
@@ -90,7 +93,7 @@ namespace cbm::algo
     std::map<uint16_t, std::pair<uint16_t, uint8_t>> fReadoutMap = {};  //!
 
     // --- TRD2D channel map
-    // --- Map index: (equipment, asic, chan), map value: (pad address, R pairing flag, daq offset)
+    // --- Map index: (equipment, asic, chan), map value: (pad address, mask flag, daq offset)
     std::map<uint16_t, std::vector<std::vector<std::tuple<int32_t, bool, uint64_t>>>> fChannelMap = {};  //!
   };
 
diff --git a/algo/detectors/trd2d/UnpackTrd2d.cxx b/algo/detectors/trd2d/UnpackTrd2d.cxx
index a1eb4ec427..c23e1bc565 100644
--- a/algo/detectors/trd2d/UnpackTrd2d.cxx
+++ b/algo/detectors/trd2d/UnpackTrd2d.cxx
@@ -100,8 +100,8 @@ namespace cbm::algo
     const uint64_t tdaqOffset         = asicPar.fChanParams[messes[0].ch].fDaqOffset;
 
     for (auto imess : messes) {
-      const int32_t pad                   = asicPar.fChanParams[imess.ch].fPadAddress;
-      const bool hasPairingR              = asicPar.fChanParams[imess.ch].fHasPairingR;
+      const int32_t pad                   = std::abs(asicPar.fChanParams[imess.ch].fPadAddress);
+      const bool hasPairingR              = bool(asicPar.fChanParams[imess.ch].fPadAddress > 0);
       const uint64_t lTime                = time + tdaqOffset + imess.tlab;
       const uint16_t lchR                 = hasPairingR ? imess.data : 0;
       const uint16_t lchT                 = hasPairingR ? 0 : imess.data;
diff --git a/algo/detectors/trd2d/UnpackTrd2d.h b/algo/detectors/trd2d/UnpackTrd2d.h
index 9cb8a3580f..5381e067ac 100644
--- a/algo/detectors/trd2d/UnpackTrd2d.h
+++ b/algo/detectors/trd2d/UnpackTrd2d.h
@@ -54,7 +54,7 @@ namespace cbm::algo
    **/
   struct UnpackTrd2dChannelPar {
     int32_t fPadAddress;      ///< Pad address for channel
-    bool fHasPairingR;        ///< Flag for R or T compoment
+    bool fMask;               ///< Flag for channel masking
     uint64_t fDaqOffset = 0;  ///< Time calibration parameter
   };
 
-- 
GitLab