CMS 3D CMS Logo

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