R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
PedeLauncher.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2023 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "PedeLauncher.h"
15#include <boost/process/search_path.hpp>
16#include <boost/process/v2/process.hpp>
17#include <boost/process/v2/stdio.hpp>
18#include <exception>
19#include <filesystem>
20#include <fmt/core.h>
21#include <fmt/format.h>
22#include <fmt/ranges.h> // NOLINT
23#include <stdexcept>
24#include <string>
25#include <system_error>
26#include <vector>
27
28namespace bp = boost::process;
29namespace fs = std::filesystem;
30namespace bpv2 = boost::process::v2;
31
32namespace R3B::Millepede
33{
35 {
36#ifndef WITH_MILLEPEDE
37 throw std::runtime_error(
38 "Program is not compiled with with millepde2. Please enable \"WITH_MILLEPEDE=ON\" with CMake.");
39#endif
40 const auto exe_string = fmt::format("{}/{}", binary_directory_, executable_);
41 auto launch_args = std::vector<std::string>{ "-i", steer_filename_ };
42 auto exe_path = std::filesystem::path(exe_string);
43 if (not std::filesystem::exists(exe_path))
44 {
45 throw std::runtime_error(fmt::format("Command {:?} cannot be found or executed!", exe_string));
46 }
47
48 try
49 {
50 auto pede_program = bpv2::process{
51 ios_, exe_path, launch_args, bpv2::process_stdio{ .in = nullptr, .out = stdout, .err = stderr }
52 };
53 pede_program.async_wait(
54 [](const std::error_code& err, int ret)
55 {
56 if (err)
57 {
58 fmt::println("Error occured from the pede program. Error message: {}", err.message());
59 }
60 fmt::println("The pede program is closed successfully with the return value: {}", ret);
61 });
62 ios_.run();
63 ios_.reset();
64 }
65 catch (std::exception& ex)
66 {
67 throw std::runtime_error(fmt::format("Error occured when launching pede with \"{} {}\": \n {}",
68 bp::search_path(executable_).string(),
69 fmt::join(launch_args, " "),
70 ex.what()));
71 }
72 }
73
75 {
76 auto old_result_path = fs::current_path() / fs::path{ DEFAULT_RES_FILENAME };
77 auto new_result_path = fs::current_path() / fs::path{ parameter_file_ };
78
79 if (fs::exists(old_result_path))
80 {
81 fmt::print(
82 "Info: copy the file {:?} to the file {:?}!\n", old_result_path.string(), new_result_path.string());
83 fs::copy(old_result_path, new_result_path, fs::copy_options::overwrite_existing);
84 }
85 else
86 {
87 fmt::print("Error: millepede.res doesn't exist!\n");
88 }
89 }
90
91} // namespace R3B::Millepede
boost::asio::io_context ios_
std::string binary_directory_
constexpr auto DEFAULT_RES_FILENAME