TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkmeasure.cpp
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#include "tkmeasure.h"
15
16namespace tkn {
24}
25
26using namespace tkn;
27
28tkmeasure::tkmeasure(double _value, const tkunit_manager::units_keys &_unit, double _err)
29{
30 fValue.set_value(_value, _unit);
31 fError.set_value(_err, _unit);
32 fErrorLow.set_unit(_unit);
33 fErrorHigh.set_unit(_unit);
34}
35
36tkmeasure::tkmeasure(double _value, const tkunit_manager::units_keys &_unit, double _errlow, double _errhigh)
37{
38 fValue.set_value(_value, _unit);
39 fError.set_unit(_unit);
40 fErrorLow.set_value(_errlow, _unit);
41 fErrorHigh.set_value(_errhigh, _unit);
42
43 fAsymError = true;
44}
45
46void tkmeasure::set_info_tag(const tkstring &_tag)
47{
48 finfo_tag = _tag;
49
50 if(_tag.is_empty()) return;
51 if(_tag.contains(";ERR=?")) {
53 finfo_tag.remove_all(";ERR=?");
54 }
55 if(_tag.contains(";ERR=CA")) {
57 finfo_tag.remove_all(";ERR=CA");
58 }
59 if(_tag.contains(";ERR=AP")) {
61 finfo_tag.remove_all(";ERR=AP");
62 }
63 if(_tag.contains(";ERR=SY")) {
65 finfo_tag.remove_all(";ERR=SY");
66 }
67 if(_tag.contains(";VAL=()")) {
69 finfo_tag.remove_all(";VAL=()");
70 }
71 if(_tag.contains(";ERR=LT") || _tag.contains(";ERR=GT") || _tag.contains(";ERR=LE") || _tag.contains(";ERR=GE")) {
73 }
74 if(_tag.contains(";ERR=CONV")) {
76 finfo_tag.remove_all(";ERR=CONV");
77 }
78}
79
80void tkmeasure::set(const double &_val, const tkstring &_unit)
81{
82 if(_unit != "" ) set_unit(_unit);
83 fValue.set_value(_val);
84}
85
86void tkmeasure::set(const double &_val, const tkunit_manager::units_keys &_unit)
87{
88 set_unit(_unit);
89 fValue.set_value(_val,_unit);
90}
91
93{
94 // when we change the unit type, the error is not trivial as an error conversion
95 if(fValue.get_unit_key() && gunits->get_type(fValue.get_unit_key()) != gunits->get_type(_unit) ) {
96 double value = fValue.get_value();
97 double value_low = value-fError.get_value();
98 if(fAsymError) value_low = value-fErrorLow.get_value();
99 double value_high = value+fError.get_value();
100 if(fAsymError) value_high = value+fErrorHigh.get_value();
101
102 if(!fValue.set_unit(_unit)) return false;
103 fErrorLow.set_value(value_low);
104 fErrorLow.set_unit(_unit);
105 value_low = fErrorLow.get_value();
106
107 fErrorHigh.set_value(value_high);
108 fErrorHigh.set_unit(_unit);
109 value_high = fErrorHigh.get_value();
110
111 if(value_low>value_high) {
112 double tmp = value_high;
113 value_high = value_low;
114 value_low = tmp;
115 }
116 value_low = fValue.get_value() - value_low;
117 value_high = value_high-fValue.get_value();
118 if(value_low != value_high) {
119 fError.set_value(-1);
120 fErrorLow.set_value(value_low);
121 fErrorHigh.set_value(value_high);
122 fAsymError = true;
123 }
124 else {
125 fError.set_value(value_low);
126 fErrorLow.set_value(-1);
127 fErrorHigh.set_value(-1);
128 fAsymError = false;
129 }
130 }
131 else {
132 if(!fValue.set_unit(_unit)) return false;
133 fError.set_unit(_unit);
134 fErrorLow.set_unit(_unit);
135 fErrorHigh.set_unit(_unit);
136 }
137
138 return true;
139}
140
142{
143 auto oldunit = get_unit_key();
144 if(oldunit==_unit) return get_value();
145 set_unit(_unit);
146 double val = get_value();
147 set_unit(oldunit);
148 return val;
149}
150
151double tkmeasure::get_error() const
152{
153 if(fAsymError) {
154 glog << warning_o << "The measure contains assymetric errors, the return value is the largest one" << do_endl;
155 return std::max(fErrorLow.get_value(),fErrorHigh.get_value());
156 }
157 return fError.get_value();
158}
159
162 auto oldunit = get_unit_key();
163 set_unit(_unit);
164 double val = get_error();
165 set_unit(oldunit);
166 return val;
168
169double tkmeasure::get_error_low() const
170{
171 if(!fAsymError) {
172 return fError.get_value();
173 }
174 return fErrorLow.get_value();
175}
176
178{
179 auto oldunit = get_unit_key();
180 set_unit(_unit);
181 double val = get_error_low();
182 set_unit(oldunit);
183 return val;
184}
185
186double tkmeasure::get_error_high() const
187{
188 if(!fAsymError) {
189 return fError.get_value();
190 }
191 return fErrorHigh.get_value();
192}
193
195{
196 auto oldunit = get_unit_key();
197 set_unit(_unit);
198 double val = get_error_high();
199 set_unit(oldunit);
200 return val;
201}
202
203#ifdef HAS_ROOT
204ClassImp(tkmeasure);
205#endif
206
207
Stores information on an experimental measure.
Definition tkmeasure.h:37
double get_error() const
returns the error on the measured value
double get_error_high() const
returns the high assymetric error on the measured value
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition tkmeasure.h:114
double get_value() const
returns the measure value
Definition tkmeasure.h:74
bool set_unit(const tkstring &_unit_name)
set the measured data unit (value and errors)
Definition tkmeasure.h:117
double get_error_low() const
returns the low assymetric error on the measured value
void set(const double &_val, const tkstring &_unit="")
set the measured value and unit. No given units keeps the current one
void set_info_tag(const tkstring &_tag)
define the info tag (calculation, systematic, approx value...)
void set_info(tkproperty::data_info _info)
to set some information about this data
Definition tkproperty.h:55
void set_converted(bool _val=true)
to define the value as a converted one
Definition tkproperty.h:49
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
bool is_empty() const
Definition tkstring.h:140
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition tkstring.h:174
units_keys
units identifiers
Definition tkunit.h:46
double get_value() const
get the value in the current unit
Definition tkunit.h:134
void set_value(const double &_val, const tkstring &_unit="")
set the value and unit from string, if _unit not defined, let in the current unit
Definition tkunit.cpp:108
Definition tklog.cpp:16
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & do_endl(tklog &log)
Definition tklog.h:212