From 45651dc3c9cec40c401c306fdc21ee99e29bcc49 Mon Sep 17 00:00:00 2001
From: "s.zharko@gsi.de" <s.zharko@gsi.de>
Date: Tue, 24 Sep 2024 21:53:05 +0200
Subject: [PATCH] CA: reading KF geo-setup in related QA-tasks and the online
 binary

---
 algo/ca/TrackingChain.cxx           |  9 +++++---
 algo/ca/core/pars/CaInitManager.cxx | 14 ++++++++++++
 algo/ca/core/pars/CaInitManager.h   | 15 +++++++++++++
 algo/kf/core/geo/KfModuleIndexMap.h | 16 ++++++++++---
 algo/kf/core/geo/KfSetup.h          |  1 +
 algo/kf/core/geo/KfSetupBuilder.cxx |  1 +
 algo/kf/core/geo/KfSetupBuilder.h   |  1 +
 reco/L1/qa/CbmCaInputQaSetup.cxx    | 10 ++++++++-
 reco/L1/qa/CbmCaOutputQa.cxx        | 35 +++++++++++++++--------------
 reco/L1/qa/CbmCaOutputQa.h          |  3 ++-
 10 files changed, 80 insertions(+), 25 deletions(-)

diff --git a/algo/ca/TrackingChain.cxx b/algo/ca/TrackingChain.cxx
index 8429b2d913..b6c968d377 100644
--- a/algo/ca/TrackingChain.cxx
+++ b/algo/ca/TrackingChain.cxx
@@ -57,11 +57,13 @@ void TrackingChain::Init()
   fConfig = yaml::ReadFromFile<TrackingChainConfig>(Opts().ParamsDir() / parFiles.ca.mainConfig);
 
   // ------ Read parameters from binary
-  auto geomCfgFile = (Opts().ParamsDir() / fConfig.fsGeomConfig).string();
-  auto mainCfgFile = (Opts().ParamsDir() / fConfig.fsMainConfig).string();
-  auto userCfgFile = fConfig.fsUserConfig;
+  auto geomCfgFile  = (Opts().ParamsDir() / fConfig.fsGeomConfig).string();
+  auto setupCfgFile = (Opts().ParamsDir() / fConfig.fsSetupFilename).string();
+  auto mainCfgFile  = (Opts().ParamsDir() / fConfig.fsMainConfig).string();
+  auto userCfgFile  = fConfig.fsUserConfig;
 
   L_(info) << "Tracking Chain: reading geometry from CA parameters file " << GNb << geomCfgFile << CL << '\n';
+  L_(info) << "Tracking Chain: reading geometry setup file " << GNb << setupCfgFile << CL << '\n';
   L_(info) << "Tracking Chain: reading parameters from CA main config " << GNb << mainCfgFile << CL << '\n';
   auto manager = InitManager{};
   manager.SetDetectorNames(ca::kDetName);
@@ -72,6 +74,7 @@ void TrackingChain::Init()
   }
   manager.ReadParametersObject(geomCfgFile);  // geometry setup
   manager.ReadInputConfigs();
+  manager.ReadGeometrySetup(setupCfgFile);
   auto parameters = manager.TakeParameters();
   L_(info) << "Tracking Chain: parameters object: \n" << parameters.ToString(1) << '\n';
 
diff --git a/algo/ca/core/pars/CaInitManager.cxx b/algo/ca/core/pars/CaInitManager.cxx
index 703ca67540..feb8f35208 100644
--- a/algo/ca/core/pars/CaInitManager.cxx
+++ b/algo/ca/core/pars/CaInitManager.cxx
@@ -10,6 +10,7 @@
 #include "CaInitManager.h"
 
 #include "CaConfigReader.h"
+#include "KfSetupBuilder.h"
 
 #include <boost/archive/binary_iarchive.hpp>
 #include <boost/archive/binary_oarchive.hpp>
@@ -345,6 +346,14 @@ void InitManager::ReadInputConfigs()
   }
 }
 
+// ---------------------------------------------------------------------------------------------------------------------
+//
+void InitManager::ReadGeometrySetup(const std::string& fileName)
+{
+  // Open input binary file
+  this->SetGeometrySetup(cbm::algo::kf::SetupBuilder::Load<fvec>(fileName));
+}
+
 // ---------------------------------------------------------------------------------------------------------------------
 //
 void InitManager::ReadParametersObject(const std::string& fileName)
