From 81495853d248447ded40d2cdd4b1e090ec4c9df7 Mon Sep 17 00:00:00 2001
From: Dominik Smith <d.smith@gsi.de>
Date: Fri, 27 Oct 2023 12:56:32 +0200
Subject: [PATCH] Removed RPC-wise calibration from cbm::algo::Clusterizer.

---
 algo/detectors/tof/Clusterizer.cxx   | 80 ----------------------------
 algo/detectors/tof/Clusterizer.h     |  1 -
 algo/detectors/tof/Hitfind.h         |  5 +-
 reco/tasks/CbmTaskTofClusterizer.cxx | 11 ++--
 4 files changed, 4 insertions(+), 93 deletions(-)

diff --git a/algo/detectors/tof/Clusterizer.cxx b/algo/detectors/tof/Clusterizer.cxx
index ca9b4726a3..b6b89733e5 100644
--- a/algo/detectors/tof/Clusterizer.cxx
+++ b/algo/detectors/tof/Clusterizer.cxx
@@ -18,7 +18,6 @@ namespace cbm::algo::tof
 
   Clusterizer::resultType Clusterizer::operator()(const inputType& digisIn)
   {
-    //std::vector<inputType> input = calibrateDigis(digisIn);
     std::vector<inputType> input = chanSortDigis(digisIn);
     return buildClusters(input);
   }
@@ -45,85 +44,6 @@ namespace cbm::algo::tof
     return result;
   }
 
