TkN 2.7
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#include <mutex>
22#include <shared_mutex>
23
24#ifdef HAS_ROOT
25#include "TClass.h"
26#endif
27
28#include "tkstring.h"
29#include "tklog.h"
30
31namespace tkn {
32
34{
35 friend class tkunit;
36
37public:
38
45 };
46
49 eV, keV, MeV, GeV, TeV, // energy
50 as, fs, ps, ns, us, ms, s, min, h, d, y, // time
51 am, fm, pm, nm, um, mm, cm, m, // lenght
52 };
53
54 static const std::vector<std::tuple<tkstring, units_type, double>> funits_properties;
55
56protected:
57 static std::shared_mutex &get_mutex() {
58 static std::shared_mutex mutex;
59 return mutex;
60 }
61
62 static std::vector<tkstring> funits_name;
63 static std::vector<units_type> funits_type;
64 static std::vector<double> fcoeffs;
65
66 std::map<units_keys, tkstring> funits_map_id_to_string;
67 std::map<tkstring,units_keys> funits_map_string_to_id;
68 std::map<tkstring,units_keys> funits_map_string_to_id_low;
69 std::map<units_keys,double> funits_map_conv_coeffs;
70 std::map<units_keys,units_type> funits_map_types;
71
72public:
74 virtual ~tkunit_manager() = default;
75
78 const auto index = static_cast<std::size_t>(_key);
79 if (_key >= Undef_unit && index < funits_properties.size())
80 return std::get<0>(funits_properties[index]);
81 std::shared_lock<std::shared_mutex> lock(get_mutex());
82 if(funits_map_id_to_string.count(_key)) return funits_map_id_to_string.at(_key);
83 else glog << warning_o << "data_units_manager::get_name(" << _key << "): unkown key " << do_endl;
84 return funits_map_id_to_string.at(Undef_unit);
85 }
86
87 units_keys get_key(const tkstring &_name) {
88 tkstring tmp = _name.copy().to_lower();
89 {
90 std::shared_lock<std::shared_mutex> lock(get_mutex());
91 const auto existing = funits_map_string_to_id_low.find(tmp);
92 if(existing != funits_map_string_to_id_low.end()) return existing->second;
93 }
94
95 // Only an actual custom-unit insertion needs exclusive access. Check
96 // again after acquiring it because another thread may have inserted
97 // the same name while this thread was waiting.
98 std::unique_lock<std::shared_mutex> lock(get_mutex());
99 const auto existing = funits_map_string_to_id_low.find(tmp);
100 if(existing != funits_map_string_to_id_low.end()) return existing->second;
101
102 const auto key = static_cast<units_keys>(funits_map_id_to_string.size());
103 funits_map_id_to_string[key] = _name;
104 funits_map_string_to_id[_name] = key;
105 funits_map_string_to_id_low[tmp] = key;
106 funits_map_conv_coeffs[key] = 1;
107 funits_map_types[key] = kNoConvUnits;
108 return key;
109 }
110
112 const auto index = static_cast<std::size_t>(_key);
113 if (_key >= Undef_unit && index < funits_properties.size())
114 return std::get<1>(funits_properties[index]);
115 std::shared_lock<std::shared_mutex> lock(get_mutex());
116 if(funits_map_types.count(_key)) return funits_map_types.at(_key);
117 else glog << warning_o << "data_units_manager::get_type(" << _key << "): unkown key " << do_endl;
118 return funits_map_types.at(Undef_unit);
119 }
120
123
124private:
125 double get_factor(const units_keys &_key) {
126 const auto index = static_cast<std::size_t>(_key);
127 if (_key >= Undef_unit && index < funits_properties.size())
128 return std::get<2>(funits_properties[index]);
129 std::shared_lock<std::shared_mutex> lock(get_mutex());
130 if(funits_map_conv_coeffs.count(_key)) return funits_map_conv_coeffs.at(_key);
131 else glog << warning_o << "data_units_manager::get_factor(" << _key << "): unkown key " << do_endl;
132 return funits_map_conv_coeffs.at(Undef_unit);
133 }
134
135#ifdef HAS_ROOT
137 ClassDef(tkunit_manager,0);
138#endif
139};
140
141#define gunits (tkn::tkunit_manager::the_unit_manager())
142
144{
145protected:
147 double fFactor = std::nan("0");
148 double fValue = -1.;
149
150public:
151 tkunit() {;}
152 tkunit(double _value, const tkunit_manager::units_keys &_unit);
153 tkunit(double _value, const tkstring &_unit) : tkunit(_value, gunits->get_key(_unit)) {}
154
155 virtual ~tkunit() {}
156
158 double get_value() const { return fValue/fFactor;}
159
161 void set_value(const double &_val, const tkstring &_unit="");
163 void set_value(const double &_val, const tkunit_manager::units_keys &_unit);
164
166 bool set_unit(const tkstring &_unit);
168 bool set_unit(const tkunit_manager::units_keys &_unit);
169
171 tkstring get_unit_name() const { return gunits->get_name(fUnit_key);}
173 tkunit_manager::units_keys get_unit_key() const { return fUnit_key;}
174
176 void clear() { fValue = 0.;}
177
178#ifdef HAS_ROOT
180 ClassDef(tkunit,0);
181#endif
182
183};
184}
185
186#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
tkstring copy() const
Returns a copy of this string.
Definition tkstring.cpp:377
tkstring & to_lower()
Change all letters to lower case.
Definition tkstring.cpp:118
Manages the different units.
Definition tkunit.h:34
units_keys
units identifiers
Definition tkunit.h:48
units_keys get_key(const tkstring &_name)
retuns unit key
Definition tkunit.h:87
friend class tkunit
Definition tkunit.h:35
virtual ~tkunit_manager()=default
units_type
flags that qualify the unit type
Definition tkunit.h:40
units_type get_type(const units_keys &_key)
retuns unit type
Definition tkunit.h:111
tkstring get_name(const units_keys &_key)
retuns unit name
Definition tkunit.h:77
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition tkunit.cpp:79
static const std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition tkunit.h:54
A measured value associated to its unit.
Definition tkunit.h:144
tkunit_manager::units_keys get_unit_key() const
returns the unit key
Definition tkunit.h:173
virtual ~tkunit()
Definition tkunit.h:155
double get_value() const
get the value in the current unit
Definition tkunit.h:158
tkunit(double _value, const tkstring &_unit)
Definition tkunit.h:153
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:93
tkstring get_unit_name() const
returns the unit name
Definition tkunit.h:171
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition tkunit.cpp:109
void clear()
reset the data_unit
Definition tkunit.h:176
Definition tklog.cpp:16
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & do_endl(tklog &log)
Definition tklog.h:212