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#define BOOST_PROCESS_USE_STD_FS 1
15
16#include "PedeLauncher.h"
17#include <boost/process.hpp>
18#include <filesystem>
19#include <fmt/format.h>
20#include <fmt/ranges.h>
21
22namespace bp = boost::process;
23namespace fs = std::filesystem;
24
25namespace R3B::Millepede
26{
28 {
29#ifndef WITH_MILLEPEDE
30 throw std::runtime_error(
31 "Program is not compiled with with millepde2. Please enable \"WITH_MILLEPEDE=ON\" with CMake.");
32#endif
33 const auto exe_string = fmt::format("{}/{}", binary_directory_, executable_);
34 auto launch_args = std::vector<std::string>{ "-i", steer_filename_ };
35 auto exe_path = std::filesystem::path(exe_string);
36 if (not std::filesystem::exists(exe_path))
37 {
38 throw std::runtime_error(fmt::format("Command {:?} cannot be found or executed!", exe_string));
39 }
40
41 try
42 {
43 auto pede_program =
44 bp::child{ exe_path,
45 bp::args(launch_args),
46 bp::std_out > stdout,
47 bp::std_err > stderr,
48 ios_,
49 bp::on_exit = [](int pid, const std::error_code& err)
50 { fmt::print("Child process returns {} with error code: {}\n", pid, err.message()); } };
51 ios_.run();
52 pede_program.wait();
53 fmt::print("Child process with PID {} ended.\n", pede_program.id());
54 ios_.reset();
55 }
56 catch (std::exception& ex)
57 {
58 throw std::runtime_error(fmt::format("Error occured when launching pede with \"{} {}\": \n {}",
59 bp::search_path(executable_).string(),
60 fmt::join(launch_args, " "),
61 ex.what()));
62 }
63 }
64
66 {
67 auto old_result_path = fs::current_path() / fs::path{ DEFAULT_RES_FILENAME };
68 auto new_result_path = fs::current_path() / fs::path{ parameter_file_ };
69
70 if (fs::exists(old_result_path))
71 {
72 fmt::print(
73 "Info: copy the file {:?} to the file {:?}!\n", old_result_path.string(), new_result_path.string());
74 fs::copy(old_result_path, new_result_path, fs::copy_options::overwrite_existing);
75 }
76 else
77 {
78 fmt::print("Error: millepede.res doesn't exist!\n");
79 }
80 }
81
82} // namespace R3B::Millepede
constexpr auto DEFAULT_RES_FILENAME