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