From caa1f8cfceb777d083b25fd562b55babcb5cf2f4 Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Mon, 13 Nov 2023 21:56:02 +0000
Subject: [PATCH] Ca: minor

---
 algo/ca/core/utils/CaMonitor.h                |  2 +-
 reco/KF/CbmKFTrackFitter.cxx                  |  4 +-
 .../CbmL1GlobalFindTracksEvents.cxx           | 10 ++--
 reco/alignment/CbmBbaAlignmentTask.cxx        | 50 ++++++++++++-------
 reco/alignment/CbmBbaAlignmentTask.h          |  1 +
 5 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/algo/ca/core/utils/CaMonitor.h b/algo/ca/core/utils/CaMonitor.h
index 55c524ec7b..dcc7912b5b 100644
--- a/algo/ca/core/utils/CaMonitor.h
+++ b/algo/ca/core/utils/CaMonitor.h
@@ -79,7 +79,7 @@ namespace cbm::algo::ca
 
     /// \brief Gets timer
     /// \param key Timer key
-    const Timer& GetTimer(ETimerKey key) const { return fMonitorData.GetTimer(); }
+    const Timer& GetTimer(ETimerKey /*key*/) const { return fMonitorData.GetTimer(); }
 
     /// \brief Gets timer name
     /// \param key Timer key
diff --git a/reco/KF/CbmKFTrackFitter.cxx b/reco/KF/CbmKFTrackFitter.cxx
index ba8347cc1f..281f08cd3c 100644
--- a/reco/KF/CbmKFTrackFitter.cxx
+++ b/reco/KF/CbmKFTrackFitter.cxx
@@ -470,6 +470,8 @@ void CbmKFTrackFitter::FilterFirstMeasurement(const FitNode& n)
   tr.SetC10(mxy.Dxy());
   tr.SetX(mxy.X());
   tr.SetY(mxy.Y());
