From 7d7b1a1d1849867844b47cbef08d2365b0050003 Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Thu, 26 Sep 2024 17:06:05 +0200
Subject: [PATCH] Ca: improve thread allocation for multi-threaded mode

---
 algo/ca/core/tracking/CaTrackFinder.cxx | 42 +++++++++++--------------
 algo/ca/core/tracking/CaTrackFinder.h   |  3 --
 2 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/algo/ca/core/tracking/CaTrackFinder.cxx b/algo/ca/core/tracking/CaTrackFinder.cxx
index c3650fe621..4706cd67d3 100644
--- a/algo/ca/core/tracking/CaTrackFinder.cxx
+++ b/algo/ca/core/tracking/CaTrackFinder.cxx
@@ -147,7 +147,7 @@ TrackFinder::Output_t TrackFinder::FindTracks(const InputData& input, TimesliceH
       }
       maxTimeBeforeHit       = std::max(maxTimeBeforeHit, info.fEventTimeMax);
       info.fMaxTimeBeforeHit = maxTimeBeforeHit;
-      fStatTsStart           = std::min(fStatTsStart, info.fEventTimeMax - fWindowMargin);  // 5 ns margin
+      fStatTsStart           = std::min(fStatTsStart, info.fEventTimeMax);
       fStatTsEnd             = std::max(fStatTsEnd, info.fEventTimeMin);
     }
 
@@ -187,22 +187,19 @@ TrackFinder::Output_t TrackFinder::FindTracks(const InputData& input, TimesliceH
       }
     }
   }
-  // all hits belong to one sub-timeslice; 500 ms maximal length of the TS
-  fStatTsEnd = std::clamp(fStatTsEnd, fStatTsStart, fStatTsStart + 500.e6f);
+  // all hits belong to one sub-timeslice; 1 s is the maximal length of the TS
+  fStatTsEnd = std::clamp(fStatTsEnd, fStatTsStart, fStatTsStart + 1.e9f);
 
   LOG(debug) << "CA tracker process time slice " << fStatTsStart * 1.e-6 << " -- " << fStatTsEnd * 1.e-6
              << " [ms] with " << fStatNhitsTotal << " hits";
 
-  int nWindows = static_cast<int>((fStatTsEnd - fStatTsStart - fWindowOverlap - fWindowMargin)
-                                  / (fWindowLength - fWindowOverlap - fWindowMargin))
-                 + 1;
+  int nWindows = static_cast<int>((fStatTsEnd - fStatTsStart) / fWindowLength) + 1;
   if (nWindows < 1) {  // Situation, when fStatTsEnd == fStatTsStart
     nWindows = 1;
   }
 
   // int nWindowsThread = nWindows / fNofThreads;
