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 <TObject.h>
16#include <cmath>
17#include <type_traits>
18
19namespace R3B
20{
21 template <typename DataType>
23 {
24 bool valid = true;
25 DataType value{};
26 DataType error{};
27
28 ValueError(const DataType& val, const DataType& err)
29 : value{ val }
30 , error{ err }
31 {
32 }
33
35 : valid{ false }
36 {
37 }
38
39 auto operator-() const -> ValueError<DataType> { return { -value, error }; }
40
41 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
42 auto operator+(OtherType other) const -> ValueError<DataType>
43 {
44 return { value + other, error };
45 }
46
47 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
48 auto operator*(OtherType other) const -> ValueError<DataType>
49 {
50 return { value * other, error * other };
51 }
52
53 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
54 auto operator/(OtherType other) const -> ValueError<DataType>
55 {
56 return { value / other, error / other };
57 }
58
59 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
60 auto operator-(OtherType val) const -> ValueError<DataType>
61 {
62 return { value - val, error };
63 }
64
65 template <typename OtherType, typename = std::enable_if_t<std::is_arithmetic_v<OtherType>>>
66 void operator-=(OtherType other)
67 {
68 value -= other;
69 }
70
71 public:
73 };
74
78
79 template <typename DataType>
81 {
82 auto new_value = left.value * right.value;
83 auto new_error = std::sqrt(right.value * right.value * left.error * left.error +
84 left.value * left.value * right.error * right.error);
85 return { new_value, new_error };
86 }
87
88 template <typename DataType>
90 {
91 auto new_value = numerator.value / denominator.value;
92 auto new_error = std::sqrt(numerator.error * numerator.error +
93 denominator.error * denominator.error * new_value * new_value) /
94 denominator.value;
95 return { new_value, std::abs(new_error) };
96 }
97
98 template <typename DataType>
100 {
101 auto new_value = left.value + right.value;
102 // TODO: not the fastest way
103 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
104 return { new_value, new_err };
105 }
106
107 template <typename DataType>
109 {
110 auto new_value = left.value - right.value;
111 // TODO: not the fastest way
112 auto new_err = std::sqrt(left.error * left.error + right.error * right.error);
113 return { new_value, new_err };
114 }
115
116 template <typename DataType>
122
123 template <typename DataType>
129} // 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
auto operator/(ValueError< DataType > numerator, ValueError< DataType > denominator) -> ValueError< DataType >
auto operator-=(ValueError< DataType > &left, const ValueError< DataType > &right) -> ValueError< DataType > &
ValueError< int > ValueErrorI
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 >
ValueError(const DataType &val, const DataType &err)
void operator-=(OtherType other)
auto operator*(OtherType other) const -> ValueError< DataType >
auto operator-() const -> ValueError< DataType >
ClassDefNV(ValueError, 1)
auto operator/(OtherType other) const -> ValueError< DataType >