From 41b118999b11158c58c1fbe285044f0e61e2ad3b 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

---
 algo/detectors/trd2d/Trd2dReadoutConfig.cxx | 25 +++++++++++++----
 algo/detectors/trd2d/Trd2dReadoutConfig.h   |  5 +++-
 algo/detectors/trd2d/UnpackTrd2d.cxx        |  4 +--
 algo/detectors/trd2d/UnpackTrd2d.h          |  2 +-
 reco/tasks/CbmTaskUnpack.cxx                | 31 ++++++++++++---------
 5 files changed, 45 insertions(+), 22 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 e6709b4a00..18d8494c6b 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 25cc2886c2..98e6895608 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
   };
 
diff --git a/reco/tasks/CbmTaskUnpack.cxx b/reco/tasks/CbmTaskUnpack.cxx
index fa3887566d..de6c25b14c 100644
--- a/reco/tasks/CbmTaskUnpack.cxx
+++ b/reco/tasks/CbmTaskUnpack.cxx
@@ -454,10 +454,10 @@ InitStatus CbmTaskUnpack::Init()
 
       for (size_t chan = 0; chan < numChans; chan++) {
         UnpackTrd2dChannelPar chanPar;
-        auto pars            = fTrd2dConfig.ChanMap(equip, asic, chan);
-        chanPar.fPadAddress  = std::get<0>(pars);  // Pad address for channel
-        chanPar.fHasPairingR = std::get<1>(pars);  // Flag for R or T compoment
-        chanPar.fDaqOffset   = std::get<2>(pars);  // Time calibration parameter
+        auto pars           = fTrd2dConfig.ChanMap(equip, asic, chan);
+        chanPar.fPadAddress = std::get<0>(pars);  // Pad address for channel
+        chanPar.fMask       = std::get<1>(pars);  // Flag channel mask
+        chanPar.fDaqOffset  = std::get<2>(pars);  // Time calibration parameter
         asicPar.fChanParams.push_back(chanPar);
       }
       auto comppars = fTrd2dConfig.CompMap(equip);
@@ -499,7 +499,9 @@ void CbmTaskUnpack::InitTrd2dReadoutConfig()
   if (asciiInput.open(asicparfile.data())) { asicparset.init(&asciiInput); }
   asciiInput.close();
 
-  // Map (equipId, asicId, chanId) -> (pad address, R pairing flag, daq offset)
+  // Map (moduleId) -> (array of crobId)
+  std::map<uint32_t, uint16_t[NCROBMOD]> crobMap;
+  // Map (equipId, asicId, chanId) -> (pad address, mask flag, daq offset [FASP clk])
   std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, uint64_t>>>> channelMap;
 
   // Loop through a list of module IDs from the .digi file (can in principle contradict crob_map).
@@ -513,33 +515,36 @@ void CbmTaskUnpack::InitTrd2dReadoutConfig()
     if (setDet->GetAsicType() != CbmTrdDigi::eCbmTrdAsicType::kFASP) continue;
     auto digipar = entry.second;
 
-    int crob_map[NCROBMOD];
-    memcpy(crob_map, setDet->GetCrobAddresses(), NCROBMOD * sizeof(int));
+    const int* crobs = setDet->GetCrobAddresses();
+    for (int icrob(0); icrob < NCROBMOD; icrob++)
+      crobMap[moduleId][icrob] = crobs[icrob];
+
     // Loop through ASICs for this module
     std::vector<int32_t> addresses;
     setDet->GetAsicAddresses(&addresses);
     for (auto add : addresses) {
-
       //Get local IDs for this component / equipment.
       const int32_t fasp_in_mod = add - 1000 * moduleId;
       const int32_t fasp_in_eq  = fasp_in_mod % (NFASPCROB);
       const int32_t crob_in_mod = fasp_in_mod / (NFASPCROB);
-      const uint16_t eq_id      = crob_map[crob_in_mod];
+      const uint16_t eq_id      = crobMap[moduleId][crob_in_mod];
 
       // ASIC parameter set
       CbmTrdParFasp* fasppar = (CbmTrdParFasp*) setDet->GetAsicPar(add);
 
       // Loop through channels for this ASIC and fill map
       for (int chan = 0; chan < fasppar->GetNchannels(); chan++) {
-        const int32_t pad              = fasppar->GetPadAddress(chan);
-        const bool hasPairingR         = fasppar->GetChannel(chan)->HasPairingR();
-        uint64_t daq_offset            = 0;
+        const CbmTrdParFaspChannel* faspch = fasppar->GetChannel(chan);
+        const int32_t pad                  = fasppar->GetPadAddress(chan) * (faspch->HasPairingR() ? 1 : -1);
+        const bool isMasked                = faspch->IsMasked();
+        uint64_t daq_offset                = 0;
         if (((CbmTrdParModDigi*) digipar)->GetPadRow(pad) % 2 == 0) daq_offset = 3;
-        channelMap[eq_id][fasp_in_eq][chan] = std::make_tuple(pad, hasPairingR, daq_offset);
+        channelMap[eq_id][fasp_in_eq][chan] = std::make_tuple(pad, isMasked, daq_offset);
       }
     }
   }
 
+  fTrd2dConfig.InitComponentMap(crobMap);
   fTrd2dConfig.InitChannelMap(channelMap);
 }
 
-- 
GitLab