From 0b299d729ea1cf1742d1968b422946be24507829 Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <s.zharko@gsi.de>
Date: Sat, 28 Oct 2023 18:36:03 +0200
Subject: [PATCH] CA online: monitor fixes

---
 algo/ca/TrackingChain.cxx              | 15 ++++++++++++++-
 algo/ca/TrackingChain.h                |  2 ++
 algo/ca/core/utils/CaMonitor.h         |  6 +++---
 algo/ca/core/utils/CaTimer.h           |  2 +-
 algo/ca/core/utils/CaTrackingMonitor.h |  4 ++--
 reco/L1/CbmL1.cxx                      |  3 +--
 6 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/algo/ca/TrackingChain.cxx b/algo/ca/TrackingChain.cxx
index f34bc1c550..868f32e46a 100644
--- a/algo/ca/TrackingChain.cxx
+++ b/algo/ca/TrackingChain.cxx
@@ -11,6 +11,7 @@
 
 #include "tof/Config.h"
 
+#include <fstream>
 #include <unordered_map>
 
 #include <xpu/host.h>
@@ -60,12 +61,14 @@ TrackingChain::Return_t TrackingChain::Run(Input_t recoResults)
   fCaMonitorData.Reset();
 
   // ----- Init input data ---------------------------------------------------------------------------------------------
+  fCaMonitorData.StartTimer(ca::ETimer::PrepareInput);
   this->PrepareInput(recoResults);
+  fCaMonitorData.StopTimer(ca::ETimer::PrepareInput);
 
   // ----- Run reconstruction ------------------------------------------------------------------------------------------
   fCaFramework.SetMonitorData(fCaMonitorData);
-  fCaMonitorData = fCaFramework.GetMonitorData();
   fCaFramework.fTrackFinder.FindTracks();
+  fCaMonitorData = fCaFramework.GetMonitorData();
   L_(info) << "TrackingChain: Timeslice contains " << fCaMonitorData.GetCounterValue(ca::ECounter::RecoTrack)
            << " tracks, the FindTracks routine ran " << fCaMonitorData.GetTimer(ca::ETimer::FindTracks).GetTotal()
            << " s";
@@ -100,6 +103,9 @@ void TrackingChain::PrepareInput(Input_t recoResults)
 template<EDetectorID DetID>
 void TrackingChain::ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hits)
 {
+  std::ofstream out;
+  if constexpr (kDEBUG) { out.open(std::string("./Debug_hits_") + ca::kDetName[DetID] + ".txt"); }
+
   using Hit_t           = ca::HitTypes_t::at<DetID>;
   constexpr bool IsMvd  = (DetID == EDetectorID::Mvd);
   constexpr bool IsSts  = (DetID == EDetectorID::Sts);
@@ -129,6 +135,7 @@ void TrackingChain::ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hi
     size_t iOffset = hits.Offsets()[iPartition];
     if (iStActive < 0) { continue; }
     double lastTime = -1e9;
+    double prevTime = -1;
     for (size_t iPartHit = 0; iPartHit < vHits.size(); ++iPartHit) {
       const auto& hit = vHits[iPartHit];
       int iHitExt     = iOffset + iPartHit;
@@ -161,6 +168,11 @@ void TrackingChain::ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hi
         lastTime = caHit.T();
         if (!IsTof) { dataStream = extHitAddress; }
         fCaDataManager.PushBackHit(caHit, dataStreamDet | dataStream);
+        if constexpr (kDEBUG) {
+          out << (dataStreamDet | dataStream) << " ----- " << caHit.ToString() << '\n';
+          if (prevTime > caHit.T()) { out << "TIME IS UNSORTED\n"; }
+        }
+
         if (fNofHitKeys <= caHit.FrontKey()) { fNofHitKeys = caHit.FrontKey() + 1; }
         if (fNofHitKeys <= caHit.BackKey()) { fNofHitKeys = caHit.BackKey() + 1; }
       }
@@ -171,6 +183,7 @@ void TrackingChain::ReadHits(PartitionedSpan<const ca::HitTypes_t::at<DetID>> hi
         if constexpr (IsTrd) { fCaMonitorData.IncrementCounter(ca::ECounter::UndefinedTrdHit); }
         if constexpr (IsTof) { fCaMonitorData.IncrementCounter(ca::ECounter::UndefinedTofHit); }
       }
+      prevTime = caHit.T();
       // ---- Update number of hit keys
     }  // iPartHit
   }    // iPartition
diff --git a/algo/ca/TrackingChain.h b/algo/ca/TrackingChain.h
index 29301e005f..b1338cf03a 100644
--- a/algo/ca/TrackingChain.h
+++ b/algo/ca/TrackingChain.h
@@ -82,6 +82,8 @@ namespace cbm::algo
     // **  Auxilary variables
 
     ca::HitKeyIndex_t fNofHitKeys = 0;  ///< Current number of hit keys (aux)
