From 2018bab5718025e54eaa6a3aa2d2ba374e75303b Mon Sep 17 00:00:00 2001
From: P-A Loizeau <p.-a.loizeau@gsi.de>
Date: Fri, 30 Sep 2022 11:05:53 +0200
Subject: [PATCH] [mCBM] Add utils fct for RunId to setup mapping

- Add new file and namespace in utils part of the CbmBase library
- Add Mcbm::GetSetupFromRunId function
- Add Mcbm::Dummy class to force loading of the library n macros and circumvent deprecation of functions dict in Root6/Cling
- Update all macros with the if/else mapping to use the new function and catch exception for unmapped runs
---
 core/base/CMakeLists.txt                      |  1 +
 core/base/CbmBaseLinkDef.h                    |  7 +++
 core/base/utils/CbmMcbmUtils.cxx              | 54 +++++++++++++++++++
 core/base/utils/CbmMcbmUtils.h                | 18 +++++++
 macro/beamtime/mcbm2022/mcbm_digievent_reco.C | 28 ++++------
 macro/beamtime/mcbm2022/mcbm_event_reco.C     | 42 ++++++---------
 macro/beamtime/mcbm2022/mcbm_event_reco_L1.C  | 27 +++-------
 macro/beamtime/mcbm2022/mcbm_unp_event.C      | 30 ++++-------
 macro/run/run_unpack_online.C                 | 30 ++++-------
 macro/run/run_unpack_online_bmon.C            | 28 +++-------
 macro/run/run_unpack_tsa.C                    | 29 ++++------
 macro/run/run_unpack_tsa_bmon.C               | 28 +++-------
 12 files changed, 156 insertions(+), 166 deletions(-)
 create mode 100644 core/base/utils/CbmMcbmUtils.cxx
 create mode 100644 core/base/utils/CbmMcbmUtils.h

diff --git a/core/base/CMakeLists.txt b/core/base/CMakeLists.txt
index 00ff9e160c..567c4c3a0e 100644
--- a/core/base/CMakeLists.txt
+++ b/core/base/CMakeLists.txt
@@ -25,6 +25,7 @@ set(SRCS
   report/CbmLatexReportElement.cxx
   utils/CbmUtils.cxx
   utils/CbmGeometryUtils.cxx
+  utils/CbmMcbmUtils.cxx
   utils/CbmMediaList.cxx
   utils/CbmFileUtils.cxx
   )
diff --git a/core/base/CbmBaseLinkDef.h b/core/base/CbmBaseLinkDef.h
index 5a79379a93..df3992ea83 100644
--- a/core/base/CbmBaseLinkDef.h
+++ b/core/base/CbmBaseLinkDef.h
@@ -79,4 +79,11 @@
 #pragma link C++ class std::pair < TString, TString>;
 
 #pragma link C++ function Cbm::File::IsRootFile( string );
+
+#pragma link C++ namespace cbm;
+#pragma link C++ namespace cbm::mcbm;
+#pragma link C++ function cbm::mcbm::GetSetupFromRunId(uint64_t);
+// Class needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+#pragma link C++ class cbm::mcbm::ToForceLibLoad;
+
 #endif
