From ca3740dc608434736bbb1693780295d961788c63 Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Thu, 26 Oct 2023 11:59:32 +0200
Subject: [PATCH] Fixed cbm::algo::tof::Hitfind::CalibRawDigis(). Changed
 interfaces accordingly.

---
 algo/detectors/tof/Hitfind.cxx        | 21 +++++++++++++--------
 algo/detectors/tof/Hitfind.h          |  4 ++--
 algo/detectors/tof/HitfinderChain.cxx |  4 ++--
 algo/global/Reco.cxx                  | 12 ++++++------
 reco/tasks/CbmTaskTofClusterizer.cxx  | 11 ++++++-----
 5 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/algo/detectors/tof/Hitfind.cxx b/algo/detectors/tof/Hitfind.cxx
index 4937cf80c6..d02d56cbae 100644
--- a/algo/detectors/tof/Hitfind.cxx
+++ b/algo/detectors/tof/Hitfind.cxx
@@ -21,16 +21,18 @@ namespace cbm::algo::tof
 
     // --- Output data
     resultType result = {};
-    auto& clusterTs   = result.first;
-    auto& monitor     = result.second;
+
+    auto& clusterTs = std::get<0>(result);
+    auto& monitor   = std::get<1>(result);
+    auto& calDigi   = std::get<2>(result);
 
     // Do calibration globally (optional, should not be used together with RPC-wise calibration)
-    CalibRawDigis(digiIn, monitor);
+    calDigi = CalibRawDigis(digiIn, monitor);
 
     // Loop over the digis array and store the Digis in separate vectors for
     // each RPC modules
-    for (int32_t idigi = 0; idigi < digiIn.size(); idigi++) {
-      CbmTofDigi* pDigi = &(digiIn[idigi]);
+    for (int32_t idigi = 0; idigi < calDigi.size(); idigi++) {
+      CbmTofDigi* pDigi = &(calDigi[idigi]);
 
       // These are doubles in the digi class
       const double SmType = pDigi->GetType();
@@ -160,10 +162,11 @@ namespace cbm::algo::tof
 
 
   // -------------------------- Calibration -------------------------------------
-  void Hitfind::CalibRawDigis(gsl::span<CbmTofDigi> digiVec, HitfindMonitorData& monitor)
+  std::vector<CbmTofDigi> Hitfind::CalibRawDigis(gsl::span<CbmTofDigi> digiVec, HitfindMonitorData& monitor)
   {
     // channel deadtime map
     std::map<int32_t, double> mChannelDeadTime;
+    std::vector<CbmTofDigi> calDigiVecOut;
 
     for (size_t iDigi = 0; iDigi < digiVec.size(); iDigi++) {
 
@@ -228,12 +231,14 @@ namespace cbm::algo::tof
         if (0 < iWx) { dWT -= dDTot * (walk[iWx - 1] - walk[iWx]); }
       }
       pCalDigi.SetTime(pCalDigi.GetTime() - dWT);  // calibrate Digi Time
-      digiVec[iDigi] = pCalDigi;
+      calDigiVecOut.push_back(pCalDigi);
     }
 
     /// Sort the buffers of hits due to the time offsets applied
-    std::sort(digiVec.begin(), digiVec.end(),
+    std::sort(calDigiVecOut.begin(), calDigiVecOut.end(),
               [](const CbmTofDigi& a, const CbmTofDigi& b) -> bool { return a.GetTime() < b.GetTime(); });
+
+    return calDigiVecOut;
   }
 
 }  // namespace cbm::algo::tof
diff --git a/algo/detectors/tof/Hitfind.h b/algo/detectors/tof/Hitfind.h
index a8d80d8df4..a281ebee80 100644
--- a/algo/detectors/tof/Hitfind.h
+++ b/algo/detectors/tof/Hitfind.h
@@ -52,7 +52,7 @@ namespace cbm::algo::tof
   class Hitfind {
 
   public:
-    typedef std::pair<PartitionedVector<Hit>, HitfindMonitorData> resultType;
+    typedef std::tuple<PartitionedVector<Hit>, HitfindMonitorData, std::vector<CbmTofDigi>> resultType;
 
     /** @brief Algorithm execution
      ** @param fles timeslice to hitfind
@@ -89,7 +89,7 @@ namespace cbm::algo::tof
     std::vector<int32_t> fNbRpc;
 
     /** @brief Applies calibration to input digis **/
-    void CalibRawDigis(gsl::span<CbmTofDigi> digiVec, HitfindMonitorData& monitor);
+    std::vector<CbmTofDigi> CalibRawDigis(gsl::span<CbmTofDigi> digiVec, HitfindMonitorData& monitor);
   };
 }  // namespace cbm::algo::tof
 
