From 6745e41ec8a038a231d50f3f23df939cc90124d0 Mon Sep 17 00:00:00 2001
From: Pascal Raisig <praisig@ikf.uni-frankfurt.de>
Date: Thu, 15 Jul 2021 03:04:57 +0200
Subject: [PATCH] Finally fully add rich in a hopefully working state

---
 macro/run/run_unpack_tsa.C                       | 3 +--
 reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx | 9 +++++++--
 reco/detectors/rich/unpack/CbmRichUnpackAlgo.h   | 2 ++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/macro/run/run_unpack_tsa.C b/macro/run/run_unpack_tsa.C
index aa0f262f34..5c10bb01b4 100644
--- a/macro/run/run_unpack_tsa.C
+++ b/macro/run/run_unpack_tsa.C
@@ -83,9 +83,8 @@ void run_unpack_tsa(std::string infile = "test.tsa", UInt_t runid = 0, const cha
 
   // ---- RICH ----
   auto richconfig = std::make_shared<CbmRichUnpackConfig>("", runid);
-  // psdconfig->SetDebugState();
+  // richconfig->SetDebugState();
   richconfig->SetDoWriteOutput();
-  // psdconfig->SetDoWriteOptOutA("CbmPsdDsp");
   std::string parfilesbasepathRich = Form("%s/macro/beamtime/mcbm2021/", srcDir.Data());
   richconfig->SetParFilesBasePath(parfilesbasepathRich);
   // -------------
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
index ff5b18c608..83fbf66209 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
+++ b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
@@ -11,6 +11,8 @@
 #include <Rtypes.h>
 #include <RtypesCore.h>
 
+#include <cstdint>
+
 
 CbmRichUnpackAlgo::CbmRichUnpackAlgo() : CbmRecoUnpackAlgo(fgkFlesSubsystemIdTrdR, "CbmRichUnpackAlgo") {}
 
@@ -90,6 +92,9 @@ bool CbmRichUnpackAlgo::unpack(const fles::Timeslice* ts, std::uint16_t icomp, U
   CbmRichUnpackAlgoMicrosliceReader reader;
   reader.SetData(mv.content(), msDesc.size);
 
+  auto mstime = msDesc.idx;
+  fMsRefTime  = mstime - fTsStartTime;
+
   // There are a lot of MS  with 8 bytes size
   // Does one need these MS?
   if (reader.GetSize() <= 8) return true;
@@ -394,8 +399,8 @@ void CbmRichUnpackAlgo::writeOutputDigi(Int_t fpgaID, Int_t channel, Double_t ti
   Double_t ToTcorr = fbDoToTCorr ? fUnpackPar.GetToTshift(fpgaID, channel) : 0.;
   Int_t pixelUID   = this->getPixelUID(fpgaID, channel);
   //check ordering
-  //   Double_t finalTime = time + (Double_t) msRefTS - fSystemTimeoffset;
-  Double_t finalTime = time - fSystemTimeoffset - fTsStartTime;
+  Double_t finalTime = time + (Double_t) fMsRefTime - fSystemTimeoffset;
+  //   Double_t finalTime = time - fSystemTimeoffset - fTsStartTime;
 
   Double_t lastTime = 0.;
 
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
index e87175ca6e..1190677b63 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
+++ b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
@@ -319,6 +319,8 @@ protected:
   double fMbsPrevTimeCh0 = 0.;
   double fMbsPrevTimeCh1 = 0.;
 
+  size_t fMsRefTime = 0;
+
   double fToTMin = -20.;
   double fToTMax = 100.;
 
-- 
GitLab