-  //This calibration cannot reproduce the non RPC-parallel version, as the digi indices
-  //will be invalidated whenever digis are discarded in the dead-time check.
-  std::vector<Clusterizer::inputType> Clusterizer::calibrateDigis(const inputType& digisIn)
-  {
-    std::vector<inputType> result(fParams.fChanPar.size());
-    const int32_t numClWalkBinX = fParams.numClWalkBinX;
-    const double TOTMax         = fParams.TOTMax;
-    const double TOTMin         = fParams.TOTMin;
-
-    // channel deadtime map
-    std::map<int32_t, double> mChannelDeadTime;
-
-    for (size_t iDigi = 0; iDigi < digisIn.size(); iDigi++) {
-      CbmTofDigi* pDigi = digisIn[iDigi].first;
-      const double chan = pDigi->GetChannel();
-      const double side = pDigi->GetSide();
-      const double type = pDigi->GetType();
-
-      if (fParams.swapChannelSides && 5 != type && 8 != type) {
-        pDigi->SetAddress(pDigi->GetSm(), pDigi->GetRpc(), chan, (0 == side) ? 1 : 0, type);
-      }
-
-      // Check dead time
-      const int32_t addr = pDigi->GetAddress();
-      auto it            = mChannelDeadTime.find(addr);
-      if (it != mChannelDeadTime.end() && pDigi->GetTime() <= it->second) {
-        it->second = pDigi->GetTime() + fParams.channelDeadtime;
-        continue;
-      }
-      mChannelDeadTime[addr] = pDigi->GetTime() + fParams.channelDeadtime;
-
-      // Create calibrated digi
-      CbmTofDigi* pCalDigi = new CbmTofDigi(*pDigi);
-
-      // calibrate Digi Time
-      pCalDigi->SetTime(pCalDigi->GetTime() - fParams.fChanPar[chan].fvCPTOff[side]);
-
-      // subtract Offset
-      double charge = pCalDigi->GetTot() - fParams.fChanPar[chan].fvCPTotOff[side];
-      if (charge < 0.001) charge = 0.001;
-
-      // calibrate Digi ToT
-      pCalDigi->SetTot(charge * fParams.fChanPar[chan].fvCPTotGain[side]);
-
-      // walk correction
-      const double chargeBinSize = (TOTMax - TOTMin) / numClWalkBinX;
-      int32_t iWx                = (int32_t)((pCalDigi->GetTot() - TOTMin) / chargeBinSize);
-
-      if (0 > iWx) { iWx = 0; }
-      if (iWx >= numClWalkBinX) { iWx = numClWalkBinX - 1; }
-
-      std::vector<double>& walk = fParams.fChanPar[chan].fvCPWalk[side];
-      const double dDTot        = (pCalDigi->GetTot() - TOTMin) / chargeBinSize - (double) iWx - 0.5;
-      double dWT                = walk[iWx];
-
-      // linear interpolation to next bin   //memory leak??? (D.Smith 10.8.23: Clarify this comment!)
-      if (dDTot > 0) {
-        if (iWx < numClWalkBinX - 1) { dWT += dDTot * (walk[iWx + 1] - walk[iWx]); }
-      }
-      else {
-        if (0 < iWx) { dWT -= dDTot * (walk[iWx - 1] - walk[iWx]); }
-      }
-      pCalDigi->SetTime(pCalDigi->GetTime() - dWT);  // calibrate Digi Time
-
-      const int32_t index = digisIn[iDigi].second;
-      result[chan].push_back(std::make_pair(pCalDigi, index));
-    }
-
-    /// Sort the buffers of hits due to the time offsets applied
-    for (size_t chan = 0; chan < fParams.fChanPar.size(); chan++) {
-      std::sort(result[chan].begin(), result[chan].end(),
-                [](const std::pair<CbmTofDigi*, int32_t>& a, const std::pair<CbmTofDigi*, int32_t>& b) -> bool {
-                  return a.first->GetTime() < b.first->GetTime();
-                });
-    }
-
-    return result;
-  }
-
   //Iterator-based version. Faster than index-based version.
   Clusterizer::resultType Clusterizer::buildClusters(std::vector<inputType>& input)
   {
diff --git a/algo/detectors/tof/Clusterizer.h b/algo/detectors/tof/Clusterizer.h
index 6771ba8a41..aa13c2ebff 100644
--- a/algo/detectors/tof/Clusterizer.h
+++ b/algo/detectors/tof/Clusterizer.h
@@ -51,7 +51,6 @@ namespace cbm::algo::tof
   private:
     ClusterizerRpcPar fParams = {};  ///< Parameter container
 
-    std::vector<inputType> calibrateDigis(const inputType& digisIn);
     std::vector<inputType> chanSortDigis(const inputType& digisIn);
 
     resultType buildClusters(std::vector<inputType>& input);
diff --git a/algo/detectors/tof/Hitfind.h b/algo/detectors/tof/Hitfind.h
index a281ebee80..0b02d2d201 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::tuple<PartitionedVector<Hit>, HitfindMonitorData, std::vector<CbmTofDigi>> resultType;
+    typedef std::pair<PartitionedVector<Hit>, HitfindMonitorData> resultType;
 
     /** @brief Algorithm execution
      ** @param fles timeslice to hitfind
@@ -87,9 +87,6 @@ namespace cbm::algo::tof
 
     /** @brief Number of RPCs per super module type **/
     std::vector<int32_t> fNbRpc;
-
-    /** @brief Applies calibration to input digis **/
-    std::vector<CbmTofDigi> CalibRawDigis(gsl::span<CbmTofDigi> digiVec, HitfindMonitorData& monitor);
   };
 }  // namespace cbm::algo::tof
 
diff --git a/reco/tasks/CbmTaskTofClusterizer.cxx b/reco/tasks/CbmTaskTofClusterizer.cxx
index d3dba4ee11..f750fa4b9a 100644
--- a/reco/tasks/CbmTaskTofClusterizer.cxx
+++ b/reco/tasks/CbmTaskTofClusterizer.cxx
@@ -190,7 +190,7 @@ void CbmTaskTofClusterizer::Exec(Option_t* option)
       const CbmTofDigi* tDigi = fDigiMan->Get<CbmTofDigi>(iDigi);
       fTofDigiVec.push_back(CbmTofDigi(*tDigi));
 
-      if (iDigi == 10000) break;  // Only use 10000 digis for now. D.Smith
+      //   if (iDigi == 10000) break;  // Only use 10000 digis for now. D.Smith
     }
     ExecEvent(option);
   }
@@ -348,18 +348,13 @@ bool CbmTaskTofClusterizer::BuildClusters()
     }
   }
 
-  // Calibrated digis (calibration done in algo)
-  // Can be left out if calibrated digis not stored separately!
+  //Calibrate digis
   *fTofCalDigiVec = fCalibrate(fTofDigiVec).first;
 
-  //call cluster finder
+  //Call cluster finder
   auto clusterOut = fAlgo(*fTofCalDigiVec);
   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()) {
     const int32_t hitIndex = fTofHitsColl->GetEntriesFast();
-- 
GitLab