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