R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
SteerWriter.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 "SteerWriter.h"
15#include <filesystem>
16#include <fmt/core.h>
17#include <fmt/format.h>
18#include <fmt/ranges.h> // NOLINT: provides formatter for std::pair<>
19#include <fstream>
20#include <ios>
21#include <magic_enum/magic_enum.hpp>
22#include <stdexcept>
23#include <string>
24#include <string_view>
25#include <utility>
26#include <vector>
27
28namespace R3B
29{
30 constexpr auto SteerWriter::convert_method_to_str(Method method) -> std::string_view
31 {
32 return magic_enum::enum_name(method);
33 }
34
35 void SteerWriter::add_parameter_default(int par_num, const std::pair<float, float>& values)
36 {
37 if (auto iter = parameter_defaults_.find(par_num); iter != parameter_defaults_.end())
38 {
39 throw std::logic_error(fmt::format(
40 "Default value of parameter {} has been already set to {}!", iter->second.first, iter->second.second));
41 }
42 parameter_defaults_.emplace(par_num, values);
43 }
44
45 void SteerWriter::add_method(Method method, const std::pair<float, float>& values)
46 {
47 if (auto iter = methods_.find(method); iter != methods_.end())
48 {
49 throw std::logic_error(fmt::format("Method {} has been already set to {}!", iter->first, iter->second));
50 }
51 methods_.emplace(method, values);
52 }
53
54 void SteerWriter::add_other_options(std::vector<std::string> options)
55 {
56 other_options_.push_back(std::move(options));
57 }
58
60 {
61 auto ofile = std::ofstream{ std::filesystem::path{ working_dir_ } / filepath_,
62 std::ios_base::out | std::ios_base::trunc };
63
64 if (not ofile.is_open())
65 {
66 throw std::runtime_error(fmt::format("Failed to open file {}", filepath_));
67 }
68
70 write_data_file(ofile);
71 write_methods(ofile);
72 write_others(ofile);
73 ofile << "end\n";
74 }
75
76 void SteerWriter::write_data_file(std::ofstream& ofile)
77 {
78 ofile << "Cfiles\n";
79 ofile << data_filepath_ << "\n";
80 ofile << parameter_file_ << "\n";
81 ofile << "\n";
82 }
83
85 {
86 auto ofile = std::ofstream{ std::filesystem::path{ working_dir_ } / parameter_file_,
87 std::ios_base::out | std::ios_base::trunc };
88
89 if (not ofile.is_open())
90 {
91 throw std::runtime_error(fmt::format("Can't open file {}", parameter_file_));
92 }
93
94 ofile << "Parameter\n";
95 for (const auto& [par_id, values] : parameter_defaults_)
96 {
97 ofile << fmt::format("{} {:.1f} {:.1f}\n", par_id, values.first, values.second);
98 }
99 }
100
101 void SteerWriter::write_methods(std::ofstream& ofile)
102 {
103 for (const auto& [method, values] : methods_)
104 {
105 ofile << fmt::format("method {} {} {}\n", method, values.first, values.second);
106 }
107 ofile << "\n";
108 }
109
110 void SteerWriter::write_others(std::ofstream& ofile)
111 {
112 for (const auto& other : other_options_)
113 {
114 ofile << fmt::format("{}\n", fmt::join(other, " "));
115 }
116 }
117} // namespace R3B
std::string data_filepath_
Definition SteerWriter.h:65
std::vector< std::vector< std::string > > other_options_
Definition SteerWriter.h:67
std::string parameter_file_
Definition SteerWriter.h:64
void write_data_file(std::ofstream &ofile)
void add_method(Method method, const std::pair< float, float > &values)
void write_parameter_defaults()
std::map< Method, std::pair< float, float > > methods_
Definition SteerWriter.h:62
std::string filepath_
Definition SteerWriter.h:63
std::string working_dir_
Definition SteerWriter.h:68
void add_other_options(std::vector< std::string > options)
void add_parameter_default(int par_num, const std::pair< float, float > &values)
static constexpr auto convert_method_to_str(Method) -> std::string_view
std::unordered_map< int, std::pair< float, float > > parameter_defaults_
Definition SteerWriter.h:66
void write_methods(std::ofstream &ofile)
void write_others(std::ofstream &ofile)