From 3cd7b04fa0879c6bfe7aee326472415d58cd9452 Mon Sep 17 00:00:00 2001
From: Dominik Smith <smith@th.physik.uni-frankfurt.de>
Date: Thu, 2 Nov 2023 11:01:00 +0100
Subject: [PATCH] Removed digi indices from cbm::algo::tof::Hit. Stored indices
 in separate vector in cbm::algo::tof::Clusterizer.

---
 algo/detectors/tof/Clusterizer.cxx | 27 +++++++++++++++++----------
 algo/detectors/tof/Clusterizer.h   |  4 ++--
 algo/detectors/tof/Hit.h           | 18 ++++++++----------
 3 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/algo/detectors/tof/Clusterizer.cxx b/algo/detectors/tof/Clusterizer.cxx
index 14490b8888..537480d20f 100644
--- a/algo/detectors/tof/Clusterizer.cxx
+++ b/algo/detectors/tof/Clusterizer.cxx
@@ -52,9 +52,10 @@ namespace cbm::algo::tof
 
     // Output variables
     resultType result;
-    std::vector<Hit>& clustersOut   = std::get<0>(result);  // Hits
-    std::vector<size_t>& chanSizes  = std::get<1>(result);  // nClusters per channel
-    std::vector<u32>& chanAddresses = std::get<2>(result);  // channel addresses
+    std::vector<Hit>& clustersOut    = std::get<0>(result);  // Hits
+    std::vector<size_t>& chanSizes   = std::get<1>(result);  // nClusters per channel
+    std::vector<u32>& chanAddresses  = std::get<2>(result);  // channel addresses
+    std::vector<int32_t>& digiIndRef = std::get<3>(result);  // digi indices of clusters
 
     // Reference cell of a cluster
     TofCell* cell = nullptr;
@@ -85,7 +86,7 @@ namespace cbm::algo::tof
 
       while (1 < std::distance(digiIt, storDigi.end())) {
 
-        while (digiIt->first->GetSide() == std::next(digiIt, 1)->first->GetSide()) {  // Not one Digi of each end!
+        while (digiIt->first->GetSide() == std::next(digiIt)->first->GetSide()) {  // Not one Digi of each end!
           digiIt++;
           if (2 > std::distance(digiIt, storDigi.end())) { break; }
         }
@@ -94,7 +95,7 @@ namespace cbm::algo::tof
         // 2 Digis = both sides present
         cell               = &fParams.fChanPar[chan].cell;
         CbmTofDigi* xDigiA = digiIt->first;
-        CbmTofDigi* xDigiB = std::next(digiIt, 1)->first;
+        CbmTofDigi* xDigiB = std::next(digiIt)->first;
 
         // use local coordinates, (0,0,0) is in the center of counter  ?
         ROOT::Math::XYZVector pos(((-(double) numChan / 2. + (double) chan) + 0.5) * cell->sizeX, 0., 0.);
@@ -148,13 +149,16 @@ namespace cbm::algo::tof
             cluster.reset();
           }
         }
-        cluster.add(pos, time, totSum, totSum, digiIt->second, std::next(digiIt, 1)->second);
+        cluster.add(pos, time, totSum, totSum);
+        digiIndRef.push_back(digiIt->second);
+        digiIndRef.push_back(std::next(digiIt)->second);
+
         digiIt += 2;
 
         lastChan = chan;
         lastPosY = pos.Y();
         lastTime = time;
-        AddNextChan(input, lastChan, cluster, clustersOut, &lastChanPos);
+        AddNextChan(input, lastChan, cluster, clustersOut, digiIndRef, &lastChanPos);
       }  // while( 1 < storDigi.size() )
 
       // Apply subtraction such that chanSize constains hit count per channel