+  tr.SetChiSq(0.);
+  tr.SetChiSqTime(0.);
   tr.SetNdf(-5 + mxy.NdfX() + mxy.NdfY());
   if (mt.NdfT()[0] > 0) {
     tr.SetTime(mt.T());
@@ -634,4 +636,4 @@ CbmKFTrackFitter::Smooth2D(ca::fvec x1, ca::fvec y1, ca::fvec Cxx1, ca::fvec /*C
   auto [x, Cxx] = Smooth1D(x1, Cxx1, x2, Cxx2);
   auto [y, Cyy] = Smooth1D(y1, Cyy1, y2, Cyy2);
   return std::tuple(x, y, Cxx, ca::fvec::Zero(), Cyy);
-}
\ No newline at end of file
+}
diff --git a/reco/L1/OffLineInterface/CbmL1GlobalFindTracksEvents.cxx b/reco/L1/OffLineInterface/CbmL1GlobalFindTracksEvents.cxx
index 7cfc8282a3..1bbc1ca5d5 100644
--- a/reco/L1/OffLineInterface/CbmL1GlobalFindTracksEvents.cxx
+++ b/reco/L1/OffLineInterface/CbmL1GlobalFindTracksEvents.cxx
@@ -97,11 +97,11 @@ void CbmL1GlobalFindTracksEvents::Exec(Option_t* /*opt*/)
   timer.Start();
 
   // --- Clear output array
-  fGlobalTracks->Delete();
-  fStsTrackArray->Delete();
-  fMuchTrackArray->Delete();
-  fTrdTrackArray->Delete();
-  fTofTrackArray->Delete();
+  fGlobalTracks->Clear("C");
+  fStsTrackArray->Clear("C");
+  fMuchTrackArray->Clear("C");
+  fTrdTrackArray->Clear("C");
+  fTofTrackArray->Clear("C");
 
 
   // --- Event loop (from event objects)
diff --git a/reco/alignment/CbmBbaAlignmentTask.cxx b/reco/alignment/CbmBbaAlignmentTask.cxx
index 02cb14022a..aa1197cc50 100644
--- a/reco/alignment/CbmBbaAlignmentTask.cxx
+++ b/reco/alignment/CbmBbaAlignmentTask.cxx
@@ -39,6 +39,8 @@
 #include "CbmTrdTrack.h"
 #include "CbmTrdTrackingInterface.h"
 
+#include <cmath>
+
 #include "bba/BBA.h"
 
 // c++ and std headers
@@ -167,8 +169,7 @@ void CbmBbaAlignmentTask::Exec(Option_t* /*opt*/)
 
   static int statGlobalTracks = 0;
   statGlobalTracks += fInputGlobalTracks->GetEntriesFast();
-  static int statGlobalTracks1 = 0;
-  static int statGlobalTracks2 = 0;
+
   // select tracks for alignment and store them
   if (fTrackingMode == kSts && fInputStsTracks) {
     for (int iTr = 0; iTr < fInputStsTracks->GetEntriesFast(); iTr++) {
@@ -182,7 +183,7 @@ void CbmBbaAlignmentTask::Exec(Option_t* /*opt*/)
       t.fAlignedTrack = t.fUnalignedTrack;
       if (t.fNstsHits < l1Par.GetNstationsActive(ca::EDetectorID::kSts)) continue;
       const auto& par = t.fUnalignedTrack.fNodes[t.fUnalignedTrack.fFirstHitNode].fTrack;
-      if (!std::isfinite(par.GetQp()[0])) continue;
+      if (!std::isfinite((ca::fscal) par.GetQp()[0])) continue;
       if (fabs(par.GetQp()[0]) > 1.) continue;  // select tracks with min 1 GeV momentum
       fTracks.push_back(t);
     }
@@ -191,15 +192,15 @@ void CbmBbaAlignmentTask::Exec(Option_t* /*opt*/)
     for (int iTr = 0; iTr < fInputGlobalTracks->GetEntriesFast(); iTr++) {
       if (static_cast<int>(fTracks.size()) >= fMaxNtracks) { break; }
       const CbmGlobalTrack* globalTrack = dynamic_cast<const CbmGlobalTrack*>(fInputGlobalTracks->At(iTr));
-      if (!globalTrack) continue;
-      statGlobalTracks1++;
+      if (!globalTrack) {
+        LOG(fatal) << "BBA: null pointer to the global track!";
+        break;
+      }
       TrackContainer t;
       if (!fFitter.CreateGlobalTrack(t.fUnalignedTrack, *globalTrack)) {
-        LOG(error) << "BBA: can not create a global track for the fit! ";
-        exit(0);
-        continue;
+        LOG(fatal) << "BBA: can not create the global track for the fit! ";
+        break;
       }
-      statGlobalTracks2++;
       t.MakeConsistent();
       fFitter.FitTrack(t.fUnalignedTrack);
       t.fAlignedTrack = t.fUnalignedTrack;
@@ -243,12 +244,17 @@ double CbmBbaAlignmentTask::CostFunction(const std::vector<double>& par)
 {
   // apply new parameters to the hits
 
+  for (auto& t : fTracks) {
+    t.fAlignedTrack = t.fUnalignedTrack;
+  }
+
   ApplyAlignment(par);
 
   fChi2Total = 0.;
   fNdfTotal  = 0;
 
   for (auto& t : fTracks) {
+    if (!t.fIsActive) continue;
     fFitter.FitTrack(t.fAlignedTrack);
     double chi2 = t.fAlignedTrack.fNodes[t.fAlignedTrack.fFirstHitNode].fTrack.GetChiSq()[0];
     double ndf  = t.fAlignedTrack.fNodes[t.fAlignedTrack.fFirstHitNode].fTrack.GetNdf()[0];
@@ -261,10 +267,10 @@ double CbmBbaAlignmentTask::CostFunction(const std::vector<double>& par)
   }
 
   double cost = fChi2Total / (fNdfTotal + 1);
+  if (fFixedNdf > 0 && fNdfTotal != fFixedNdf) { cost = 1.e30; }
   LOG(info) << "BBA: cost function:  ndf " << fNdfTotal << ", cost " << cost
             << ", diff to ideal cost: " << cost - fCostIdeal;
 
-  if (fFixedNdf > 0 && fNdfTotal != fFixedNdf) { cost = 1.e30; }
   return cost;
 }
 
@@ -317,11 +323,13 @@ void CbmBbaAlignmentTask::Finish()
   // set active parameters for other stations
 
   for (int i = 1; i < nStations - 1; i++) {
-    par[3 * i + 0].SetActive(1);
-    par[3 * i + 1].SetActive(1);
-    par[3 * i + 2].SetActive(1);
+    par[3 * i + 0].SetActive(0);
+    par[3 * i + 1].SetActive(0);
+    par[3 * i + 2].SetActive(0);
   }
 
+  par[3 * 1 + 0].SetActive(1);
+
   gRandom->SetSeed(1);
 
   for (int is = 0; is < nStations; is++) {
@@ -361,15 +369,19 @@ void CbmBbaAlignmentTask::Finish()
   }
 
   {
-    fCostInitial   = CostFunction(parMisaligned);
-    auto tmpTracks = fTracks;
-    fTracks.clear();
-    for (auto& t : tmpTracks) {
-      if (t.fAlignedTrack.fNodes[t.fAlignedTrack.fFirstHitNode].fTrack.GetNdf()[0] > 0) { fTracks.push_back(t); }
+    std::cout << "initial cost function..." << std::endl;
+    fCostInitial = CostFunction(parMisaligned);
+    for (auto& t : fTracks) {
+      double ndf  = t.fAlignedTrack.fNodes[t.fAlignedTrack.fFirstHitNode].fTrack.GetNdf()[0];
+      double chi2 = t.fAlignedTrack.fNodes[t.fAlignedTrack.fFirstHitNode].fTrack.GetChiSq()[0];
+      if (ndf < 0. || chi2 < 0. || !std::isfinite(ndf) || !std::isfinite(chi2)) { t.fIsActive = false; }
     }
-    fFixedNdf = fNdfTotal;
+    std::cout << "recalculate initial cost function..." << std::endl;
+    fCostInitial = CostFunction(parMisaligned);
+    fFixedNdf    = -1;  //fNdfTotal;
   }
 
+  std::cout << "ideal cost function..." << std::endl;
   fCostIdeal = CostFunction(parAligned);
 
 
diff --git a/reco/alignment/CbmBbaAlignmentTask.h b/reco/alignment/CbmBbaAlignmentTask.h
index 02bcc1cab7..f2e91a5ddd 100644
--- a/reco/alignment/CbmBbaAlignmentTask.h
+++ b/reco/alignment/CbmBbaAlignmentTask.h
@@ -73,6 +73,7 @@ public:
     int fNtrdHits {0};   // number of TRD hits
     int fNtofHits {0};   // number of TOF hits
 
+    bool fIsActive {1};  // is the track active
     void MakeConsistent();
   };
 
-- 
GitLab