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/start_dir.hpp>
18#include <boost/process/v2/stdio.hpp>
19#include <exception>
20#include <filesystem>
21#include <fmt/core.h>
22#include <fmt/format.h>
23#include <fmt/ranges.h> // NOLINT
24#include <stdexcept>
25#include <string>
26#include <system_error>
27#include <vector>
28
29namespace bp = boost::process;
30namespace fs = std::filesystem;
31namespace bpv2 = boost::process::v2;
32
33namespace R3B::Millepede
34{
36 {
37#ifndef WITH_MILLEPEDE
38 throw std::runtime_error(
39 "Program is not compiled with with millepde2. Please enable \"WITH_MILLEPEDE=ON\" with CMake.");
40#endif
41 fs::create_directories(fs::path{ working_directory_ });
42 const auto exe_string = fmt::format("{}/{}", binary_directory_, executable_);
43 const auto steer_filepath = fs::absolute(fs::path{ working_directory_ } / fs::path{ steer_filename_ });
44 auto launch_args = std::vector<std::string>{ "-i", steer_filepath.string() };
45 auto exe_path = std::filesystem::path(exe_string);
46 if (not std::filesystem::exists(exe_path))
47 {
48 throw std::runtime_error(fmt::format("Command {:?} cannot be found or executed!", exe_string));
49 }
50
51 try
52 {
53 auto pede_program = bpv2::process{ ios_,
54 exe_path,
55 launch_args,
56 bpv2::process_start_dir{ working_directory_ },
57 bpv2::process_stdio{ .in = nullptr, .out = stdout, .err = stderr } };
58 pede_program.async_wait(
59 [](const std::error_code& err, int ret)
60 {
61 if (err)
62 {
63 fmt::println("Error occured from the pede program. Error message: {}", err.message());
64 }
65 fmt::println("The pede program is closed successfully with the return value: {}", ret);
66 });
67 ios_.run();
68 ios_.reset();
69 }
70 catch (std::exception& ex)
71 {
72 throw std::runtime_error(fmt::format("Error occured when launching pede with \"{} {}\": \n {}",
73 bp::search_path(executable_).string(),
74 fmt::join(launch_args, " "),
75 ex.what()));
76 }
77 }
78
80 {
81 auto old_result_path = fs::path{ working_directory_ } / fs::path{ DEFAULT_RES_FILENAME };
82 auto new_result_path = fs::path{ working_directory_ } / fs::path{ parameter_file_ };
83
84 if (fs::exists(old_result_path))
85 {
86 fmt::print(
87 "Info: copy the file {:?} to the file {:?}!\n", old_result_path.string(), new_result_path.string());
88 fs::copy(old_result_path, new_result_path, fs::copy_options::overwrite_existing);
89 }
90 else
91 {
92 fmt::print("Error: millepede.res doesn't exist!\n");
93 }
94 }
95
96} // namespace R3B::Millepede
std::string working_directory_
boost::asio::io_context ios_
std::string binary_directory_
constexpr auto DEFAULT_RES_FILENAME