R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
ParResultReader.cxx
Go to the documentation of this file.
1#include "ParResultReader.h"
2#include <fmt/base.h>
3#include <fmt/core.h>
4#include <fmt/format.h>
5#include <fmt/ranges.h>
6#include <fstream>
7#include <glaze/core/context.hpp>
8#include <glaze/core/reflect.hpp>
9#include <glaze/json/read.hpp>
10#include <ios>
11#include <optional>
12#include <range/v3/view/map.hpp>
13#include <string>
14#include <string_view>
15#include <unordered_map>
16#include <vector>
17
18namespace rng = ranges;
19
20namespace
21{
23 auto split_string_view(std::string_view str, std::string_view del) -> std::vector<std::string_view>
24 {
25 auto splits = std::vector<std::string_view>{};
26
27 for (auto sub_begin = str.find_first_not_of(del); sub_begin != std::string_view::npos;
28 sub_begin = str.find_first_not_of(del, sub_begin))
29 {
30 auto sub_end = str.find_first_of(del, sub_begin);
31 sub_end = (sub_end == std::string_view::npos) ? str.size() : sub_end;
32 auto sub_str = str.substr(sub_begin, sub_end - sub_begin);
33 splits.push_back(sub_str);
34 sub_begin = sub_end;
35 }
36 return splits;
37 }
38
39 auto parse_parameter(std::string_view line_string) -> std::optional<ParResultEntry>
40 {
41 auto splits_view = split_string_view(line_string, " ");
42 auto result = ParResultEntry{};
43
44 constexpr auto max_size = 5;
45 constexpr auto min_size = 3;
46 if (splits_view.size() >= min_size)
47 {
48 result.par_num = std::stoi(std::string{ splits_view.front() });
49 result.value = std::stof(std::string{ splits_view[1] });
50 result.pre_sigma = std::stof(std::string{ splits_view[2] });
51 }
52 else
53 {
54 return {};
55 }
56
57 if (splits_view.size() == max_size)
58 {
59 result.value_diff = std::stof(std::string{ splits_view[3] });
60 result.error = std::stof(std::string{ splits_view.back() });
61 }
62 else if (splits_view.size() != min_size)
63 {
64 return {};
65 }
66
67 return result;
68 }
69} // namespace
70
72{
73
75 {
76 par_results_.clear();
77
78 if (mode_ == Mode::res)
79 {
81 }
82 else
83 {
85 }
86 }
87
89 {
90
91 auto ifile = std::ifstream(filename_, std::ios_base::in);
92 auto buffer = std::string{};
93 if (ifile.is_open())
94 {
95 std::getline(ifile, buffer);
96 while (std::getline(ifile, buffer))
97 {
98 auto result = parse_parameter(buffer);
99 if (result.has_value())
100 {
101 par_results_.emplace(result->par_num, result.value());
102 }
103 }
104 }
105 else
106 {
107 fmt::println(stderr, "ERROR: parameter file {:?} cannot be read", filename_);
108 }
109 }
110
112 {
113
114 auto obj = std::unordered_map<int, JsonParResultEntry>{};
115 auto buffer = std::string{};
116 auto error_code = glz::read_file_json(obj, filename_, buffer);
117 if (error_code == glz::error_code::file_open_failure)
118 {
119 fmt::println(stderr, "Parameter JSON file {:?} doesn't exist!", filename_);
120 }
121 else if (error_code)
122 {
123 fmt::println(stderr, "{}", glz::format_error(error_code, buffer));
124 }
125
126 for (const auto& [key, value] : obj)
127 {
128 par_results_.emplace(value.label,
129 ParResultEntry{ .par_num = value.label,
130 .value = value.correction,
131 .pre_sigma = 0.,
132 .value_diff = value.correction,
133 .error = value.error });
134 }
135 }
136 void ResultReader::print() { fmt::print("{}\n", fmt::join(par_results_ | rng::views::values, "\n")); }
137
138} // namespace R3B::Millepede
std::unordered_map< int, ParResultEntry > par_results_