-  const fscal windowDelta = fWindowLength - fWindowOverlap - fWindowMargin;
-  //LOG(info) << "CA: estimated number of time windows: " << nWindows;
+  // LOG(info) << "CA: estimated number of time windows: " << nWindows;
 
   std::vector<std::pair<fscal, fscal>> vWindowRangeThread(fNofThreads);
   {  // Estimation of number of hits in time windows
@@ -212,15 +209,17 @@ TrackFinder::Output_t TrackFinder::FindTracks(const InputData& input, TimesliceH
     const int nSt             = fParameters.GetNstationsActive();
 
     // Count number of hits per window and station
-    const double a = fStatTsStart + fWindowOverlap + fWindowMargin;
-    const double b = fWindowLength - fWindowOverlap - fWindowMargin;
     std::vector<HitIndex_t> nHitsWindowSta(nWindows * nSt, 0);
 
     for (HitIndex_t iHit = 0; iHit < nHitsTot; ++iHit) {
-      const auto& hit      = input.GetHit(iHit);
-      const size_t iWindow = static_cast<size_t>((hit.T() - a) / b);
-      if (iWindow >= static_cast<size_t>(nWindows)) {
-        LOG(error) << "ca: Hit out of range: iHit = " << iHit << ", time = " << hit.T() * 1.e-6
+      const auto& hit  = input.GetHit(iHit);
+      const auto& info = fHitTimeInfo[iHit];
+      int iWindow      = static_cast<int>((info.fEventTimeMin - fStatTsStart) / fWindowLength);
+      if (iWindow < 0) {
+        iWindow = 0;
+      }
+      if (iWindow >= nWindows) {
+        LOG(error) << "ca: Hit out of range: iHit = " << iHit << ", min. event time = " << info.fEventTimeMin * 1.e-6
                    << " ms, window = " << iWindow;
         continue;
       }
@@ -252,7 +251,7 @@ TrackFinder::Output_t TrackFinder::FindTracks(const InputData& input, TimesliceH
     for (int iTh = 1; iTh < fNofThreads; ++iTh) {
       windowIt                           = std::lower_bound(windowIt, nHitsWindow.end(), iTh * nHitsPerThread);
       const size_t iWbegin               = std::distance(nHitsWindow.begin(), windowIt) + 1;
-      vWindowRangeThread[iTh].first      = fStatTsStart + iWbegin * windowDelta;
+      vWindowRangeThread[iTh].first      = fStatTsStart + iWbegin * fWindowLength;
       vWindowRangeThread[iTh - 1].second = vWindowRangeThread[iTh].first;
     }
     vWindowRangeThread[fNofThreads - 1].second = fStatTsEnd;
@@ -264,16 +263,16 @@ TrackFinder::Output_t TrackFinder::FindTracks(const InputData& input, TimesliceH
 
   // cut data into sub-timeslices and process them one by one
   //for (int iThread = 0; iThread < fNofThreads; ++iThread) {
-  //  vWindowStartThread[iThread] = fStatTsStart + iThread * nWindowsThread * windowDelta;
+  //  vWindowStartThread[iThread] = fStatTsStart + iThread * nWindowsThread * fWindowLength;
   //  vWindowEndThread[iThread] =
-  //    vWindowStartThread[iThread] + nWindowsThread * windowDelta + fWindowOverlap + fWindowMargin;
+  //    vWindowStartThread[iThread] + nWindowsThread * fWindowLength;
   //}
 
   for (int iThread = 0; iThread < fNofThreads; ++iThread) {
     auto& entry  = vWindowRangeThread[iThread];
     double start = entry.first * 1.e-6;
     double end   = entry.second * 1.e-6;
-    LOG(trace) << "Thread: " << iThread << " from " << start << " ms  to " << end << " ms (delta = " << end - start
+    LOG(debug) << "Thread: " << iThread << " from " << start << " ms  to " << end << " ms (delta = " << end - start
                << " ms)";
   }
 
@@ -453,7 +452,7 @@ void TrackFinder::FindTracksThread(const InputData& input, int iThread, std::pai
 
         // the hit belongs to the sub-slice
         wData.TsHitIndices(h.Station()).push_back(caHitId);
-        if (info.fMaxTimeBeforeHit < windowRange.first + fWindowLength - fWindowOverlap) {
+        if (info.fMaxTimeBeforeHit < windowRange.first + fWindowLength) {
           range.first = caHitId + 1;  // this hit and all hits before are before the overlap
         }
       }
@@ -513,7 +512,7 @@ void TrackFinder::FindTracksThread(const InputData& input, int iThread, std::pai
 
     // save reconstructed tracks with no hits in the overlap region
     //if (windowRange.first > 13.23e6 && windowRange.first < 13.26e6) {
-    windowRange.first += fWindowLength - fWindowOverlap;
+    windowRange.first += fWindowLength;
     // we should add hits from reconstructed but not stored tracks to the new sub-timeslice
     // we do it in a simple way by extending the tsStartNew
     // TODO: only add those hits from the region before tsStartNew that belong to the not stored tracks
@@ -556,9 +555,6 @@ void TrackFinder::FindTracksThread(const InputData& input, int iThread, std::pai
     if (windowRange.first > windowRange.second) {
       break;
     }
-    else {
-      windowRange.first -= fWindowMargin;  // do 5 ns margin
-    }
     if (!areUntouchedDataLeft) {
       break;
     }
diff --git a/algo/ca/core/tracking/CaTrackFinder.h b/algo/ca/core/tracking/CaTrackFinder.h
index 5a1cd6d810..c922c527c8 100644
--- a/algo/ca/core/tracking/CaTrackFinder.h
+++ b/algo/ca/core/tracking/CaTrackFinder.h
@@ -82,9 +82,6 @@ namespace cbm::algo::ca
     std::vector<Vector<HitIndex_t>> fvRecoHitIndices;  ///< packed hits of reconstructed tracks
 
     fscal fWindowLength  = 0.;   ///< Time window length [ns]
-    fscal fWindowOverlap = 15.;  ///< Time window overlap [ns]
-    fscal fWindowMargin  = 5.;   ///< Time window margin [ns] (TODO: Why cannot we include margin into the overlap?)
-
     fscal fStatTsStart  = 0.;
     fscal fStatTsEnd    = 0.;
     int fStatNhitsTotal = 0;
-- 
GitLab