From 0694254937b8e373d69171af9d07e887e64dcd30 Mon Sep 17 00:00:00 2001
From: Dominik Smith <d.smith@gsi.de>
Date: Thu, 22 Jun 2023 10:44:54 +0200
Subject: [PATCH] cbm::algo::UnpackSts and related classes: Changed asic
 enumeration to account for downward counting on module backside.

---
 algo/detectors/sts/ReadoutConfig.cxx          |  5 +++--
 algo/detectors/sts/StsReadoutConfigLegacy.cxx |  5 +++--
 algo/detectors/sts/UnpackSts.cxx              | 13 +++++--------
 algo/detectors/sts/UnpackStsXpu.cxx           | 13 +++++--------
 algo/unpack/Unpack.cxx                        |  8 +-------
 5 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/algo/detectors/sts/ReadoutConfig.cxx b/algo/detectors/sts/ReadoutConfig.cxx
index dfec7cbcb2..fa33f79c34 100644
--- a/algo/detectors/sts/ReadoutConfig.cxx
+++ b/algo/detectors/sts/ReadoutConfig.cxx
@@ -61,6 +61,7 @@ void sts::ReadoutConfig::Init(const ReadoutSetup& config)
   const uint16_t numCrobPerComp = config.components.at(0).feb2module.size();  // Number of CROBs per component
   // const uint16_t numFebsPerCrob   = config.components.at(0).feb2module.at(0).size();  // Number of FEBs per CROB
   const uint16_t numAsicsPerFeb   = config.numAsicsPerFeb;  // Number of ASICs per FEB
+  const uint16_t numAsicsPerMod   = 2 * numAsicsPerFeb;     // Number of ASICs per module
   const uint16_t numElinksPerCrob = config.elinks.size();   // Number of elinks per CROB
 
   // Constructing the map (equipmentId, eLink) -> (module, ASIC within module)
@@ -93,8 +94,8 @@ void sts::ReadoutConfig::Init(const ReadoutSetup& config)
             int16_t febType    = (moduleType == 0 ? moduleSide : !moduleSide);  // 0 = FEB A, 1 = FEB B
             uint32_t asicIndex = (febType == 0 ? elink.toAsicFebA : elink.toAsicFebB);
             uint32_t asicInFeb = asicIndex % numAsicsPerFeb;  // ASIC number within FEB
-
-            asicInModule = (moduleSide == 1 ? asicInFeb : asicInFeb + numAsicsPerFeb);
+            // Asic number is counted downward from numAsicsPerMod - 1 for p side
+            asicInModule = (moduleSide == 1 ? asicInFeb : numAsicsPerMod - 1 - asicInFeb);
           }
         }
         fReadoutConfig[equipment][elinkIdx] = {moduleAddress, asicInModule, isPulser};
diff --git a/algo/detectors/sts/StsReadoutConfigLegacy.cxx b/algo/detectors/sts/StsReadoutConfigLegacy.cxx
index 4802a74bc4..1cd4078a5f 100644
--- a/algo/detectors/sts/StsReadoutConfigLegacy.cxx
+++ b/algo/detectors/sts/StsReadoutConfigLegacy.cxx
@@ -79,6 +79,7 @@ namespace cbm::algo
     const uint16_t numCrobPerComp   = 1;   // Number of CROBs per component
     const uint16_t numFebsPerCrob   = 5;   // Number of FEBs per CROB
     const uint16_t numAsicsPerFeb   = 8;   // Number of ASICs per FEB
+    const uint16_t numAsicsPerMod   = 16;  // Number of ASICs per module
     const uint16_t numElinksPerCrob = 42;  // Number of elinks per CROB
 
     // Equipment IDs for each component
@@ -169,8 +170,8 @@ namespace cbm::algo
               int16_t febType    = (moduleType == 0 ? moduleSide : !moduleSide);  // 0 = FEB A, 1 = FEB B
               uint32_t asicIndex = (febType == 0 ? elink2AsicFebA[elinkId] : elink2AsicFebB[elinkId]);
               uint32_t asicInFeb = asicIndex % numAsicsPerFeb;  // ASIC number within FEB
-
-              asicInModule = (moduleSide == 1 ? asicInFeb : asicInFeb + numAsicsPerFeb);
+              // Asic number is counted downward from numAsicsPerMod - 1 for p side
+              asicInModule = (moduleSide == 1 ? asicInFeb : numAsicsPerMod - 1 - asicInFeb);
             }
           }
           fReadoutMap[equipment][elink] = std::make_pair(moduleAddress, asicInModule);
diff --git a/algo/detectors/sts/UnpackSts.cxx b/algo/detectors/sts/UnpackSts.cxx
index f7d72fa3f7..ad1a586ac1 100644
--- a/algo/detectors/sts/UnpackSts.cxx
+++ b/algo/detectors/sts/UnpackSts.cxx
@@ -105,15 +105,12 @@ namespace cbm::algo
     uint32_t asicNr                   = elinkPar.fAsicNr;
 
     // --- Hardware-to-software address
