CMS 3D CMS Logo

CocoaUnitsTable.cc
Go to the documentation of this file.
1 
3 #include <CLHEP/Units/SystemOfUnits.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)
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  using namespace CLHEP;
126  //Length
127  new CocoaUnitDefinition("meter", "m", "Length", meter);
128  new CocoaUnitDefinition("centimeter", "cm", "Length", centimeter);
129  new CocoaUnitDefinition("millimeter", "mm", "Length", millimeter);
130  // new CocoaUnitDefinition("micrometer","um" ,"Length",micrometer);
131  new CocoaUnitDefinition("micrometer", "mum", "Length", micrometer);
132  new CocoaUnitDefinition("nanometer", "nm", "Length", nanometer);
133  new CocoaUnitDefinition("angstrom", "Ang", "Length", angstrom);
134  new CocoaUnitDefinition("fermi", "fm", "Length", fermi);
135 
136  //Surface
137  new CocoaUnitDefinition("kilometer2", "km2", "Surface", kilometer2);
138  new CocoaUnitDefinition("meter2", "m2", "Surface", meter2);
139  new CocoaUnitDefinition("centimeter2", "cm2", "Surface", centimeter2);
140  new CocoaUnitDefinition("millimeter2", "mm2", "Surface", millimeter2);
141  new CocoaUnitDefinition("barn", "barn", "Surface", barn);
142  new CocoaUnitDefinition("millibarn", "mbarn", "Surface", millibarn);
143  new CocoaUnitDefinition("microbarn", "mubarn", "Surface", microbarn);
144  new CocoaUnitDefinition("nanobarn", "nbarn", "Surface", nanobarn);
145  new CocoaUnitDefinition("picobarn", "pbarn", "Surface", picobarn);
146 
147  //Volume
148  new CocoaUnitDefinition("kilometer3", "km3", "Volume", kilometer3);
149  new CocoaUnitDefinition("meter3", "m3", "Volume", meter3);
150  new CocoaUnitDefinition("centimeter3", "cm3", "Volume", centimeter3);
151  new CocoaUnitDefinition("millimeter3", "mm3", "Volume", millimeter3);
152 
153  //Angle
154  new CocoaUnitDefinition("radian", "rad", "Angle", radian);
155  new CocoaUnitDefinition("milliradian", "mrad", "Angle", milliradian);
156  new CocoaUnitDefinition("steradian", "sr", "Angle", steradian);
157  new CocoaUnitDefinition("degree", "deg", "Angle", degree);
158 
159  //Time
160  new CocoaUnitDefinition("second", "s", "Time", second);
161  new CocoaUnitDefinition("millisecond", "ms", "Time", millisecond);
162  new CocoaUnitDefinition("microsecond", "mus", "Time", microsecond);
163  new CocoaUnitDefinition("nanosecond", "ns", "Time", nanosecond);
164  new CocoaUnitDefinition("picosecond", "ps", "Time", picosecond);
165 
166  //Frequency
167  new CocoaUnitDefinition("hertz", "Hz", "Frequency", hertz);
168  new CocoaUnitDefinition("kilohertz", "kHz", "Frequency", kilohertz);
169  new CocoaUnitDefinition("megahertz", "MHz", "Frequency", megahertz);
170 
171  //Electric charge
172  new CocoaUnitDefinition("eplus", "e+", "Electric charge", eplus);
173  new CocoaUnitDefinition("coulomb", "C", "Electric charge", coulomb);
174 
175  //Energy
176  new CocoaUnitDefinition("electronvolt", "eV", "Energy", electronvolt);
177  new CocoaUnitDefinition("kiloelectronvolt", "keV", "Energy", kiloelectronvolt);
178  new CocoaUnitDefinition("megaelectronvolt", "MeV", "Energy", megaelectronvolt);
179  new CocoaUnitDefinition("gigaelectronvolt", "GeV", "Energy", gigaelectronvolt);
180  new CocoaUnitDefinition("teraelectronvolt", "TeV", "Energy", teraelectronvolt);
181  new CocoaUnitDefinition("petaelectronvolt", "PeV", "Energy", petaelectronvolt);
182  new CocoaUnitDefinition("joule", "J", "Energy", joule);
183 
184  //Mass
185  new CocoaUnitDefinition("milligram", "mg", "Mass", milligram);
186  new CocoaUnitDefinition("gram", "g", "Mass", gram);
187  new CocoaUnitDefinition("kilogram", "kg", "Mass", kilogram);
188 
189  //Volumic Mass
190  new CocoaUnitDefinition("g/cm3", "g/cm3", "Volumic Mass", g / cm3);
191  new CocoaUnitDefinition("mg/cm3", "mg/cm3", "Volumic Mass", mg / cm3);
192  new CocoaUnitDefinition("kg/m3", "kg/m3", "Volumic Mass", kg / m3);
193 
194  //Power
195  new CocoaUnitDefinition("watt", "W", "Power", watt);
196 
197  //Force
198  new CocoaUnitDefinition("newton", "N", "Force", newton);
199 
200  //Pressure
201  new CocoaUnitDefinition("pascal", "Pa", "Pressure", pascal);
202  new CocoaUnitDefinition("bar", "bar", "Pressure", bar);
203  new CocoaUnitDefinition("atmosphere", "atm", "Pressure", atmosphere);
204 
205  //Electric current
206  new CocoaUnitDefinition("ampere", "A", "Electric current", ampere);
207  new CocoaUnitDefinition("milliampere", "mA", "Electric current", milliampere);
208  new CocoaUnitDefinition("microampere", "muA", "Electric current", microampere);
209  new CocoaUnitDefinition("nanoampere", "nA", "Electric current", nanoampere);
210 
211  //Electric potential
212  new CocoaUnitDefinition("volt", "V", "Electric potential", volt);
213  new CocoaUnitDefinition("kilovolt", "kV", "Electric potential", kilovolt);
214  new CocoaUnitDefinition("megavolt", "MV", "Electric potential", megavolt);
215 
216  //Magnetic flux
217  new CocoaUnitDefinition("weber", "Wb", "Magnetic flux", weber);
218 
219  //Magnetic flux density
220  new CocoaUnitDefinition("tesla", "T", "Magnetic flux density", tesla);
221  new CocoaUnitDefinition("kilogauss", "kG", "Magnetic flux density", kilogauss);
222  new CocoaUnitDefinition("gauss", "G", "Magnetic flux density", gauss);
223 
224  //Temperature
225  new CocoaUnitDefinition("kelvin", "K", "Temperature", kelvin);
226 
227  //Amount of substance
228  new CocoaUnitDefinition("mole", "mol", "Amount of substance", mole);
229 
230  //Activity
231  new CocoaUnitDefinition("becquerel", "Bq", "Activity", becquerel);
232  new CocoaUnitDefinition("curie", "Ci", "Activity", curie);
233 
234  //Dose
235  new CocoaUnitDefinition("gray", "Gy", "Dose", gray);
236 }
237 
238 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
239 
241  std::cout << "\n ----- The Table of Units ----- \n";
242  for (size_t i = 0; i < theUnitsTable.size(); i++) {
243  theUnitsTable[i]->PrintCategory();
244  }
245 }
246 
247 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
248 
249 CocoaUnitsCategory::CocoaUnitsCategory(const ALIstring& name) : Name(name), UnitsList(), NameMxLen(0), SymbMxLen(0) {}
250 
251 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
252 
254 
255 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
256 
258 
259 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
260 
262  if (this != &right) {
263  Name = right.Name;
264  UnitsList = right.UnitsList;
265  NameMxLen = right.NameMxLen;
266  SymbMxLen = right.SymbMxLen;
267  }
268  return *this;
269 }
270 
271 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
272 
273 ALIint CocoaUnitsCategory::operator==(const CocoaUnitsCategory& right) const { return (this == &right); }
274 
275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
276 
277 ALIint CocoaUnitsCategory::operator!=(const CocoaUnitsCategory& right) const { return (this != &right); }
278 
279 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
280 
282  std::cout << "\n category: " << Name << std::endl;
283  for (size_t i = 0; i < UnitsList.size(); i++)
284  UnitsList[i]->PrintDefinition();
285 }
286 
287 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
288 
290  // find the category
292  if (theUnitsTable.empty())
293  CocoaUnitDefinition::BuildUnitsTable(); //t should be done somewhere else
294  size_t nbCat = theUnitsTable.size();
295  size_t i = 0;
296  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
297  i++;
298  if (i == nbCat) {
299  std::cout << " CocoaBestUnit: the category " << category << " does not exist !!" << nbCat << std::endl;
300  std::exception(); //"Missing unit category !");
301  }
302  //
303  IndexOfCategory = i;
304  nbOfVals = 1;
305  Value[0] = value;
306  Value[1] = 0.;
307  Value[2] = 0.;
308 
309  //COCOA internal units are in meters, not mm as in CLHEP
310  if (category == "Length") {
311  Value[0] *= 1000.;
312  Value[1] *= 1000.;
313  Value[2] *= 1000.;
314  }
315 }
316 
317 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
318 
319 CocoaBestUnit::CocoaBestUnit(const CLHEP::Hep3Vector& value, const ALIstring& category) {
320  // find the category
322  size_t nbCat = theUnitsTable.size();
323  size_t i = 0;
324  while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
325  i++;
326  if (i == nbCat) {
327  std::cerr << " CocoaBestUnit: the category " << category << " does not exist." << std::endl;
328  std::exception(); //"Unit category not existing !");
329  }
330  //
331  IndexOfCategory = i;
332  nbOfVals = 3;
333  Value[0] = value.x();
334  Value[1] = value.y();
335  Value[2] = value.z();
336 }
337 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
338 
340 
341 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
342 
343 std::ostream& operator<<(std::ostream& flux, CocoaBestUnit a) {
345  CocoaUnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
346  ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
347 
348  ALIint ksup(-1), kinf(-1);
349  ALIdouble umax(0.), umin(ALI_DBL_MAX);
350  ALIdouble rsup(ALI_DBL_MAX), rinf(0.);
351 
352  //for a ThreeVector, choose the best unit for the biggest value
353  ALIdouble value = std::max(std::max(std::abs(a.Value[0]), std::abs(a.Value[1])), std::abs(a.Value[2]));
354 
355  for (size_t k = 0; k < List.size(); k++) {
356  ALIdouble unit = List[k]->GetValue();
357  if (value == ALI_DBL_MAX) {
358  if (unit > umax) {
359  umax = unit;
360  ksup = k;
361  }
362  } else if (value <= ALI_DBL_MIN) {
363  if (unit < umin) {
364  umin = unit;
365  kinf = k;
366  }
367  }
368 
369  else {
370  ALIdouble ratio = value / unit;
371  if ((ratio >= 1.) && (ratio < rsup)) {
372  rsup = ratio;
373  ksup = k;
374  }
375  if ((ratio < 1.) && (ratio > rinf)) {
376  rinf = ratio;
377  kinf = k;
378  }
379  }
380  }
381 
382  ALIint index = ksup;
383  if (index == -1)
384  index = kinf;
385  if (index == -1)
386  index = 0;
387 
388  for (ALIint j = 0; j < a.nbOfVals; j++) {
389  flux << a.Value[j] / (List[index]->GetValue()) << " ";
390  }
391 
392  std::ios::fmtflags oldform = flux.flags();
393 
394  flux.setf(std::ios::left, std::ios::adjustfield);
395  flux << std::setw(len) << List[index]->GetSymbol();
396  flux.flags(oldform);
397 
398  return flux;
399 }
400 
401 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
std::vector< CocoaUnitDefinition * > CocoaUnitsContainer
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()
constexpr double joule(6.24150e+12)
ALIdouble Value[3]
size_t IndexOfCategory
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
U second(std::pair< T, U > const &p)
CocoaUnitDefinition(const ALIstring &name, const ALIstring &symbol, const ALIstring &category, ALIdouble value)
CocoaUnitDefinition & operator=(const CocoaUnitDefinition &)
ALIint operator==(const CocoaUnitDefinition &) const
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
Basic3DVector unit() const
const ALIstring & GetName() const
ALIint operator!=(const CocoaUnitsCategory &) const
static CocoaUnitsTable theUnitsTable
static ALIdouble GetValueOf(const ALIstring &)
deadvectors [0] push_back({0.0175431, 0.538005, 6.80997, 13.29})
TString units(TString variable, Char_t axis)
double a
Definition: hdecay.h:121
std::string ALIstring
Definition: CocoaGlobals.h:9
#define str(s)
ALIint operator!=(const CocoaUnitDefinition &) const
ALIint operator==(const CocoaUnitsCategory &) const
const double ALI_DBL_MAX
Definition: CocoaGlobals.h:24