From a6f48a8e02e49074d167249fad812203ac4de360 Mon Sep 17 00:00:00 2001
From: P-A Loizeau <p.-a.loizeau@gsi.de>
Date: Thu, 28 Nov 2024 17:23:25 +0100
Subject: [PATCH] Add new service binary: tsadump -f xxxxx.tsa

---
 services/CMakeLists.txt              |  1 +
 services/tsa_dump/Application.cxx    | 45 ++++++++++++++++++++
 services/tsa_dump/Application.h      | 18 ++++++++
 services/tsa_dump/CMakeLists.txt     | 38 +++++++++++++++++
 services/tsa_dump/ProgramOptions.cxx | 62 ++++++++++++++++++++++++++++
 services/tsa_dump/ProgramOptions.h   | 14 +++++++
 services/tsa_dump/main.cxx           | 13 ++++++
 7 files changed, 191 insertions(+)
 create mode 100644 services/tsa_dump/Application.cxx
 create mode 100644 services/tsa_dump/Application.h
 create mode 100644 services/tsa_dump/CMakeLists.txt
 create mode 100644 services/tsa_dump/ProgramOptions.cxx
 create mode 100644 services/tsa_dump/ProgramOptions.h
 create mode 100644 services/tsa_dump/main.cxx

diff --git a/services/CMakeLists.txt b/services/CMakeLists.txt
index 824b7748eb..39051bcf61 100644
--- a/services/CMakeLists.txt
+++ b/services/CMakeLists.txt
@@ -1,3 +1,4 @@
 add_subdirectory(archive_explorer)
 add_subdirectory(histserv)
 add_subdirectory(online_par_dump)
