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