From a525d540422aed3bf32d255520e48d3f6a4a5666 Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Mon, 15 Jan 2024 15:36:00 +0000
Subject: [PATCH] Ca: fix contradictory cuts on min n hits

---
 algo/ca/core/tracking/CaFramework.cxx         |  2 -
 algo/ca/core/tracking/CaFramework.h           |  2 -
 algo/ca/core/tracking/CaTrackFinderWindow.cxx | 71 +++++++++----------
 macro/L1/configs/ca_params_mcbm.yaml          |  2 +-
 reco/L1/CbmL1.cxx                             | 10 +--
 reco/L1/CbmL1.h                               |  2 -
 6 files changed, 40 insertions(+), 49 deletions(-)

diff --git a/algo/ca/core/tracking/CaFramework.cxx b/algo/ca/core/tracking/CaFramework.cxx
index 173093f53b..0bc41352f2 100644
--- a/algo/ca/core/tracking/CaFramework.cxx
+++ b/algo/ca/core/tracking/CaFramework.cxx
@@ -59,8 +59,6 @@ void Framework::ReceiveParameters(Parameters&& parameters)
                                      [](const ca::StationV& s, int edge) { return bool(s.fieldStatus) > edge; })
                     - fParameters.GetStations().cbegin();
 
-  fGhostSuppression = fParameters.GetGhostSuppression();
-
   ca::FieldRegionV::ForceUseOfOriginalField(fParameters.DevIsUseOfOriginalField());
 }
 
diff --git a/algo/ca/core/tracking/CaFramework.h b/algo/ca/core/tracking/CaFramework.h
index ab3019b5ca..b3e0e8d86d 100644
--- a/algo/ca/core/tracking/CaFramework.h
+++ b/algo/ca/core/tracking/CaFramework.h
@@ -321,8 +321,6 @@ namespace cbm::algo::ca
     ca::FieldValueV fTargB _fvecalignment{};  // field in the target point (modifiable, do not touch!!)
     ca::MeasurementXy<fvec> TargetMeasurement _fvecalignment{};  // target constraint  [cm]
 
-    int fGhostSuppression{1};  // NOTE: Should be equal to 0 in TRACKS_FROM_TRIPLETS mode!
-
   } _fvecalignment;
 
 
diff --git a/algo/ca/core/tracking/CaTrackFinderWindow.cxx b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
index d942c55333..ab625fc587 100644
--- a/algo/ca/core/tracking/CaTrackFinderWindow.cxx
+++ b/algo/ca/core/tracking/CaTrackFinderWindow.cxx
@@ -351,6 +351,7 @@ void TrackFinderWindow::CaTrackFinderSlice()
 
     // search for neighbouring triplets
     frAlgo.fMonitorData.StartTimer(ETimer::NeighboringTripletSearch);
+
     for (int istal = frAlgo.GetParameters().GetNstationsActive() - 2; istal >= frAlgo.fFirstCAstation;
          istal--) {  // start with downstream chambers
 
@@ -361,8 +362,6 @@ void TrackFinderWindow::CaTrackFinderSlice()
         tr.SetFNeighbour(0);
         tr.SetNNeighbours(0);
 
-        if (istal > (frAlgo.GetParameters().GetNstationsActive() - 4)) break;
-
         unsigned int nNeighbours = frAlgo.fHitNtriplets[tr.GetMHit()];
 
         unsigned int neighLocation = frAlgo.fHitFirstTriplet[tr.GetMHit()];
@@ -457,21 +456,20 @@ void TrackFinderWindow::CaTrackFinderSlice()
               || frAlgo.fvHitKeyFlags[frAlgo.fWindowHits[first_trip.GetLHit()].BackKey()]) {
             continue;
           }
