TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
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 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#ifndef tkunit_H
15#define tkunit_H
16
17#include "tkn_config.h"
18
19#include <map>
20#include <cmath>
21
22#ifdef HAS_ROOT
23#include "TClass.h"
24#endif
25
26#include "tkstring.h"
27#include "tklog.h"
28
29namespace tkn {
30
32{
33 friend class tkunit;
34
35public:
36
43 };
44
47 eV, keV, MeV, GeV, TeV, // energy
48 as, fs, ps, ns, us, ms, s, min, h, d, y, // time
49 am, fm, pm, nm, um, mm, cm, m, // lenght
50 };
51
52 static std::vector< std::tuple<tkstring, units_type, double>> funits_properties;
53
54protected:
55
56 static tkunit_manager *g_unit_manager;
57
58 static std::vector<tkstring> funits_name;
59 static std::vector<units_type> funits_type;
60 static std::vector<double> fcoeffs;
61
62 std::map<units_keys, tkstring> funits_map_id_to_string;
63 std::map<tkstring,units_keys> funits_map_string_to_id;
64 std::map<tkstring,units_keys> funits_map_string_to_id_low;
65 std::map<units_keys,double> funits_map_conv_coeffs;
66 std::map<units_keys,units_type> funits_map_types;
67
68public:
70 virtual ~tkunit_manager() = default;
71
74 if(funits_map_id_to_string.count(_key)) return funits_map_id_to_string.at(_key);
75 else glog << warning_o << "data_units_manager::get_name(" << _key << "): unkown key " << do_endl;
76 return funits_map_id_to_string.at(Undef_unit);
77 }
78
79 units_keys get_key(const tkstring &_name) {
80 tkstring tmp = _name.copy().to_lower();
81 if(funits_map_string_to_id_low.count(tmp)) return funits_map_string_to_id_low.at(tmp);
82 else { // add no conversion unit
83 int i = funits_map_id_to_string.size();
84 funits_map_id_to_string[(units_keys)i] = _name;
85 funits_map_string_to_id[_name] = (units_keys)i;
86 funits_map_string_to_id_low[tmp] = (units_keys)i;
87 funits_map_conv_coeffs[(units_keys)i] = 1;
88 funits_map_types[(units_keys)i] = kNoConvUnits;
89 return funits_map_string_to_id_low.at(tmp);
90 }
91// else glog << warning_o << "data_units_manager::get_key(" << _name << "): unkown unit name " << do_endl;
92 return Undef_unit;
93 }
94
96 if(funits_map_types.count(_key)) return funits_map_types.at(_key);
97 else glog << warning_o << "data_units_manager::get_type(" << _key << "): unkown key " << do_endl;
98 return funits_map_types.at(Undef_unit);
99 }
100
103
104private:
105 double get_factor(const units_keys &_key) {
106 if(funits_map_conv_coeffs.count(_key)) return funits_map_conv_coeffs.at(_key);
107 else glog << warning_o << "data_units_manager::get_factor(" << _key << "): unkown key " << do_endl;
108 return funits_map_conv_coeffs.at(Undef_unit);
109 }
110
111#ifdef HAS_ROOT
113 ClassDef(tkunit_manager,0);
114#endif
115};
116
117#define gunits (tkn::tkunit_manager::the_unit_manager())
118
120{
121protected:
123 double fFactor = std::nan("0");
124 double fValue = -1.;
125
126public:
127 tkunit() {;}
128 tkunit(double _value, const tkunit_manager::units_keys &_unit);
129 tkunit(double _value, const tkstring &_unit) : tkunit(_value, gunits->get_key(_unit)) {}
130
131 virtual ~tkunit() {}
132
134 double get_value() const { return fValue/fFactor;}
135
137 void set_value(const double &_val, const tkstring &_unit="");
139 void set_value(const double &_val, const tkunit_manager::units_keys &_unit);
140
142 bool set_unit(const tkstring &_unit);
144 bool set_unit(const tkunit_manager::units_keys &_unit);
145
147 tkstring get_unit_name() const { return gunits->get_name(fUnit_key);}
149 tkunit_manager::units_keys get_unit_key() const { return fUnit_key;}
150
152 void clear() { fValue = 0.;}
153
154#ifdef HAS_ROOT
156 ClassDef(tkunit,0);
157#endif
158
159};
160}
161
162#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:347
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:36
Manages the different units.
Definition tkunit.h:32
units_keys
units identifiers
Definition tkunit.h:46
units_keys get_key(const tkstring &_name)
retuns unit key
Definition tkunit.h:79
static std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition tkunit.h:52
friend class tkunit
Definition tkunit.h:33
virtual ~tkunit_manager()=default
units_type
flags that qualify the unit type
Definition tkunit.h:38
units_type get_type(const units_keys &_key)
retuns unit type
Definition tkunit.h:95
tkstring get_name(const units_keys &_key)
retuns unit name
Definition tkunit.h:73
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition tkunit.cpp:91
A measured value associated to its unit.
Definition tkunit.h:120
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition tkunit.h:149
virtual ~tkunit()
Definition tkunit.h:131
double get_value() const
get the value in the current unit
Definition tkunit.h:134
tkunit(double _value, const tkstring &_unit)
Definition tkunit.h:129
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
tkstring get_unit_name() const
returns the unit name
Definition tkunit.h:147
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition tkunit.cpp:124
void clear()
reset the data_unit
Definition tkunit.h:152
Definition tklog.cpp:16
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & do_endl(tklog &log)
Definition tklog.h:212