From 5cb644d98fe76d37cc875bb5f1e872aa5abbe097 Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Wed, 1 Mar 2023 21:52:53 +0000
Subject: [PATCH] L1: optimise reading of MC data in debug mode

---
 reco/L1/CbmL1.cxx                             | 45 ++++++++++++++++---
 reco/L1/CbmL1MCTrack.cxx                      |  2 +-
 reco/L1/CbmL1MCTrack.h                        |  2 +
 reco/L1/L1Algo/L1CaTrackFinder.cxx            |  6 +--
 .../L1/OffLineInterface/CbmL1StsTrackFinder.h |  9 ++--
 5 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 0a1dec0c40..38a63d8a9b 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -34,6 +34,7 @@
 #include <boost/filesystem.hpp>
 // TODO: include of CbmSetup.h creates problems on Mac
 // #include "CbmSetup.h"
+#include "CbmEvent.h"
 #include "CbmMCDataObject.h"
 #include "CbmStsFindTracks.h"
 #include "CbmStsHit.h"
@@ -882,14 +883,41 @@ InitStatus CbmL1::Init()
 void CbmL1::Reconstruct(CbmEvent* event)
 {
   static int nevent = 0;
+
   fvSelectedMcEvents.clear();
 
+  int bestMcFile  = -1;
+  int bestMcEvent = -1;
+
   if (fPerformance) {
-    int nofEvents = fpMcEventList->GetNofEvents();
-    for (int iE = 0; iE < nofEvents; iE++) {
-      int fileId  = fpMcEventList->GetFileIdByIndex(iE);
-      int eventId = fpMcEventList->GetEventIdByIndex(iE);
-      fvSelectedMcEvents.insert(DFSET::value_type(fileId, eventId));
+    if (event) {
+      CbmMatch* match = event->GetMatch();
+      if (!match) {
+        LOG(error) << "CbmL1: match between reco and MC events missing!! Performance can not be evaluated!!";
+        fPerformance = 0;
+      }
+      else {
+        cout << "CbmL1: mc events all " << fpMcEventList->GetNofEvents() << " mc events linked " << match->GetNofLinks()
+             << std::endl;
+        for (int iLink = 0; iLink < match->GetNofLinks(); iLink++) {
+          const CbmLink& link = match->GetLink(iLink);
+          fvSelectedMcEvents.insert(DFSET::value_type(link.GetFile(), link.GetEntry()));
+          cout << "CbmL1: linked mc event file " << link.GetFile() << " event  " << link.GetEntry() << std::endl;
+        }
+        if (match->GetNofLinks() > 1) {
+          const CbmLink& link = match->GetMatchedLink();
+          bestMcFile          = link.GetFile();
+          bestMcEvent         = link.GetEntry();
+        }
+      }
+    }
+    else {
+      int nofEvents = fpMcEventList->GetNofEvents();
+      for (int iE = 0; iE < nofEvents; iE++) {
+        int fileId  = fpMcEventList->GetFileIdByIndex(iE);
+        int eventId = fpMcEventList->GetEventIdByIndex(iE);
+        fvSelectedMcEvents.insert(DFSET::value_type(fileId, eventId));
+      }
     }
   }
 
@@ -902,16 +930,19 @@ void CbmL1::Reconstruct(CbmEvent* event)
 
   ReadEvent(event);
 
-
   if (fPerformance) {
     HitMatch();
     // calculate the max number of Hits\mcPoints on continuous(consecutive) stations
     for (auto it = fvMCTracks.begin(); it != fvMCTracks.end(); ++it) {
       it->Init();
     }
+    if (bestMcFile >= 0) {  // suppress mc tracks from complementary mc events
+      for (auto it = fvMCTracks.begin(); it != fvMCTracks.end(); ++it) {
+        if (it->iFile != bestMcFile || it->iEvent != bestMcEvent) { it->SetIsReconstructable(false); }
+      }
+    }
   }
 
-
   if ((fPerformance) && (fSTAPDataMode < 2)) { InputPerformance(); }
 
   //  FieldApproxCheck();
diff --git a/reco/L1/CbmL1MCTrack.cxx b/reco/L1/CbmL1MCTrack.cxx
index befff96e6c..d4df61048e 100644
--- a/reco/L1/CbmL1MCTrack.cxx
+++ b/reco/L1/CbmL1MCTrack.cxx
@@ -78,7 +78,7 @@ void CbmL1MCTrack::Init()
   CalculateMaxNStaMC();
   CalculateMaxNStaHits();
   CalculateIsReconstructable();
-}  // void CbmL1MCTrack::Init()
+}
 
 
 float CbmL1MCTrack::Fraction_MC()
diff --git a/reco/L1/CbmL1MCTrack.h b/reco/L1/CbmL1MCTrack.h
index 007b07bbd3..6dcac4bd72 100644
--- a/reco/L1/CbmL1MCTrack.h
+++ b/reco/L1/CbmL1MCTrack.h
@@ -61,6 +61,8 @@ public:
   void AddTouchTrack(CbmL1Track* tTr) { tTracks.push_back_no_warning(tTr); }
   bool IsDisturbed() const { return tTracks.size(); }
 
+  void SetIsReconstructable(bool v) { isReconstructable = v; }
+
   friend class CbmL1;
 
   double pt() { return sqrt(px * px + py * py); }
diff --git a/reco/L1/L1Algo/L1CaTrackFinder.cxx b/reco/L1/L1Algo/L1CaTrackFinder.cxx
index ba9d808422..daeb6ad4dc 100644
--- a/reco/L1/L1Algo/L1CaTrackFinder.cxx
+++ b/reco/L1/L1Algo/L1CaTrackFinder.cxx
@@ -62,8 +62,8 @@ void L1Algo::CaTrackFinder()
 
   for (int iS = 0; iS < fParameters.GetNstationsActive(); ++iS) {
 
-    const L1Station& st       = fParameters.GetStation(iS);
-    int nStationHits          = fInputData.GetNhits(iS);
+    const L1Station& st = fParameters.GetStation(iS);
+    int nStationHits    = fInputData.GetNhits(iS);
 
     fscal maxTimeBeforeHit = std::numeric_limits<fscal>::min();
 
@@ -220,4 +220,4 @@ void L1Algo::CaTrackFinder()
   auto timerEnd = std::chrono::high_resolution_clock::now();
   fCaRecoTime   = (double) (std::chrono::duration<double>(timerEnd - timerStart).count());
   std::cout << "CaTracker: nSubSlices processed = " << nSubSlices << std::endl;
-}
\ No newline at end of file
+}
diff --git a/reco/L1/OffLineInterface/CbmL1StsTrackFinder.h b/reco/L1/OffLineInterface/CbmL1StsTrackFinder.h
index d215562322..06b9292860 100644
--- a/reco/L1/OffLineInterface/CbmL1StsTrackFinder.h
+++ b/reco/L1/OffLineInterface/CbmL1StsTrackFinder.h
@@ -37,22 +37,23 @@ public:
 
 
   /** Destructor **/
-  virtual ~CbmL1StsTrackFinder();
+  ~CbmL1StsTrackFinder();
 
 
   /** Initialisation **/
-  virtual void Init();
+  void Init();
 
 
   /** Track finding algorithm
    **/
-  virtual Int_t DoFind();
+  Int_t DoFind();
 
   /** Execute track finding on one event
    ** @param event  Pointer to event object
    ** @value Number of created tracks
    **/
-  virtual Int_t FindTracks(CbmEvent* event);
+  Int_t FindTracks(CbmEvent* event);
+
 
   /// set a default particle mass for the track fit
   /// it is used during reconstruction
-- 
GitLab