+add_subdirectory(tsa_dump)
diff --git a/services/tsa_dump/Application.cxx b/services/tsa_dump/Application.cxx
new file mode 100644
index 0000000000..ecc2c581e5
--- /dev/null
+++ b/services/tsa_dump/Application.cxx
@@ -0,0 +1,45 @@
+/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#include "Application.h"
+
+#include "CbmFormatTsPrintout.h"
+
+#include <StorableTimeslice.hpp>
+#include <TimesliceAutoSource.hpp>
+
+#include <Logger.h>
+
+#include <iomanip>
+#include <ios>
+#include <iostream>
+#include <sstream>
+
+void Application::Run()
+{
+  LOG(info) << "Calling string constructor with ";
+  LOG(info) << fOpts.sFullFilename;
+  fles::TimesliceAutoSource* fTsSource = new fles::TimesliceAutoSource(fOpts.sFullFilename);
+
+  std::unique_ptr<fles::Timeslice> ts;
+  ts             = fTsSource->get();
+  uint64_t uTsNb = 0;
+  while (ts) {
+    // Use << operator defined in <cbmroot_src>core/base/utils/flestools/CbmFormatTsPrintout.h
+    std::stringstream ss;
+    ss << (*(ts.get()));
+    LOG(info) << "=====================================\n" << ss.str();
+    uTsNb++;
+    if (fOpts.uNbTimeslices == uTsNb) {
+      break;
+    }
+    ts = fTsSource->get();
+  }
+  if (fOpts.uNbTimeslices == uTsNb) {
+    LOG(info) << "Requested number of TS reached; stopping there. Dumped " << uTsNb << " TS";
+  }
+  else {
+    LOG(info) << "End of archive reached; stopping there. Dumped " << uTsNb << " TS";
+  }
+}
diff --git a/services/tsa_dump/Application.h b/services/tsa_dump/Application.h
new file mode 100644
index 0000000000..6732607ba4
--- /dev/null
+++ b/services/tsa_dump/Application.h
@@ -0,0 +1,18 @@
+/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#pragma once
+
+#include "ProgramOptions.h"
+
+class Application {
+
+ public:
+  Application(ProgramOptions opts) : fOpts(opts) {}
+
+  void Run();
+
+ private:
+  ProgramOptions fOpts;
+};
diff --git a/services/tsa_dump/CMakeLists.txt b/services/tsa_dump/CMakeLists.txt
new file mode 100644
index 0000000000..994461aca9
--- /dev/null
+++ b/services/tsa_dump/CMakeLists.txt
@@ -0,0 +1,38 @@
+# Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+# SPDX-License-Identifier: GPL-3.0-only
+# Authors: Pierre-Alain Loizeau [committer]
+
+set(APP tsadump)
+
+set(INCLUDE_DIRECTORIES
+  ${INCLUDE_DIRECTORIES}
+  ${CMAKE_CURRENT_SOURCE_DIR}
+  ${CBMROOT_SOURCE_DIR}
+  )
+
+set(SRCS
+  Application.cxx
+  ProgramOptions.cxx
+  main.cxx
+  )
+
+set(HEADERS
+  Application.h
+  ProgramOptions.h
+  )
+
+add_executable(${APP} ${SRCS} ${HEADERS})
+target_link_libraries(${APP}
+  PUBLIC
+    CbmFlibFlesTools
+    FairLogger::FairLogger
+    external::fles_ipc
+    libzmq
+    cppzmq
+  PRIVATE
+    Boost::program_options
+    external::fles_logging # Needed for TimesliceSource in CbmSourceTsArchive, not sure of conflicts with FairLogger
+  )
+
+
+install(TARGETS ${APP} DESTINATION bin)
diff --git a/services/tsa_dump/ProgramOptions.cxx b/services/tsa_dump/ProgramOptions.cxx
new file mode 100644
index 0000000000..e0c3846749
--- /dev/null
+++ b/services/tsa_dump/ProgramOptions.cxx
@@ -0,0 +1,62 @@
+/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#include "ProgramOptions.h"
+
+#include <boost/program_options.hpp>
+
+#include <iostream>
+
+namespace po = boost::program_options;
+
+ProgramOptions::ProgramOptions(int argc, char** argv)
+{
+  po::options_description required("Required options");
+  // clang-format off
+  required.add_options()
+    ("filename,f", po::value(&sFullFilename)->value_name("<outputDir>")->required(),
+      "Full path to TSA file")
+  ;
+  // clang-format on
+
+  po::options_description optional{"Other options"};
+  // clang-format off
+  optional.add_options()
+    ("timeslices,n", po::value(&uNbTimeslices),
+      "number of timeslices")
+    ("help,h", "Print help message")
+  ;
+  // clang-format on
+
+
+  po::options_description cmdline_options;
+  cmdline_options.add(required).add(optional);
+
+  po::variables_map vm;
+  po::command_line_parser parser{argc, argv};
+  parser.options(cmdline_options);
+  try {
+    auto result = parser.run();
+    po::store(result, vm);
+  }
+  catch (const std::exception& e) {
+    std::cerr << "Error: " << e.what() << std::endl;
+    std::cerr << "Use '-h' to display all valid options." << std::endl;
+    std::exit(EXIT_FAILURE);
+  }
+
+  if (vm.count("help") > 0) {
+    std::cout << cmdline_options << std::endl;
+    std::exit(EXIT_SUCCESS);
+  }
+
+  try {
+    po::notify(vm);
+  }
+  catch (const po::required_option& e) {
+    std::cerr << "Error: " << e.what() << std::endl;
+    std::cerr << "Use '-h' to display all valid options." << std::endl;
+    std::exit(EXIT_FAILURE);
+  }
+}
diff --git a/services/tsa_dump/ProgramOptions.h b/services/tsa_dump/ProgramOptions.h
new file mode 100644
index 0000000000..6edd3bacf4
--- /dev/null
+++ b/services/tsa_dump/ProgramOptions.h
@@ -0,0 +1,14 @@
+/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#pragma once
+
+#include <string>
+
+struct ProgramOptions {
+  ProgramOptions(int argc, char** argv);
+
+  uint64_t uNbTimeslices = 0;
+  std::string sFullFilename;
+};
diff --git a/services/tsa_dump/main.cxx b/services/tsa_dump/main.cxx
new file mode 100644
index 0000000000..6f64705d0b
--- /dev/null
+++ b/services/tsa_dump/main.cxx
@@ -0,0 +1,13 @@
+/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
+   SPDX-License-Identifier: GPL-3.0-only
+   Authors: Pierre-Alain Loizeau [committer] */
+
+#include "Application.h"
+
+int main(int argc, char** argv)
+{
+  ProgramOptions opts{argc, argv};
+  Application app{opts};
+  app.Run();
+  return 0;
+}
-- 
GitLab