From ebbb46b79dd5f563940677524c8fa774834fb975 Mon Sep 17 00:00:00 2001
From: "P.-A. Loizeau" <p.-a.loizeau@gsi.de>
Date: Mon, 4 Apr 2022 12:25:17 +0200
Subject: [PATCH] [mBmon] Add Tof unpack par Alias for BMon + related flags and
 conversions

Needed to have two instances of the CbmMcbm2018TofPar in the same FairRoot Runtime DB manager, which is needed at least for MQ par server
- new CbmMcbm2018BmonPar class as transparent derivation of CbmMcbm2018TofPar
- Control flags in TOF and BMon unpacking code (algo + config)
- Type and pointer conversions in TOF unpack algo to hide the alias class and use only base class internaly
---
 core/detectors/tof/CbmMcbm2018TofPar.h        | 12 +++++++++++
 core/detectors/tof/CbmTofBaseLinkDef.h        |  1 +
 core/detectors/tof/CbmTofContFact.cxx         |  8 ++++++++
 macro/beamtime/mcbm2022/mBmonCriPar.par       |  2 +-
 .../tof/unpack/CbmBmonUnpackConfig.cxx        |  1 +
 .../detectors/tof/unpack/CbmTofUnpackAlgo.cxx | 20 ++++++++++++++++---
 reco/detectors/tof/unpack/CbmTofUnpackAlgo.h  |  8 ++++++++
 .../tof/unpack/CbmTofUnpackConfig.cxx         |  5 +++++
 .../detectors/tof/unpack/CbmTofUnpackConfig.h |  8 ++++++++
 9 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/core/detectors/tof/CbmMcbm2018TofPar.h b/core/detectors/tof/CbmMcbm2018TofPar.h
index 901e3169a8..338fb98fc8 100644
--- a/core/detectors/tof/CbmMcbm2018TofPar.h
+++ b/core/detectors/tof/CbmMcbm2018TofPar.h
@@ -168,4 +168,16 @@ private:
 
   ClassDef(CbmMcbm2018TofPar, 1);
 };
+
+class CbmMcbm2018BmonPar : public CbmMcbm2018TofPar {
+public:
+  /** Standard constructor **/
+  CbmMcbm2018BmonPar(const char* name = "CbmMcbm2018BmonPar", const char* title = "Bmon unpacker parameters",
+                     const char* context = "Default")
+    : CbmMcbm2018TofPar(name, title, context) {};
+
+private:
+  // just an alias for the Parameter container to allow two instances
+  ClassDef(CbmMcbm2018BmonPar, 1);
+};
 #endif  // CBMMCBM2018TOFPAR_H
diff --git a/core/detectors/tof/CbmTofBaseLinkDef.h b/core/detectors/tof/CbmTofBaseLinkDef.h
index 47794dbeb7..302372dec1 100644
--- a/core/detectors/tof/CbmTofBaseLinkDef.h
+++ b/core/detectors/tof/CbmTofBaseLinkDef.h
@@ -15,6 +15,7 @@
 #pragma link C++ class CbmTofDigiPar + ;
 #pragma link C++ class CbmMcbm2018TofPar + ;
 #pragma link C++ class CbmTofGeoHandler + ;
+#pragma link C++ class CbmMcbm2018BmonPar + ;
 
 #pragma link C++ class TTrbHeader + ;
 #endif
diff --git a/core/detectors/tof/CbmTofContFact.cxx b/core/detectors/tof/CbmTofContFact.cxx
index 21cc24ad66..ff2bd329ab 100644
--- a/core/detectors/tof/CbmTofContFact.cxx
+++ b/core/detectors/tof/CbmTofContFact.cxx
@@ -58,6 +58,11 @@ void CbmTofContFact::setAllContainers()
   FairContainer* beamPars = new FairContainer("CbmMcbm2018TofPar", "TOF at MCBM 2018 Unpack Parameters", "Default");
   beamPars->addContext("Default");
   containers->Add(beamPars);
+
+  FairContainer* beamParsBmon = new FairContainer(
+    "CbmMcbm2018BmonPar", "BMon at MCBM 2022+ Unpack Parameters, interim for dual instances", "Default");
+  beamParsBmon->addContext("Default");
+  containers->Add(beamParsBmon);
 }
 
 FairParSet* CbmTofContFact::createContainer(FairContainer* c)
