From deb4102df5717e392e82991bd814c7399a87ba27 Mon Sep 17 00:00:00 2001
From: "P.-A. Loizeau" <p.-a.loizeau@gsi.de>
Date: Wed, 2 Jun 2021 11:31:57 +0200
Subject: [PATCH] [mCBM] In TOF unpacker, add protection against MS blocks
 without epochs (FW bug)

---
 fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.cxx | 10 +++++++++-
 fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.h   |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.cxx b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.cxx
index 34d204afce..8f4670e923 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.cxx
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.cxx
@@ -733,6 +733,7 @@ Bool_t CbmMcbm2018UnpackerAlgoTof::ProcessMs(const fles::Timeslice& ts, size_t u
 
   // Prepare variables for the loop on contents
   Int_t messageType              = -111;
+  fbEpochFoundInThisMs           = kFALSE;
   const uint64_t* pInBuff        = reinterpret_cast<const uint64_t*>(msContent);  // for epoch cycle
   const gdpbv100::Message* pMess = reinterpret_cast<const gdpbv100::Message*>(pInBuff);
   for (uint32_t uIdx = 0; uIdx < uNbMessages; uIdx++) {
@@ -794,6 +795,7 @@ Bool_t CbmMcbm2018UnpackerAlgoTof::ProcessMs(const fles::Timeslice& ts, size_t u
       }  // case gdpbv100::MSG_HIT:
       case gdpbv100::MSG_EPOCH: {
         if (gdpbv100::kuChipIdMergedEpoch == fuGet4Id) {
+          fbEpochFoundInThisMs = kTRUE;
           ProcessEpoch(pMess[uIdx], uIdx);
         }  // if this epoch message is a merged one valid for all chips
         else {
@@ -838,7 +840,13 @@ Bool_t CbmMcbm2018UnpackerAlgoTof::ProcessMs(const fles::Timeslice& ts, size_t u
   /// ===> Until it is decided either to get rid of the epoch suppr. buffer or
   ///      to implement the "closing epoch" in the FW, do the following:
   /// For now this method is used to fake a "closing" epoch at the end of the MS
-  ProcessEndOfMsEpoch();
+  if (kTRUE == fbEpochFoundInThisMs) {
+    /// Found in 2021: sometimes multiple MS without any Epoch, then buffer should be ignored
+    ProcessEndOfMsEpoch();
+  }
+  else {
+    fvvmEpSupprBuffer[fuCurrDpbIdx].clear();
+  }
 
   return kTRUE;
 }
diff --git a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.h b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.h
index b2e9f0c601..536f2757ad 100644
--- a/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.h
+++ b/fles/mcbm2018/unpacker/CbmMcbm2018UnpackerAlgoTof.h
@@ -133,6 +133,7 @@ private:
   /// Buffers
   std::vector<std::vector<gdpbv100::Message>>
     fvvmEpSupprBuffer;  //! [DPB], FIXME: dimension to be removed as not needed with real MS
+  Bool_t fbEpochFoundInThisMs = kFALSE;
 
   /// STAR TRIGGER detection
   std::vector<ULong64_t> fvulGdpbTsMsb;
-- 
GitLab