From d04694210f7adee59834f40d20637469ef662a26 Mon Sep 17 00:00:00 2001
From: "Adrian A. Weber" <adrian.a.weber@physik.uni-giessen.de>
Date: Thu, 22 Jul 2021 09:49:24 +0000
Subject: [PATCH] add mRICH time offset correction and add masking of DiRICH
 boards to avoid the error messages. Error messages tells you now, which board
 has a problem.

---
 macro/run/run_unpack_tsa.C                    |  5 ++--
 .../rich/unpack/CbmRichUnpackAlgo.cxx         | 30 +++++++++++++++----
 .../detectors/rich/unpack/CbmRichUnpackAlgo.h |  6 ++++
 .../rich/unpack/CbmRichUnpackConfig.cxx       |  3 ++
 .../rich/unpack/CbmRichUnpackConfig.h         |  4 +++
 5 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/macro/run/run_unpack_tsa.C b/macro/run/run_unpack_tsa.C
index 444693d290..45d3e1f2d1 100644
--- a/macro/run/run_unpack_tsa.C
+++ b/macro/run/run_unpack_tsa.C
@@ -90,13 +90,14 @@ void run_unpack_tsa(std::string infile = "test.tsa", UInt_t runid = 0, const cha
   // ---- RICH ----
   std::shared_ptr<CbmRichUnpackConfig> richconfig = nullptr;
 
-  // richconfig = std::make_shared<CbmRichUnpackConfig>("", runid);
+  richconfig = std::make_shared<CbmRichUnpackConfig>("", runid);
   if (richconfig) {
     richconfig->SetDebugState();
     richconfig->SetDoWriteOutput();
     std::string parfilesbasepathRich = Form("%s/macro/beamtime/mcbm2021/", srcDir.Data());
     richconfig->SetParFilesBasePath(parfilesbasepathRich);
-    richconfig->SetSystemTimeOffset(0);  // [ns] value to be updated
+    richconfig->SetSystemTimeOffset(256000 - 1200);  // [ns] 1 MS and additional correction
+    if (runid == 1588) richconfig->MaskDiRICH(0x7150);
   }
   // -------------
 
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
index 2e7099eab6..92f82d36ec 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
+++ b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.cxx
@@ -27,7 +27,7 @@ std::vector<std::pair<std::string, std::shared_ptr<FairParGenericSet>>>*
   std::string temppath = "";
 
   // // Get parameter container
-  temppath = basepath + "mRichPar.par";
+  temppath = basepath + "mRichPar_70.par";
 
   fParContVec.emplace_back(std::make_pair(temppath, std::make_shared<CbmMcbm2018RichPar>()));
 
@@ -227,13 +227,19 @@ void CbmRichUnpackAlgo::processSubSubEvent(CbmRichUnpackAlgoMicrosliceReader& re
   // this array is used to match raising and falling edges
   std::vector<double> raisingTime(33, -1.);
 
+  // check if DiRICH (SubSubEvId) is masked
+  bool DiRICH_masked = false;
+  if (checkMaskedDiRICH(subSubEventId)) { DiRICH_masked = true; }
+
   for (int i = 0; i < nofTimeWords; i++) {
-    uint32_t word                     = reader.NextWord();
+    uint32_t word = reader.NextWord();
+    if (DiRICH_masked) continue;
     CbmRichUnpackAlgoTdcWordType type = CbmRichUnpackAlgoTdcWordReader::GetTdcWordType(word);
 
     if (type == CbmRichUnpackAlgoTdcWordType::TimeData) {
       if (!wasHeader || !wasEpoch || wasTrailer) {
-        LOG(error) << getLogHeader(reader) << "illegal position of TDC Time (before header/epoch or after trailer)";
+        LOG(error) << getLogHeader(reader) << "DiRICH 0x" << std::hex << subSubEventId << std::dec
+                   << ": illegal position of TDC Time (before header/epoch or after trailer)";
         errorInData = true;
         continue;
       }
@@ -242,7 +248,8 @@ void CbmRichUnpackAlgo::processSubSubEvent(CbmRichUnpackAlgoMicrosliceReader& re
     }
     else if (type == CbmRichUnpackAlgoTdcWordType::Epoch) {
       if (!wasHeader || wasTrailer) {
-        LOG(error) << getLogHeader(reader) << "illegal position of TDC Epoch (before header or after trailer)";
+        LOG(error) << getLogHeader(reader) << "DiRICH 0x" << std::hex << subSubEventId << std::dec
+                   << ": illegal position of TDC Epoch (before header or after trailer)";
         errorInData = true;
         continue;
       }
@@ -252,7 +259,8 @@ void CbmRichUnpackAlgo::processSubSubEvent(CbmRichUnpackAlgoMicrosliceReader& re
     }
     else if (type == CbmRichUnpackAlgoTdcWordType::Header) {
       if (wasEpoch || wasTime || wasTrailer) {
-        LOG(error) << getLogHeader(reader) << "illegal position of TDC Header (after time/epoch/trailer)";
+        LOG(error) << getLogHeader(reader) << "DiRICH 0x" << std::hex << subSubEventId << std::dec
+                   << ": illegal position of TDC Header (after time/epoch/trailer)";
         errorInData = true;
         continue;
       }
@@ -263,7 +271,8 @@ void CbmRichUnpackAlgo::processSubSubEvent(CbmRichUnpackAlgoMicrosliceReader& re
     }
     else if (type == CbmRichUnpackAlgoTdcWordType::Trailer) {
       if (!wasEpoch || !wasTime || !wasHeader) {
-        LOG(error) << getLogHeader(reader) << "illegal position of TDC Trailer (before time/epoch/header)";
+        LOG(error) << getLogHeader(reader) << "DiRICH 0x" << std::hex << subSubEventId << std::dec
+                   << ": illegal position of TDC Trailer (before time/epoch/header)";
         errorInData = true;
         continue;
       }
@@ -425,4 +434,13 @@ void CbmRichUnpackAlgo::writeOutputDigi(Int_t fpgaID, Int_t channel, Double_t ti
   }
 }
 
+bool CbmRichUnpackAlgo::checkMaskedDiRICH(Int_t subSubEventId)
+{
+  for (unsigned int i = 0; i < fMaskedDiRICHes->size(); ++i) {
+    if (fMaskedDiRICHes->at(i) == subSubEventId) return true;
+  }
+
+  return false;
+}
+
 ClassImp(CbmRichUnpackAlgo)
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
index 014e8882b2..c6009fcdf4 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
+++ b/reco/detectors/rich/unpack/CbmRichUnpackAlgo.h
@@ -219,6 +219,8 @@ public:
   virtual std::vector<std::pair<std::string, std::shared_ptr<FairParGenericSet>>>*
   GetParContainerRequest(std::string geoTag, std::uint32_t runId);
 
+  void SetMaskedDiRICHes(std::vector<Int_t>* maskedDiRICHes) { fMaskedDiRICHes = maskedDiRICHes; }
+
 protected:
   double calculateTime(uint32_t epoch, uint32_t coarse, uint32_t fine);
 
@@ -310,9 +312,13 @@ protected:
   */
   bool unpack(const fles::Timeslice* ts, std::uint16_t icomp, UInt_t imslice);
 
+  bool checkMaskedDiRICH(Int_t subSubEventId);
+
   /** @brief Parameters for the unpacking */
   CbmMcbm2018RichPar fUnpackPar;
 
+  std::vector<Int_t>* fMaskedDiRICHes = nullptr;
+
   double fMbsPrevTimeCh0 = 0.;
   double fMbsPrevTimeCh1 = 0.;
 
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackConfig.cxx b/reco/detectors/rich/unpack/CbmRichUnpackConfig.cxx
index de94c27a96..f7ce79dae6 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackConfig.cxx
+++ b/reco/detectors/rich/unpack/CbmRichUnpackConfig.cxx
@@ -36,6 +36,9 @@ void CbmRichUnpackConfig::InitUnpacker()
   if (fDoLog) LOG(info) << fName << "::Init - SetParFilesBasePath";
   algo->SetParFilesBasePath(fParFilesBasePath);
 
+  if (fDoLog) LOG(info) << fName << "::Init - SetMaskedDiRICHes";
+  algo->SetMaskedDiRICHes(&fMaskedDiRICHes);
+
   // Initialise the parameter containers required by the unpacker algo. Includes loading the corresponding ascii files
   auto reqparvec = algo->GetParContainerRequest(fGeoSetupTag, fRunId);
   initOk &= initParContainers(reqparvec);
diff --git a/reco/detectors/rich/unpack/CbmRichUnpackConfig.h b/reco/detectors/rich/unpack/CbmRichUnpackConfig.h
index 632226eb98..2641ae6782 100644
--- a/reco/detectors/rich/unpack/CbmRichUnpackConfig.h
+++ b/reco/detectors/rich/unpack/CbmRichUnpackConfig.h
@@ -68,6 +68,8 @@ public:
 
   // Setters
 
+  void MaskDiRICH(Int_t DiRICH) { fMaskedDiRICHes.push_back(DiRICH); }
+
 protected:
   /**
    * @brief Choose the derived unpacker algorithm to be used for the DAQ output to Digi translation. If algo was already set manually by the user this algorithm is used.
@@ -82,6 +84,8 @@ protected:
   /** @brief RunId of the current run, if not known 0 is a valid runtime case. Used runId based parameter loading. */
   UInt_t fRunId = 0;
 
+  std::vector<Int_t> fMaskedDiRICHes;
+
 private:
   ClassDef(CbmRichUnpackConfig, 2)
 };
-- 
GitLab