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