From fcf0c97ae8b0b3979d8c039669df82ef87b59b3c Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <s.zharko@gsi.de>
Date: Sat, 21 Oct 2023 23:28:08 +0200
Subject: [PATCH] CA: monitor updates

---
 algo/ca/core/tracking/CaFramework.h           |  8 +++++--
 algo/ca/core/tracking/CaTrackFinder.cxx       | 23 +++++++++++--------
 algo/ca/core/tracking/CaTrackFinder.h         |  8 +++----
 algo/ca/core/tracking/CaTrackFinderWindow.cxx | 10 ++++----
 algo/ca/core/utils/CaMonitor.h                |  4 ++++
 algo/ca/core/utils/CaMonitorData.h            | 17 ++++++++++++++
 algo/ca/core/utils/CaTimer.h                  | 20 ++++++++++++++++
 algo/ca/core/utils/CaTrackingMonitor.h        |  3 ++-
 reco/L1/CbmL1.cxx                             |  3 +--
 9 files changed, 72 insertions(+), 24 deletions(-)

diff --git a/algo/ca/core/tracking/CaFramework.h b/algo/ca/core/tracking/CaFramework.h
index a279435e0d..eca8048358 100644
--- a/algo/ca/core/tracking/CaFramework.h
+++ b/algo/ca/core/tracking/CaFramework.h
@@ -179,9 +179,12 @@ namespace cbm::algo::ca
 
     float GetMaxInvMom() const { return fMaxInvMom[0]; }
 
-    /// Gets reference to the monitor
+    /// \brief Gets reference to the monitor
     const TrackingMonitor& GetMonitor() const { return fMonitor; }
 
+    /// \brief Gets reference to the monitor data per call
+    const TrackingMonitorData& GetMonitorDataPerCall() const { return fMonitorDataPerCall; }
+
   public:
     /// Gets number of stations before the pipe (MVD stations in CBM)
     int GetNstationsBeforePipe() const { return fNstationsBeforePipe; }
@@ -213,7 +216,8 @@ namespace cbm::algo::ca
     Vector<unsigned char> fvHitKeyFlags {
       "Framework::fvHitKeyFlags"};  ///< List of key flags: has been this hit or cluster already used
 
-    ca::TrackingMonitor fMonitor {"CA Algo"};  ///< Tracking monitor
+    ca::TrackingMonitor fMonitor {"CA Algo"};        ///< Tracking monitor (statistics per run)
+    ca::TrackingMonitorData fMonitorDataPerCall {};  ///< Tracking monitor data (statistics per call)
 
   public:
     Vector<CaHitTimeInfo> fHitTimeInfo;
diff --git a/algo/ca/core/tracking/CaTrackFinder.cxx b/algo/ca/core/tracking/CaTrackFinder.cxx
index 5e9732bd68..4d80fb1771 100644
--- a/algo/ca/core/tracking/CaTrackFinder.cxx
+++ b/algo/ca/core/tracking/CaTrackFinder.cxx
@@ -49,9 +49,9 @@ void TrackFinder::FindTracks()
   //
 
   // Reset monitor
-  frAlgo.fMonitor.Reset();
-  frAlgo.fMonitor.IncrementCounter(ECounter::RecoHit, frAlgo.fInputData.GetNhits());
-  frAlgo.fMonitor.StartTimer(ETimer::Tracking);
+  frAlgo.fMonitorDataPerCall.Reset();
+  frAlgo.fMonitorDataPerCall.IncrementCounter(ECounter::RecoHit, frAlgo.fInputData.GetNhits());
+  frAlgo.fMonitorDataPerCall.StartTimer(ETimer::Tracking);
 
   auto timerStart = std::chrono::high_resolution_clock::now();
 
@@ -190,13 +190,13 @@ void TrackFinder::FindTracks()
       }
     }
 
-    frAlgo.fMonitor.StartTimer(ETimer::TrackFinder);
+    frAlgo.fMonitorDataPerCall.StartTimer(ETimer::TrackFinder);
     frAlgo.fTrackFinderWindow.CaTrackFinderSlice();
-    frAlgo.fMonitor.StopTimer(ETimer::TrackFinder);
+    frAlgo.fMonitorDataPerCall.StopTimer(ETimer::TrackFinder);
 
-    frAlgo.fMonitor.StartTimer(ETimer::TrackFitter);
+    frAlgo.fMonitorDataPerCall.StartTimer(ETimer::TrackFitter);
     frAlgo.fTrackFitter.FitCaTracks();