@@ -177,7 +181,8 @@ namespace cbm::algo::tof
   }
 
   bool Clusterizer::AddNextChan(std::vector<inputType>& input, int32_t lastChan, Hit& cluster,
-                                std::vector<Hit>& clustersOut, std::vector<inputType::iterator>* lastChanPos)
+                                std::vector<Hit>& clustersOut, std::vector<int32_t>& digiIndRef,
+                                std::vector<inputType::iterator>* lastChanPos)
   {
     //D.Smith 25.8.23: Why are "C" digis (position "2") not considered here?
 
@@ -246,14 +251,16 @@ namespace cbm::algo::tof
         const double totSum = xDigiA->GetTot() + xDigiB->GetTot();
 
         ROOT::Math::XYZVector pos(posX, posY, 0.);
-        cluster.add(pos, time, totSum, totSum, i1->second, i2->second);
+        cluster.add(pos, time, totSum, totSum);
+        digiIndRef.push_back(i1->second);
+        digiIndRef.push_back(i2->second);
 
         // remove digis at positions i1 and i2 from pool in efficient way (replaces two vector::erase calls).
         std::copy(i1 + 1, i2, i1);
         std::copy(i2 + 1, storDigi.end(), i2 - 1);
         storDigi.resize(storDigi.size() - 2);
 
-        if (AddNextChan(input, chan, cluster, clustersOut)) {
+        if (AddNextChan(input, chan, cluster, clustersOut, digiIndRef)) {
           return true;  // signal hit was already added
         }
         i1 = storDigi.end();  // jump to end of outer loop
diff --git a/algo/detectors/tof/Clusterizer.h b/algo/detectors/tof/Clusterizer.h
index e1605eeea5..d44899daec 100644
--- a/algo/detectors/tof/Clusterizer.h
+++ b/algo/detectors/tof/Clusterizer.h
@@ -27,7 +27,7 @@ namespace cbm::algo::tof
 {
   class Clusterizer {
   public:
-    typedef std::tuple<std::vector<Hit>, std::vector<size_t>, std::vector<u32>> resultType;
+    typedef std::tuple<std::vector<Hit>, std::vector<size_t>, std::vector<u32>, std::vector<int32_t>> resultType;
     typedef std::vector<std::pair<CbmTofDigi*, int32_t>> inputType;
 
     /**
@@ -58,7 +58,7 @@ namespace cbm::algo::tof
     resultType buildClusters(std::vector<inputType>& input);
 
     bool AddNextChan(std::vector<inputType>& input, int32_t iLastChan, Hit& cluster, std::vector<Hit>& clustersOut,
-                     std::vector<inputType::iterator>* lastChanPos = nullptr);
+                     std::vector<int32_t>& digiIndRef, std::vector<inputType::iterator>* lastChanPos = nullptr);
   };
 
 }  // namespace cbm::algo::tof
diff --git a/algo/detectors/tof/Hit.h b/algo/detectors/tof/Hit.h
index e32939886f..b9f2cc3a6a 100644
--- a/algo/detectors/tof/Hit.h
+++ b/algo/detectors/tof/Hit.h
@@ -48,9 +48,8 @@ namespace cbm::algo::tof
     double hitTime                  = 0.0;
     double hitTimeErr               = 0.0;
     int32_t address                 = 0;
-
-    std::vector<int32_t> vDigiIndRef;
-    double weightsSum = 0.0;
+    size_t numchan                  = 0;
+    double weightsSum               = 0.0;
 
     // Interface for tracker
 
@@ -65,27 +64,26 @@ namespace cbm::algo::tof
 
     // Interface end
 
-    int32_t numChan() { return vDigiIndRef.size() / 2; }
+    int32_t numChan() const { return numchan; }
 
     void reset()
     {
-      vDigiIndRef.clear();
       hitPos     = ROOT::Math::XYZVector(0.0, 0.0, 0.0);
-      hitPosErr  = ROOT::Math::XYZVector(0.5, 0.5, 0.5);  //D.Smith 15.8.23: Set this back to zero eventually.
+      hitPosErr  = ROOT::Math::XYZVector(0.0, 0.0, 0.0);
       hitTime    = 0.0;
       hitTimeErr = 0.0;
       weightsSum = 0.0;
       address    = 0;
+      numchan    = 0;
     }
 
-    void add(ROOT::Math::XYZVector pos, double time, double timeErr, double weight, int32_t digiIndA, int32_t digiIndB)
+    void add(ROOT::Math::XYZVector pos, double time, double timeErr, double weight)
     {
-      vDigiIndRef.push_back(digiIndA);
-      vDigiIndRef.push_back(digiIndB);
       hitPos += pos * weight;
       hitTime += time * weight;
       hitTimeErr += timeErr * weight;
       weightsSum += weight;
+      numchan++;
     }
 
     void normalize(double timeErr)
@@ -137,7 +135,7 @@ namespace cbm::algo::tof
       ar& hitTime;
       ar& hitTimeErr;
       ar& address;
-      ar& vDigiIndRef;
+      ar& numchan;
       ar& weightsSum;
     }
   };
-- 
GitLab