@@ -77,6 +82,9 @@ FairParSet* CbmTofContFact::createContainer(FairContainer* c)
   else if (strcmp(name, "CbmMcbm2018TofPar") == 0) {
     p = new CbmMcbm2018TofPar(c->getConcatName().Data(), c->GetTitle(), c->getContext());
   }
+  else if (strcmp(name, "CbmMcbm2018BmonPar") == 0) {
+    p = new CbmMcbm2018BmonPar(c->getConcatName().Data(), c->GetTitle(), c->getContext());
+  }
 
   return p;
 }
diff --git a/macro/beamtime/mcbm2022/mBmonCriPar.par b/macro/beamtime/mcbm2022/mBmonCriPar.par
index 98525587b7..330ca7e7a6 100644
--- a/macro/beamtime/mcbm2022/mBmonCriPar.par
+++ b/macro/beamtime/mcbm2022/mBmonCriPar.par
@@ -1,5 +1,5 @@
 ####################################################################################################
-[CbmMcbm2018TofPar]
+[CbmMcbm2018BmonPar]
 //----------------------------------------------------------------------------
 NrOfGdpbs: Int_t 4
 GdpbIdArray: Int_t \
diff --git a/reco/detectors/tof/unpack/CbmBmonUnpackConfig.cxx b/reco/detectors/tof/unpack/CbmBmonUnpackConfig.cxx
index 7bd185d252..d4a0ab13c1 100644
--- a/reco/detectors/tof/unpack/CbmBmonUnpackConfig.cxx
+++ b/reco/detectors/tof/unpack/CbmBmonUnpackConfig.cxx
@@ -17,6 +17,7 @@
 CbmBmonUnpackConfig::CbmBmonUnpackConfig(std::string detGeoSetupTag, UInt_t runid)
   : CbmTofUnpackConfig(detGeoSetupTag, runid)
 {
+  SetFlagBmonParMode();
 }
 
 CbmBmonUnpackConfig::~CbmBmonUnpackConfig() {}
diff --git a/reco/detectors/tof/unpack/CbmTofUnpackAlgo.cxx b/reco/detectors/tof/unpack/CbmTofUnpackAlgo.cxx
index e688c7e107..e962d40299 100644
--- a/reco/detectors/tof/unpack/CbmTofUnpackAlgo.cxx
+++ b/reco/detectors/tof/unpack/CbmTofUnpackAlgo.cxx
@@ -25,14 +25,21 @@ CbmTofUnpackAlgo::~CbmTofUnpackAlgo() {}
 std::vector<std::pair<std::string, std::shared_ptr<FairParGenericSet>>>*
   CbmTofUnpackAlgo::GetParContainerRequest(std::string /*geoTag*/, std::uint32_t /*runId*/)
 {
-  // Basepath for default Trd parameter sets (those connected to a geoTag)
+  // Basepath for default Tof parameter sets (those connected to a geoTag)
   std::string basepath = Form("%s", fParFilesBasePath.data());
   std::string temppath = "";
 
   // // Get parameter container
   temppath = basepath + fParFileName;
   LOG(info) << fName << "::GetParContainerRequest - Trying to open file " << temppath;
-  fParContVec.emplace_back(std::make_pair(temppath, std::make_shared<CbmMcbm2018TofPar>()));
+  if (fbBmonParMode) {
+    /// Should be enabled only when both TOF and BMON are used with same RunManagerDb (only MQ for now)
+    LOG(info) << fName << "::GetParContainerRequest - Requesting CbmMcbm2018BmonPar instead of CbmMcbm2018TofPar";
+    fParContVec.emplace_back(std::make_pair(temppath, std::make_shared<CbmMcbm2018BmonPar>()));
+  }
+  else {
+    fParContVec.emplace_back(std::make_pair(temppath, std::make_shared<CbmMcbm2018TofPar>()));
+  }
 
   return &fParContVec;
 }
