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 <fmt/format.h>
16#include <fmt/ranges.h>
17#include <fstream>
18
19namespace R3B
20{
21 void SteerWriter::add_parameter_default(int par_num, const std::pair<float, float>& values)
22 {
23 if (auto iter = parameter_defaults_.find(par_num); iter != parameter_defaults_.end())
24 {
25 throw std::logic_error(fmt::format(
26 "Default value of parameter {} has been already set to {}!", iter->second.first, iter->second.second));
27 }
28 parameter_defaults_.emplace(par_num, values);
29 }
30
31 void SteerWriter::add_method(Method method, const std::pair<float, float>& values)
32 {
33 if (auto iter = methods_.find(method); iter != methods_.end())
34 {
35 throw std::logic_error(fmt::format("Method {} has been already set to {}!", iter->first, iter->second));
36 }
37 methods_.emplace(method, values);
38 }
39
40 void SteerWriter::add_other_options(std::vector<std::string> options)
41 {
42 other_options_.push_back(std::move(options));
43 }
44
46 {
47 auto ofile = std::ofstream{ filepath_, std::ios_base::out | std::ios_base::trunc };
48
49 if (not ofile.is_open())
50 {
51 throw std::runtime_error(fmt::format("Failed to open file {}", filepath_));
52 }
53
54 write_parameter_defaults();
55 write_data_file(ofile);
56 write_methods(ofile);
57 write_others(ofile);
58 ofile << "end\n";
59 }
60
61 void SteerWriter::write_data_file(std::ofstream& ofile)
62 {
63 ofile << "Cfiles\n";
64 ofile << data_filepath_ << "\n";
65 ofile << parameter_file_ << "\n";
66 ofile << "\n";
67 }
68
69 void SteerWriter::write_parameter_defaults()
70 {
71 auto ofile = std::ofstream{ parameter_file_, std::ios_base::out | std::ios_base::trunc };
72
73 if (not ofile.is_open())
74 {
75 throw std::runtime_error(fmt::format("Can't open file {}", parameter_file_));
76 }
77
78 ofile << "Parameter\n";
79 for (const auto& [par_id, values] : parameter_defaults_)
80 {
81 ofile << fmt::format("{} {:.1f} {:.1f}\n", par_id, values.first, values.second);
82 }
83 }
84
85 void SteerWriter::write_methods(std::ofstream& ofile)
86 {
87 for (const auto& [method, values] : methods_)
88 {
89 ofile << fmt::format("method {} {} {}\n", method, values.first, values.second);
90 }
91 ofile << "\n";
92 }
93
94 void SteerWriter::write_others(std::ofstream& ofile)
95 {
96 for (const auto& other : other_options_)
97 {
98 ofile << fmt::format("{}\n", fmt::join(other, " "));
99 }
100 }
101} // namespace R3B
void add_method(Method method, const std::pair< float, float > &values)
void add_other_options(std::vector< std::string > options)
void add_parameter_default(int par_num, const std::pair< float, float > &values)