From 281b2e85d89ddc94e7f66e55097429f812c672f4 Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Fri, 28 Apr 2023 16:19:01 +0200
Subject: [PATCH] CbmTrdUnpackFaspAlgo: Switched to storage of inverted instead
 of direct crob map.

---
 .../trd/unpack/CbmTrdUnpackFaspAlgo.cxx       | 42 ++++++++++++-------
 .../trd/unpack/CbmTrdUnpackFaspAlgo.h         |  4 +-
 2 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.cxx b/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.cxx
index 5fa8763ad3..c299bcfab4 100644
--- a/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.cxx
+++ b/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.cxx
@@ -141,8 +141,20 @@ void CbmTrdUnpackFaspAlgo::SetAsicMapping(const std::map<uint32_t, uint8_t[NFASP
 //_________________________________________________________________________________
 void CbmTrdUnpackFaspAlgo::SetCrobMapping(const std::map<uint32_t, uint16_t[NCROBMOD]>& map)
 {
-  if (fCrobMap) delete fCrobMap;
-  fCrobMap = new std::map<uint32_t, uint16_t[NCROBMOD]>(map);
+  if (fCompMap) delete fCompMap;
+  fCompMap = new std::map<uint16_t, std::pair<uint16_t, uint16_t>>;  /// map eq_id -> (mod_id, 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 (fCompMap->find(eq_id) != fCompMap->end()) {
+        LOG(error) << GetName() << "::SetCrobMapping: multiple entries for eq_id " << (int) eq_id << " found.";
+        return;
+      }
+      (*fCompMap)[eq_id] = std::make_pair(mod_id, crob_id);
+    }
+  }
 }
 
 //_________________________________________________________________________________
@@ -366,32 +378,30 @@ bool CbmTrdUnpackFaspAlgo::unpack(const fles::Timeslice* ts, std::uint16_t icomp
   if (VERBOSE) printf("CbmTrdUnpackFaspAlgo::unpack 0x%04x %d\n", icomp, imslice);
   // LOG(info) << "Component " << icomp << " connected to config CbmTrdUnpackConfig2D. Slice "<<imslice;
 
-  uint8_t crob_id = 0;
-  bool unpackOk   = true;
+  bool unpackOk = true;
   //Double_t fdMsSizeInNs = 1.28e6;
 
   auto msdesc = ts->descriptor(icomp, imslice);
+
   // Cast required to silence a warning on macos (there a uint64_t is a llu)
   if (VERBOSE) printf("time start %lu\n", static_cast<size_t>(msdesc.idx));
+
   // define time wrt start of time slice in TRD/FASP clks [80 MHz]
   fTime = ULong64_t((msdesc.idx - fTsStartTime - fSystemTimeOffset) / 12.5);
 
-  // get MOD_id and CROB id from the equipment
+  // get MOD_id and CROB id from the equipment, using the comp map: eq_id -> (mod_id, crob_id)
   const uint16_t eq_id = msdesc.eq_id;
-  bool mapped          = false;
-  for (auto mod_id : fModuleId) {
-    for (crob_id = 0; crob_id < NCROBMOD; crob_id++) {
-      if (((*fCrobMap)[mod_id])[crob_id] == eq_id) break;
-    }
-    if (crob_id == NCROBMOD) continue;
-    fMod   = mod_id;
-    mapped = true;
-    break;
-  }
-  if (!mapped) {
+  const auto it        = fCompMap->find(eq_id);
+  if (it == fCompMap->end() || std::find(fModuleId.begin(), fModuleId.end(), it->second.first) == fModuleId.end()) {
     LOG(error) << GetName() << "::unpack - CROB eq_id=" << eq_id << " not registered in the unpacker.";
     return false;
   }
+  fMod                  = (*fCompMap)[eq_id].first;
+  const uint8_t crob_id = (*fCompMap)[eq_id].second;
+
+
+  ///////// To do: Make fMod a local variable
+
   if (fCrob == 0xffff) fCrob = icomp;
 
   // Get the µslice size in bytes to calculate the number of completed words
diff --git a/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.h b/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.h
index 7c105f0a2b..bbd864af5d 100644
--- a/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.h
+++ b/reco/detectors/trd/unpack/CbmTrdUnpackFaspAlgo.h
@@ -202,8 +202,8 @@ protected:
 
 private:
   void prt_wd(uint32_t w);
-  std::map<uint32_t, uint8_t[NFASPMOD]>* fFaspMap  = nullptr;  ///> FASP mapping update wrt the default setting
-  std::map<uint32_t, uint16_t[NCROBMOD]>* fCrobMap = nullptr;  ///> CRI mapping setting
+  std::map<uint32_t, uint8_t[NFASPMOD]>* fFaspMap = nullptr;  ///> FASP mapping update wrt the default setting
+  std::map<uint16_t, std::pair<uint16_t, uint16_t>>* fCompMap        = nullptr;  ///> Map eq_id -> (mod_id, crob_id)
   std::array<std::vector<CbmTrdDigi>, NFASPMOD* NFASPCH> fDigiBuffer = {
     {}};  ///> Buffered digi for each pad in CROB component
   /** @brief Potential (online) monitor for the unpacking process */
-- 
GitLab