-    frAlgo.fMonitor.StopTimer(ETimer::TrackFitter);
+    frAlgo.fMonitorDataPerCall.StopTimer(ETimer::TrackFitter);
 
     // save reconstructed tracks with no hits in the overlap region
 
@@ -252,9 +252,12 @@ void TrackFinder::FindTracks()
     tsStart -= 5;  // do 5 ns margin
   }
 
-  frAlgo.fMonitor.StopTimer(ETimer::Tracking);
-  frAlgo.fMonitor.IncrementCounter(ECounter::RecoTrack, frAlgo.fRecoTracks.size());
-  frAlgo.fMonitor.IncrementCounter(ECounter::RecoHitUsed, frAlgo.fRecoHits.size());
+  frAlgo.fMonitorDataPerCall.StopTimer(ETimer::Tracking);
+  frAlgo.fMonitorDataPerCall.IncrementCounter(ECounter::RecoTrack, frAlgo.fRecoTracks.size());
+  frAlgo.fMonitorDataPerCall.IncrementCounter(ECounter::RecoHitUsed, frAlgo.fRecoHits.size());
+
+  // Sum up the monitor data into the total monitor
+  frAlgo.fMonitor.AddMonitorData(frAlgo.fMonitorDataPerCall);
 
   auto timerEnd      = std::chrono::high_resolution_clock::now();
   frAlgo.fCaRecoTime = (double) (std::chrono::duration<double>(timerEnd - timerStart).count());
diff --git a/algo/ca/core/tracking/CaTrackFinder.h b/algo/ca/core/tracking/CaTrackFinder.h
index 4e2db5dccc..535241c34b 100644
--- a/algo/ca/core/tracking/CaTrackFinder.h
+++ b/algo/ca/core/tracking/CaTrackFinder.h
@@ -50,15 +50,15 @@ namespace cbm::algo::ca
     void FindTracks();
 
   private:
-    ///-------------------------------
-    /// Private methods
+    // -------------------------------
+    // Private methods
 
     bool checkTripletMatch(const ca::Triplet& l, const ca::Triplet& r, fscal& dchi2) const;
 
 
   private:
-    ///-------------------------------
-    /// Data members
+    // -------------------------------
+    // Data members
 
     ca::Framework& frAlgo;  ///< Reference to the main track finder algorithm class
   };
diff --git a/algo/ca/core/tracking/CaTrackFinderWindow.cxx b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
index 25b4dcdec6..e00da9e137 100644
--- a/algo/ca/core/tracking/CaTrackFinderWindow.cxx
+++ b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
@@ -302,7 +302,7 @@ void TrackFinderWindow::CaTrackFinderSlice()
 
     ///   stage for triplets creation
 
-    frAlgo.fMonitor.StartTimer(ETimer::TripletConstruction);
+    frAlgo.fMonitorDataPerCall.StartTimer(ETimer::TripletConstruction);
 
     ca::TripletConstructor constructor1(&frAlgo);
     ca::TripletConstructor constructor2(&frAlgo);
@@ -335,10 +335,10 @@ void TrackFinderWindow::CaTrackFinderSlice()
       }
     }  // istal
 
-    frAlgo.fMonitor.StopTimer(ETimer::TripletConstruction);
+    frAlgo.fMonitorDataPerCall.StopTimer(ETimer::TripletConstruction);
 
     // search for neighbouring triplets
-    frAlgo.fMonitor.StartTimer(ETimer::NeighboringTripletSearch);
+    frAlgo.fMonitorDataPerCall.StartTimer(ETimer::NeighboringTripletSearch);
     for (int istal = frAlgo.GetParameters().GetNstationsActive() - 2; istal >= frAlgo.fFirstCAstation;
          istal--) {  // start with downstream chambers
 
@@ -379,10 +379,10 @@ void TrackFinderWindow::CaTrackFinderSlice()
         tr.SetLevel(level);
       }  // neighbour search
 
-      frAlgo.fMonitor.IncrementCounter(ECounter::Triplet, frAlgo.fTriplets[istal].size());
+      frAlgo.fMonitorDataPerCall.IncrementCounter(ECounter::Triplet, frAlgo.fTriplets[istal].size());
 
     }  // istal
-    frAlgo.fMonitor.StopTimer(ETimer::NeighboringTripletSearch);
+    frAlgo.fMonitorDataPerCall.StopTimer(ETimer::NeighboringTripletSearch);
 
 
     ///====================================================================
