From bfcd018fd3ffa3dab69ea21d12abbb88ab4a1ee3 Mon Sep 17 00:00:00 2001
From: Dominik Smith <d.smith@gsi.de>
Date: Wed, 31 Aug 2022 10:45:35 +0200
Subject: [PATCH] Added protection against out-of-sequence timeslice reading to
 CbmSourceTs::ReadEvent(). This is not supported but could previously be
 requested by the user, leading to unexpected behavior.

---
 reco/tasks/CbmSourceTs.cxx | 40 ++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/reco/tasks/CbmSourceTs.cxx b/reco/tasks/CbmSourceTs.cxx
index 86cbfa7d62..5bc8f8f7e5 100644
--- a/reco/tasks/CbmSourceTs.cxx
+++ b/reco/tasks/CbmSourceTs.cxx
@@ -50,24 +50,40 @@ Bool_t CbmSourceTs::Init()
 
 
 // -----   Read one time slice from archive   ---------------------------------
-Int_t CbmSourceTs::ReadEvent(UInt_t)
+Int_t CbmSourceTs::ReadEvent(UInt_t tsIndex)
 {
-  // Timeslices can only be read sequentially, so the argument is ignored.
+  // Timeslices can only be read sequentially.
   // It appears that the first call to this method from FairRunOnline is in the
   // init stage. In order not to always lose the first timeslice, a call to
   // TimesliceSource::get is avoided in the first call.
-  if (fNumCalls == 0) LOG(info) << "SourceTs: Init call to ReadEvent";
+  if (fNumCalls == 0) {
+    LOG(info) << "SourceTs: Init call to ReadEvent";
+    fNumCalls++;
+  }
+  else if (tsIndex > 0 && tsIndex < fNumCalls - 1) {
+    LOG(error) << "SourceTs: Out-of-sequence reading of timeslices not supported.";
+    return 1;
+  }
   else {
-    fFlesTs = nullptr;
-    fFlesTs = fFlesSource->get();
-    if (!fFlesTs) {
-      LOG(info) << "SourceTs: End of archive reached; stopping run.";
-      return 1;
-    }
-    LOG(info) << "SourceTs: Reading time slice " << GetNumTs() << " (index " << fFlesTs->index()
-              << ") at t = " << fFlesTs->start_time() << " ns";
+    do {
+      fFlesTs = nullptr;
+      fFlesTs = fFlesSource->get();
+      if (!fFlesTs) {
+        LOG(info) << "SourceTs: End of archive reached; stopping run.";
+        return 1;
+      }
+      if (tsIndex == fNumCalls - 1) {
+        LOG(info) << "SourceTs: Reading time slice " << GetNumTs() << " (index " << fFlesTs->index()
+                  << ") at t = " << fFlesTs->start_time() << " ns";
+      }
+      else {
+        LOG(info) << "SourceTs: Skipping time slice " << GetNumTs() << " (index " << fFlesTs->index()
+                  << ") at t = " << fFlesTs->start_time() << " ns";
+        LOG(info) << "(TS is still fetched from disk, this may take some time)";
+      }
+      fNumCalls++;
+    } while (tsIndex >= fNumCalls - 1);
   }
-  fNumCalls++;
   return 0;
 }
 // ----------------------------------------------------------------------------
-- 
GitLab