@@ -373,6 +382,11 @@ void InitManager::ReadParametersObject(const std::string& fileName)
         << "\" has incorrect data format or was corrupted";
     throw std::runtime_error(msg.str());
   }
+
+  fInitController.SetFlag(EInitKey::kStationLayoutInitialized, true);
+  fInitController.SetFlag(EInitKey::kPrimaryVertexField, true);
+  fInitController.SetFlag(EInitKey::kSearchWindows, true);
+  fInitController.SetFlag(EInitKey::kActiveDetectorIDs);
 }
 
 // ---------------------------------------------------------------------------------------------------------------------
diff --git a/algo/ca/core/pars/CaInitManager.h b/algo/ca/core/pars/CaInitManager.h
index 09ed81e030..7dc949619f 100644
--- a/algo/ca/core/pars/CaInitManager.h
+++ b/algo/ca/core/pars/CaInitManager.h
@@ -165,6 +165,12 @@ namespace cbm::algo::ca
     /// \param zStep step between nodal points
     void InitTargetField(double zStep);
 
+    /// \brief Checks, if the detector is active
+    bool IsActive(EDetectorID detectorID) const { return GetNstationsActive(detectorID) != 0; }
+
+    /// \brief Checks, if the detector is present in the geometry
+    bool IsPresent(EDetectorID detectorID) const { return GetNstationsGeometry(detectorID) != 0; }
+
     /// \brief Pushes an CA track finder iteration into a sequence of iteration using reference
     void PushBackCAIteration(const Iteration& iteration);
 
@@ -177,6 +183,10 @@ namespace cbm::algo::ca
     /// \brief Reads main and user parameters configs
     void ReadInputConfigs();
 
+    /// \brief Reads geometry setup from file
+    /// \param fileName  Name of input file
+    void ReadGeometrySetup(const std::string& fileName);
+
     /// \brief Reads parameters object from boost-serialized binary file
     /// \param  fileName  Name of input file
     void ReadParametersObject(const std::string& fileName);
