From 17d33507714da4f9dd1af190808927182798416b Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Thu, 2 Nov 2023 12:41:51 +0100
Subject: [PATCH] Re-introduced digi index links in CbmTaskTofClusterizer.

---
 algo/detectors/tof/Clusterizer.h     |  2 --
 algo/detectors/tof/Hitfind.cxx       | 14 ++++++++++----
 algo/detectors/tof/Hitfind.h         |  6 +++---
 algo/global/Reco.cxx                 |  6 +++---
 reco/tasks/CbmTaskTofClusterizer.cxx | 18 +++++++++++-------
 5 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/algo/detectors/tof/Clusterizer.h b/algo/detectors/tof/Clusterizer.h
index d44899daec..4d46a33b21 100644
--- a/algo/detectors/tof/Clusterizer.h
+++ b/algo/detectors/tof/Clusterizer.h
@@ -21,8 +21,6 @@ class CbmTofDigi;
 
 #include <cmath>
 
-#include "PartitionedVector.h"
-
 namespace cbm::algo::tof
 {
   class Clusterizer {
diff --git a/algo/detectors/tof/Hitfind.cxx b/algo/detectors/tof/Hitfind.cxx
index 0d09fec500..be682c87bf 100644
--- a/algo/detectors/tof/Hitfind.cxx
+++ b/algo/detectors/tof/Hitfind.cxx
@@ -25,10 +25,11 @@ namespace cbm::algo::tof
 
     auto& clusterTs = std::get<0>(result);
     auto& monitor   = std::get<1>(result);
+    auto& digiInd   = std::get<2>(result);  // digi indices
 
     // 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++) {
+    for (size_t idigi = 0; idigi < digiIn.size(); idigi++) {
       CbmTofDigi* pDigi = &(digiIn[idigi]);
 
       // These are doubles in the digi class
@@ -49,8 +50,8 @@ namespace cbm::algo::tof
 
     // ---  RPC loop
     for (uint32_t SmType = 0; SmType < fNbSm.size(); SmType++) {
-      const int NbRpc = fNbRpc[SmType];
-      const int NbSm  = fNbSm[SmType];
+      const uint32_t NbRpc = fNbRpc[SmType];
+      const uint32_t NbSm  = fNbSm[SmType];
       for (uint32_t Sm = 0; Sm < NbSm; Sm++) {
         for (uint32_t Rpc = 0; Rpc < NbRpc; Rpc++) {
 
@@ -62,6 +63,7 @@ namespace cbm::algo::tof
           std::vector<Hit>& clusters  = std::get<0>(rpcresult);  // Hits
           std::vector<size_t>& sizes  = std::get<1>(rpcresult);  // nClusters per channel
           std::vector<u32>& addresses = std::get<2>(rpcresult);  // channel addresses
+          std::vector<i32>& indices   = std::get<3>(rpcresult);  // digi indices
 
           // Append clusters to output
           clustersFlat.insert(clustersFlat.end(), std::make_move_iterator(clusters.begin()),
@@ -75,6 +77,10 @@ namespace cbm::algo::tof
           chanSizes.insert(chanSizes.end(), std::make_move_iterator(sizes.begin()),
                            std::make_move_iterator(sizes.end()));
 
+          // store digi indices
+          digiInd.insert(digiInd.end(), std::make_move_iterator(indices.begin()),
+                         std::make_move_iterator(indices.end()));
+
           // Clear digi storage
           digiExp.clear();
         }
@@ -109,7 +115,7 @@ namespace cbm::algo::tof
     fStorDigi.resize(fNbSm.size());
 
     // Create one algorithm per RPC for TOF and configure it with parametersa
-    for (int32_t SmType = 0; SmType < fNbSm.size(); SmType++) {
+    for (uint32_t SmType = 0; SmType < fNbSm.size(); SmType++) {
 
       int32_t NbSm  = fNbSm[SmType];
       int32_t NbRpc = fNbRpc[SmType];
diff --git a/algo/detectors/tof/Hitfind.h b/algo/detectors/tof/Hitfind.h
index 0d0fe25418..951c32161e 100644
--- a/algo/detectors/tof/Hitfind.h
+++ b/algo/detectors/tof/Hitfind.h
@@ -30,8 +30,8 @@ namespace cbm::algo::tof
   struct HitfindMonitorData {
     //std::vector<tof::ClusterizerMonitorData> fMonitor;   //Per RPC monitoring data, to be implemented
     xpu::timings fTime;
-    size_t fNumDigis            = 0;
-    size_t fNumHits             = 0;
+    size_t fNumDigis = 0;
+    size_t fNumHits  = 0;
 
     std::string print() const
     {
@@ -51,7 +51,7 @@ namespace cbm::algo::tof
   class Hitfind {
 
   public:
-    typedef std::pair<PartitionedVector<Hit>, HitfindMonitorData> resultType;
+    typedef std::tuple<PartitionedVector<Hit>, HitfindMonitorData, std::vector<i32>> resultType;
 
     /** @brief Algorithm execution
      ** @param fles timeslice to hitfind
diff --git a/algo/global/Reco.cxx b/algo/global/Reco.cxx
index 4b03acfae3..3e6badfdaf 100644
--- a/algo/global/Reco.cxx
+++ b/algo/global/Reco.cxx
@@ -162,9 +162,9 @@ RecoResults Reco::Run(const fles::Timeslice& ts)
 
     PartitionedVector<tof::Hit> tofHits;
     if (Opts().HasStep(Step::LocalReco) && Opts().HasDetector(fles::Subsystem::TOF)) {
-      auto [caldigis, calmonitor] = fTofCalibrator.Run(unpackResult.first.fTof);
-      auto [hits, hitmonitor]     = fTofHitFinder.Run(caldigis);
-      tofHits                     = std::move(hits);
+      auto [caldigis, calmonitor]          = fTofCalibrator.Run(unpackResult.first.fTof);
+      auto [hits, hitmonitor, digiindices] = fTofHitFinder.Run(caldigis);
+      tofHits                              = std::move(hits);
       QueueTofCalibMetrics(calmonitor);
       QueueTofRecoMetrics(hitmonitor);
     }
diff --git a/reco/tasks/CbmTaskTofClusterizer.cxx b/reco/tasks/CbmTaskTofClusterizer.cxx
index bdfd3ff575..2a58a0ac07 100644
--- a/reco/tasks/CbmTaskTofClusterizer.cxx
+++ b/reco/tasks/CbmTaskTofClusterizer.cxx
@@ -353,8 +353,10 @@ bool CbmTaskTofClusterizer::BuildClusters()
   *fTofCalDigiVec = fCalibrate(fTofDigiVec).first;
 
   //Call cluster finder
-  auto clusterOut = fAlgo(*fTofCalDigiVec);
-  auto clusters   = std::get<0>(clusterOut);
+  auto clusterOut    = fAlgo(*fTofCalDigiVec);
+  auto& clusters     = std::get<0>(clusterOut);
+  auto& indices      = std::get<2>(clusterOut);
+  size_t indexOffset = 0;
 
   //Store hits and match
   for (auto const& cluster : clusters.Data()) {
@@ -362,17 +364,19 @@ bool CbmTaskTofClusterizer::BuildClusters()
     TVector3 hitpos        = TVector3(cluster.hitPos.X(), cluster.hitPos.Y(), cluster.hitPos.Z());
     TVector3 hiterr        = TVector3(cluster.hitPosErr.X(), cluster.hitPosErr.Y(), cluster.hitPosErr.Z());
     new ((*fTofHitsColl)[hitIndex])
-      CbmTofHit(cluster.address, hitpos, hiterr, fiNbHits, cluster.hitTime, cluster.hitTimeErr,
-                cluster.vDigiIndRef.size(), int32_t(cluster.weightsSum * 10.));
+      CbmTofHit(cluster.address, hitpos, hiterr, fiNbHits, cluster.hitTime, cluster.hitTimeErr, cluster.numChan() * 2,
+                int32_t(cluster.weightsSum * 10.));
 
     fiNbHits++;
     //if (event) event->AddData(ECbmDataType::kTofHit, hitIndex);
 
     CbmMatch* digiMatch = new ((*fTofDigiMatchColl)[hitIndex]) CbmMatch();
-    for (uint32_t i = 0; i < cluster.vDigiIndRef.size(); i++) {
-      double tot = fTofCalDigiVec->at(cluster.vDigiIndRef.at(i)).GetTot();
-      digiMatch->AddLink(CbmLink(tot, cluster.vDigiIndRef.at(i), fiOutputTreeEntry, fiFileIndex));
+    for (uint32_t i = 0; i < cluster.numChan() * 2; i++) {
+      size_t digiInd = indices.at(indexOffset + i);
+      double tot     = fTofCalDigiVec->at(digiInd).GetTot();
+      digiMatch->AddLink(CbmLink(tot, digiInd, fiOutputTreeEntry, fiFileIndex));
     }
+    indexOffset += cluster.numChan() * 2;
   }
   std::cout << "hits " << fiNbHits << std::endl;
 
-- 
GitLab