R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BValueError.h
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#pragma once
15#include <Rtypes.h>
16#include <TObject.h>
17#include <cmath>
18#include <nlohmann/json.hpp>
19#include <type_traits>
20
21namespace R3B
22{
23 template <typename DataType>
25 {
26 bool valid = true;
27 DataType value{};
28 DataType error{};
29
30 constexpr ValueError(DataType val, DataType err)
31 : value{ val }
32 , error{ err }
33 {
34 }
35
36 // ValueError(const DataType& val, const DataType& err)
37 // : value{ val }
38 // , error{ err }
39 // {
40 // }
41
43 : valid{ false }
44 {
45 }
46
47 auto operator-() const -> ValueError<DataType> { return { -value, error }; }
48
49 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
50 auto operator+(OtherType other) const -> ValueError<DataType>
51 {
52 return { value + other, error };
53 }
54
55 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
56 auto operator*(OtherType other) const -> ValueError<DataType>
57 {
58 return { value * other, error * other };
59 }
60
61 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
62 auto operator/(OtherType other) const -> ValueError<DataType>
63 {
64 return { value / other, error / other };
65 }
66
67 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
68 auto operator-(OtherType val) const -> ValueError<DataType>
69 {
70 return { value - val, error };
71 }
72
73 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
74 void operator-=(OtherType other)
75 {
76 value -= other;
77 }
78
79 public:
81 };
82
86
87 template <typename DataType>
89 {
90 auto new_value = left.value * right.value;
91 auto new_error = std::sqrt(right.value * right.value * left.error * left.error +
92 left.value * left.value * right.error * right.error);
93 return { new_value, new_error };
94 }
95
96 template <typename DataType>
98 {
99 auto new_value = numerator.value / denominator.value;
100 auto new_error = std::sqrt(numerator.error * numerator.error +
101 denominator.error * denominator.error * new_value * new_value) /
102 denominator.value;
103 return { new_value, std::abs(new_error) };
104 }
105
106 template <typename DataType>
108 {
109 auto new_value = left.value + right.value;
110 // TODO: not the fastest way
111 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
112 return { new_value, new_err };
113 }
114
115 template <typename DataType>
117 {
118 auto new_value = left.value - right.value;
119 // TODO: not the fastest way
120 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
121 return { new_value, new_err };
122 }
123
124 template <typename DataType>
130
131 template <typename DataType>
137
138 template <typename DataType>
139 void to_json(nlohmann::ordered_json& json_obj, const ValueError<DataType>& value)
140 {
141 json_obj = nlohmann::ordered_json{
142 { "value", value.value },
143 { "error", value.error },
144 };
145 }
146
147 template <typename DataType>
148 void from_json(const nlohmann::ordered_json& json_obj, ValueError<DataType>& value)
149 {
150 json_obj.at("value").get_to(value.value);
151 json_obj.at("error").get_to(value.error);
152 }
153} // namespace R3B
auto operator*(ValueError< DataType > left, ValueError< DataType > right) -> ValueError< DataType >
auto operator+(ValueError< DataType > left, ValueError< DataType > right) -> ValueError< DataType >
ValueError< float > ValueErrorF
void from_json(const nlohmann::ordered_json &json_obj, MinMaxValue< DataType > &value)
auto operator/(ValueError< DataType > numerator, ValueError< DataType > denominator) -> ValueError< DataType >
auto operator-=(ValueError< DataType > &left, const ValueError< DataType > &right) -> ValueError< DataType > &
ValueError< int > ValueErrorI
void to_json(nlohmann::ordered_json &json_obj, const MinMaxValue< DataType > &value)
auto operator+=(ValueError< DataType > &left, const ValueError< DataType > &right) -> ValueError< DataType > &
auto operator-(ValueError< DataType > left, ValueError< DataType > right) -> ValueError< DataType >
ValueError< double > ValueErrorD
auto operator+(OtherType other) const -> ValueError< DataType >
auto operator-(OtherType val) const -> ValueError< DataType >
void operator-=(OtherType other)
auto operator*(OtherType other) const -> ValueError< DataType >
auto operator-() const -> ValueError< DataType >
ClassDefNV(ValueError, 1)
constexpr ValueError(DataType val, DataType err)
auto operator/(OtherType other) const -> ValueError< DataType >