From 5e1acbf08460cb495c882103358ebeaba609f424 Mon Sep 17 00:00:00 2001
From: Alexandru Bercuci <abercuci@niham.nipne.ro>
Date: Sun, 16 Feb 2025 01:12:18 +0200
Subject: [PATCH] finalize TRD2D calibration - add masking of dynamic noise

---
 algo/detectors/trd2d/ReadoutConfig.cxx  |  3 ++-
 algo/detectors/trd2d/ReadoutConfig.h    |  6 +++---
 algo/detectors/trd2d/UnpackMS.cxx       | 16 ++++++++++++++--
 algo/detectors/trd2d/UnpackMS.h         |  4 ++--
 reco/tasks/CbmTaskTrdUnpackParWrite.cxx |  2 +-
 5 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/algo/detectors/trd2d/ReadoutConfig.cxx b/algo/detectors/trd2d/ReadoutConfig.cxx
index faeb1cb9c..a65ca1e2c 100644
--- a/algo/detectors/trd2d/ReadoutConfig.cxx
+++ b/algo/detectors/trd2d/ReadoutConfig.cxx
@@ -119,7 +119,8 @@ namespace cbm::algo::trd2d
 
   // ---  Initialise the mapping structure   --------------------------------------------
   void ReadoutSetup::InitChannelMap(
-    const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int8_t, uint16_t>>>>& channelMap)
+    const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int16_t, uint16_t>>>>&
+      channelMap)
   {
     // Constructing the map (equipId, asicId, chanId) -> (pad address, mask flag, daq offset)
     for (auto compMap : channelMap) {
diff --git a/algo/detectors/trd2d/ReadoutConfig.h b/algo/detectors/trd2d/ReadoutConfig.h
index fe590915b..673b55f87 100644
--- a/algo/detectors/trd2d/ReadoutConfig.h
+++ b/algo/detectors/trd2d/ReadoutConfig.h
@@ -116,7 +116,7 @@ namespace cbm::algo::trd2d
 
     /** @brief Initialisation of channel map **/
     void InitChannelMap(
-      const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int8_t, uint16_t>>>>&
+      const std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int16_t, uint16_t>>>>&
         channelMap);
 
     /** @brief Get system time offset **/
@@ -149,8 +149,8 @@ namespace cbm::algo::trd2d
 
    public:
     struct ChanNoise {
-      uint8_t tDelay;    /// time [clk] delay wrt to causing primary signal
-      uint8_t tWindow;   /// time [clk] window to search for noisy signal
+      uint16_t tDelay;   /// time [clk] delay wrt to causing primary signal
+      uint16_t tWindow;  /// time [clk] window to search for noisy signal
       uint16_t lDThres;  /// Threshold [ADU] for dynamic channel noise
       uint16_t lSThres;  /// threshold [ADU] for static channel noise (happens independently)
 
diff --git a/algo/detectors/trd2d/UnpackMS.cxx b/algo/detectors/trd2d/UnpackMS.cxx
index 64060dd3f..b143ea98c 100644
--- a/algo/detectors/trd2d/UnpackMS.cxx
+++ b/algo/detectors/trd2d/UnpackMS.cxx
@@ -499,7 +499,7 @@ namespace cbm::algo::trd2d
       double r, t;
       int32_t dt;
       const int32_t dtime = id.GetTime() - lTime;
-      bool use(false);
+      bool use(false), noise(false);
       if (abs(dtime) < 5) {  // test message part of (last) digi
         r = id.GetCharge(t, dt);
         if (lchR && r < 0.1) {  // set R charge on an empty slot
@@ -513,8 +513,20 @@ namespace cbm::algo::trd2d
         }
       }
 
+      // check if signal can be flagged as dynamic noise
+      if (!use && calPar.noise.tWindow > 0) {
+        if (lchR) {
+          if (lchR < calPar.noise.lThreshold && std::abs(-dtime * 12.5 - calPar.noise.tDelay) < calPar.noise.tWindow)
+            noise = true;
+        }
+        else {
+          if (lchT < calPar.noise.lThreshold && std::abs(-dtime * 12.5 - calPar.noise.tDelay) < calPar.noise.tWindow)
+            noise = true;
+        }
+      }
+
       // build digi for message when update failed
-      if (!use) {
+      if (!use && !noise) {
         digiBuffer.emplace_back(pad, lchT, lchR, lTime);
         digiBuffer.back().SetAddressModule(fParams.fModId);
       }
diff --git a/algo/detectors/trd2d/UnpackMS.h b/algo/detectors/trd2d/UnpackMS.h
index 7989bdd17..9696671b6 100644
--- a/algo/detectors/trd2d/UnpackMS.h
+++ b/algo/detectors/trd2d/UnpackMS.h
@@ -138,8 +138,8 @@ namespace cbm::algo::trd2d
    ** @brief TRD2D noise parameters for one Asic channel
    **/
   struct NoiseChannelPar {
-    uint8_t tDelay        = 0;  /// time [clk] delay wrt to causing primary signal
-    uint8_t tWindow       = 0;  /// time [clk] window to search for noisy signal
+    uint16_t tDelay       = 0;  /// time [clk] delay wrt to causing primary signal
+    uint16_t tWindow      = 0;  /// time [clk] window to search for noisy signal
     uint16_t lThreshold   = 0;  ///< Signal threshold for induced - dynamic noise
     uint16_t fSignalThres = 0;  ///< Signal threshold for independent - static noise
   };
diff --git a/reco/tasks/CbmTaskTrdUnpackParWrite.cxx b/reco/tasks/CbmTaskTrdUnpackParWrite.cxx
index 92e14baf5..6679dc599 100644
--- a/reco/tasks/CbmTaskTrdUnpackParWrite.cxx
+++ b/reco/tasks/CbmTaskTrdUnpackParWrite.cxx
@@ -39,7 +39,7 @@ InitStatus CbmTaskTrdUnpackParWrite::Init()
     // Map (moduleId) -> (array of crobId)
     std::map<uint32_t, std::vector<uint16_t>> crobMap;
     // Map (equipId, asicId, chanId) -> (pad address, mask flag, daq offset [FASP clk])
-    std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int8_t, uint16_t>>>> channelMap;
+    std::map<size_t, std::map<size_t, std::map<size_t, std::tuple<int32_t, bool, int16_t, uint16_t>>>> channelMap;
 
     // Loop through a list of module IDs from the .digi file (can in principle contradict crob_map).
     for (auto entry : digiparset.GetModuleMap()) {
-- 
GitLab