From e4cd01aa8176fcb6de023089e549b1349fb8d96f Mon Sep 17 00:00:00 2001
From: Florian Uhlig <f.uhlig@gsi.de>
Date: Fri, 1 Dec 2023 15:05:51 +0100
Subject: [PATCH] Allow to change the used latency in CbmDaq

The latency value is used to decide when data can be moved from the daq buffer
to the timeslice. It is a value for the time disordering between digis in the
data send to the daq buffer during digitization. In case of the MVD detector
this delay can be tens of microseconds such that in case the MVD is in the
setup the used latency must be increased from the default value.

This commit implements the possibility that each detector system can define
its own latency. The latency used in CbmDaq is then the maximum of all defined
detector latencies. Currently only the MVD detector defines a non standard
latency.
---
 core/base/CbmDaq.cxx                  | 2 --
 core/base/CbmDaq.h                    | 7 +++++--
 core/base/CbmDigitize.h               | 8 ++++++++
 core/base/CbmDigitizeBase.h           | 5 +++++
 sim/response/base/CbmDigitization.cxx | 1 +
 5 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/core/base/CbmDaq.cxx b/core/base/CbmDaq.cxx
index be7cd8f21d..9b02abaa84 100644
--- a/core/base/CbmDaq.cxx
+++ b/core/base/CbmDaq.cxx
@@ -47,7 +47,6 @@ CbmDaq::CbmDaq(Bool_t eventMode)
   : FairTask("Daq")
   , fIsEventByEvent(eventMode)
   , fTimeSliceLength(-1.)
-  , fLatency(5000.)
   , fStoreEmptySlices(kFALSE)
   , fTimeEventPrevious(-1.)
   , fNofEvents(0)
@@ -76,7 +75,6 @@ CbmDaq::CbmDaq(Double_t tsLength)
   : FairTask("Daq")
   , fIsEventByEvent(kFALSE)
   , fTimeSliceLength(tsLength)
-  , fLatency(2000.)
   , fStoreEmptySlices(kFALSE)
   , fTimeEventPrevious(-1.)
   , fNofEvents(0)
diff --git a/core/base/CbmDaq.h b/core/base/CbmDaq.h
index 4e5dec84b1..7fb9248232 100644
--- a/core/base/CbmDaq.h
+++ b/core/base/CbmDaq.h
@@ -86,10 +86,13 @@ public:
      ** at least be the maximum dead time of all detectors plus
      ** some safety margin accounting for the time resolution of the
      ** detectors.
+     ** The latency is only changed if the latency passed as argument
+     ** is larger than the current value to avoid that a call from
+     ** a second digitizer lowers the latency set by a first digitizer.
      ** The current default of 2,000 ns corresponds to the STS with
      ** dead time of 800 ns and time resolution of 5 ns.
      **/
-  void SetLatency(Double_t time) { fLatency = time; }
+  void SetLatency(Double_t time) { if (time > fLatency) fLatency = time; }
 
 
   /** @brief Set the digitizer for a given system
@@ -116,7 +119,7 @@ public:
 private:
   Bool_t fIsEventByEvent;       ///< Flag for event-by-event mode
   Double_t fTimeSliceLength;    ///< Time-slice length [ns]
-  Double_t fLatency;            ///< Maximal time disorder of input data [ns]
+  Double_t fLatency = 5000.;    ///< Maximal time disorder of input data [ns]
   Bool_t fStoreEmptySlices;     ///< Flag to store also empty time slices
   Double_t fTimeEventPrevious;  ///< Time of previous event [ns]
 
diff --git a/core/base/CbmDigitize.h b/core/base/CbmDigitize.h
index 286180bd27..4a4db9f9f4 100644
--- a/core/base/CbmDigitize.h
+++ b/core/base/CbmDigitize.h
@@ -82,6 +82,14 @@ public:
   }
   // --------------------------------------------------------------------------
 
+  /** @brief Return the detector specific latency
+     ** @value latency
+     **
+     ** If there is no detector sopecific implementation the return
+     ** value is 0. which does not change the default value set in
+     ** CbmDaq.
+     **/
+  Double_t GetLatency() const { return 0.;}
 
   // --------------------------------------------------------------------------
   /** @brief Clear the output arrays **/
diff --git a/core/base/CbmDigitizeBase.h b/core/base/CbmDigitizeBase.h
index b0321af639..172dd6e5a2 100644
--- a/core/base/CbmDigitizeBase.h
+++ b/core/base/CbmDigitizeBase.h
@@ -136,6 +136,11 @@ public:
      **/
   virtual ECbmModuleId GetSystemId() const = 0;
 
+  /** @brief Detector system ID
+     ** @return Detector specific latency
+     **/
+  virtual Double_t GetLatency() const = 0;
+
 
   /** @brief Time of last datum in DAQ buffer
      ** @value Time of last datum in DAQ buffer
diff --git a/sim/response/base/CbmDigitization.cxx b/sim/response/base/CbmDigitization.cxx
index ab6e14216e..7c54d1fcf5 100644
--- a/sim/response/base/CbmDigitization.cxx
+++ b/sim/response/base/CbmDigitization.cxx
@@ -390,6 +390,7 @@ void CbmDigitization::Run(Int_t event1, Int_t event2)
       digitizer->SetProduceNoise(fProduceNoise);
       digitizer->SetCreateMatches(fCreateMatches);
       digitizer->SetRunStartTime(fSource->GetStartTime());
+      fDaq->SetLatency(digitizer->GetLatency());
       run->AddTask(digitizer);
       LOG(info) << fName << ": Added task " << digitizer->GetName();
     }  //? active and digitizer instance present
-- 
GitLab