From 61ae7c2e21cb85198734dcf467365048cf56b466 Mon Sep 17 00:00:00 2001
From: P-A Loizeau <p.-a.loizeau@gsi.de>
Date: Mon, 18 Nov 2024 16:07:35 +0100
Subject: [PATCH] [online] add SystemTimeOffset parameter for BMon unpacker

---
 algo/detectors/bmon/ReadoutConfig.cxx |  2 ++
 algo/detectors/bmon/ReadoutConfig.h   | 18 ++++++++++++++++--
 algo/detectors/bmon/Unpack.cxx        |  3 ++-
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/algo/detectors/bmon/ReadoutConfig.cxx b/algo/detectors/bmon/ReadoutConfig.cxx
index 6e6e97f141..ab27178e17 100644
--- a/algo/detectors/bmon/ReadoutConfig.cxx
+++ b/algo/detectors/bmon/ReadoutConfig.cxx
@@ -79,6 +79,8 @@ namespace cbm::algo::bmon
 
   void ReadoutConfig::Init(const ReadoutSetup& pars)
   {
+    fTimeOffset = pars.timeOffset;
+
     // Constructing the map (equipmentId, eLink, channel) -> (TOF address)
     const uint32_t numChanPerComp = pars.NChansPerComponent();
 
diff --git a/algo/detectors/bmon/ReadoutConfig.h b/algo/detectors/bmon/ReadoutConfig.h
index 13133d6b3b..a16d72aa61 100644
--- a/algo/detectors/bmon/ReadoutConfig.h
+++ b/algo/detectors/bmon/ReadoutConfig.h
@@ -16,6 +16,10 @@
 namespace cbm::algo::bmon
 {
 
+  /**
+   * @brief Readout setup / Hardware cabling for BMon
+   * Used to create the hardware mapping for the BMon unpacker.
+   */
   struct ReadoutSetup {
 
     struct CROB {
@@ -30,9 +34,10 @@ namespace cbm::algo::bmon
         yaml::Property(&CROB::rpcType, "rpcType", "add explanation."),
         yaml::Property(&CROB::rpcSide, "rpcSide", "add explanation."),
         yaml::Property(&CROB::nRPC, "nRPC", "number of RPCs."),
-	yaml::Property(&CROB::timeOffset, "timeOffset", "time offset for CROB"));
+        yaml::Property(&CROB::timeOffset, "timeOffset", "time offset for CROB"));
     };
 
+    i32 timeOffset;
     i32 nFebsPerComponent;
     i32 nAsicsPerFeb;
     i32 nChannelsPerAsic;
@@ -40,7 +45,8 @@ namespace cbm::algo::bmon
     std::vector<CROB> crobs;
     std::vector<u16> eqIds;
 
-    CBM_YAML_PROPERTIES(yaml::Property(&ReadoutSetup::nFebsPerComponent, "nFebsPerComponent", "Number of FEBs per component"),
+    CBM_YAML_PROPERTIES(yaml::Property(&ReadoutSetup::timeOffset, "timeOffset", "Time offset for BMon"),
+                        yaml::Property(&ReadoutSetup::nFebsPerComponent, "nFebsPerComponent", "Number of FEBs per component"),
                         yaml::Property(&ReadoutSetup::nAsicsPerFeb, "nAsicsPerFeb", "Number of ASICs per FEB"),
                         yaml::Property(&ReadoutSetup::nChannelsPerAsic, "nChannelsPerAsic", "Number of channels per ASIC"),
                         yaml::Property(&ReadoutSetup::nCrobPerComponent, "nCrobPerComponent", "Number of CROBs per component"),
@@ -70,6 +76,11 @@ namespace cbm::algo::bmon
     /** @brief Destructor **/
     virtual ~ReadoutConfig();
 
+    /** @brief System time offset for BMon
+     ** @return Value of system time offset
+     **/
+    i32 TimeOffset() const { return fTimeOffset; }
+
     /** @brief Equipment in the configuration
      ** @return Vector of equipment IDs
      **/
@@ -96,6 +107,9 @@ namespace cbm::algo::bmon
     int32_t TimeOffsetMap(uint16_t equipId, uint16_t elink);
 
    private:
+    // --- BMon system time offset
+    int32_t fTimeOffset = 0;
+
     // --- Bmon elink time offsets
     // --- Map index: (equipment, elink), map value: (time offset)
     std::map<uint16_t, std::vector<int32_t>> fTimeOffsetMap = {};
diff --git a/algo/detectors/bmon/Unpack.cxx b/algo/detectors/bmon/Unpack.cxx
index 36dbc050e1..60935649c8 100644
--- a/algo/detectors/bmon/Unpack.cxx
+++ b/algo/detectors/bmon/Unpack.cxx
@@ -12,6 +12,7 @@ using fles::Subsystem;
 Unpack::Unpack(const ReadoutConfig& readout) : fReadout(readout)
 {
   constexpr u8 SystemVersion = 0x00;
+  i32 SysTimeOffset          = fReadout.TimeOffset();
 
   // Create one algorithm per component for Bmon and configure it with parameters
   auto equipIdsBmon = fReadout.GetEquipmentIds();
@@ -21,7 +22,7 @@ Unpack::Unpack(const ReadoutConfig& readout) : fReadout(readout)
     for (size_t elink = 0; elink < numElinks; elink++) {
       bmon::UnpackElinkPar elinkPar;
       elinkPar.fChannelUId = fReadout.Map(equip, elink);            // Vector of Bmon addresses for this elink
-      elinkPar.fTimeOffset = fReadout.TimeOffsetMap(equip, elink);  // Time offset for this elink
+      elinkPar.fTimeOffset = SysTimeOffset + fReadout.TimeOffsetMap(equip, elink);  // Time offset for this elink
       par.fElinkParams.push_back(elinkPar);
     }
     auto algo                      = std::make_unique<UnpackMS>(par);
-- 
GitLab