diff --git a/core/base/utils/CbmMcbmUtils.cxx b/core/base/utils/CbmMcbmUtils.cxx
new file mode 100644
index 0000000000..e99e9f04d7
--- /dev/null
+++ b/core/base/utils/CbmMcbmUtils.cxx
@@ -0,0 +1,54 @@
+/* Copyright (C) 2022 Facility for Antiproton and Ion Research in Europe, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#include "CbmMcbmUtils.h"
+
+#include <stdexcept>
+
+namespace cbm
+{
+  namespace mcbm
+  {
+    std::string GetSetupFromRunId(uint64_t ulRunId)
+    {
+      /// General remark: only runs known to exist on disk/tape are mapped so "holes" are not an oversight
+      /// => if necessary exception throwing can also be added for them but anyway analysis will crash (no raw data)
+
+      /// 2021 "CRI" runs: 1575 - 1588 = 15/07/2021
+      std::string sSetupName = "mcbm_beam_2021_07_surveyed";
+      if (ulRunId < 1575) {
+        /// Only runs at earliest in the 2021 benchmark beamtime and with all 6 systems are supported by this function
+        /// From mCBM redmine wiki page: "run 1575: 1st run with 6 subsystems"
+        throw(std::invalid_argument("RunId smaller than the earliest run mapped (1575 in 2021 campaign)"));
+      }
+      /// Setup changed multiple times between the 2022 carbon and uranium runs
+      else if (2060 <= ulRunId && ulRunId <= 2065) {
+        /// Carbon runs: 2060 - 2065 = 10/03/2022
+        sSetupName = "mcbm_beam_2022_03_09_carbon";
+      }
+      else if (2150 <= ulRunId && ulRunId <= 2160) {
+        /// Iron runs: 2150 - 2160 = 24-25/03/2022
+        sSetupName = "mcbm_beam_2022_03_22_iron";
+      }
+      else if (2176 <= ulRunId && ulRunId <= 2310) {
+        /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
+        sSetupName = "mcbm_beam_2022_03_28_uranium";
+      }
+      else if (2350 <= ulRunId && ulRunId <= 2397) {
+        /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022 (Lambda Benchmark but mTOF troubles)
+        sSetupName = "mcbm_beam_2022_05_23_nickel";
+      }
+      else if (2454 <= ulRunId && ulRunId <= 2497) {
+        /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
+        sSetupName = "mcbm_beam_2022_06_16_gold";
+      }
+      else if (2497 < ulRunId) {
+        /// Future runs, exception there to force implementation and support from users side
+        throw(std::invalid_argument("RunId bigger than latest run mapped (2497, mCBM 2022)! Please complete the map!"));
+      }
+
+      return sSetupName;
+    }
+  }  // namespace mcbm
+}  // namespace cbm
diff --git a/core/base/utils/CbmMcbmUtils.h b/core/base/utils/CbmMcbmUtils.h
new file mode 100644
index 0000000000..0074d21d65
--- /dev/null
+++ b/core/base/utils/CbmMcbmUtils.h
@@ -0,0 +1,18 @@
+/* Copyright (C) 2022 Facility for Antiproton and Ion Research in Europe, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#include <cstdint>
+#include <string>
+
+namespace cbm
+{
+  namespace mcbm
+  {
+    std::string GetSetupFromRunId(uint64_t uRunId);
+
+    /// Class needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    class ToForceLibLoad {
+    };
+  }  // namespace mcbm
+}  // namespace cbm
diff --git a/macro/beamtime/mcbm2022/mcbm_digievent_reco.C b/macro/beamtime/mcbm2022/mcbm_digievent_reco.C
index 75f695b291..1901e6a82c 100644
--- a/macro/beamtime/mcbm2022/mcbm_digievent_reco.C
+++ b/macro/beamtime/mcbm2022/mcbm_digievent_reco.C
@@ -105,26 +105,16 @@ Bool_t mcbm_digievent_reco(UInt_t uRunId               = 2365,
 
   // --- Load the geometry setup ----
   // This is currently only required by the TRD (parameters)
-  TString geoSetupTag = "mcbm_beam_2021_07_surveyed";
-  if (2060 <= uRunId) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (uRunId <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_09_carbon";
-    }
-    else if (2150 <= uRunId && uRunId <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= uRunId && uRunId <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2352 <= uRunId) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_05_20_nickel";
-    }
+  cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+  TString geoSetupTag = "";
+  try {
+    geoSetupTag = cbm::mcbm::GetSetupFromRunId(uRunId);
+  }
+  catch (const std::invalid_argument& e) {
+    std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+    return kFALSE;
   }
+
   TString geoFile    = srcDir + "/macro/mcbm/data/" + geoSetupTag + ".geo.root";
   CbmSetup* geoSetup = CbmSetup::Instance();
   geoSetup->LoadSetup(geoSetupTag);
diff --git a/macro/beamtime/mcbm2022/mcbm_event_reco.C b/macro/beamtime/mcbm2022/mcbm_event_reco.C
index e4640c7645..5cbf432aad 100644
--- a/macro/beamtime/mcbm2022/mcbm_event_reco.C
+++ b/macro/beamtime/mcbm2022/mcbm_event_reco.C
@@ -117,26 +117,16 @@ Bool_t mcbm_event_reco(UInt_t uRunId                   = 2391,
 
   // --- Load the geometry setup ----
   // This is currently only required by the TRD (parameters)
-  TString geoSetupTag = "mcbm_beam_2021_07_surveyed";
-  if (2060 <= uRunId) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (uRunId <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_09_carbon";
-    }
-    else if (2150 <= uRunId && uRunId <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= uRunId && uRunId <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2352 <= uRunId) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_05_23_nickel";
-    }
+  cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+  TString geoSetupTag = "";
+  try {
+    geoSetupTag = cbm::mcbm::GetSetupFromRunId(uRunId);
+  }
+  catch (const std::invalid_argument& e) {
+    std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+    return kFALSE;
   }
+
   TString geoFile    = srcDir + "/macro/mcbm/data/" + geoSetupTag + ".geo.root";
   CbmSetup* geoSetup = CbmSetup::Instance();
   geoSetup->LoadSetup(geoSetupTag);
@@ -720,16 +710,16 @@ Bool_t mcbm_event_reco(UInt_t uRunId                   = 2391,
         tofFindTracks->SetStation(15, 0, 0, 4);
 
         /*
-       tofFindTracks->SetStation(16, 0, 3, 2);         
-       tofFindTracks->SetStation(17, 0, 4, 2);  
-       tofFindTracks->SetStation(18, 0, 3, 1);         
+       tofFindTracks->SetStation(16, 0, 3, 2);
+       tofFindTracks->SetStation(17, 0, 4, 2);
+       tofFindTracks->SetStation(18, 0, 3, 1);
        tofFindTracks->SetStation(19, 0, 4, 1);
-       tofFindTracks->SetStation(20, 0, 3, 3);         
+       tofFindTracks->SetStation(20, 0, 3, 3);
        tofFindTracks->SetStation(21, 0, 4, 3);
-       tofFindTracks->SetStation(22, 0, 3, 0);         
+       tofFindTracks->SetStation(22, 0, 3, 0);
        tofFindTracks->SetStation(23, 0, 4, 0);
-       tofFindTracks->SetStation(24, 0, 3, 4);         
-       tofFindTracks->SetStation(25, 0, 4, 4); 
+       tofFindTracks->SetStation(24, 0, 3, 4);
+       tofFindTracks->SetStation(25, 0, 4, 4);
        */
         break;
 
