TkN 2.1
Toolkit for Nuclei
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 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#include "tkmeasure.h"
38
39namespace tkn {
47}
48
49using namespace tkn;
50
51tkmeasure::tkmeasure(double _value, const tkunit_manager::units_keys &_unit, double _err)
52{
53 fValue.set_value(_value, _unit);
54 fError.set_value(_err, _unit);
55 fErrorLow.set_unit(_unit);
56 fErrorHigh.set_unit(_unit);
57}
58
59tkmeasure::tkmeasure(double _value, const tkunit_manager::units_keys &_unit, double _errlow, double _errhigh)
60{
61 fValue.set_value(_value, _unit);
62 fError.set_unit(_unit);
63 fErrorLow.set_value(_errlow, _unit);
64 fErrorHigh.set_value(_errhigh, _unit);
65
66 fAsymError = true;
67}
68
70{
71 finfo_tag = _tag;
72
73 if(_tag.is_empty()) return;
74 if(_tag.contains(";ERR=?")) {
76 finfo_tag.remove_all(";ERR=?");
77 }
78 if(_tag.contains(";ERR=CA")) {
80 finfo_tag.remove_all(";ERR=CA");
81 }
82 if(_tag.contains(";ERR=AP")) {
84 finfo_tag.remove_all(";ERR=AP");
85 }
86 if(_tag.contains(";ERR=SY")) {
88 finfo_tag.remove_all(";ERR=SY");
89 }
90 if(_tag.contains(";VAL=()")) {
92 finfo_tag.remove_all(";VAL=()");
93 }
94 if(_tag.contains(";ERR=LT") || _tag.contains(";ERR=GT") || _tag.contains(";ERR=LE") || _tag.contains(";ERR=GE")) {
96 }
97 if(_tag.contains(";ERR=CONV")) {
99 finfo_tag.remove_all(";ERR=CONV");
100 }
101}
102
103void tkmeasure::set(const double &_val, const tkstring &_unit)
104{
105 if(_unit != "" ) set_unit(_unit);
106 fValue.set_value(_val);
107}
108
109void tkmeasure::set(const double &_val, const tkunit_manager::units_keys &_unit)
110{
111 set_unit(_unit);
112 fValue.set_value(_val,_unit);
113}
114
116{
117 // when we change the unit type, the error is not trivial as an error conversion
118 if(fValue.get_unit_key() && gunits->get_type(fValue.get_unit_key()) != gunits->get_type(_unit) ) {
119 double value = fValue.get_value();
120 double value_low = value-fError.get_value();
121 if(fAsymError) value_low = value-fErrorLow.get_value();
122 double value_high = value+fError.get_value();
123 if(fAsymError) value_high = value+fErrorHigh.get_value();
124
125 if(!fValue.set_unit(_unit)) return false;
126 fErrorLow.set_value(value_low);
127 fErrorLow.set_unit(_unit);
128 value_low = fErrorLow.get_value();
129
130 fErrorHigh.set_value(value_high);
131 fErrorHigh.set_unit(_unit);
132 value_high = fErrorHigh.get_value();
133
134 if(value_low>value_high) {
135 double tmp = value_high;
136 value_high = value_low;
137 value_low = tmp;
138 }
139 value_low = fValue.get_value() - value_low;
140 value_high = value_high-fValue.get_value();
141 if(value_low != value_high) {
142 fError.set_value(-1);
143 fErrorLow.set_value(value_low);
144 fErrorHigh.set_value(value_high);
145 fAsymError = true;
146 }
147 else {
148 fError.set_value(value_low);
149 fErrorLow.set_value(-1);
150 fErrorHigh.set_value(-1);
151 fAsymError = false;
152 }
153 }
154 else {
155 if(!fValue.set_unit(_unit)) return false;
156 fError.set_unit(_unit);
157 fErrorLow.set_unit(_unit);
158 fErrorHigh.set_unit(_unit);
159 }
160
161 return true;
162}
163
165{
166 auto oldunit = get_unit_key();
167 if(oldunit==_unit) return get_value();
168 set_unit(_unit);
169 double val = get_value();
170 set_unit(oldunit);
171 return val;
172}
173
175{
176 if(fAsymError) {
177 glog << warning_o << "The measure contains assymetric errors, the return value is the largest one" << do_endl;
178 return std::max(fErrorLow.get_value(),fErrorHigh.get_value());
179 }
180 return fError.get_value();
181}
182
184{
185 auto oldunit = get_unit_key();
186 set_unit(_unit);
187 double val = get_error();
188 set_unit(oldunit);
189 return val;
190}
191
193{
194 if(!fAsymError) {
195 return fError.get_value();
196 }
197 return fErrorLow.get_value();
198}
199
201{
202 auto oldunit = get_unit_key();
203 set_unit(_unit);
204 double val = get_error_low();
205 set_unit(oldunit);
206 return val;
207}
208
210{
211 if(!fAsymError) {
212 return fError.get_value();
213 }
214 return fErrorHigh.get_value();
215}
216
218{
219 auto oldunit = get_unit_key();
220 set_unit(_unit);
221 double val = get_error_high();
222 set_unit(oldunit);
223 return val;
224}
225
226#ifdef HAS_ROOT
227ClassImp(tkmeasure);
228#endif
229
230
Stores information on an experimental measure.
Definition: tkmeasure.h:60
double get_error() const
returns the error on the measured value
Definition: tkmeasure.cpp:174
double get_error_high() const
returns the high assymetric error on the measured value
Definition: tkmeasure.cpp:209
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition: tkmeasure.h:137
double get_value() const
returns the measure value
Definition: tkmeasure.h:97
bool set_unit(const tkstring &_unit_name)
set the measured data unit (value and errors)
Definition: tkmeasure.h:140
double get_error_low() const
returns the low assymetric error on the measured value
Definition: tkmeasure.cpp:192
void set(const double &_val, const tkstring &_unit="")
set the measured value and unit. No given units keeps the current one
Definition: tkmeasure.cpp:103
void set_info_tag(const tkstring &_tag)
define the info tag (calculation, systematic, approx value...)
Definition: tkmeasure.cpp:69
void set_info(tkproperty::data_info _info)
to set some information about this data
Definition: tkproperty.h:78
void set_converted(bool _val=true)
to define the value as a converted one
Definition: tkproperty.h:72
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
bool is_empty() const
Definition: tkstring.h:163
tkstring & remove_all(const tkstring &_s1)
Definition: tkstring.h:215
bool contains(const char *_pat, ECaseCompare _cmp=kExact) const
Definition: tkstring.h:197
units_keys
units identifiers
Definition: tkunit.h:69
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition: tkunit.h:172
double get_value() const
get the value in the current unit
Definition: tkunit.h:157
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:122
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition: tkunit.cpp:138
Definition: tklog.cpp:39
tklog & warning_o(tklog &log)
Definition: tklog.h:348
tklog & do_endl(tklog &log)
Definition: tklog.h:235