+
+    static constexpr bool kDEBUG = false;  ///< Debug mode
   };
 
 
diff --git a/algo/ca/core/utils/CaMonitor.h b/algo/ca/core/utils/CaMonitor.h
index 3410552d14..55c524ec7b 100644
--- a/algo/ca/core/utils/CaMonitor.h
+++ b/algo/ca/core/utils/CaMonitor.h
@@ -152,13 +152,13 @@ namespace cbm::algo::ca
     using std::setw;
     std::stringstream msg;
     constexpr size_t widthKey = 30;
-    constexpr size_t width    = 12;
+    constexpr size_t width    = 24;
     msg << "\n===== Monitor: " << fsName << "\n";
     if constexpr (!std::is_same_v<ETimerKey, EDummy>) {
       msg << "\n----- Timers:\n";
       msg << setw(widthKey) << left << "Key" << ' ';
       msg << setw(width) << left << "N Calls" << ' ';
-      //msg << setw(width) << left << "Average [s]" << ' ';
+      msg << setw(width) << left << "Average [s]" << ' ';
       msg << setw(width) << left << "Min [s]" << ' ';
       msg << setw(width) << left << "Max [s]" << ' ';
       msg << setw(width) << left << "Total [s]" << '\n';
@@ -167,7 +167,7 @@ namespace cbm::algo::ca
         const Timer& timer = fMonitorData.GetTimer(static_cast<ETimerKey>(iKey));
         msg << setw(widthKey) << faTimerNames[iKey] << ' ';
         msg << setw(width) << timer.GetNofCalls() << ' ';
-        //msg << setw(width) << timer.GetAverage() << ' ';
+        msg << setw(width) << timer.GetAverage() << ' ';
         msg << setw(width) << timer.GetMin() << ' ';
         msg << setw(width) << timer.GetMax() << ' ';
         msg << setw(width) << timer.GetTotal() << '\n';
diff --git a/algo/ca/core/utils/CaTimer.h b/algo/ca/core/utils/CaTimer.h
index 1714bebc90..8ce67e669d 100644
--- a/algo/ca/core/utils/CaTimer.h
+++ b/algo/ca/core/utils/CaTimer.h
@@ -49,7 +49,7 @@ namespace cbm::algo::ca
     void AddTimer(const Timer& other);
 
     /// \brief Gets average time [s]
-    double GetAverage() const { return static_cast<double>(fTotal / fNofCalls) * 1.e-9; }
+    double GetAverage() const { return static_cast<double>(fTotal) / fNofCalls * 1.e-9; }
 
     /// \brief Gets time of the longest call [s]
     double GetMax() const { return static_cast<double>(fMax) * 1.e-9; }
diff --git a/algo/ca/core/utils/CaTrackingMonitor.h b/algo/ca/core/utils/CaTrackingMonitor.h
index ceae694acc..09c4f5408f 100644
--- a/algo/ca/core/utils/CaTrackingMonitor.h
+++ b/algo/ca/core/utils/CaTrackingMonitor.h
@@ -37,7 +37,7 @@ namespace cbm::algo::ca
   enum class ETimer
   {
     //Tracking,
-    //PrepareInput,
+    PrepareInput,
     FindTracks,
     TrackFinder,
     TrackFitter,
@@ -67,7 +67,7 @@ namespace cbm::algo::ca
       SetCounterName(ECounter::UndefinedTrdHit, "undefined TRD hits");
       SetCounterName(ECounter::UndefinedTofHit, "undefined TOF hits");
       //SetTimerName(ETimer::Tracking, "full tracking");
-      //SetTimerName(ETimer::PrepareInput, "input data initialization");
+      SetTimerName(ETimer::PrepareInput, "input data initialization");
       SetTimerName(ETimer::FindTracks, "find tracks procedure");
       SetTimerName(ETimer::TrackFinder, "track finder");
       SetTimerName(ETimer::TrackFitter, "track fitter");
diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 7037b2e521..bf232132fa 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -918,8 +918,7 @@ void CbmL1::Finish()
   LOG(info) << "\033[31;1m -------------------------------------------------------------\033[0m";
 
   // monitor of the reconstructed tracks
-  // FIXME: For some reason this string causes the crash of the analysis tree converter. Further investigations are needed
-  //LOG(info) << fMonitor.ToString();
+  LOG(info) << fMonitor.ToString();
 
   TDirectory* curr   = gDirectory;
   TFile* currentFile = gFile;
-- 
GitLab