diff --git a/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C b/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
index bb957967f5..2648f98947 100644
--- a/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
+++ b/macro/beamtime/mcbm2022/mcbm_event_reco_L1.C
@@ -125,25 +125,14 @@ Bool_t mcbm_event_reco_L1(UInt_t uRunId                   = 2391,
 
   // --- Load the geometry setup ----
   // This is currently only required by the TRD (parameters)
-  TString geoSetupTag = "mcbm_beam_2021_07_surveyed";
-  if (2060 <= uRunId) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (uRunId <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_09_carbon";
-    }
-    else if (2150 <= uRunId && uRunId <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      geoSetupTag = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= uRunId && uRunId <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2352 <= uRunId) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      geoSetupTag = "mcbm_beam_2022_05_23_nickel";
-    }
+  cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+  TString geoSetupTag = "";
+  try {
+    geoSetupTag = cbm::mcbm::GetSetupFromRunId(uRunId);
+  }
+  catch (const std::invalid_argument& e) {
+    std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+    return kFALSE;
   }
 
   TString geoFile    = sInpDir + "/" + geoSetupTag + ".geo.root";
diff --git a/macro/beamtime/mcbm2022/mcbm_unp_event.C b/macro/beamtime/mcbm2022/mcbm_unp_event.C
index d9ddfb4a36..e8cebd9f0a 100644
--- a/macro/beamtime/mcbm2022/mcbm_unp_event.C
+++ b/macro/beamtime/mcbm2022/mcbm_unp_event.C
@@ -367,27 +367,15 @@ Bool_t mcbm_unp_event(std::string infile,
 
 
   // -----   CbmSetup   -----------------------------------------------------
-  if (2060 <= uRunId && defaultSetupName == setupName) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (uRunId <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      setupName = "mcbm_beam_2022_03_09_carbon";
-    }
-    else if (2150 <= uRunId && uRunId <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      setupName = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= uRunId && uRunId <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      setupName = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2350 <= uRunId && uRunId <= 2397) {
-      /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022
-      setupName = "mcbm_beam_2022_05_23_nickel";
-    }
-    else if (2454 <= uRunId && uRunId <= 2497) {
-      /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
-      setupName = "mcbm_beam_2022_06_16_gold";
+  /// Do automatic mapping only if not overridden by user
+  if (defaultSetupName == setupName) {
+    cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    try {
+      setupName = cbm::mcbm::GetSetupFromRunId(runid);
+    }
+    catch (const std::invalid_argument& e) {
+      std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+      return;
     }
     if (defaultSetupName != setupName) {
       std::cout << "Automatic setup choice for run " << uRunId << ": " << setupName << std::endl;
diff --git a/macro/run/run_unpack_online.C b/macro/run/run_unpack_online.C
index f34b29975f..f5c1b5196a 100644
--- a/macro/run/run_unpack_online.C
+++ b/macro/run/run_unpack_online.C
@@ -73,27 +73,15 @@ void run_unpack_online(std::vector<std::string> publisher = {"tcp://localhost:55
 
 
   // -----   CbmSetup   -----------------------------------------------------
-  if (2060 <= runid && defaultSetupName == setupName) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (runid <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      setupName = "mcbm_beam_2022_03_09_carbon";
-    }
-    else if (2150 <= runid && runid <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      setupName = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= runid && runid <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      setupName = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2350 <= runid && runid <= 2397) {
-      /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022
-      setupName = "mcbm_beam_2022_05_23_nickel";
-    }
-    else if (2454 <= runid && runid <= 2497) {
-      /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
-      setupName = "mcbm_beam_2022_06_16_gold";
+  /// Do automatic mapping only if not overridden by user
+  if (defaultSetupName == setupName) {
+    cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    try {
+      setupName = cbm::mcbm::GetSetupFromRunId(runid);
+    }
+    catch (const std::invalid_argument& e) {
+      std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+      return;
     }
     if (defaultSetupName != setupName) {
       std::cout << "Automatic setup choice for run " << runid << ": " << setupName << std::endl;
diff --git a/macro/run/run_unpack_online_bmon.C b/macro/run/run_unpack_online_bmon.C
index 07802aab9b..80b4113402 100644
--- a/macro/run/run_unpack_online_bmon.C
+++ b/macro/run/run_unpack_online_bmon.C
@@ -65,27 +65,15 @@ void run_unpack_online_bmon(std::vector<std::string> publisher = {"tcp://localho
 
 
   // -----   CbmSetup   -----------------------------------------------------
-  if (2060 <= runid && defaultSetupName == setupName) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (runid <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      setupName = "mcbm_beam_2022_03_09_carbon";
+  /// Do automatic mapping only if not overridden by user
+  if (defaultSetupName == setupName) {
+    cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    try {
+      setupName = cbm::mcbm::GetSetupFromRunId(runid);
     }
-    else if (2150 <= runid && runid <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      setupName = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= runid && runid <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      setupName = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2350 <= runid && runid <= 2397) {
-      /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022
-      setupName = "mcbm_beam_2022_05_23_nickel";
-    }
-    else if (2454 <= runid && runid <= 2497) {
-      /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
-      setupName = "mcbm_beam_2022_06_16_gold";
+    catch (const std::invalid_argument& e) {
+      std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+      return;
     }
     if (defaultSetupName != setupName) {
       std::cout << "Automatic setup choice for run " << runid << ": " << setupName << std::endl;
diff --git a/macro/run/run_unpack_tsa.C b/macro/run/run_unpack_tsa.C
index abbaf51496..dafd149d70 100644
--- a/macro/run/run_unpack_tsa.C
+++ b/macro/run/run_unpack_tsa.C
@@ -91,27 +91,15 @@ void run_unpack_tsa(std::vector<std::string> infile = {"test.tsa"}, UInt_t runid
 
 
   // -----   CbmSetup   -----------------------------------------------------
-  if (2060 <= runid && defaultSetupName == setupName) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (runid <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      setupName = "mcbm_beam_2022_03_09_carbon";
+  /// Do automatic mapping only if not overridden by user
+  if (defaultSetupName == setupName) {
+    cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    try {
+      setupName = cbm::mcbm::GetSetupFromRunId(runid);
     }
-    else if (2150 <= runid && runid <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      setupName = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= runid && runid <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      setupName = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2350 <= runid && runid <= 2397) {
-      /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022
-      setupName = "mcbm_beam_2022_05_23_nickel";
-    }
-    else if (2454 <= runid && runid <= 2497) {
-      /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
-      setupName = "mcbm_beam_2022_06_16_gold";
+    catch (const std::invalid_argument& e) {
+      std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+      return;
     }
     if (defaultSetupName != setupName) {
       std::cout << "Automatic setup choice for run " << runid << ": " << setupName << std::endl;
@@ -119,6 +107,7 @@ void run_unpack_tsa(std::vector<std::string> infile = {"test.tsa"}, UInt_t runid
   }
   auto cbmsetup = CbmSetup::Instance();
   cbmsetup->LoadSetup(setupName.c_str());
+
   // ------------------------------------------------------------------------
 
   // -----   UnpackerConfigs   ----------------------------------------------
diff --git a/macro/run/run_unpack_tsa_bmon.C b/macro/run/run_unpack_tsa_bmon.C
index 8cc767029f..c9bf7137f8 100644
--- a/macro/run/run_unpack_tsa_bmon.C
+++ b/macro/run/run_unpack_tsa_bmon.C
@@ -78,27 +78,15 @@ void run_unpack_tsa_bmon(std::vector<std::string> infile = {"test.tsa"}, UInt_t
 
 
   // -----   CbmSetup   -----------------------------------------------------
-  if (2060 <= runid && defaultSetupName == setupName) {
-    /// Setup changed multiple times between the 2022 carbon and uranium runs
-    if (runid <= 2065) {
-      /// Carbon runs: 2060 - 2065 = 10/03/2022
-      setupName = "mcbm_beam_2022_03_09_carbon";
+  /// Do automatic mapping only if not overridden by user
+  if (defaultSetupName == setupName) {
+    cbm::mcbm::ToForceLibLoad dummy;  /// Needed to trigger loading of the library as no fct dict in ROOT6 and CLING
+    try {
+      setupName = cbm::mcbm::GetSetupFromRunId(runid);
     }
-    else if (2150 <= runid && runid <= 2160) {
-      /// Iron runs: 2150 - 2160 = 24-25/03/2022
-      setupName = "mcbm_beam_2022_03_22_iron";
-    }
-    else if (2176 <= runid && runid <= 2310) {
-      /// Uranium runs: 2176 - 2310 = 30/03/2022 - 01/04/2022
-      setupName = "mcbm_beam_2022_03_28_uranium";
-    }
-    else if (2350 <= runid && runid <= 2397) {
-      /// Nickel runs: 2350 - 2397 = 23/05/2022 - 25/05/2022
-      setupName = "mcbm_beam_2022_05_23_nickel";
-    }
-    else if (2454 <= runid && runid <= 2497) {
-      /// Lambda Benchmark Gold runs: 2454 - 2497 = 16/06/2022 - 18/06/2022
-      setupName = "mcbm_beam_2022_06_16_gold";
+    catch (const std::invalid_argument& e) {
+      std::cout << "Error in mapping from runID to setup name: " << e.what() << std::endl;
+      return;
     }
     if (defaultSetupName != setupName) {
       std::cout << "Automatic setup choice for run " << runid << ": " << setupName << std::endl;
-- 
GitLab