@@ -44,7 +51,14 @@ Bool_t CbmTofUnpackAlgo::init() { return kTRUE; }
 Bool_t CbmTofUnpackAlgo::initParSet(FairParGenericSet* parset)
 {
   LOG(info) << fName << "::initParSet - for container " << parset->ClassName();
-  if (parset->IsA() == CbmMcbm2018TofPar::Class()) return initParSet(static_cast<CbmMcbm2018TofPar*>(parset));
+  if (parset->IsA() == CbmMcbm2018BmonPar::Class()) {
+    /// Should be the case only if fbBmonParMode is enabled and when both TOF and BMON are used with same
+    /// RunManagerDb (only MQ for now). CbmMcbm2018BmonPar is an identical derivation of CbmMcbm2018TofPar
+    return initParSet(dynamic_cast<CbmMcbm2018TofPar*>(parset));
+  }
+  else if (parset->IsA() == CbmMcbm2018TofPar::Class()) {
+    return initParSet(static_cast<CbmMcbm2018TofPar*>(parset));
+  }
 
   // If we do not know the derived ParSet class we return false
   LOG(error)
diff --git a/reco/detectors/tof/unpack/CbmTofUnpackAlgo.h b/reco/detectors/tof/unpack/CbmTofUnpackAlgo.h
index 5d67958d9f..aa04de6a42 100644
--- a/reco/detectors/tof/unpack/CbmTofUnpackAlgo.h
+++ b/reco/detectors/tof/unpack/CbmTofUnpackAlgo.h
@@ -72,6 +72,13 @@ public:
   */
   void SetFlagEpochCountHack2021(bool bFlagin = true) { fbEpochCountHack2021 = bFlagin; }
 
+  /**
+   * @brief Sets the flag switching to a request of CbmMcbm2018BmonPar. Default is enable.
+   *
+   * @param[in] Optional: boolean flag value, default is true
+  */
+  void SetFlagBmonParMode(bool bFlagin = true) { fbBmonParMode = bFlagin; }
+
   /**
    * @brief Sets the name of the parameter file to be used.
    *
@@ -194,6 +201,7 @@ private:
 
   /// Control flags
   bool fbEpochCountHack2021             = false;
+  bool fbBmonParMode                    = false;
   std::vector<bool> fvbMaskedComponents = {};
   bool fbLastEpochGood                  = false;
 
diff --git a/reco/detectors/tof/unpack/CbmTofUnpackConfig.cxx b/reco/detectors/tof/unpack/CbmTofUnpackConfig.cxx
index a646cb8108..7e13c63952 100644
--- a/reco/detectors/tof/unpack/CbmTofUnpackConfig.cxx
+++ b/reco/detectors/tof/unpack/CbmTofUnpackConfig.cxx
@@ -41,6 +41,11 @@ std::shared_ptr<CbmTofUnpackAlgo> CbmTofUnpackConfig::chooseAlgo()
   // Unpacker algo from mcbm 2021 on and hopefully default for a long time.
   auto algo = std::make_shared<CbmTofUnpackAlgo>();
   LOG(info) << fName << "::chooseAlgo() - selected algo = " << algo->Class_Name();
+  if (fbBmonParMode) {
+    LOG(info) << fName << "::chooseAlgo - Setting the new algo in BMon Par mode";
+    algo->SetFlagBmonParMode(fbBmonParMode);
+  }
+
   return algo;
 
   LOG(error) << fName
diff --git a/reco/detectors/tof/unpack/CbmTofUnpackConfig.h b/reco/detectors/tof/unpack/CbmTofUnpackConfig.h
index d6bb8313c9..f743abab93 100644
--- a/reco/detectors/tof/unpack/CbmTofUnpackConfig.h
+++ b/reco/detectors/tof/unpack/CbmTofUnpackConfig.h
@@ -76,6 +76,13 @@ public:
   */
   void SetFlagEpochCountHack2021(bool bFlagin = true) { fbEpochCountHack2021 = bFlagin; }
 
+  /**
+   * @brief Sets the flag switching to a request of CbmMcbm2018BmonPar. Default is enable.
+   *
+   * @param[in] Optional: boolean flag value, default is true
+  */
+  void SetFlagBmonParMode(bool bFlagin = true) { fbBmonParMode = bFlagin; }
+
   /**
    * @brief Sets the name of the parameter file to be used.
    *
@@ -98,6 +105,7 @@ protected:
 private:
   /// Control flags
   bool fbEpochCountHack2021 = false;
+  bool fbBmonParMode        = false;
   /// Parameter file name
   std::string fsParFileName = "mTofCriPar.par";
 
-- 
GitLab