From 2bb07117bbdfeb9f16f1896f536b545f29a97dc2 Mon Sep 17 00:00:00 2001
From: Alexandru Bercuci <abercuci@niham.nipne.ro>
Date: Fri, 14 Feb 2025 23:07:08 +0200
Subject: [PATCH] add channel time-offset calibration at ns level

---
 algo/detectors/trd2d/ReadoutConfig.h | 2 +-
 algo/detectors/trd2d/Unpack.cxx      | 1 +
 algo/detectors/trd2d/UnpackMS.cxx    | 6 ++++--
 algo/detectors/trd2d/UnpackMS.h      | 1 +
 4 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/algo/detectors/trd2d/ReadoutConfig.h b/algo/detectors/trd2d/ReadoutConfig.h
index f63026808..84c7266b1 100644
--- a/algo/detectors/trd2d/ReadoutConfig.h
+++ b/algo/detectors/trd2d/ReadoutConfig.h
@@ -44,7 +44,7 @@ namespace cbm::algo::trd2d
     struct ChanMapping {
       int32_t padAddress;   /// map pad and pairing to FASP channel
       bool maskFlag;        /// HW mask flag for channel
-      uint8_t tOffset;      /// time correction in clk
+      int8_t tOffset;      /// time correction in clk
       uint16_t lThreshold;  /// SW threshold for ringing channels
 
       CBM_YAML_FORMAT(YAML::Flow);
diff --git a/algo/detectors/trd2d/Unpack.cxx b/algo/detectors/trd2d/Unpack.cxx
index 5cc2d37bc..d4f661e1a 100644
--- a/algo/detectors/trd2d/Unpack.cxx
+++ b/algo/detectors/trd2d/Unpack.cxx
@@ -51,6 +51,7 @@ Unpack::Unpack(const Config& config) : fConfig(config)
                                 {calCh.noise.tDelay, calCh.noise.tWindow, calCh.noise.lDThres, calCh.noise.lSThres}};
         chanPar.fMask        = pars.maskFlag;    // Flag channel mask
         chanPar.fDaqOffset   = pars.tOffset;     // Time calibration parameter
+        par.toff[ch/2]       = pars.tOffset;
         chanPar.fSignalThres = pars.lThreshold;  // Threshold cut
         asicPar.fChanParams.push_back(chanPar);
       }
diff --git a/algo/detectors/trd2d/UnpackMS.cxx b/algo/detectors/trd2d/UnpackMS.cxx
index 9851cd5d1..19f9b8dfe 100644
--- a/algo/detectors/trd2d/UnpackMS.cxx
+++ b/algo/detectors/trd2d/UnpackMS.cxx
@@ -442,7 +442,9 @@ namespace cbm::algo::trd2d
         * Correct the time with the system time offset which is derived in calibration.*/
         uint64_t gtime = id.GetTime() * fAsicClockPeriod;
         gtime >>= 1;
-        if (gtime >= uint64_t(fParams.fSystemTimeOffset)) gtime -= fParams.fSystemTimeOffset;
+        int toffGlobalCorrection = fParams.fSystemTimeOffset + fParams.toff[ipad];
+        if (toffGlobalCorrection > 0 && gtime < uint64_t(toffGlobalCorrection)) gtime = 0;
+        else gtime -= (fParams.fSystemTimeOffset + fParams.toff[ipad]);
         id.SetTime(gtime);
         outputDigis.emplace_back(std::move(id));
       }
@@ -474,7 +476,7 @@ namespace cbm::algo::trd2d
       if (calPar.noise.fSignalThres && imess.data <= calPar.noise.fSignalThres) continue;
       const uint32_t pad     = mch / 2;
       const bool hasPairingR = bool(chPar.fPadAddress > 0);
-      const uint64_t lTime   = time + imess.tlab + calPar.fDaqOffset;
+      const uint64_t lTime   = time + imess.tlab;
       const uint16_t sgn =
         uint16_t((imess.data - fParams.fRefSignal) * calPar.fGainFee - calPar.fBaseline + fParams.fRefSignal);
       const uint16_t lchR                 = hasPairingR ? sgn : 0;
diff --git a/algo/detectors/trd2d/UnpackMS.h b/algo/detectors/trd2d/UnpackMS.h
index b09536dfd..c75452a6a 100644
--- a/algo/detectors/trd2d/UnpackMS.h
+++ b/algo/detectors/trd2d/UnpackMS.h
@@ -171,6 +171,7 @@ namespace cbm::algo::trd2d
     std::map<uint8_t, UnpackAsicPar> fAsicParams = {};    ///< Parameters for each ASIC
     std::array<CalibChannelPar, NFASPMOD* NFASPCH> fCalibParams =
       {};  ///< Parameters for each ASIC channel for each module
+    int toff[NFASPMOD * NFASPPAD];
 
     /** \brief Write to the debug stream the content of the current param object*/
     void dump() const;
-- 
GitLab