R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
ParResultReader.cxx
Go to the documentation of this file.
1#include "ParResultReader.h"
2#include <fmt/core.h>
3#include <fmt/format.h>
4#include <fmt/ranges.h>
5#include <fstream>
6#include <ios>
7#include <optional>
8#include <range/v3/view/map.hpp>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace rng = ranges;
14
15namespace
16{
18 auto split_string_view(std::string_view str, std::string_view del) -> std::vector<std::string_view>
19 {
20 auto splits = std::vector<std::string_view>{};
21
22 for (auto sub_begin = str.find_first_not_of(del); sub_begin != std::string_view::npos;
23 sub_begin = str.find_first_not_of(del, sub_begin))
24 {
25 auto sub_end = str.find_first_of(del, sub_begin);
26 sub_end = (sub_end == std::string_view::npos) ? str.size() : sub_end;
27 auto sub_str = str.substr(sub_begin, sub_end - sub_begin);
28 splits.push_back(sub_str);
29 sub_begin = sub_end;
30 }
31 return splits;
32 }
33
34 auto parse_parameter(std::string_view line_string) -> std::optional<ParResultEntry>
35 {
36 auto splits_view = split_string_view(line_string, " ");
37 auto result = ParResultEntry{};
38
39 constexpr auto max_size = 5;
40 constexpr auto min_size = 3;
41 if (splits_view.size() >= min_size)
42 {
43 result.par_num = std::stoi(std::string{ splits_view.front() });
44 result.value = std::stof(std::string{ splits_view[1] });
45 result.sigma = std::stof(std::string{ splits_view[2] });
46 }
47 else
48 {
49 return {};
50 }
51
52 if (splits_view.size() == max_size)
53 {
54 result.value_diff = std::stof(std::string{ splits_view[3] });
55 result.error = std::stof(std::string{ splits_view.back() });
56 }
57 else if (splits_view.size() != min_size)
58 {
59 return {};
60 }
61
62 return result;
63 }
64
65} // namespace
66
68{
69
71 {
72 par_results_.clear();
73 auto ifile = std::ifstream(filename_, std::ios_base::in);
74 auto buffer = std::string{};
75 if (ifile.is_open())
76 {
77 std::getline(ifile, buffer);
78 while (std::getline(ifile, buffer))
79 {
80 auto result = parse_parameter(buffer);
81 if (result.has_value())
82 {
83 par_results_.emplace(result->par_num, result.value());
84 }
85 }
86 }
87 else
88 {
89 fmt::println("ERROR: parameter file {:?} cannot be read", filename_);
90 }
91 }
92 void ResultReader::print() { fmt::print("{}\n", fmt::join(par_results_ | rng::views::values, "\n")); }
93
94} // namespace R3B::Millepede
std::unordered_map< int, ParResultEntry > par_results_