-    uint32_t numChansPerModule = fParams.fNumAsicsPerModule * fParams.fNumChansPerAsic;
-    uint32_t address           = elinkPar.fAddress;
-    uint32_t channel           = 0;
-    if (asicNr < fParams.fNumAsicsPerModule / 2) {  // front side (n side)
+    uint32_t channel = 0;
+    if (asicNr < fParams.fNumAsicsPerModule / 2) {  // front side (n side); channels counted upward
       channel = message.GetHitChannel() + fParams.fNumChansPerAsic * asicNr;
     }
-    else {  // back side (p side)
-      channel = numChansPerModule - message.GetHitChannel()
-                - fParams.fNumChansPerAsic * (asicNr - fParams.fNumAsicsPerModule / 2) - 1;
+    else {  // back side (p side); channels counted downward
+      channel = fParams.fNumChansPerAsic * (asicNr + 1) - message.GetHitChannel() - 1;
     }
 
     // --- Expand time stamp to time within timeslice (in clock cycle)
@@ -132,7 +129,7 @@ namespace cbm::algo
     double charge = elinkPar.fAdcOffset + (message.GetHitAdc() - 1) * elinkPar.fAdcGain;
 
     // --- Create output digi
-    digiVec.emplace_back(address, channel, messageTime, charge);
+    digiVec.emplace_back(elinkPar.fAddress, channel, messageTime, charge);
   }
   // --------------------------------------------------------------------------
 
diff --git a/algo/detectors/sts/UnpackStsXpu.cxx b/algo/detectors/sts/UnpackStsXpu.cxx
index 5339ccb56c..bc62c05839 100644
--- a/algo/detectors/sts/UnpackStsXpu.cxx
+++ b/algo/detectors/sts/UnpackStsXpu.cxx
@@ -214,15 +214,12 @@ namespace cbm::algo
     uint32_t asicNr                      = elinkPar.fAsicNr;
 
     // --- Hardware-to-software address
-    uint32_t numChansPerModule = unpackPar.fNumAsicsPerModule * unpackPar.fNumChansPerAsic;
-    uint32_t address           = elinkPar.fAddress;
-    uint32_t channel           = 0;
-    if (asicNr < unpackPar.fNumAsicsPerModule / 2) {  // front side (n side)
+    uint32_t channel = 0;
+    if (asicNr < unpackPar.fNumAsicsPerModule / 2) {  // front side (n side); channels counted upward
       channel = message.GetHitChannel() + unpackPar.fNumChansPerAsic * asicNr;
     }
-    else {  // back side (p side)
-      channel = numChansPerModule - message.GetHitChannel()
-                - unpackPar.fNumChansPerAsic * (asicNr - unpackPar.fNumAsicsPerModule / 2) - 1;
+    else {  // back side (p side); channels counted downward
+      channel = unpackPar.fNumChansPerAsic * (asicNr + 1) - message.GetHitChannel() - 1;
     }
 
     // --- Expand time stamp to time within timeslice (in clock cycle)
@@ -239,7 +236,7 @@ namespace cbm::algo
     double charge = elinkPar.fAdcOffset + (message.GetHitAdc() - 1) * elinkPar.fAdcGain;
 
     // --- Create output digi
-    digis[numDigis] = CbmStsDigi(address, channel, messageTime, charge);
+    digis[numDigis] = CbmStsDigi(elinkPar.fAddress, channel, messageTime, charge);
     numDigis++;
   }
   // --------------------------------------------------------------------------
diff --git a/algo/unpack/Unpack.cxx b/algo/unpack/Unpack.cxx
index 8f2111e853..b6c9b40d79 100644
--- a/algo/unpack/Unpack.cxx
+++ b/algo/unpack/Unpack.cxx
@@ -168,13 +168,7 @@ namespace cbm::algo
         elinkPar.fTimeOffset = 0.;
         elinkPar.fAdcOffset  = 1.;
         elinkPar.fAdcGain    = 1.;
-        if (elinkPar.fAsicNr < numAsicsPerModuleSts / 2) {
-          elinkPar.fWalk = fStsConfig.WalkMap(elinkPar.fAddress, elinkPar.fAsicNr);
-        }
-        else {
-          elinkPar.fWalk = fStsConfig.WalkMap(elinkPar.fAddress,
-                                              numAsicsPerModuleSts - (elinkPar.fAsicNr - numAsicsPerModuleSts / 2) - 1);
-        }
+        elinkPar.fWalk       = fStsConfig.WalkMap(elinkPar.fAddress, elinkPar.fAsicNr);
         // TODO: Add parameters for time and ADC calibration
         par->fElinkParams.push_back(elinkPar);
       }
-- 
GitLab