CMS 3D CMS Logo

ALIUnitsTable.cc
Go to the documentation of this file.
1 
3 #include <CLHEP/Units/SystemOfUnits.h>
4 
5 #include <iomanip>
6 #include <cstdlib>
7 #include <cmath> // include floating-point std::abs functions
8 
10 
11 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
12 
14  : Name(name), SymbolName(symbol), Value(value) {
15  //
16  //does the Category objet already exist ?
17  size_t nbCat = theUnitsTable.size();
18  size_t i = 0;
19  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
20  i++;
21  if (i == nbCat)
22  theUnitsTable.push_back(new ALIUnitsCategory(category));
23  CategoryIndex = i;
24  //
25  //insert this Unit in the Unitstable
26  (theUnitsTable[CategoryIndex]->GetUnitsList()).emplace_back(shared_from_this());
27 
28  //update ALIstring max length for name and symbol
29  theUnitsTable[i]->UpdateNameMxLen((ALIint)name.length());
30  theUnitsTable[i]->UpdateSymbMxLen((ALIint)symbol.length());
31 }
32 
33 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
34 
36 
37 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
38 
40  : std::enable_shared_from_this<ALIUnitDefinition>(right) {
41  *this = right;
42 }
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
45 
47  if (this != &right) {
48  Name = right.Name;
49  SymbolName = right.SymbolName;
50  Value = right.Value;
52  }
53  return *this;
54 }
55 
56 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
57 
58 ALIint ALIUnitDefinition::operator==(const ALIUnitDefinition& right) const { return (this == &right); }
59 
60 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
61 
62 ALIint ALIUnitDefinition::operator!=(const ALIUnitDefinition& right) const { return (this != &right); }
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
66  if (theUnitsTable.empty())
68  ALIstring name, symbol;
69  for (size_t i = 0; i < theUnitsTable.size(); i++) {
70  ALIUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
71  for (size_t j = 0; j < units.size(); j++) {
72  name = units[j]->GetName();
73  symbol = units[j]->GetSymbol();
74  if (stri == name || stri == symbol)
75  return units[j]->GetValue();
76  }
77  }
78  std::cout << "Warning from ALIUnitDefinition::GetValueOf(" << stri << ")."
79  << " The unit " << stri << " does not exist in UnitsTable."
80  << " Return Value = 0." << std::endl;
81  return 0.;
82 }
83 
84 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
85 
87  if (theUnitsTable.empty())
89  ALIstring name, symbol;
90  for (size_t i = 0; i < theUnitsTable.size(); i++) {
91  ALIUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
92  for (size_t j = 0; j < units.size(); j++) {
93  name = units[j]->GetName();
94  symbol = units[j]->GetSymbol();
95  if (stri == name || stri == symbol)
96  return theUnitsTable[i]->GetName();
97  }
98  }
99  std::cout << "Warning from ALIUnitDefinition::GetCategory(" << stri << ")."
100  << " The unit " << stri << " does not exist in UnitsTable."
101  << " Return category = None" << std::endl;
102  name = "None";
103  return name;
104 }
105 
106 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
107 
109  ALIint nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
110  ALIint symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
111  std::cout << std::setw(nameL) << Name << " (" << std::setw(symbL) << SymbolName << ") = " << Value << std::endl;
112 }
113 
114 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
115 
117  using namespace CLHEP;
118  //Length
119  std::make_shared<ALIUnitDefinition>("kilometer", "km", "Length", kilometer);
120  std::make_shared<ALIUnitDefinition>("meter", "m", "Length", meter);
121  std::make_shared<ALIUnitDefinition>("centimeter", "cm", "Length", centimeter);
122  std::make_shared<ALIUnitDefinition>("millimeter", "mm", "Length", millimeter);
123  std::make_shared<ALIUnitDefinition>("micrometer", "mum", "Length", micrometer);
124  std::make_shared<ALIUnitDefinition>("nanometer", "nm", "Length", nanometer);
125  std::make_shared<ALIUnitDefinition>("angstrom", "Ang", "Length", angstrom);
126  std::make_shared<ALIUnitDefinition>("fermi", "fm", "Length", fermi);
127 
128  //Surface
129  std::make_shared<ALIUnitDefinition>("kilometer2", "km2", "Surface", kilometer2);
130  std::make_shared<ALIUnitDefinition>("meter2", "m2", "Surface", meter2);
131  std::make_shared<ALIUnitDefinition>("centimeter2", "cm2", "Surface", centimeter2);
132  std::make_shared<ALIUnitDefinition>("millimeter2", "mm2", "Surface", millimeter2);
133  std::make_shared<ALIUnitDefinition>("barn", "barn", "Surface", barn);
134  std::make_shared<ALIUnitDefinition>("millibarn", "mbarn", "Surface", millibarn);
135  std::make_shared<ALIUnitDefinition>("microbarn", "mubarn", "Surface", microbarn);
136  std::make_shared<ALIUnitDefinition>("nanobarn", "nbarn", "Surface", nanobarn);
137  std::make_shared<ALIUnitDefinition>("picobarn", "pbarn", "Surface", picobarn);
138 
139  //Volume
140  std::make_shared<ALIUnitDefinition>("kilometer3", "km3", "Volume", kilometer3);
141  std::make_shared<ALIUnitDefinition>("meter3", "m3", "Volume", meter3);
142  std::make_shared<ALIUnitDefinition>("centimeter3", "cm3", "Volume", centimeter3);
143  std::make_shared<ALIUnitDefinition>("millimeter3", "mm3", "Volume", millimeter3);
144 
145  //Angle
146  std::make_shared<ALIUnitDefinition>("radian", "rad", "Angle", radian);
147  std::make_shared<ALIUnitDefinition>("milliradian", "mrad", "Angle", milliradian);
148  std::make_shared<ALIUnitDefinition>("milliradian", "murad", "Angle", 0.001 * milliradian);
149  std::make_shared<ALIUnitDefinition>("steradian", "sr", "Angle", steradian);
150  std::make_shared<ALIUnitDefinition>("degree", "deg", "Angle", degree);
151 
152  //Time
153  std::make_shared<ALIUnitDefinition>("second", "s", "Time", second);
154  std::make_shared<ALIUnitDefinition>("millisecond", "ms", "Time", millisecond);
155  std::make_shared<ALIUnitDefinition>("microsecond", "mus", "Time", microsecond);
156  std::make_shared<ALIUnitDefinition>("nanosecond", "ns", "Time", nanosecond);
157  std::make_shared<ALIUnitDefinition>("picosecond", "ps", "Time", picosecond);
158 
159  //Frequency
160  std::make_shared<ALIUnitDefinition>("hertz", "Hz", "Frequency", hertz);
161  std::make_shared<ALIUnitDefinition>("kilohertz", "kHz", "Frequency", kilohertz);
162  std::make_shared<ALIUnitDefinition>("megahertz", "MHz", "Frequency", megahertz);
163 
164  //Electric charge
165  std::make_shared<ALIUnitDefinition>("eplus", "e+", "Electric charge", eplus);
166  std::make_shared<ALIUnitDefinition>("coulomb", "C", "Electric charge", coulomb);
167 
168  //Energy
169  std::make_shared<ALIUnitDefinition>("electronvolt", "eV", "Energy", electronvolt);
170  std::make_shared<ALIUnitDefinition>("kiloelectronvolt", "keV", "Energy", kiloelectronvolt);
171  std::make_shared<ALIUnitDefinition>("megaelectronvolt", "MeV", "Energy", megaelectronvolt);
172  std::make_shared<ALIUnitDefinition>("gigaelectronvolt", "GeV", "Energy", gigaelectronvolt);
173  std::make_shared<ALIUnitDefinition>("teraelectronvolt", "TeV", "Energy", teraelectronvolt);
174  std::make_shared<ALIUnitDefinition>("petaelectronvolt", "PeV", "Energy", petaelectronvolt);
175  std::make_shared<ALIUnitDefinition>("joule", "J", "Energy", joule);
176 
177  //Mass
178  std::make_shared<ALIUnitDefinition>("milligram", "mg", "Mass", milligram);
179  std::make_shared<ALIUnitDefinition>("gram", "g", "Mass", gram);
180  std::make_shared<ALIUnitDefinition>("kilogram", "kg", "Mass", kilogram);
181 
182  //Volumic Mass
183  std::make_shared<ALIUnitDefinition>("g/cm3", "g/cm3", "Volumic Mass", g / cm3);
184  std::make_shared<ALIUnitDefinition>("mg/cm3", "mg/cm3", "Volumic Mass", mg / cm3);
185  std::make_shared<ALIUnitDefinition>("kg/m3", "kg/m3", "Volumic Mass", kg / m3);
186 
187  //Power
188  std::make_shared<ALIUnitDefinition>("watt", "W", "Power", watt);
189 
190  //Force
191  std::make_shared<ALIUnitDefinition>("newton", "N", "Force", newton);
192 
193  //Pressure
194  std::make_shared<ALIUnitDefinition>("pascal", "Pa", "Pressure", pascal);
195  std::make_shared<ALIUnitDefinition>("bar", "bar", "Pressure", bar);
196  std::make_shared<ALIUnitDefinition>("atmosphere", "atm", "Pressure", atmosphere);
197 
198  //Electric current
199  std::make_shared<ALIUnitDefinition>("ampere", "A", "Electric current", ampere);
200  std::make_shared<ALIUnitDefinition>("milliampere", "mA", "Electric current", milliampere);
201  std::make_shared<ALIUnitDefinition>("microampere", "muA", "Electric current", microampere);
202  std::make_shared<ALIUnitDefinition>("nanoampere", "nA", "Electric current", nanoampere);
203 
204  //Electric potential
205  std::make_shared<ALIUnitDefinition>("volt", "V", "Electric potential", volt);
206  std::make_shared<ALIUnitDefinition>("kilovolt", "kV", "Electric potential", kilovolt);
207  std::make_shared<ALIUnitDefinition>("megavolt", "MV", "Electric potential", megavolt);
208 
209  //Magnetic flux
210  std::make_shared<ALIUnitDefinition>("weber", "Wb", "Magnetic flux", weber);
211 
212  //Magnetic flux density
213  std::make_shared<ALIUnitDefinition>("tesla", "T", "Magnetic flux density", tesla);
214  std::make_shared<ALIUnitDefinition>("kilogauss", "kG", "Magnetic flux density", kilogauss);
215  std::make_shared<ALIUnitDefinition>("gauss", "G", "Magnetic flux density", gauss);
216 
217  //Temperature
218  std::make_shared<ALIUnitDefinition>("kelvin", "K", "Temperature", kelvin);
219 
220  //Amount of substance
221  std::make_shared<ALIUnitDefinition>("mole", "mol", "Amount of substance", mole);
222 
223  //Activity
224  std::make_shared<ALIUnitDefinition>("becquerel", "Bq", "Activity", becquerel);
225  std::make_shared<ALIUnitDefinition>("curie", "Ci", "Activity", curie);
226 
227  //Dose
228  std::make_shared<ALIUnitDefinition>("gray", "Gy", "Dose", gray);
229 }
230 
231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
232 
234  std::cout << "\n ----- The Table of Units ----- \n";
235  for (size_t i = 0; i < theUnitsTable.size(); i++)
236  theUnitsTable[i]->PrintCategory();
237 }
238 
239 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
240 
241 ALIUnitsCategory::ALIUnitsCategory(ALIstring name) : Name(name), NameMxLen(0), SymbMxLen(0) {
242  UnitsList = *(new ALIUnitsContainer);
243 }
244 
245 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
246 
248 
249 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
250 
252 
253 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
254 
256  if (this != &right) {
257  Name = right.Name;
258  UnitsList = right.UnitsList;
259  NameMxLen = right.NameMxLen;
260  SymbMxLen = right.SymbMxLen;
261  }
262  return *this;
263 }
264 
265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
266 
267 ALIint ALIUnitsCategory::operator==(const ALIUnitsCategory& right) const { return (this == &right); }
268 
269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
270 
271 ALIint ALIUnitsCategory::operator!=(const ALIUnitsCategory& right) const { return (this != &right); }
272 
273 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
274 
276  std::cout << "\n category: " << Name << std::endl;
277  for (size_t i = 0; i < UnitsList.size(); i++)
278  UnitsList[i]->PrintDefinition();
279 }
280 
281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
282 
284  // find the category
286  size_t nbCat = theUnitsTable.size();
287  size_t i = 0;
288  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
289  i++;
290  if (i == nbCat) {
291  std::cout << " ALIBestUnit: the category " << category << " does not exist !!" << std::endl;
292  std::cerr << "Missing unit category !" << std::endl;
293  abort();
294  }
295  //
296  IndexOfCategory = i;
297  nbOfVals = 1;
298  Value[0] = value;
299  Value[1] = 0.;
300  Value[2] = 0.;
301 }
302 
303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
304 
305 ALIBestUnit::ALIBestUnit(const CLHEP::Hep3Vector& value, ALIstring category) {
306  // find the category
308  size_t nbCat = theUnitsTable.size();
309  size_t i = 0;
310  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
311  i++;
312  if (i == nbCat) {
313  std::cerr << " ALIBestUnit: the category " << category << " does not exist." << std::endl;
314  std::cerr << "Unit category not existing !" << std::endl;
315  abort();
316  }
317  //
318  IndexOfCategory = i;
319  nbOfVals = 3;
320  Value[0] = value.x();
321  Value[1] = value.y();
322  Value[2] = value.z();
323 }
324 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
325 
327 
328 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
329 
330 std::ostream& operator<<(std::ostream& flux, ALIBestUnit a) {
332  ALIUnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
333  ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
334 
335  ALIint ksup(-1), kinf(-1);
336  ALIdouble umax(0.), umin(1.E12);
337  ALIdouble rsup(1.E12), rinf(0.);
338 
339  //for a ThreeVector, choose the best unit for the biggest value
340  ALIdouble value = std::max(std::max(std::abs(a.Value[0]), std::abs(a.Value[1])), std::abs(a.Value[2]));
341 
342  for (size_t k = 0; k < List.size(); k++) {
343  ALIdouble unit = List[k]->GetValue();
344  if (value == 1.E12) {
345  if (unit > umax) {
346  umax = unit;
347  ksup = k;
348  }
349  } else if (value <= -1.E12) {
350  if (unit < umin) {
351  umin = unit;
352  kinf = k;
353  }
354  }
355 
356  else {
357  ALIdouble ratio = value / unit;
358  if ((ratio >= 1.) && (ratio < rsup)) {
359  rsup = ratio;
360  ksup = k;
361  }
362  if ((ratio < 1.) && (ratio > rinf)) {
363  rinf = ratio;
364  kinf = k;
365  }
366  }
367  }
368 
369  ALIint index = ksup;
370  if (index == -1)
371  index = kinf;
372  if (index == -1)
373  index = 0;
374 
375  for (ALIint j = 0; j < a.nbOfVals; j++) {
376  flux << a.Value[j] / (List[index]->GetValue()) << " ";
377  }
378 
379 #ifdef ALIUSE_STD_NAMESPACE
380  std::ios::fmtflags oldform = std::cout.flags();
381 #else
382  // ALIint oldform = std::cout.flags();
383 #endif
384 
385  flux.setf(std::ios::left, std::ios::adjustfield);
386  flux << std::setw(len) << List[index]->GetSymbol();
387  //?? flux.flags(oldform);
388 
389  return flux;
390 }
391 
392 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
long double ALIdouble
Definition: CocoaGlobals.h:11
ALIint operator!=(const ALIUnitsCategory &) const
ALIint operator!=(const ALIUnitDefinition &) const
std::ostream & operator<<(std::ostream &flux, ALIBestUnit a)
std::vector< ALIUnitsCategory * > ALIUnitsTable
Definition: ALIUnitsTable.h:47
ALIUnitDefinition & operator=(const ALIUnitDefinition &)
ALIstring SymbolName
Definition: ALIUnitsTable.h:82
constexpr double joule(6.24150e+12)
ALIBestUnit(ALIdouble internalValue, ALIstring category)
int ALIint
Definition: CocoaGlobals.h:15
ALIUnitDefinition(ALIstring name, ALIstring symbol, ALIstring category, ALIdouble value)
static ALIstring GetCategory(ALIstring)
ALIint nbOfVals
ALIUnitsContainer UnitsList
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
U second(std::pair< T, U > const &p)
ALIint operator==(const ALIUnitDefinition &) const
ALIUnitsCategory & operator=(const ALIUnitsCategory &)
static void PrintUnitsTable()
ALIstring GetName() const
Definition: ALIUnitsTable.h:66
size_t IndexOfCategory
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ALIUnitsCategory(ALIstring name)
reco::JetExtendedAssociation::JetExtendedData Value
static void BuildUnitsTable()
ALIdouble Value[3]
Definition: value.py:1
Basic3DVector unit() const
std::vector< std::shared_ptr< ALIUnitDefinition > > ALIUnitsContainer
Definition: ALIUnitsTable.h:92
ALIint operator==(const ALIUnitsCategory &) const
static ALIdouble GetValueOf(ALIstring)
TString units(TString variable, Char_t axis)
double a
Definition: hdecay.h:121
std::string ALIstring
Definition: CocoaGlobals.h:9
static ALIUnitsTable theUnitsTable
Definition: ALIUnitsTable.h:85
static ALIUnitsTable & GetUnitsTable()
Definition: ALIUnitsTable.h:75