R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BValueError.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2024 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2024-2026 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 <Math/GenVector/Cartesian3D.h>
16#include <Math/GenVector/CoordinateSystemTags.h>
17#include <Math/GenVector/DisplacementVector3D.h>
18#include <Rtypes.h>
19#include <TObject.h>
20#include <cmath>
21#ifndef __CLING__
22#include <fmt/base.h>
23#include <fmt/core.h>
24#endif
25#include <nlohmann/json.hpp>
26#include <nlohmann/json_fwd.hpp>
27#ifndef HAS_CPP_STANDARD_17
28#else
29#include <type_traits>
30#endif
31
32namespace R3B
33{
34
35#ifndef HAS_CPP_STANDARD_17
36 template <typename T>
37 concept Arithmetic = std::is_arithmetic_v<T>;
38#endif
39
40 template <typename DataType>
42 {
43 bool valid = true;
44 DataType value{};
45 DataType error{};
46
47 constexpr ValueError(DataType val, DataType err)
48 : value{ val }
49 , error{ err }
50 {
51 }
52
53 // NOLINTNEXTLINE (google-explicit-constructor)
54 constexpr ValueError(DataType val)
55 : ValueError{ val, DataType{} }
56 {
57 }
58 // ValueError(const DataType& val, const DataType& err)
59 // : value{ val }
60 // , error{ err }
61 // {
62 // }
63
65 : valid{ false }
66 {
67 }
68
69 auto operator-() const -> ValueError<DataType> { return { -value, error }; }
70
71#ifndef HAS_CPP_STANDARD_17
72 auto operator+(Arithmetic auto other) const -> ValueError<DataType>
73#else
74 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
75 auto operator+(OtherType other) const -> ValueError<DataType>
76#endif
77 {
78 return { value + other, error };
79 }
80
81#ifndef HAS_CPP_STANDARD_17
82 auto operator*(Arithmetic auto other) const -> ValueError<DataType>
83#else
84 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
85 auto operator*(OtherType other) const -> ValueError<DataType>
86#endif
87 {
88 return { value * other, error * other };
89 }
90
91#ifndef HAS_CPP_STANDARD_17
92 auto operator/(Arithmetic auto other) const -> ValueError<DataType>
93#else
94 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
95 auto operator/(OtherType other) const -> ValueError<DataType>
96#endif
97 {
98 return { value / other, error / other };
99 }
100
101#ifndef HAS_CPP_STANDARD_17
103#else
104 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
105 auto operator-(OtherType val) const -> ValueError<DataType>
106#endif
107 {
108 return { value - val, error };
109 }
110
111#ifndef HAS_CPP_STANDARD_17
112 void operator-=(Arithmetic auto val)
113#else
114 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
115 void operator-=(OtherType val)
116#endif
117 {
118 value -= val;
119 }
120
121#ifndef HAS_CPP_STANDARD_17
122 auto operator>(Arithmetic auto val) -> bool
123#else
124 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
125 auto operator>(OtherType val) -> bool
126#endif
127 {
128 return value > val;
129 }
130
131#ifndef HAS_CPP_STANDARD_17
132 auto operator<(Arithmetic auto val) -> bool
133#else
134 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
135 auto operator<(OtherType val) -> bool
136#endif
137 {
138 return value < val;
139 }
140
141 public:
143 };
144
149 ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<ValueErrorD>, ROOT::Math::DefaultCoordinateSystemTag>;
150
151 template <typename DataType>
153 {
154 auto new_value = left.value * right.value;
155 auto new_error = std::sqrt(right.value * right.value * left.error * left.error +
156 left.value * left.value * right.error * right.error);
157 return { new_value, new_error };
158 }
159
160 template <typename DataType>
162 {
163 auto new_value = numerator.value / denominator.value;
164 auto new_error = std::sqrt(numerator.error * numerator.error +
165 denominator.error * denominator.error * new_value * new_value) /
166 denominator.value;
167 return { new_value, std::abs(new_error) };
168 }
169
170 template <typename DataType>
172 {
173 auto new_value = left.value + right.value;
174 // TODO: not the fastest way
175 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
176 return { new_value, new_err };
177 }
178
179 template <typename DataType>
181 {
182 auto new_value = left.value - right.value;
183 // TODO: not the fastest way
184 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
185 return { new_value, new_err };
186 }
187
188 template <typename DataType>
190 {
191 left = left - right;
192 return left;
193 }
194
195 template <typename DataType>
197 {
198 left = left + right;
199 return left;
200 }
201
202 template <typename DataType>
203 auto operator>(ValueError<DataType>& left, const ValueError<DataType>& right) -> bool
204 {
205 return left.value > right.value;
206 }
207
208 template <typename DataType>
209 auto operator<(ValueError<DataType>& left, const ValueError<DataType>& right) -> bool
210 {
211 return left.value < right.value;
212 }
213
214 template <typename DataType>
215 void to_json(nlohmann::ordered_json& json_obj, const ValueError<DataType>& value)
216 {
217 json_obj = nlohmann::ordered_json{
218 { "value", value.value },
219 { "error", value.error },
220 };
221 }
222
223 template <typename DataType>
224 void from_json(const nlohmann::ordered_json& json_obj, ValueError<DataType>& value)
225 {
226 json_obj.at("value").get_to(value.value);
227 json_obj.at("error").get_to(value.error);
228 }
229} // namespace R3B
230
231#ifndef __CLING__
232template <typename DataType>
233class fmt::formatter<R3B::ValueError<DataType>>
234{
235 public:
236 // TODO: add more options
237 static constexpr auto parse(format_parse_context& ctx) { return ctx.end(); }
238 template <typename FmtContent>
239 constexpr auto format(const R3B::ValueError<DataType>& value_error, FmtContent& ctn) const
240 {
241 return fmt::format_to(ctn.out(), "{}+/-{}", value_error.value, value_error.error);
242 }
243};
244
245template <>
246class fmt::formatter<R3B::XYZVectorValueErrorD>
247{
248 public:
249 static constexpr auto parse(fmt::format_parse_context& ctx) { return ctx.end(); }
250 template <typename FmtContent>
251 constexpr auto format(const R3B::XYZVectorValueErrorD& hit, FmtContent& ctn) const
252 {
253 return fmt::format_to(ctn.out(), "[x: {}, y: {}, z: {}]", hit.X(), hit.Y(), hit.Z());
254 }
255};
256#endif
constexpr auto format(const R3B::ValueError< DataType > &value_error, FmtContent &ctn) const
static constexpr auto parse(format_parse_context &ctx)
constexpr auto format(const R3B::XYZVectorValueErrorD &hit, FmtContent &ctn) const
static constexpr auto parse(fmt::format_parse_context &ctx)
auto operator*(ValueError< DataType > left, ValueError< DataType > right) -> ValueError< DataType >
auto operator+(ValueError< DataType > left, ValueError< DataType > right) -> ValueError< DataType >
auto operator>(ValueError< DataType > &left, const ValueError< DataType > &right) -> bool
ValueError< float > ValueErrorF
void from_json(const nlohmann::ordered_json &json_obj, MinMaxValue< DataType > &value)
auto operator<(ValueError< DataType > &left, const ValueError< DataType > &right) -> bool
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)
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< ValueErrorD >, ROOT::Math::DefaultCoordinateSystemTag > XYZVectorValueErrorD
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>(Arithmetic auto val) -> bool
void operator-=(Arithmetic auto val)
auto operator/(Arithmetic auto other) const -> ValueError< DataType >
auto operator-() const -> ValueError< DataType >
ClassDefNV(ValueError, 1)
auto operator+(Arithmetic auto other) const -> ValueError< DataType >
auto operator-(Arithmetic auto val) const -> ValueError< DataType >
auto operator<(Arithmetic auto val) -> bool
constexpr ValueError(DataType val)
constexpr ValueError(DataType val, DataType err)
auto operator*(Arithmetic auto other) const -> ValueError< DataType >