From 2d9fec27b390b65d986ecc1811b0a9c3edb13182 Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Tue, 4 Jul 2023 15:41:34 +0200
Subject: [PATCH] Implemented channel masking (by address) for
 cbm::algo::UnpackMuch.

---
 algo/detectors/much/MuchReadoutConfig.cxx | 47 ++++++++++++++++++++++-
 algo/detectors/much/MuchReadoutConfig.h   | 11 ++++++
 algo/detectors/much/UnpackMuch.cxx        |  5 ++-
 algo/detectors/much/UnpackMuch.h          |  1 +
 algo/unpack/Unpack.cxx                    |  1 +
 5 files changed, 62 insertions(+), 3 deletions(-)

diff --git a/algo/detectors/much/MuchReadoutConfig.cxx b/algo/detectors/much/MuchReadoutConfig.cxx
index 4a2ae11522..813d1d4c72 100644
--- a/algo/detectors/much/MuchReadoutConfig.cxx
+++ b/algo/detectors/much/MuchReadoutConfig.cxx
@@ -7,6 +7,7 @@
 #include "CbmMuchAddress.h"
 
 #include <bitset>
+#include <unordered_set>
 
 #include "AlgoFairloggerCompat.h"
 
@@ -42,7 +43,6 @@ namespace cbm::algo
   }
   // ------------------------------------------------------------------------------------
 
-
   // ---  Mapping (equimentId, elink) -> address[channel]  ------------------------------
   std::vector<uint32_t> MuchReadoutConfig::Map(uint16_t equipmentId, uint16_t elinkId)
   {
@@ -55,6 +55,20 @@ namespace cbm::algo
   }
   // ------------------------------------------------------------------------------------
 
+  // ---  Mapping (equimentId, elink) -> (vector of mask flags)  ------------------------
+  std::vector<bool> MuchReadoutConfig::MaskMap(uint16_t equipmentId, uint16_t elinkId)
+  {
+    std::vector<bool> result;
+    auto equipIter = fMaskMap.find(equipmentId);
+    if (equipIter != fMaskMap.end()) {
+      auto elinkMap  = equipIter->second;
+      auto elinkIter = elinkMap.find(elinkId);
+      if (elinkIter != elinkMap.end()) { result = elinkIter->second; }
+    }
+    return result;
+  }
+  // ------------------------------------------------------------------------------------
+
 
   void MuchReadoutConfig::Init()
   {
@@ -84,6 +98,27 @@ namespace cbm::algo
     // FEB IDs connected to RPC Module
     fnFebsIdsArrayRpc = {3, -1, -1, -1, 0, 1, -1, -1, 2};  //Id for the connected FEB Position in GEM Module B
 
+    // Addresses of masked channels, taken from mMuChNoisyChannelMarch2022.txt
+    std::unordered_set<uint32_t> maskedaddr = {
+      44302340,  1703940,   27066372,  1343492,   1310724,   177963012, 161251332, 185991172, 186056708, 186253316,
+      186417156, 144670724, 186286084, 153026564, 178159620, 144703492, 144736260, 26869764,  1933316,   10092548,
+      59736068,  35487748,  51970068,  43941892,  35586052,  59801604,  51314692,  52068356,  18481156,  144801796,
+      145162244, 144769028, 161644548, 153223172, 51445764,  68026372,  51347460,  51412996,  59834372,  59867140,
+      44171268,  85622804,  161611780, 59408420,  161218564, 153354244, 153157636, 161742852, 186220548, 94273540,
+      51281924,  51478532,  59670532,  68124676,  59768836,  68059140,  59899908,  59637764,  169410564, 153092100,
+      177831940, 186777604, 144637956, 161710084, 153059332, 161447940, 51249156,  68091908,  59703300,  186187780,
+      177897476, 9207844,   43745284,  27000836,  9109508,   884740,    1867780,   17825796,  1802244,   18972676,
+      27328516,  1671172,   1376260,   42860548,  1409028,   9666564,   1605636,   51544068,  59965444,  161546244,
+      1572868,   1441796,   60293124,  1507332,   9699332,   1474564,   169672708, 161284100, 186548228, 178094084,
+      186449924, 177930244, 153190404, 186384388, 153518084, 153255940, 169607172, 178028548, 186482692, 161677316,
+      153124868, 153485316, 145063940, 186318852, 153288708, 186515460, 178192388, 9764868,   9797636,   60227588,
+      51740676,  51773444,  169508868, 169476100, 177995780, 169639940, 178126852, 145031172, 169443332, 161349636,
+      145391620, 178061316, 186351620, 153321476, 153550852, 145096708, 153387012, 186810372, 178520068, 186646532,
+      186843140, 169541636, 177864708, 161480708, 178356228, 186875908, 145129476, 169902084, 169967620, 186580996,
+      178225156, 170000388, 186679300, 169836548, 186613764, 60260356,  170164228, 170033156, 9732100,   60194820,
+      18022404,  51806212,  85622788,  917508,    51838980,  60129300,  52035604,  52068372,  60162068,  52101140,
+      118620180, 118489108, 126910484, 60358676,  169869316, 161775620, 786468,    25821220};
+
     // Init arrays which store positions of PADs
     InitChannelToPadMaps();
 
@@ -105,7 +140,15 @@ namespace cbm::algo
 
           for (uint16_t chanInAsic = 0; chanInAsic < numChanPerAsic; chanInAsic++) {
             uint32_t channel                       = chanInAsic + numChanPerAsic * (feb % numAsicsPerFeb);  //Fix this!
-            fReadoutMap[equipment][elink][channel] = CreateMuchAddress(comp, febId, channel);
+            uint32_t address                       = CreateMuchAddress(comp, febId, channel);
+            fReadoutMap[equipment][elink][channel] = address;
+
+            // Apply channel mask
+            if (maskedaddr.count(address)) {
+              std::vector<bool>& chanMask = fMaskMap[equipment][elink];
+              if (chanMask.empty()) { chanMask.resize(numChanPerAsic, false); }
+              chanMask[channel] = true;
+            }
           }  //# channel
         }    //# elink
       }      //# CROB