@@ -249,6 +259,11 @@ namespace cbm::algo::ca
     template<typename DataT>
     void SetGeometrySetup(const cbm::algo::kf::Setup<DataT>& setup)
     {
+      if (!fInitController.GetFlag(EInitKey::kStationLayoutInitialized)) {
+        std::stringstream msg;
+        msg << "ca::InitManager: setup cannot be set until the station layout is initialized";
+        throw std::runtime_error(msg.str());
+      }
       fParameters.fGeometrySetup = kf::Setup<fvec>(setup);
       fParameters.fActiveSetup   = fParameters.fGeometrySetup;
       for (int iStGeo = 0; iStGeo < setup.GetNofLayers(); ++iStGeo) {
diff --git a/algo/kf/core/geo/KfModuleIndexMap.h b/algo/kf/core/geo/KfModuleIndexMap.h
index 29b7a92ca1..791d2b549d 100644
--- a/algo/kf/core/geo/KfModuleIndexMap.h
+++ b/algo/kf/core/geo/KfModuleIndexMap.h
@@ -140,23 +140,33 @@ namespace cbm::algo::kf
   template<class EDetID>
   void ModuleIndexMap::Disable(EDetID detIdDisable, int locIdDisable)
   {
+    std::cout << "---- Disabling layer: " << static_cast<int>(detIdDisable) << ", " << locIdDisable << '\n';
     // Check, if the detector id is there
-    auto iDetExtDsbl{static_cast<int>(detIdDisable)};
-    if (iDetExtDsbl >= static_cast<int>(fvDetIntToExt.size())) {
+    auto iDetIntDsbl{fvDetExtToInt[static_cast<int>(detIdDisable)]};
+    std::cout << "iDetIntDsbl = " << iDetIntDsbl << '\n';
+    if (iDetIntDsbl >= static_cast<int>(fvDetIntToExt.size())) {
       return;  // Nothing to disable, detector is already not in the map
     }
 
-    auto& iGlbDsbl = fvLocToGlb[fvDetLocOffset[fvDetExtToInt[iDetExtDsbl]] + locIdDisable];
+
+    auto& iGlbDsbl = fvLocToGlb[fvDetLocOffset[iDetIntDsbl] + locIdDisable];
+    std::cout << "iGlbDsbl = " << iGlbDsbl << '\n';
     if (iGlbDsbl == -1) {
       return;  // Nothing to disable, component is already inactive
     }
 
+    for (auto [a, b] : fvGlbToLoc) {
+      std::cout << "--> " << a << ", " << b << '\n';
+    }
     fvGlbToLoc.erase(fvGlbToLoc.begin() + iGlbDsbl);  // Removing component from glob->(det, loc) map
     for (auto& val : fvLocToGlb) {
       if (val > iGlbDsbl) {
         --val;
       }
     }
+    for (auto [a, b] : fvGlbToLoc) {
+      std::cout << "--> " << a << ", " << b << '\n';
+    }
     iGlbDsbl = -1;
   }
 
diff --git a/algo/kf/core/geo/KfSetup.h b/algo/kf/core/geo/KfSetup.h
index 7cf10ce8a0..aaae020500 100644
--- a/algo/kf/core/geo/KfSetup.h
+++ b/algo/kf/core/geo/KfSetup.h
@@ -131,6 +131,7 @@ namespace cbm::algo::kf
   template<class EDetID>
   void Setup<T>::DisableLayer(EDetID iDet, int iLoc)
   {
+    std::cout << "Disabling layer: " << static_cast<int>(iDet) << ", " << iLoc << '\n';
     int iLayer{fModuleIndexMap.LocalToGlobal(iDet, iLoc)};
     if (iLayer == -1) {
       return;
diff --git a/algo/kf/core/geo/KfSetupBuilder.cxx b/algo/kf/core/geo/KfSetupBuilder.cxx
index c5a599cd75..78767a5a34 100644
--- a/algo/kf/core/geo/KfSetupBuilder.cxx
+++ b/algo/kf/core/geo/KfSetupBuilder.cxx
@@ -109,6 +109,7 @@ void SetupBuilder::Reset()
   fbIfSetFromSetup     = false;
   fFieldFactory.Reset();
   fModuleIndexFactory.Reset();
+  fvMaterial.clear();
   fGeoLayers.clear();
   fpMaterialMapFactory = nullptr;
 }
diff --git a/algo/kf/core/geo/KfSetupBuilder.h b/algo/kf/core/geo/KfSetupBuilder.h
index 0476750743..4e360a05e8 100644
--- a/algo/kf/core/geo/KfSetupBuilder.h
+++ b/algo/kf/core/geo/KfSetupBuilder.h
@@ -214,6 +214,7 @@ namespace cbm::algo::kf
   template<typename T>
   void SetupBuilder::SetFromSetup(const Setup<T>& inSetup)
   {
+    LOG(info) << inSetup.ToString(2);
     fbReady = false;
     // Reset geo layer information
     fGeoLayers.clear();
diff --git a/reco/L1/qa/CbmCaInputQaSetup.cxx b/reco/L1/qa/CbmCaInputQaSetup.cxx
index 3152a36ed3..f90eac7f93 100644
--- a/reco/L1/qa/CbmCaInputQaSetup.cxx
+++ b/reco/L1/qa/CbmCaInputQaSetup.cxx
@@ -10,6 +10,7 @@
 #include "CbmCaInputQaSetup.h"
 
 #include "CaInitManager.h"
+#include "CbmKfTrackingSetupBuilder.h"
 #include "CbmMCDataManager.h"
 #include "FairRootManager.h"
 #include "TAxis.h"
@@ -256,8 +257,15 @@ try {
   {
     ca::InitManager manager;
     manager.ReadParametersObject(fsParametersFilename.c_str());
+    bool bMvd{manager.IsPresent(ca::EDetectorID::kMvd)};
+    bool bSts{manager.IsPresent(ca::EDetectorID::kSts)};
+    bool bMuch{manager.IsPresent(ca::EDetectorID::kMuch)};
+    bool bTrd{manager.IsPresent(ca::EDetectorID::kTrd)};
+    bool bTof{manager.IsPresent(ca::EDetectorID::kTof)};
+    auto* pSetupBuilder = cbm::kf::TrackingSetupBuilder::Instance();
+    pSetupBuilder->Use(bMvd, bSts, bMuch, bTrd, bTof);
+    manager.SetGeometrySetup(pSetupBuilder->MakeSetup<ca::fvec>(cbm::algo::kf::EFieldMode::Intrpl));
     fpParameters = std::make_unique<ca::Parameters<ca::fvec>>(manager.TakeParameters());
-    LOG(info) << "CA tracking parameters object: " << fsParametersFilename << '\n' << fpParameters->ToString(1);
   }
 
   auto pFairManager = FairRootManager::Instance();
diff --git a/reco/L1/qa/CbmCaOutputQa.cxx b/reco/L1/qa/CbmCaOutputQa.cxx
index a56a5303ae..fa62462616 100644
--- a/reco/L1/qa/CbmCaOutputQa.cxx
+++ b/reco/L1/qa/CbmCaOutputQa.cxx
@@ -11,6 +11,7 @@
 
 #include "CaInitManager.h"
 #include "CbmCaMCModule.h"
+#include "CbmKfTrackingSetupBuilder.h"
 #include "CbmQaCanvas.h"
 #include "FairRootManager.h"
 #include "Logger.h"
@@ -788,13 +789,28 @@ void OutputQa::CreateSummary()
 InitStatus OutputQa::InitQa()
 try {
 
-  if (!fpParameters.get()) {
+  if (fsParametersFilename.empty()) {
     std::stringstream errMsg;
-    errMsg << "CA parameters object is not defined. Please, provide initializer or read parameters from binary "
+    errMsg << "CA parameters input filename is not set. Please, provide initializer or read parameters from binary "
            << "via OutputQa::ReadParameters(filename) from the qa macro\n";
     throw std::runtime_error(errMsg.str());
   }
 
+  {
+    ca::InitManager manager;
+    manager.ReadParametersObject(fsParametersFilename.c_str());
+    bool bMvd{manager.IsPresent(ca::EDetectorID::kMvd)};
+    bool bSts{manager.IsPresent(ca::EDetectorID::kSts)};
+    bool bMuch{manager.IsPresent(ca::EDetectorID::kMuch)};
+    bool bTrd{manager.IsPresent(ca::EDetectorID::kTrd)};
+    bool bTof{manager.IsPresent(ca::EDetectorID::kTof)};
+    auto* pSetupBuilder = cbm::kf::TrackingSetupBuilder::Instance();
+    pSetupBuilder->Use(bMvd, bSts, bMuch, bTrd, bTof);
+    manager.SetGeometrySetup(pSetupBuilder->MakeSetup<ca::fvec>(cbm::algo::kf::EFieldMode::Intrpl));
+    fpParameters = std::make_shared<ca::Parameters<ca::fvec>>(manager.TakeParameters());
+  }
+
+
   // Turn off detectors that are not used in the reconstruction setup
 
   fbUseMvd  = fbUseMvd && (fpParameters->GetNstationsActive(ca::EDetectorID::kMvd) > 0);
@@ -1014,18 +1030,3 @@ void OutputQa::InitDrawingAttributes()
   fvTrackDrawAtts[ETrackType::kSecMUM]  = {kMagenta - 10, 32};
 }
 
-// ---------------------------------------------------------------------------------------------------------------------
-//
-void OutputQa::ReadParameters(const char* filename)
-{
-  ca::InitManager manager;
-  try {
-    manager.ReadParametersObject(filename);
-    fpParameters = std::make_shared<ca::Parameters<ca::fvec>>(manager.TakeParameters());
-  }
-  catch (std::runtime_error& err) {
-    LOG(fatal) << err.what();
-  }
-
-  LOG(info) << '\n' << fpParameters->ToString(1);
-}
diff --git a/reco/L1/qa/CbmCaOutputQa.h b/reco/L1/qa/CbmCaOutputQa.h
index b490eef796..3d0be68af9 100644
--- a/reco/L1/qa/CbmCaOutputQa.h
+++ b/reco/L1/qa/CbmCaOutputQa.h
@@ -141,7 +141,7 @@ namespace cbm::ca
     /// \brief Reads defined parameters object from file
     /// \param filename  Name of parameter file
     /// \note  TEMPORARY FUNCTION, A SEPARATE PARAMETERS INITIALIZATION CLASS IS TO BE USED
-    void ReadParameters(const char* filename);
+    void ReadParameters(const char* filename) { fsParametersFilename = filename; }
 
     /// \brief Sets event display
     /// \param flag         On/off
@@ -253,6 +253,7 @@ namespace cbm::ca
 
     ECbmCaTrackingMode fTrackingMode = ECbmCaTrackingMode::kSTS;  ///< Tracking mode
 
+    std::string fsParametersFilename                   = "";
     std::unique_ptr<TimeSliceReader> fpTSReader    = nullptr;  ///< Reader of the time slice
     std::shared_ptr<MCModule> fpMCModule           = nullptr;  ///< MC module
     std::shared_ptr<ca::DataManager> fpDataManager = nullptr;  ///< Data manager
-- 
GitLab