diff --git a/algo/ca/core/utils/CaMonitor.h b/algo/ca/core/utils/CaMonitor.h
index 5a65f0f0bf..b65098e731 100644
--- a/algo/ca/core/utils/CaMonitor.h
+++ b/algo/ca/core/utils/CaMonitor.h
@@ -59,6 +59,10 @@ namespace cbm::algo::ca
     /// \brief Move assignment operator
     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); }
+
     /// \brief Gets counter name
     /// \param key Counter key
     const std::string& GetCounterName(ECounterKey key) const { return faCounterNames[key]; }
diff --git a/algo/ca/core/utils/CaMonitorData.h b/algo/ca/core/utils/CaMonitorData.h
index ef316ce0d9..5c1475d40f 100644
--- a/algo/ca/core/utils/CaMonitorData.h
+++ b/algo/ca/core/utils/CaMonitorData.h
@@ -48,6 +48,10 @@ namespace cbm::algo::ca
     /// \brief Move assignment operator
     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);
+
     /// \brief Gets counter value
     /// \param key
     int GetCounterValue(ECounterKey key) const { return faCounters[key]; }
@@ -91,6 +95,19 @@ namespace cbm::algo::ca
   // **  Template function implementations  **
   // *****************************************
 
+  // ---------------------------------------------------------------------------------------------------------------------
+  //
+  template<class ECounterKey, class ETimerKey>
+  inline void MonitorData<ECounterKey, ETimerKey>::AddMonitorData(const MonitorData<ECounterKey, ETimerKey>& other)
+  {
+    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]);
+    }
+  }
+
   // ---------------------------------------------------------------------------------------------------------------------
   //
   template<class ECounterKey, class ETimerKey>
diff --git a/algo/ca/core/utils/CaTimer.h b/algo/ca/core/utils/CaTimer.h
index 8646dc5656..1714bebc90 100644
--- a/algo/ca/core/utils/CaTimer.h
+++ b/algo/ca/core/utils/CaTimer.h
@@ -44,6 +44,10 @@ namespace cbm::algo::ca
     /// \brief Move assignment operator
     Timer& operator=(Timer&&) = default;
 
+    /// \brief Adds another timer
+    /// \param other  Reference to the other Timer object to add
+    void AddTimer(const Timer& other);
+
     /// \brief Gets average time [s]
     double GetAverage() const { return static_cast<double>(fTotal / fNofCalls) * 1.e-9; }
 
@@ -89,6 +93,22 @@ namespace cbm::algo::ca
   // **   Inline function definition   **
   // ************************************
 
+  // -------------------------------------------------------------------------------------------------------------------
+  //
+  inline void Timer::AddTimer(const Timer& other)
+  {
+    if (other.fMin < fMin) {
+      fMin          = other.fMin;
+      fMinCallIndex = other.fMinCallIndex + fNofCalls;
+    }
+    if (other.fMax > fMax) {
+      fMax          = other.fMax;
+      fMaxCallIndex = other.fMinCallIndex + fNofCalls;
+    }
+    fTotal += other.fTotal;
+    fNofCalls += other.fNofCalls;
+  }
+
   // -------------------------------------------------------------------------------------------------------------------
   //
   inline void Timer::Reset()
diff --git a/algo/ca/core/utils/CaTrackingMonitor.h b/algo/ca/core/utils/CaTrackingMonitor.h
index 9aa13cd113..2458141c41 100644
--- a/algo/ca/core/utils/CaTrackingMonitor.h
+++ b/algo/ca/core/utils/CaTrackingMonitor.h
@@ -37,5 +37,6 @@ namespace cbm::algo::ca
     kEND
   };
 
-  using TrackingMonitor = Monitor<ECounter, ETimer>;
+  using TrackingMonitor     = Monitor<ECounter, ETimer>;
+  using TrackingMonitorData = MonitorData<ECounter, ETimer>;
 }  // namespace cbm::algo::ca
diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 0a5df93def..2d19512d19 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -801,8 +801,6 @@ void CbmL1::Reconstruct(CbmEvent* event)
 
   if (fVerbose > 1) { LOG(info) << "L1 Track finder ok"; }
 
-  LOG(info) << fpAlgo->GetMonitor().ToString();
-
   // save reconstructed tracks
 
   fvRecoTracks.clear();
@@ -920,6 +918,7 @@ void CbmL1::Finish()
   LOG(info) << "\033[31;1m -------------------------------------------------------------\033[0m";
 
   // monitor of the reconstructed tracks
+  //LOG(info) << fpAlgo->GetMonitor().ToString();
   LOG(info) << '\n' << fMonitor.ToString();
 
   TDirectory* curr   = gDirectory;
-- 
GitLab