From 66d49200fb2e86385236c7fa1eac93bac13136bf Mon Sep 17 00:00:00 2001
From: praisig <praisig@ikf.uni-frankfurt.de>
Date: Thu, 27 Aug 2020 18:43:13 +0200
Subject: [PATCH] Second iteration of mapping fix

Add even/odd elink setter to unpacker task and algo
Adjust mapping scheme for asic channels used to create paramter files
---
 core/detectors/trd/CbmTrdParSpadic.cxx         | 18 +++++++-----------
 .../unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx   |  8 ++------
 .../unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx   |  1 +
 .../unpacker/CbmMcbm2018UnpackerTaskTrdR.h     |  6 ++++++
 4 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/core/detectors/trd/CbmTrdParSpadic.cxx b/core/detectors/trd/CbmTrdParSpadic.cxx
index 6b66fe56b2..ce42360dbf 100644
--- a/core/detectors/trd/CbmTrdParSpadic.cxx
+++ b/core/detectors/trd/CbmTrdParSpadic.cxx
@@ -185,18 +185,14 @@ Int_t CbmTrdParSpadic::GetNasicsPerCrob(Int_t moduleType) {
 
 // ---- GetAsicChAddress ----
 Int_t CbmTrdParSpadic::GetAsicChAddress(const Int_t asicChannel) {
-  Int_t address = -1;
   ///< Returns the nth asic Channel in asic coordinates in single asic padplane coordinates. Spadic channels are not mapped from 00 to 31 in padplane coordinates, this function returns the padplane channelnumber in the system of one asic(not in the channel map of a full module !)
-  auto elinkCh = asicChannel;
-  if (asicChannel > 15)
-    elinkCh -= NSPADICCH / 2;  // The mapping is symmetric for the elinks
-  std::vector<Int_t> chvec = {
-    15, 7, 14, 6, 13, 11, 5, 12, 10, 4, 3, 9, 8, 2, 1, 0};
-  address = chvec.at(elinkCh);
-  if (asicChannel > 15)
-    address +=
-      NSPADICCH
-      / 2;  // Get the correct value for the channels in the second elink
+
+  Int_t address = -1;
+  // Channel mapping based on channels 0-15 on the odd eLink and 16-31 on the even eLink, check setting in the unpacker for your dataset
+  std::vector<Int_t> chvec = {23, 7,  22, 6,  21, 19, 5,  20, 18, 4,  3,
+                              17, 16, 2,  1,  0,  31, 30, 29, 15, 14, 28,
+                              27, 13, 11, 26, 12, 10, 25, 9,  24, 8};
+  address                  = chvec.at(asicChannel);
   return address;
 }
 
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
index 6947b99895..f0d58a76d4 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
@@ -885,9 +885,10 @@ CbmMcbm2018UnpackerAlgoTrdR::MakeDigi(CbmTrdRawMessageSpadic raw) {
   // Int_t layerId(CbmTrdAddress::GetLayerId(uniqueModuleId));
   // Int_t moduleId(CbmTrdAddress::GetModuleId(uniqueModuleId));
 
+  // GetChannelId per eLink add NSPADICCH / 2 to the second(first) eLink in the case we start with odd(even) eLinks, since, our mapping is based on odd eLinks
   auto asicChannelId = (raw.GetElinkId() % 2) == fIsFirstChannelsElinkEven
                          ? raw.GetChannelId()
-                         : raw.GetChannelId() + NSPADICCH / 2;
+                         : raw.GetChannelId() + (NSPADICCH / 2);
 
   digiAddress = (fAsicChannelMap.find(asicAddress))->second.at(asicChannelId);
 
@@ -898,11 +899,6 @@ CbmMcbm2018UnpackerAlgoTrdR::MakeDigi(CbmTrdRawMessageSpadic raw) {
                                             digiTime,
                                             digiTriggerType,
                                             digiErrClass));
-  // digi->SetAddress(CbmTrdAddress::GetAddress(layerId, moduleId, 0, 0, 0));
-
-  // Int_t channelAddress = digi->GetAddressChannel();
-  // Int_t address = digi->GetAddress();
-  // printf("HelloWorld -- We have a digi with spadicHwAddress %lu - criId %d - eLinkId %d - asicAddress %d - layerId %d - moduleId %d - channelId %d -\n digiAddress %d -- channelAddress %d - fullAddress %d\n", spadicHwAddress, raw.GetCriId(), raw.GetElinkId(), asicAddress, layerId, moduleId, asicChannelId, digiAddress, channelAddress, address);
 
   return digi;
 }
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
index 2557d20cbb..fd653f9590 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
@@ -71,6 +71,7 @@ Bool_t CbmMcbm2018UnpackerTaskTrdR::Init() {
 
   fUnpackerAlgo->SetMsSizeInNs(
     fdMsSizeInNs);  // TODO handle this with asic parameter files
+  fUnpackerAlgo->SetFirstChannelsElinkEven(fIsFirstChannelsElinkEven);
   initOK &= fUnpackerAlgo->Init();
 
   if (initOK) {
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
index 520176a555..1300e4db4e 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
@@ -92,6 +92,10 @@ public:
     fdMsSizeInNs = msSizeInNs;
   }  // TODO handle this with asic parameter files
 
+  void SetFirstChannelsElinkEven(bool isEven) {
+    fIsFirstChannelsElinkEven = isEven;
+  }  /// < Set wether channels 00..15 are on the even (true) or the odd (false and default) elink
+
 private:
   // Control flags
   Bool_t
@@ -108,6 +112,8 @@ private:
     fSystemIdentifier;  ///< by default set to: fles::SubsystemIdentifier::TRD, changable via setter
   Double_t
     fdMsSizeInNs;  ///< microslice size in ns to be passed to the unpacker // TODO handle this with asic parameter files
+  bool fIsFirstChannelsElinkEven =
+    false;  ///< define if the first 16 channels (00..15) are found on the even (set true) or odd (false) eLinkId, default for mCbm2020 is false thus, initialized as false
 
   TString fMonitorHistoFileName;
   std::vector<bool> fIsActiveHistoVec;  // Define active histos in algo
-- 
GitLab