From 36db0433695e903ef0512c0a3a009e45a24ab393 Mon Sep 17 00:00:00 2001
From: Felix Weiglhofer <weiglhofer@fias.uni-frankfurt.de>
Date: Sat, 10 Jun 2023 13:12:46 +0000
Subject: [PATCH] algo: Forward declare cbm::Monitor in ChainContext.

This is a workaround for a bug in older cling versions.
---
 algo/CMakeLists.txt        |  1 +
 algo/base/ChainContext.cxx | 11 +++++++++++
 algo/base/ChainContext.h   | 16 ++++++++++++++--
 algo/base/SubChain.h       |  4 ++--
 algo/global/Reco.cxx       |  3 ++-
 5 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 algo/base/ChainContext.cxx

diff --git a/algo/CMakeLists.txt b/algo/CMakeLists.txt
index d107ef3998..dbb8289793 100644
--- a/algo/CMakeLists.txt
+++ b/algo/CMakeLists.txt
@@ -11,6 +11,7 @@ set(DEVICE_SRCS
 
 set(SRCS
   ${DEVICE_SRCS}
+  base/ChainContext.cxx
   base/Options.cxx
   base/util/TimingsFormat.cxx
   data/sts/LandauTable.cxx
diff --git a/algo/base/ChainContext.cxx b/algo/base/ChainContext.cxx
new file mode 100644
index 0000000000..9e75e9d807
--- /dev/null
+++ b/algo/base/ChainContext.cxx
@@ -0,0 +1,11 @@
+/* Copyright (C) 2023 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Felix Weiglhofer [committer] */
+#include "ChainContext.h"
+
+#include <Monitor.hpp>
+
+using namespace cbm::algo;
+
+ChainContext::ChainContext() {}
+ChainContext::~ChainContext() {}
diff --git a/algo/base/ChainContext.h b/algo/base/ChainContext.h
index 19f9e15fb6..46da5b9d83 100644
--- a/algo/base/ChainContext.h
+++ b/algo/base/ChainContext.h
@@ -4,18 +4,30 @@
 #ifndef CBM_ALGO_BASE_CHAINCONTEXT_H
 #define CBM_ALGO_BASE_CHAINCONTEXT_H
 
-#include <Monitor.hpp>
+#include <memory>
 #include <optional>
 
 #include "Options.h"
 #include "RecoParams.h"
 
+namespace cbm
+{
+  // cbm::Monitor must be forward declared. This prevents an issue in older ROOT versions,
+  // where cling would crash upon parsing the header file (in some stl header)
+  class Monitor;
+}  // namespace cbm
+
 namespace cbm::algo
 {
   struct ChainContext {
+    // default constructor / destructor
+    // But have to be defined in the .cxx file, because of forward declaration of cbm::Monitor
+    ChainContext();
+    ~ChainContext();
+
     Options opts;
     RecoParams recoParams;
-    std::optional<cbm::Monitor> monitor;  //!
+    std::unique_ptr<cbm::Monitor> monitor;  //! Monitor
   };
 }  // namespace cbm::algo
 
diff --git a/algo/base/SubChain.h b/algo/base/SubChain.h
index b206247190..b23538e09e 100644
--- a/algo/base/SubChain.h
+++ b/algo/base/SubChain.h
@@ -18,12 +18,12 @@ namespace cbm::algo
     const Options& Opts() const { return gsl::make_not_null(fContext)->opts; }
     const RecoParams& Params() const { return gsl::make_not_null(fContext)->recoParams; }
 
-    bool HasMonitor() const { return gsl::make_not_null(fContext)->monitor.has_value(); }
+    bool HasMonitor() const { return gsl::make_not_null(fContext)->monitor != nullptr; }
 
     Monitor& GetMonitor() const
     {
       // Need Get-prefix to avoid conflict with Monitor-class name
-      return gsl::make_not_null(fContext)->monitor.value();
+      return *gsl::make_not_null(fContext)->monitor;
     }
 
   private:
diff --git a/algo/global/Reco.cxx b/algo/global/Reco.cxx
index 9984134c92..e0ddaee20e 100644
--- a/algo/global/Reco.cxx
+++ b/algo/global/Reco.cxx
@@ -3,6 +3,7 @@
    Authors: Felix Weiglhofer [committer] */
 #include "Reco.h"
 
+#include <Monitor.hpp>
 #include <System.hpp>
 
 #include <xpu/host.h>
@@ -30,7 +31,7 @@ void Reco::Init(const Options& opts)
   L_(info) << "Running CBM Reco on Device " << props.name();
 
   if (!opts.MonitorUri().empty()) {
-    fContext.monitor.emplace(opts.MonitorUri());
+    fContext.monitor = std::make_unique<cbm::Monitor>(opts.MonitorUri());
     L_(info) << "Monitoring enabled, sending to " << opts.MonitorUri();
   }
 
-- 
GitLab