From 5acfd390396ef2471620d96bd9289b700f2ac8cf Mon Sep 17 00:00:00 2001
From: Jan de Cuveland <cuveland@compeng.uni-frankfurt.de>
Date: Tue, 24 May 2022 14:04:50 +0200
Subject: [PATCH] Add HttpServer and configuration to cbmreco_fairrun

---
 reco/app/cbmreco_fairrun/Application.cxx    | 3 ++-
 reco/app/cbmreco_fairrun/ProgramOptions.cxx | 2 ++
 reco/app/cbmreco_fairrun/ProgramOptions.h   | 6 +++++-
 reco/tasks/CbmReco.cxx                      | 7 +++++--
 reco/tasks/CbmReco.h                        | 2 ++
 reco/tasks/CbmRecoConfigExample.yaml        | 2 ++
 6 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/reco/app/cbmreco_fairrun/Application.cxx b/reco/app/cbmreco_fairrun/Application.cxx
index 4c60fc4090..80dc57401a 100644
--- a/reco/app/cbmreco_fairrun/Application.cxx
+++ b/reco/app/cbmreco_fairrun/Application.cxx
@@ -10,7 +10,8 @@ Application::Application(ProgramOptions const& opt) : fOpt(opt)
   config.LoadYaml(fOpt.ConfigYamlFile());
   if (!fOpt.SaveConfigYamlFile().empty()) { config.SaveYaml(fOpt.SaveConfigYamlFile()); }
 
-  fCbmReco = std::make_unique<CbmReco>(fOpt.InputUri(), fOpt.OutputRootFile(), fOpt.MaxNumTs(), config);
+  fCbmReco =
+    std::make_unique<CbmReco>(fOpt.InputUri(), fOpt.OutputRootFile(), fOpt.MaxNumTs(), config, fOpt.HttpServerPort());
 }
 
 void Application::Run() { fCbmReco->Run(); }
diff --git a/reco/app/cbmreco_fairrun/ProgramOptions.cxx b/reco/app/cbmreco_fairrun/ProgramOptions.cxx
index ce62996546..2571caf9fd 100644
--- a/reco/app/cbmreco_fairrun/ProgramOptions.cxx
+++ b/reco/app/cbmreco_fairrun/ProgramOptions.cxx
@@ -49,6 +49,8 @@ void ProgramOptions::ParseOptions(int argc, char* argv[])
              "save configuration to yaml file (mostly for debugging)");
   config_add("max-timeslice-number,n", po::value<int32_t>(&fMaxNumTs)->value_name("<n>"),
              "quit after processing given number of timeslices (default: unlimited)");
+  config_add("http-server-port,p", po::value<uint16_t>(&fHttpServerPort)->value_name("<n>"),
+             "port number for the HTTP server. If 0, server will not be activated (default)");
 
   po::options_description cmdline_options("Allowed options");
   cmdline_options.add(generic).add(config);
diff --git a/reco/app/cbmreco_fairrun/ProgramOptions.h b/reco/app/cbmreco_fairrun/ProgramOptions.h
index d57563303c..2ed6d8ca87 100644
--- a/reco/app/cbmreco_fairrun/ProgramOptions.h
+++ b/reco/app/cbmreco_fairrun/ProgramOptions.h
@@ -51,6 +51,9 @@ public:
   /** @brief Get maximum number of timeslices to process **/
   [[nodiscard]] int32_t MaxNumTs() const { return fMaxNumTs; }
 
+  /** @brief Get the port number for the HTTP server **/
+  [[nodiscard]] uint16_t HttpServerPort() const { return fHttpServerPort; }
+
 private:
   /** @brief Parse command line arguments using boost program_options **/
   void ParseOptions(int argc, char* argv[]);
@@ -59,7 +62,8 @@ private:
   std::string fOutputRootFile = "/dev/null";  ///< Output file name (.root format)
   std::string fConfigYamlFile;                ///< Configuration file name (.yaml format)
   std::string fSaveConfigYamlFile;            ///< Save configuration file name (.yaml format)
-  int32_t fMaxNumTs = INT32_MAX;              ///< Maximum number of timeslices to process
+  int32_t fMaxNumTs        = INT32_MAX;       ///< Maximum number of timeslices to process
+  uint16_t fHttpServerPort = 0;               ///< Port number for the HTTP server. If 0, server will not be activated.
 };
 
 #endif /* APP_CBMRECO_PROGRAMOPTIONS_H */
diff --git a/reco/tasks/CbmReco.cxx b/reco/tasks/CbmReco.cxx
index b29a262edd..54ab2384f8 100644
--- a/reco/tasks/CbmReco.cxx
+++ b/reco/tasks/CbmReco.cxx
@@ -56,6 +56,8 @@ void CbmRecoConfig::LoadYaml(const std::string& filename)
   fStoreTimeslice = config["store"]["timeslice"].as<bool>();
   fStoreTrigger   = config["store"]["triggers"].as<bool>();
   fStoreEvents    = config["store"]["events"].as<bool>();
+  // --- QA publishing
+  fHttpServerRefreshRate = config["qa"]["refreshrate"].as<int32_t>(fHttpServerRefreshRate);
 }
 // ----------------------------------------------------------------------------
 
@@ -79,6 +81,8 @@ void CbmRecoConfig::SaveYaml(const std::string& filename)
   config["store"]["timeslice"] = fStoreTimeslice;
   config["store"]["triggers"]  = fStoreTrigger;
   config["store"]["events"]    = fStoreEvents;
+  // --- QA publishing
+  config["qa"]["refreshrate"] = fHttpServerRefreshRate;
   // ---
   std::ofstream fout(filename);
   fout << config;
@@ -200,8 +204,7 @@ int32_t CbmReco::Run()
 
   // ----- HttpServer for online monitoring
   if (fHttpServerPort) {
-    Int_t serverRefreshRate = 100;  // timeslices
-    run.ActivateHttpServer(serverRefreshRate, fHttpServerPort);
+    run.ActivateHttpServer(fConfig.fHttpServerRefreshRate, fHttpServerPort);
     run.GetHttpServer()->GetSniffer()->SetScanGlobalDir(kFALSE);
   }
 
diff --git a/reco/tasks/CbmReco.h b/reco/tasks/CbmReco.h
index 61992fc1a7..2c233a4474 100644
--- a/reco/tasks/CbmReco.h
+++ b/reco/tasks/CbmReco.h
@@ -32,6 +32,8 @@ public:
   bool fStoreTimeslice = false;
   bool fStoreTrigger   = false;
   bool fStoreEvents    = false;
+  // --- QA publishing
+  int32_t fHttpServerRefreshRate = 100;
   // --- Load/save using yaml-cpp
   void LoadYaml(const std::string& filename);
   void SaveYaml(const std::string& filename);
diff --git a/reco/tasks/CbmRecoConfigExample.yaml b/reco/tasks/CbmRecoConfigExample.yaml
index 69bd607e3b..56d05361d7 100644
--- a/reco/tasks/CbmRecoConfigExample.yaml
+++ b/reco/tasks/CbmRecoConfigExample.yaml
@@ -10,3 +10,5 @@ store:
   timeslice: false
   triggers: false
   events: true
+qa:
+  refreshrate: 100
-- 
GitLab