diff --git a/algo/detectors/much/MuchReadoutConfig.h b/algo/detectors/much/MuchReadoutConfig.h
index 9083777146..8c496856c3 100644
--- a/algo/detectors/much/MuchReadoutConfig.h
+++ b/algo/detectors/much/MuchReadoutConfig.h
@@ -39,11 +39,22 @@ namespace cbm::algo
      */
     std::vector<uint32_t> Map(uint16_t equipId, uint16_t elink);
 
+    /** @brief API: Mapping from component and elink to channel mask flags
+     ** @param equipId     Equipment identifier (component)
+     ** @param elink       Elink number within component
+     ** @return (vector of mask flags for channels per asic) 
+     */
+    std::vector<bool> MaskMap(uint16_t equipId, uint16_t elink);
+
   private:
     // --- MUCH readout map
     // --- Map index: (equipment, elink, channel), map value: (MUCH address)
     std::map<uint16_t, std::vector<std::vector<uint32_t>>> fReadoutMap = {};
 
+    // --- MUCH channel mask  map
+    // --- Map index: (equipment, elink), map value: (vector of mask flags for channels per asic)
+    std::map<uint16_t, std::map<size_t, std::vector<bool>>> fMaskMap = {};  //!
+
     /** @brief Initialisation of readout map **/
     void Init();
 
diff --git a/algo/detectors/much/UnpackMuch.cxx b/algo/detectors/much/UnpackMuch.cxx
index 4ebd73300d..cd6a23445d 100644
--- a/algo/detectors/much/UnpackMuch.cxx
+++ b/algo/detectors/much/UnpackMuch.cxx
@@ -107,8 +107,11 @@ namespace cbm::algo
       return;
     }
     const UnpackMuchElinkPar& elinkPar = fParams.fElinkParams.at(elink);
+    uint16_t channel                   = message.GetHitChannel();
+
+    // --- Check for masked channel
+    if (!elinkPar.fChanMask.empty() && elinkPar.fChanMask[channel] == true) { return; }
 
-    uint16_t channel = message.GetHitChannel();
     uint32_t address = (elinkPar.fAddress)[channel];
 
     // --- Expand time stamp to time within timeslice (in clock cycle)
diff --git a/algo/detectors/much/UnpackMuch.h b/algo/detectors/much/UnpackMuch.h
index a5af4d786c..6cf45df4da 100644
--- a/algo/detectors/much/UnpackMuch.h
+++ b/algo/detectors/much/UnpackMuch.h
@@ -31,6 +31,7 @@ namespace cbm::algo
    **/
   struct UnpackMuchElinkPar {
     std::vector<uint32_t> fAddress;  ///< CbmMuchAddress for different channels
+    std::vector<bool> fChanMask;     ///< Channel masking flags
     uint64_t fTimeOffset = 0.;       ///< Time calibration parameter
   };
 
diff --git a/algo/unpack/Unpack.cxx b/algo/unpack/Unpack.cxx
index e19af981ee..2ee774e360 100644
--- a/algo/unpack/Unpack.cxx
+++ b/algo/unpack/Unpack.cxx
@@ -270,6 +270,7 @@ namespace cbm::algo
         UnpackMuchElinkPar elinkPar;
         elinkPar.fAddress    = fMuchConfig.Map(equip, elink);  // Vector of MUCH addresses for this elink
         elinkPar.fTimeOffset = fSystemTimeOffset[Subsystem::MUCH];
+        elinkPar.fChanMask   = fMuchConfig.MaskMap(equip, elink);
         par->fElinkParams.push_back(elinkPar);
       }
       fAlgoMuch[equip].SetParams(std::move(par));
-- 
GitLab