From 673bac8ae63b5f4c925ab36a9c2fe1963ad69ebf Mon Sep 17 00:00:00 2001
From: Pascal Raisig <praisig@ikf.uni-frankfurt.de>
Date: Mon, 11 Jan 2021 14:52:34 +0100
Subject: [PATCH] Include timeshift correction parameters to the Trd unpacker
 Algo and Task

This commit includes the timeshift parameters into the Trd unpacker algo and task.
If they are not passed everything should behave like before this commit.
---
 .../unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx  | 17 ++++++++++++--
 .../unpacker/CbmMcbm2018UnpackerAlgoTrdR.h    | 23 +++++++++++++++----
 .../unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx  | 12 ++++++++++
 .../unpacker/CbmMcbm2018UnpackerTaskTrdR.h    |  4 ++++
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
index 19b5bce3e1..38c885c929 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.cxx
@@ -172,7 +172,13 @@ Bool_t CbmMcbm2018UnpackerAlgoTrdR::InitParameters() {
 }
 
 Bool_t CbmMcbm2018UnpackerAlgoTrdR::ProcessTs(const fles::Timeslice& ts) {
-  fCurrTsIdx   = ts.index();
+  fCurrTsIdx         = ts.index();
+  size_t itimeslice  = fCurrTsIdx / 10;
+  auto timeshiftpair = fmapTimeshifts.find(itimeslice);
+  if (timeshiftpair != fmapTimeshifts.end()) {
+    fvecTimeshiftsPar = &timeshiftpair->second;
+  }
+
   fTsStartTime = static_cast<Double_t>(ts.descriptor(0, 0).idx);
 
   /// On first TS, extract the TS parameters from header (by definition stable over time).
@@ -862,7 +868,7 @@ CbmMcbm2018UnpackerAlgoTrdR::MakeDigi(CbmTrdRawMessageSpadic raw) {
   Float_t digiCharge =
     (Float_t) raw.GetMaxAdc()
     + 256;  // REMARK raw.GetMaxADC returns a the value in the range of -256 til 255. However, the digiCharge is stored as unsigned.  // TODO make Settable
-  ULong64_t digiTime = raw.GetTime() - fdTimeOffsetNs;
+
   // Int_t digiTriggerType = raw.GetHitType() ; // Spadic::TriggerType this does not work 03/27/2020 - PR digiTriggerType is not Spadic::TriggerType!
   Int_t digiTriggerType = raw.GetHitType();
   if (digiTriggerType == 1)
@@ -897,6 +903,13 @@ CbmMcbm2018UnpackerAlgoTrdR::MakeDigi(CbmTrdRawMessageSpadic raw) {
 
   digiAddress = (fAsicChannelMap.find(asicAddress))->second.at(asicChannelId);
 
+  ULong64_t digiTime = raw.GetTime();
+  if (fvecTimeshiftsPar) {
+    digiTime = digiTime - fvecTimeshiftsPar->at(digiAddress);
+    raw.SetTime(digiTime);
+  }
+  digiTime -= fdTimeOffsetNs;
+
   std::shared_ptr<CbmTrdDigi> digi =
     std::make_shared<CbmTrdDigi>(CbmTrdDigi(digiAddress,
                                             uniqueModuleId,
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.h b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.h
index c7273353e2..d9a6703f72 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.h
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTrdR.h
@@ -133,6 +133,12 @@ public:
     fdTimeOffsetNs = dOffsetIn;
   }
 
+  void SetTimeshiftsMap(std::map<size_t, std::vector<Int_t>>* setvalue) {
+    fmapTimeshifts.clear();
+    fmapTimeshifts.insert(setvalue->begin(), setvalue->end());
+  }
+  ///< In the mCbm 2020 beamtime timeshifts changing during the run of the correlation time to the T0 have been observed. This is part of their correction
+
   /**
 	 *  @brief Call this when Spadic Average-Baseline feature is enabled.
 	 **/
@@ -261,10 +267,19 @@ private:
   CbmTrdParSetDigi* fDigiPar;  ///< CbmTrdParameter container
   CbmTrdParSetGas* fGasPar;    ///< CbmTrdParameter container
   CbmTrdParSetGain* fGainPar;  ///< CbmTrdParameter container
-  std::map<std::uint64_t, Int_t>
-    fSpadicMap;  ///< Map to retrieve asic address from CriId/CrobId/ElinkId (see CbmTrdHardwareSetupR)
-  std::map<Int_t, std::vector<Int_t>>
-    fAsicChannelMap;  ///< Map to retrieve module channelId from asicAddress and asicChannel
+
+  std::map<std::uint64_t, Int_t> fSpadicMap;
+  ///< Map to retrieve asic address from CriId/CrobId/ElinkId (see CbmTrdHardwareSetupR)
+
+  std::map<Int_t, std::vector<Int_t>> fAsicChannelMap;
+  ///< Map to retrieve module channelId from asicAddress and asicChannel
+
+  std::map<size_t, std::vector<Int_t>> fmapTimeshifts = {};
+  ///< Map containing the timeshift parameters for the correction of the µSlice timeshifts. The keys are the tsIdx, if no key is found, the shifts of the previous tsIdx are still valid
+
+  std::vector<Int_t>* fvecTimeshiftsPar = nullptr;
+  ///< Vector containing the timeshift parameters for the correction of the µSlice timeshifts for a given tsIdx.
+
   bool
     fIsFirstChannelsElinkEven;  ///< 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
 
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
index 84275de512..910bfeaaef 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.cxx
@@ -164,6 +164,11 @@ void CbmMcbm2018UnpackerTaskTrdR::SetParContainers() {
     }
     fParContList->AddAt(updatedParSet, iParCont);
   }
+
+  // Get timeshift parameters
+  fTimeshiftPar = dynamic_cast<CbmMcbm2020TrdTshiftPar*>(
+    FairRun::Instance()->GetRuntimeDb()->getContainer(
+      "CbmMcbm2020TrdTshiftPar"));
 }
 
 Bool_t CbmMcbm2018UnpackerTaskTrdR::InitContainers() {
@@ -186,6 +191,13 @@ Bool_t CbmMcbm2018UnpackerTaskTrdR::InitContainers() {
 
   Bool_t initOK = fUnpackerAlgo->InitContainers();
 
+  if (fTimeshiftPar) {
+    auto maptimeshifts = fTimeshiftPar->GetTimeshiftsMap();
+    fUnpackerAlgo->SetTimeshiftsMap(maptimeshifts);
+    LOG(info) << "CbmMcbm2018UnpackerTaskTrdR::SetParContainers() - Parsing "
+                 "timeshift correction map to unpacker algo";
+  }
+
   /// If monitor mode enabled, trigger histos creation,
   /// obtain pointer on them and add them to the HTTP server.
   if (fbMonitorMode == kTRUE || fbDebugMonitorMode == kTRUE) {
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
index fb9224ae3a..6fccaccd2f 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerTaskTrdR.h
@@ -9,6 +9,7 @@
 
 #include "TString.h"
 
+#include "CbmMcbm2020TrdTshiftPar.h"
 #include "CbmMcbmUnpack.h"
 #include "CbmTrdDigi.h"
 #include "CbmTrdRawMessageSpadic.h"
@@ -127,6 +128,9 @@ private:
   TString fMonitorHistoFileName;
   std::vector<bool> fIsActiveHistoVec;  // Define active histos in algo
 
+  /// mCbm2020 timeshift correction parameters
+  CbmMcbm2020TrdTshiftPar* fTimeshiftPar = nullptr;
+
   /// Output Digi vector
   std::vector<CbmTrdDigi>* fTrdDigiVector;
 
-- 
GitLab