-          //               ghost suppression !!!
-          //
-
-          if (!frAlgo.fpCurrentIteration->GetTrackFromTripletsFlag()) {  // ghost suppression !!!
-            int nHits = 3 + first_trip.GetLevel();
-            if (frAlgo.fWindowHits[first_trip.GetLHit()].Station() == 0) {
-              if (nHits < frAlgo.fpCurrentIteration->GetMinNhitsStation0()) {
-                continue;
-              }
-            }
-            else {
-              if (nHits < frAlgo.fpCurrentIteration->GetMinNhits()) {
-                continue;
-              }
-            }
+
+          //  skip track candidates that are too short
+
+          int minNhits = frAlgo.fpCurrentIteration->GetMinNhits();
+
+          if (frAlgo.fWindowHits[first_trip.GetLHit()].Station() == 0) {
+            minNhits = frAlgo.fpCurrentIteration->GetMinNhitsStation0();
+          }
+          if (frAlgo.fpCurrentIteration->GetTrackFromTripletsFlag()) {
+            minNhits = 0;
+          }
+
+          if (3 + first_trip.GetLevel() < minNhits) {
+            continue;
           }
 
           // Collect triplets, which can start a track with length equal to firstTipletLevel + 3. This cut suppresses
@@ -495,6 +493,10 @@ void TrackFinderWindow::CaTrackFinderSlice()
 
           if (best_tr.NofHits() < min_level + 3) continue;  // should find all hits for min_level
 
+          if (best_tr.NofHits() < minNhits) {
+            continue;
+          }
+
           ndf = best_tr.NofHits() * 2 - 5;
 
           // TODO: automatize the NDF calculation
@@ -505,7 +507,7 @@ void TrackFinderWindow::CaTrackFinderSlice()
           }
 
           best_tr.SetChi2(best_tr.Chi2() / ndf);
