From 57dcd2c70a5068899b26bbe0e906e071fdc736bd Mon Sep 17 00:00:00 2001
From: "se.gorbunov" <se.gorbunov@gsi.de>
Date: Tue, 26 Mar 2024 12:45:15 +0000
Subject: [PATCH] Kf refit: proper initialization of the no-field case

---
 algo/ca/core/pars/CaField.cxx  |  5 ++++-
 algo/ca/core/pars/CaField.h    | 19 ++++++++++++++-----
 reco/L1/catools/CaToolsField.h |  7 ++++++-
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/algo/ca/core/pars/CaField.cxx b/algo/ca/core/pars/CaField.cxx
index c5bddcc6c1..abd749d612 100644
--- a/algo/ca/core/pars/CaField.cxx
+++ b/algo/ca/core/pars/CaField.cxx
@@ -17,13 +17,16 @@ using namespace cbm::algo::ca;
 //
 template<typename DataT>
 bool FieldRegion<DataT>::fgForceUseOfOriginalField = false;
+
 template<typename DataT>
 std::function<std::tuple<double, double, double>(double x, double y, double z)>
-  FieldRegion<DataT>::fgOdiginalField([](double, double, double) {
+  FieldRegion<DataT>::fgOriginalField([](double, double, double) {
     assert(false && "ca::FieldRegion: The original field is not set, use cbm::ca::tools::SetCbmField()");
     return std::tuple(kfdefs::Undef<double>, kfdefs::Undef<double>, kfdefs::Undef<double>);
   });
 
+template<typename DataT>
+bool FieldRegion<DataT>::fgIsZeroOriginalField = false;
 
 //----------------------------------------------------------------------------------------------------------------------
 //
diff --git a/algo/ca/core/pars/CaField.h b/algo/ca/core/pars/CaField.h
index dedb95fe0a..e518aa0cdd 100644
--- a/algo/ca/core/pars/CaField.h
+++ b/algo/ca/core/pars/CaField.h
@@ -220,7 +220,13 @@ namespace cbm::algo::ca
     FieldValue<DataT> Get(const DataT& x, const DataT& y, const DataT& z) const;
 
     /// Is the field region empty?
-    bool IsZeroField() const { return fIsZeroField; }
+    bool IsZeroField() const
+    {
+      if (fgForceUseOfOriginalField || fUseOriginalField) {
+        return fgIsZeroOriginalField;
+      }
+      return fIsZeroField;
+    }
 
     /// Interpolates the magnetic field by three nodal points and sets the result to this FieldRegion object
     /// The result is a quadratic interpolation of the field as a function of z
@@ -278,9 +284,11 @@ namespace cbm::algo::ca
     static void ForceUseOfOriginalField(bool v = true) { fgForceUseOfOriginalField = v; }
 
     /// Set the original field
-    static void SetOriginalField(std::function<std::tuple<double, double, double>(double x, double y, double z)> f)
+    static void SetOriginalField(std::function<std::tuple<double, double, double>(double x, double y, double z)> f,
+                                 bool isZeroField)
     {
-      fgOdiginalField = f;
+      fgOriginalField       = f;
+      fgIsZeroOriginalField = isZeroField;
     }
 
     /// \brief Copies all/one entries from the other class
@@ -295,7 +303,8 @@ namespace cbm::algo::ca
 
    public:
     static bool fgForceUseOfOriginalField;
-    static std::function<std::tuple<double, double, double>(double x, double y, double z)> fgOdiginalField;
+    static std::function<std::tuple<double, double, double>(double x, double y, double z)> fgOriginalField;
+    static bool fgIsZeroOriginalField;
 
     // TODO: Probably it's better to have arrays instead of separate fvec values? (S.Zharko)
     // Bx(z) = cx0 + cx1*(z-z0) + cx2*(z-z0)^2
@@ -368,7 +377,7 @@ namespace cbm::algo::ca
 
   }  // CopyBase
 
-  /// \brief Explicit instantiation declarations for FieldRegion class with specific template types to ensure proper fgOdiginalField definition
+  /// \brief Explicit instantiation declarations for FieldRegion class with specific template types to ensure proper fgOriginalField definition
   extern template class FieldRegion<fvec>;
   extern template class FieldRegion<float>;
   extern template class FieldRegion<double>;
diff --git a/reco/L1/catools/CaToolsField.h b/reco/L1/catools/CaToolsField.h
index 43fd3fda01..e747cc908f 100644
--- a/reco/L1/catools/CaToolsField.h
+++ b/reco/L1/catools/CaToolsField.h
@@ -37,7 +37,12 @@ namespace cbm::ca::tools
         return std::tuple(0., 0., 0.);
       }
     };
-    ca::FieldRegion<ca::fvec>::SetOriginalField(fld);
+    if (FairRunAna::Instance()->GetField()) {
+      ca::FieldRegion<ca::fvec>::SetOriginalField(fld, false);
+    }
+    else {
+      ca::FieldRegion<ca::fvec>::SetOriginalField(fld, true);
+    }
   }
 
 }  // namespace cbm::ca::tools
-- 
GitLab