TkN 2.1
Toolkit for Nuclei
tkunit.h
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#ifndef tkunit_H
38#define tkunit_H
39
40#include "tkn_config.h"
41
42#include <map>
43#include <cmath>
44
45#ifdef HAS_ROOT
46#include "TClass.h"
47#endif
48
49#include "tkstring.h"
50#include "tklog.h"
51
52namespace tkn {
53
55{
56 friend class tkunit;
57
58public:
59
66 };
67
70 eV, keV, MeV, GeV, TeV, // energy
71 as, fs, ps, ns, us, ms, s, min, h, d, y, // time
72 am, fm, pm, nm, um, mm, cm, m, // lenght
73 };
74
75 static std::vector< std::tuple<tkstring, units_type, double>> funits_properties;
76
77protected:
78
79 static tkunit_manager *g_unit_manager;
80
81 static std::vector<tkstring> funits_name;
82 static std::vector<units_type> funits_type;
83 static std::vector<double> fcoeffs;
84
85 std::map<units_keys, tkstring> funits_map_id_to_string;
86 std::map<tkstring,units_keys> funits_map_string_to_id;
87 std::map<tkstring,units_keys> funits_map_string_to_id_low;
88 std::map<units_keys,double> funits_map_conv_coeffs;
89 std::map<units_keys,units_type> funits_map_types;
90
91public:
93 virtual ~tkunit_manager() = default;
94
97 if(funits_map_id_to_string.count(_key)) return funits_map_id_to_string.at(_key);
98 else glog << warning_o << "data_units_manager::get_name(" << _key << "): unkown key " << do_endl;
99 return funits_map_id_to_string.at(Undef_unit);
100 }
103 tkstring tmp = _name.copy().to_lower();
104 if(funits_map_string_to_id_low.count(tmp)) return funits_map_string_to_id_low.at(tmp);
105 else { // add no conversion unit
106 int i = funits_map_id_to_string.size();
107 funits_map_id_to_string[(units_keys)i] = _name;
108 funits_map_string_to_id[_name] = (units_keys)i;
109 funits_map_string_to_id_low[tmp] = (units_keys)i;
110 funits_map_conv_coeffs[(units_keys)i] = 1;
111 funits_map_types[(units_keys)i] = kNoConvUnits;
112 return funits_map_string_to_id_low.at(tmp);
113 }
114// else glog << warning_o << "data_units_manager::get_key(" << _name << "): unkown unit name " << do_endl;
115 return Undef_unit;
116 }
119 if(funits_map_types.count(_key)) return funits_map_types.at(_key);
120 else glog << warning_o << "data_units_manager::get_type(" << _key << "): unkown key " << do_endl;
121 return funits_map_types.at(Undef_unit);
122 }
123
126
127private:
128 double get_factor(const units_keys &_key) {
129 if(funits_map_conv_coeffs.count(_key)) return funits_map_conv_coeffs.at(_key);
130 else glog << warning_o << "data_units_manager::get_factor(" << _key << "): unkown key " << do_endl;
131 return funits_map_conv_coeffs.at(Undef_unit);
132 }
133
134#ifdef HAS_ROOT
136 ClassDef(tkunit_manager,0);
137#endif
138};
139
140#define gunits (tkn::tkunit_manager::the_unit_manager())
141
143{
144protected:
145 tkunit_manager::units_keys fUnit_key = tkunit_manager::units_keys::Undef_unit;
146 double fFactor = std::nan("0");
147 double fValue = -1.;
148
149public:
150 tkunit() {;}
151 tkunit(double _value, const tkunit_manager::units_keys &_unit);
152 tkunit(double _value, const tkstring &_unit) : tkunit(_value, gunits->get_key(_unit)) {}
153
154 virtual ~tkunit() {}
155
157 double get_value() const { return fValue/fFactor;}
158
160 void set_value(const double &_val, const tkstring &_unit="");
162 void set_value(const double &_val, const tkunit_manager::units_keys &_unit);
163
165 bool set_unit(const tkstring &_unit);
167 bool set_unit(const tkunit_manager::units_keys &_unit);
168
170 tkstring get_unit_name() const { return gunits->get_name(fUnit_key);}
172 tkunit_manager::units_keys get_unit_key() const { return fUnit_key;}
173
175 void clear() { fValue = 0.;}
176
177#ifdef HAS_ROOT
179 ClassDef(tkunit,0);
180#endif
181
182};
183}
184
185#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
tkstring copy() const
Returns a copy of this string.
Definition: tkstring.cpp:370
tkstring & to_lower()
Change all letters to lower case.
Definition: tkstring.cpp:59
Manages the different units.
Definition: tkunit.h:55
units_keys
units identifiers
Definition: tkunit.h:69
units_keys get_key(const tkstring &_name)
retuns unit key
Definition: tkunit.h:102
static std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition: tkunit.h:75
virtual ~tkunit_manager()=default
units_type
flags that qualify the unit type
Definition: tkunit.h:61
units_type get_type(const units_keys &_key)
retuns unit type
Definition: tkunit.h:118
tkstring get_name(const units_keys &_key)
retuns unit name
Definition: tkunit.h:96
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition: tkunit.cpp:105
A measured value associated to its unit.
Definition: tkunit.h:143
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition: tkunit.h:172
virtual ~tkunit()
Definition: tkunit.h:154
double get_value() const
get the value in the current unit
Definition: tkunit.h:157
tkunit(double _value, const tkstring &_unit)
Definition: tkunit.h:152
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
tkstring get_unit_name() const
returns the unit name
Definition: tkunit.h:170
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition: tkunit.cpp:138
void clear()
reset the data_unit
Definition: tkunit.h:175
Definition: tklog.cpp:39
tklog & warning_o(tklog &log)
Definition: tklog.h:348
tklog & do_endl(tklog &log)
Definition: tklog.h:235