-          if (frAlgo.fGhostSuppression) {
+          if (frAlgo.GetParameters().GetGhostSuppression()) {
             if (3 == best_tr.NofHits()) {
               if (!frAlgo.fpCurrentIteration->GetPrimaryFlag() && (istaF != 0))
                 continue;  // too /*short*/ non-MAPS track
@@ -517,12 +519,12 @@ void TrackFinderWindow::CaTrackFinderSlice()
           tr.SetStation(istaF);
           tr.SetId(frAlgo.fTrackCandidates.size() - 1);
           tr.SetAlive(true);
-          if (0) {  // debug
-            cout << "track " << frAlgo.fTrackCandidates.size() - 1 << " found, L = " << best_tr.NofHits()
-                 << " chi2= " << best_tr.Chi2() << endl;
-            cout << " hits: ";
+          if constexpr (fDebug) {
+            cout << "iter " << frAlgo.fCurrentIterationIndex << ", track candidate "
+                 << frAlgo.fTrackCandidates.size() - 1 << " found, L = " << best_tr.NofHits()
+                 << " chi2= " << best_tr.Chi2() << " hits: ";
             for (auto hitId : tr.Hits()) {
-              cout << frAlgo.GetMcTrackIdForCaHit(hitId) << " ";
+              cout << hitId << " (mc " << frAlgo.GetMcTrackIdForCaHit(hitId) << ") ";
             }
             cout << endl;
           }
@@ -542,6 +544,7 @@ void TrackFinderWindow::CaTrackFinderSlice()
         repeatCompetition = false;
 
         for (ca::Branch& tr : frAlgo.fTrackCandidates) {
+
           if (tr.IsAlive()) {
             continue;
           }
@@ -626,22 +629,14 @@ void TrackFinderWindow::CaTrackFinderSlice()
 
       for (Tindex iCandidate = 0; iCandidate < (Tindex) frAlgo.fTrackCandidates.size(); ++iCandidate) {
         ca::Branch& tr = frAlgo.fTrackCandidates[iCandidate];
-        if (!tr.IsAlive()) continue;
-        if (ca::Framework::TrackingMode::kMcbm == frAlgo.fTrackingMode) {
-          if (tr.NofHits() <= 3) {
-            continue;
-          }
-        }
-        else if (ca::Framework::TrackingMode::kGlobal == frAlgo.fTrackingMode) {
-          if (tr.NofHits() < 3) {
-            continue;
-          }
-        }
-        else {
-          if (tr.NofHits() < 3) {
-            continue;
-          }
+
+        if constexpr (fDebug) {
+          cout << "iter " << frAlgo.fCurrentIterationIndex << ", track candidate " << iCandidate
+               << ": alive = " << tr.IsAlive() << std::endl;
         }
+
+        if (!tr.IsAlive()) continue;
+
         if (frAlgo.fpCurrentIteration->GetExtendTracksFlag()) {
           if (tr.NofHits() < frAlgo.GetParameters().GetNstationsActive()) {
             frAlgo.fTrackExtender.ExtendBranch(tr);
diff --git a/macro/L1/configs/ca_params_mcbm.yaml b/macro/L1/configs/ca_params_mcbm.yaml
index 3b1e82ca3f..9bcc8ca343 100644
--- a/macro/L1/configs/ca_params_mcbm.yaml
+++ b/macro/L1/configs/ca_params_mcbm.yaml
@@ -39,7 +39,7 @@ ca:
       iteration_sequence: ['AllPrim', 'AllSec']
       
       # Flag to suppress ghost tracks or not (true/false)
-      is_ghost_suppression: true
+      is_ghost_suppression: false
 
       # Max number of doublets per singlet
       max_doublets_per_singlet: 150
diff --git a/reco/L1/CbmL1.cxx b/reco/L1/CbmL1.cxx
index 2ede3bb608..f87d4b332b 100644
--- a/reco/L1/CbmL1.cxx
+++ b/reco/L1/CbmL1.cxx
@@ -638,18 +638,20 @@ void CbmL1::Reconstruct(CbmEvent* event)
       fMaterialMonitor[hit.Station()].MarkActiveBin(hit.X(), hit.Y());
     }
   }
-  LOG(info) << "CHECK: hit ids = " << fvExternalHits.size() << ", hits = " << fpIODataManager->GetNofHits()
-            << ", dbg hits = " << fvHitDebugInfo.size();
+  //LOG(info) << "CHECK: hit ids = " << fvExternalHits.size() << ", hits = " << fpIODataManager->GetNofHits()
+  //<< ", dbg hits = " << fvHitDebugInfo.size();
 
   //  FieldApproxCheck();
   //  FieldIntegralCheck();
   fpAlgo->SetMonitorData(monitorData);
 
-  LOG_IF(info, fVerbose > 1) << "L1 Track finder...";
+  LOG_IF(info, fVerbose > 1) << "Ca Track finder...";
   fpAlgo->fTrackFinder.FindTracks();
   //       IdealTrackFinder();
   fTrackingTime = fpAlgo->fCaRecoTime;  // TODO: remove (not used)
-  LOG_IF(info, fVerbose > 1) << "L1 Track finder ok";
+  LOG_IF(info, fVerbose > 1) << "Ca Track Finder ok";
+
+  LOG_IF(info, fVerbose > 0) << "Ca Track Finder found " << fpAlgo->fRecoTracks.size() << " tracks";
 
   // Update monitor data after the actual tracking
   monitorData = fpAlgo->GetMonitorData();
diff --git a/reco/L1/CbmL1.h b/reco/L1/CbmL1.h
index 7b187d3c59..4f0548dd35 100644
--- a/reco/L1/CbmL1.h
+++ b/reco/L1/CbmL1.h
@@ -262,7 +262,6 @@ class CbmL1 : public FairTask {
 
   //   void SetTrackingLevel( Int_t iLevel ){ fTrackingLevel = iLevel; }
   //   void MomentumCutOff( Double_t cut ){ fMomentumCutOff = cut; }
-  //   void SetGhostSuppression( Bool_t b ){ fGhostSuppression= b; }
   //   void SetDetectorEfficiency( Double_t eff ){ fDetectorEfficiency = eff; }
 
   /// Reconstructs tracks in a given event
@@ -488,7 +487,6 @@ class CbmL1 : public FairTask {
 
   Int_t fTrackingLevel     = 2;     // currently not used
   Double_t fMomentumCutOff = 0.1;   // currently not used
-  Bool_t fGhostSuppression = true;  // currently not used
 
   bool fUseMVD  = false;  ///< if Mvd data should be processed
   bool fUseSTS  = false;  ///< if Mvd data should be processed
-- 
GitLab