From 4b7da7709c21cb93268df1c9cb80606c65c6098b Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <s.zharko@gsi.de>
Date: Thu, 21 Mar 2024 19:51:48 +0100
Subject: [PATCH] CaCore: Timer update

---
 algo/ca/core/tracking/CaTrackFinder.cxx       |  2 +-
 algo/ca/core/tracking/CaTrackFinderWindow.cxx |  2 +-
 algo/ca/core/utils/CaMonitor.h                |  8 ++++++--
 algo/ca/core/utils/CaMonitorData.h            | 10 ++++++----
 algo/ca/core/utils/CaTimer.h                  | 20 +++++++++++++++----
 5 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/algo/ca/core/tracking/CaTrackFinder.cxx b/algo/ca/core/tracking/CaTrackFinder.cxx
index 1ab8b5399a..df883016ce 100644
--- a/algo/ca/core/tracking/CaTrackFinder.cxx
+++ b/algo/ca/core/tracking/CaTrackFinder.cxx
@@ -250,7 +250,7 @@ void TrackFinder::FindTracks()
 
   // Add thread monitors to the main monitor
   for (auto& monitor : frAlgo.fvMonitorDataThread) {
-    frAlgo.fMonitorData.AddMonitorData(monitor);
+    frAlgo.fMonitorData.AddMonitorData(monitor, true);
     monitor.Reset();
   }
 
diff --git a/algo/ca/core/tracking/CaTrackFinderWindow.cxx b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
index 1d3f744ba2..3ad43cadb0 100644
--- a/algo/ca/core/tracking/CaTrackFinderWindow.cxx
+++ b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
@@ -776,7 +776,7 @@ void TrackFinderWindow::CAFindTrack(int ista, ca::Branch& best_tr, const ca::Tri
         fscal new_chi2      = curr_tr.Chi2() + dchi2;
 
 
-        if (0) {  //SGtrd2d debug!!
+        if constexpr (0) {  //SGtrd2d debug!!
           int mc01 = frAlgo.GetMcTrackIdForWindowHit(curr_trip->GetLHit());
           int mc02 = frAlgo.GetMcTrackIdForWindowHit(curr_trip->GetMHit());
           int mc03 = frAlgo.GetMcTrackIdForWindowHit(curr_trip->GetRHit());
diff --git a/algo/ca/core/utils/CaMonitor.h b/algo/ca/core/utils/CaMonitor.h
index 32fe600c7d..389b196fef 100644
--- a/algo/ca/core/utils/CaMonitor.h
+++ b/algo/ca/core/utils/CaMonitor.h
@@ -65,8 +65,12 @@ namespace cbm::algo::ca
     Monitor& operator=(Monitor&&) = delete;
 
     /// \brief Adds the other monitor data to this
-    /// \param data  Reference to the other MonitorData object to add
-    void AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& data) { fMonitorData.AddMonitorData(data); }
+    /// \param data     Reference to the other MonitorData object to add
+    /// \param parallel True, if the monitor data were obtained in parallel (See Timer::AddMonitorData)
+    void AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& data, bool parallel = false)
+    {
+      fMonitorData.AddMonitorData(data, parallel);
+    }
 
     /// \brief Gets counter name
     /// \param key Counter key
diff --git a/algo/ca/core/utils/CaMonitorData.h b/algo/ca/core/utils/CaMonitorData.h
index 9a17298009..c1f0f8d7e8 100644
--- a/algo/ca/core/utils/CaMonitorData.h
+++ b/algo/ca/core/utils/CaMonitorData.h
@@ -53,8 +53,9 @@ namespace cbm::algo::ca
     MonitorData& operator=(MonitorData&&) = default;
 
     /// \brief Adds the other monitor data to this
-    /// \param other  Reference to the other MonitorData object to add
-    void AddMonitorData(const MonitorData& other);
+    /// \param other    Reference to the other MonitorData object to add
+    /// \param parallel If the monitors were filled in parallel (See CaTimer::AddTimer)
+    void AddMonitorData(const MonitorData& other, bool parallel = false);
 
     /// \brief Gets counter value
     /// \param key
@@ -110,13 +111,14 @@ namespace cbm::algo::ca
   // ---------------------------------------------------------------------------------------------------------------------
   //
   template<class ECounterKey, class ETimerKey>
-  inline void MonitorData<ECounterKey, ETimerKey>::AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& other)
+  inline void MonitorData<ECounterKey, ETimerKey>::AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& other,
+                                                                  bool parallel)
   {
     for (size_t iCounter = 0; iCounter < faCounters.size(); ++iCounter) {
       faCounters[iCounter] += other.faCounters[iCounter];
     }
     for (size_t iTimer = 0; iTimer < faTimers.size(); ++iTimer) {
-      faTimers[iTimer].AddTimer(other.faTimers[iTimer]);
+      faTimers[iTimer].AddTimer(other.faTimers[iTimer], parallel);
     }
   }
 
diff --git a/algo/ca/core/utils/CaTimer.h b/algo/ca/core/utils/CaTimer.h
index 37f7d059e9..6a9fc5ea64 100644
--- a/algo/ca/core/utils/CaTimer.h
+++ b/algo/ca/core/utils/CaTimer.h
@@ -13,6 +13,7 @@
 
 #include <chrono>
 #include <cstdint>
+#include <iostream>
 #include <limits>
 #include <string>
 
@@ -47,8 +48,12 @@ namespace cbm::algo::ca
     Timer& operator=(Timer&&) = default;
 
     /// \brief Adds another timer
-    /// \param other  Reference to the other Timer object to add
-    void AddTimer(const Timer& other);
+    /// \param other    Reference to the other Timer object to add
+    /// \param parallel Bool: if the timers were executed in parallel
+    ///
+    /// If the parallel flag is true then the resulting fTotal time is taken as a maximum of each total time of
+    /// the appended timers. If the parallel flag is false, the resulting fTotal is a sum of all timers.
+    void AddTimer(const Timer& other, bool parallel);
 
     /// \brief Gets average time [s]
     double GetAverage() const { return static_cast<double>(fTotal) / fNofCalls * 1.e-9; }
@@ -112,7 +117,7 @@ namespace cbm::algo::ca
 
   // -------------------------------------------------------------------------------------------------------------------
   //
-  inline void Timer::AddTimer(const Timer& other)
+  inline void Timer::AddTimer(const Timer& other, bool parallel)
   {
     if (other.fMin < fMin) {
       fMin          = other.fMin;
@@ -122,7 +127,14 @@ namespace cbm::algo::ca
       fMax          = other.fMax;
       fMaxCallIndex = other.fMinCallIndex + fNofCalls;
     }
-    fTotal += other.fTotal;
+    if (parallel) {
+      if (fTotal < other.fTotal) {
+        fTotal = other.fTotal;
+      }
+    }
+    else {
+      fTotal += other.fTotal;
+    }
     fNofCalls += other.fNofCalls;
   }
 
-- 
GitLab