diff --git a/algo/detectors/tof/HitfinderChain.cxx b/algo/detectors/tof/HitfinderChain.cxx
index 6d78c43b7c..d23e9aea52 100644
--- a/algo/detectors/tof/HitfinderChain.cxx
+++ b/algo/detectors/tof/HitfinderChain.cxx
@@ -20,7 +20,7 @@ HitfinderChain::ReturnType HitfinderChain::Run(gsl::span<CbmTofDigi> digis)
 {
   auto ret = (*fHitfind)(digis);
 
-  L_(info) << "TS contains " << ret.first.NElements() << " TOF Hits";
-  L_(error) << "TOF Digis with unknown RPCs: " << ret.second.fDigiCalibUnknownRPC;
+  L_(info) << "TS contains " << std::get<0>(ret).NElements() << " TOF Hits";
+  L_(error) << "TOF Digis with unknown RPCs: " << std::get<1>(ret).fDigiCalibUnknownRPC;
   return ret;
 }
diff --git a/algo/global/Reco.cxx b/algo/global/Reco.cxx
index 1500cfb35c..e4ba22ece9 100644
--- a/algo/global/Reco.cxx
+++ b/algo/global/Reco.cxx
@@ -76,10 +76,10 @@ void Reco::Init(const Options& opts)
   fEventBuild = std::make_unique<evbuild::EventbuildChain>(config, fSender);
 
   // STS Hitfinder
-  fs::path stsHitfinderParamsPath    = opts.ParamsDir() / "StsHitfinder.yaml";
-  yaml                               = YAML::LoadFile(stsHitfinderParamsPath.string());
-  sts::HitfinderPars hitFinderSetup  = config::Read<sts::HitfinderPars>(yaml);
-  hitFinderSetup.landauTable         = sts::LandauTable::FromFile(opts.ParamsDir() / "LandauWidthTable.txt");
+  fs::path stsHitfinderParamsPath   = opts.ParamsDir() / "StsHitfinder.yaml";
+  yaml                              = YAML::LoadFile(stsHitfinderParamsPath.string());
+  sts::HitfinderPars hitFinderSetup = config::Read<sts::HitfinderPars>(yaml);
+  hitFinderSetup.landauTable        = sts::LandauTable::FromFile(opts.ParamsDir() / "LandauWidthTable.txt");
   sts::HitfinderChainPars hitFinderPars;
   hitFinderPars.setup  = std::move(hitFinderSetup);
   hitFinderPars.memory = Params().sts.memory;
@@ -153,8 +153,8 @@ RecoResults Reco::Run(const fles::Timeslice& ts)
 
     PartitionedVector<tof::Hit> tofHits;
     if (Opts().HasStep(Step::LocalReco) && Opts().HasDetector(fles::Subsystem::TOF)) {
-      auto [hits, monitor] = fTofHitFinder.Run(unpackResult.first.fTof);
-      tofHits              = std::move(hits);
+      auto [hits, monitor, caldigis] = fTofHitFinder.Run(unpackResult.first.fTof);
+      tofHits                        = std::move(hits);
       QueueTofRecoMetrics(monitor);
     }
 
diff --git a/reco/tasks/CbmTaskTofClusterizer.cxx b/reco/tasks/CbmTaskTofClusterizer.cxx
index a6eaf631d2..c9020271df 100644
--- a/reco/tasks/CbmTaskTofClusterizer.cxx
+++ b/reco/tasks/CbmTaskTofClusterizer.cxx
@@ -347,12 +347,13 @@ bool CbmTaskTofClusterizer::BuildClusters()
     }
   }
 
-  // Make copy for calibrated digis (calibration done in algo)
-  // Can be left out if calibrated digis not stored separately!
-  *fTofCalDigiVec = fTofDigiVec;
-
   //call cluster finder
-  auto clusters = fAlgo(*fTofCalDigiVec).first;
+  auto clusterOut = fAlgo(fTofDigiVec);
+  auto clusters   = std::get<0>(clusterOut);
+
+  // Calibrated digis (calibration done in algo)
+  // Can be left out if calibrated digis not stored separately!
+  *fTofCalDigiVec = std::get<2>(clusterOut);
 
   //Store hits and match
   for (auto const& cluster : clusters.Data()) {
-- 
GitLab