Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UnitsTable.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id$
28 //
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30 //
31 // 17-05-98: first version, M.Maire
32 // 05-08-98: angstrom,microbarn,picobarn,petaelectronvolt, M.Maire
33 // 13-10-98: units and symbols printed in fixed length, M.Maire
34 // 01-03-01: parsec, M.Maire
35 // 06-03-01: migration to STL vectors, G.Cosmo
36 // 06-05-02: BestUnit operator<< flux instead of G4cout (mma)
37 // 12-08-05: cm2/g ("Surface/Mass") (mma)
38 // 30-06-05: um for micrometer (mma)
39 // 07-02-06: GeV/cm MeV/cm keV/cm eV/cm ("Energy/Length") (mma)
40 // 15-02-06: g/cm2 ("Mass/Surface")
41 // MeV*cm2/g ..etc.. ("Energy*Surface/Mass")
42 // 18-08-06: remove symbol mum (mma)
43 // 06-05-08: V/m ("Electric field") (mma)
44 // 09-08-10: new category "Solid angle" (mma)
45 //
46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
48 
49 #include <iomanip>
50 #include <sstream>
51 
52 #include "G4UnitsTable.hh"
53 #include "G4SystemOfUnits.hh"
54 
55 G4UnitsTable G4UnitDefinition::theUnitsTable;
56 
57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
58 
60  const G4String& symbol,
61  const G4String& category, G4double value)
62  : Name(name),SymbolName(symbol),Value(value)
63 {
64  // Does the Category objet already exist ?
65  //
66  size_t nbCat = theUnitsTable.size();
67  size_t i = 0;
68  while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
69  if (i == nbCat)
70  { theUnitsTable.push_back( new G4UnitsCategory(category)); }
71  CategoryIndex = i;
72 
73  // Insert this Unit in the Units table
74  //
75  (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this);
76 
77  // Update string max length for name and symbol
78  //
79  theUnitsTable[i]->UpdateNameMxLen((G4int)name.length());
80  theUnitsTable[i]->UpdateSymbMxLen((G4int)symbol.length());
81 }
82 
83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 
86 {
87 }
88 
89 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
90 
92 {
93  *this = right;
94 }
95 
96 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
97 
98 G4UnitDefinition& G4UnitDefinition::operator=(const G4UnitDefinition& right)
99 {
100  if (this != &right)
101  {
102  Name = right.Name;
103  SymbolName = right.SymbolName;
104  Value = right.Value;
105  CategoryIndex = right.CategoryIndex;
106  }
107  return *this;
108 }
109 
110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
111 
113 {
114  return (this == (G4UnitDefinition *) &right);
115 }
116 
117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
118 
120 {
121  return (this != (G4UnitDefinition *) &right);
122 }
123 
124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
125 
127 {
128  if(theUnitsTable.size()==0) { BuildUnitsTable(); }
129  return theUnitsTable;
130 }
131 
132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
133 
135 {
137  for (size_t i=0;i<(GetUnitsTable()).size();i++)
138  {
139  G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
140  for (size_t j=0;j<units.size();j++)
141  {
142  name=units[j]->GetName(); symbol=units[j]->GetSymbol();
143  if(str==name||str==symbol)
144  { return units[j]->GetValue(); }
145  }
146  }
147  std::ostringstream message;
148  message << "The unit '" << str << "' does not exist in the Units Table.";
149  G4Exception("G4UnitDefinition::GetValueOf()", "InvalidUnit",
150  JustWarning, message, "Returning Value = 0.");
151  return 0.;
152 }
153 
154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
155 
157 {
159  for (size_t i=0;i<(GetUnitsTable()).size();i++)
160  {
161  G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList();
162  for (size_t j=0;j<units.size();j++)
163  {
164  name=units[j]->GetName(); symbol=units[j]->GetSymbol();
165  if(str==name||str==symbol)
166  { return theUnitsTable[i]->GetName(); }
167  }
168  }
169  std::ostringstream message;
170  message << "The unit '" << str << "' does not exist in the Units Table.";
171  G4Exception("G4UnitDefinition::GetCategory()", "InvalidUnit",
172  JustWarning, message, "Returning Value = 0.");
173  name = "None";
174  return name;
175 }
176 
177 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
178 
180 {
181  G4int nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
182  G4int symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
183  G4cout << std::setw(nameL) << Name << " ("
184  << std::setw(symbL) << SymbolName << ") = " << Value << G4endl;
185 }
186 
187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
188 
190 {
191  //Length
192  new G4UnitDefinition( "parsec","pc" ,"Length",parsec);
193  new G4UnitDefinition( "kilometer","km" ,"Length",kilometer);
194  new G4UnitDefinition( "meter","m" ,"Length",meter);
195  new G4UnitDefinition("centimeter","cm" ,"Length",centimeter);
196  new G4UnitDefinition("millimeter","mm" ,"Length",millimeter);
197  new G4UnitDefinition("micrometer","um" ,"Length",micrometer);
198  new G4UnitDefinition( "nanometer","nm" ,"Length",nanometer);
199  new G4UnitDefinition( "angstrom","Ang" ,"Length",angstrom);
200  new G4UnitDefinition( "fermi","fm" ,"Length",fermi);
201 
202  //Surface
203  new G4UnitDefinition( "kilometer2","km2" ,"Surface",kilometer2);
204  new G4UnitDefinition( "meter2","m2" ,"Surface",meter2);
205  new G4UnitDefinition("centimeter2","cm2" ,"Surface",centimeter2);
206  new G4UnitDefinition("millimeter2","mm2" ,"Surface",millimeter2);
207  new G4UnitDefinition( "barn","barn" ,"Surface",barn);
208  new G4UnitDefinition( "millibarn","mbarn" ,"Surface",millibarn);
209  new G4UnitDefinition( "microbarn","mubarn" ,"Surface",microbarn);
210  new G4UnitDefinition( "nanobarn","nbarn" ,"Surface",nanobarn);
211  new G4UnitDefinition( "picobarn","pbarn" ,"Surface",picobarn);
212 
213  //Volume
214  new G4UnitDefinition( "kilometer3","km3" ,"Volume",kilometer3);
215  new G4UnitDefinition( "meter3","m3" ,"Volume",meter3);
216  new G4UnitDefinition("centimeter3","cm3" ,"Volume",centimeter3);
217  new G4UnitDefinition("millimeter3","mm3" ,"Volume",millimeter3);
218 
219  //Angle
220  new G4UnitDefinition( "radian","rad" ,"Angle",radian);
221  new G4UnitDefinition("milliradian","mrad" ,"Angle",milliradian);
222  new G4UnitDefinition( "degree","deg" ,"Angle",degree);
223 
224  //Solid angle
225  new G4UnitDefinition( "steradian","sr" ,"Solid angle",steradian);
226  new G4UnitDefinition("millisteradian","msr" ,"Solid angle",steradian*0.001);
227 
228  //Time
229  new G4UnitDefinition( "second","s" ,"Time",second);
230  new G4UnitDefinition("millisecond","ms" ,"Time",millisecond);
231  new G4UnitDefinition("microsecond","mus" ,"Time",microsecond);
232  new G4UnitDefinition( "nanosecond","ns" ,"Time",nanosecond);
233  new G4UnitDefinition( "picosecond","ps" ,"Time",picosecond);
234 
235  //Frequency
236  new G4UnitDefinition( "hertz","Hz" ,"Frequency",hertz);
237  new G4UnitDefinition("kilohertz","kHz" ,"Frequency",kilohertz);
238  new G4UnitDefinition("megahertz","MHz" ,"Frequency",megahertz);
239 
240  //Electric charge
241  new G4UnitDefinition( "eplus","e+" ,"Electric charge",eplus);
242  new G4UnitDefinition("coulomb","C" ,"Electric charge",coulomb);
243 
244  //Energy
245  new G4UnitDefinition( "electronvolt","eV" ,"Energy",electronvolt);
246  new G4UnitDefinition("kiloelectronvolt","keV","Energy",kiloelectronvolt);
247  new G4UnitDefinition("megaelectronvolt","MeV","Energy",megaelectronvolt);
248  new G4UnitDefinition("gigaelectronvolt","GeV","Energy",gigaelectronvolt);
249  new G4UnitDefinition("teraelectronvolt","TeV","Energy",teraelectronvolt);
250  new G4UnitDefinition("petaelectronvolt","PeV","Energy",petaelectronvolt);
251  new G4UnitDefinition( "joule","J" ,"Energy",joule);
252 
253  // Energy/Length
254  new G4UnitDefinition( "GeV/cm", "GeV/cm","Energy/Length", GeV/cm);
255  new G4UnitDefinition( "MeV/cm", "MeV/cm","Energy/Length", MeV/cm);
256  new G4UnitDefinition( "keV/cm", "keV/cm","Energy/Length", keV/cm);
257  new G4UnitDefinition( "eV/cm", "eV/cm","Energy/Length", eV/cm);
258 
259  //Mass
260  new G4UnitDefinition("milligram","mg","Mass",milligram);
261  new G4UnitDefinition( "gram","g" ,"Mass",gram);
262  new G4UnitDefinition( "kilogram","kg","Mass",kilogram);
263 
264  //Volumic Mass
265  new G4UnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3);
266  new G4UnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3);
267  new G4UnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3);
268 
269  // Mass/Surface
270  new G4UnitDefinition( "g/cm2", "g/cm2","Mass/Surface", g/cm2);
271  new G4UnitDefinition( "mg/cm2", "mg/cm2","Mass/Surface", mg/cm2);
272  new G4UnitDefinition( "kg/cm2", "kg/cm2","Mass/Surface", kg/cm2);
273 
274  // Surface/Mass
275  new G4UnitDefinition( "cm2/g", "cm2/g","Surface/Mass", cm2/g);
276 
277  // Energy.Surface/Mass
278  new G4UnitDefinition( "eV*cm2/g", " eV*cm2/g","Energy*Surface/Mass", eV*cm2/g);
279  new G4UnitDefinition("keV*cm2/g", "keV*cm2/g","Energy*Surface/Mass",keV*cm2/g);
280  new G4UnitDefinition("MeV*cm2/g", "MeV*cm2/g","Energy*Surface/Mass",MeV*cm2/g);
281  new G4UnitDefinition("GeV*cm2/g", "GeV*cm2/g","Energy*Surface/Mass",GeV*cm2/g);
282 
283  //Power
284  new G4UnitDefinition("watt","W","Power",watt);
285 
286  //Force
287  new G4UnitDefinition("newton","N","Force",newton);
288 
289  //Pressure
290  new G4UnitDefinition( "pascal","Pa" ,"Pressure",pascal);
291  new G4UnitDefinition( "bar","bar","Pressure",bar);
292  new G4UnitDefinition("atmosphere","atm","Pressure",atmosphere);
293 
294  //Electric current
295  new G4UnitDefinition( "ampere","A" ,"Electric current",ampere);
296  new G4UnitDefinition("milliampere","mA" ,"Electric current",milliampere);
297  new G4UnitDefinition("microampere","muA","Electric current",microampere);
298  new G4UnitDefinition( "nanoampere","nA" ,"Electric current",nanoampere);
299 
300  //Electric potential
301  new G4UnitDefinition( "volt","V" ,"Electric potential",volt);
302  new G4UnitDefinition("kilovolt","kV","Electric potential",kilovolt);
303  new G4UnitDefinition("megavolt","MV","Electric potential",megavolt);
304 
305  //Electric field
306  new G4UnitDefinition( "volt/m","V/m","Electric field",volt/m);
307 
308  //Magnetic flux
309  new G4UnitDefinition("weber","Wb","Magnetic flux",weber);
310 
311  //Magnetic flux density
312  new G4UnitDefinition( "tesla","T" ,"Magnetic flux density",tesla);
313  new G4UnitDefinition("kilogauss","kG","Magnetic flux density",kilogauss);
314  new G4UnitDefinition( "gauss","G" ,"Magnetic flux density",gauss);
315 
316  //Temperature
317  new G4UnitDefinition("kelvin","K","Temperature",kelvin);
318 
319  //Amount of substance
320  new G4UnitDefinition("mole","mol","Amount of substance",mole);
321 
322  //Activity
323  new G4UnitDefinition("becquerel","Bq","Activity",becquerel);
324  new G4UnitDefinition( "curie","Ci","Activity",curie);
325 
326  //Dose
327  new G4UnitDefinition("gray","Gy","Dose",gray);
328 }
329 
330 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
331 
333 {
334  G4cout << "\n ----- The Table of Units ----- \n";
335  for(size_t i=0;i<theUnitsTable.size();i++)
336  {
337  theUnitsTable[i]->PrintCategory();
338  }
339 }
340 
341 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
342 
344 {
345  for (size_t i=0;i<theUnitsTable.size();i++)
346  {
347  delete theUnitsTable[i];
348  }
349  theUnitsTable.clear();
350 }
351 
352 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
353 
355  : Name(name),UnitsList(),NameMxLen(0),SymbMxLen(0)
356 {
357 }
358 
359 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
360 
362 {
363  for(size_t i=0;i<UnitsList.size();i++)
364  {
365  delete UnitsList[i];
366  }
367  UnitsList.clear();
368 }
369 
370 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
371 
373 {
374  *this = right;
375 }
376 
377 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
378 
379 G4UnitsCategory& G4UnitsCategory::operator=(const G4UnitsCategory& right)
380 {
381  if (this != &right)
382  {
383  Name = right.Name;
384  UnitsList = right.UnitsList;
385  NameMxLen = right.NameMxLen;
386  SymbMxLen = right.SymbMxLen;
387  }
388  return *this;
389 }
390 
391 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
392 
394 {
395  return (this == (G4UnitsCategory *) &right);
396 }
397 
398 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
399 
401 {
402  return (this != (G4UnitsCategory *) &right);
403 }
404 
405 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
406 
408 {
409  G4cout << "\n category: " << Name << G4endl;
410  for(size_t i=0;i<UnitsList.size();i++)
411  { UnitsList[i]->PrintDefinition(); }
412 }
413 
414 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
415 
417  : nbOfVals(1)
418 {
419  // find the category
421  size_t nbCat = theUnitsTable.size();
422  size_t i = 0;
423  while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
424  if (i == nbCat)
425  {
426  G4cout << " G4BestUnit: the category " << category
427  << " does not exist !!" << G4endl;
428  G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
429  FatalException, "Missing unit category !") ;
430  }
431  //
432  Value[0] = value;
433  Value[1] = 0.;
434  Value[2] = 0.;
435  IndexOfCategory = i;
436 }
437 
438 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
439 
441  : nbOfVals(3)
442 {
443  // find the category
445  size_t nbCat = theUnitsTable.size();
446  size_t i = 0;
447  while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; }
448  if (i == nbCat)
449  {
450  G4cerr << " G4BestUnit: the category " << category
451  << " does not exist." << G4endl;
452  G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall",
453  FatalException, "Missing unit category !") ;
454  }
455  //
456  Value[0] = value.x();
457  Value[1] = value.y();
458  Value[2] = value.z();
459  IndexOfCategory = i;
460 }
461 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
462 
464 {}
465 
466 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
467 
468 G4BestUnit::operator G4String () const
469 {
470  std::ostringstream oss;
471  oss << *this;
472  return oss.str();
473 }
474 
475 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
476 
477 std::ostream& operator<<(std::ostream& flux, G4BestUnit a)
478 {
480  G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory]
481  ->GetUnitsList();
482  G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
483 
484  G4int ksup(-1), kinf(-1);
485  G4double umax(0.), umin(DBL_MAX);
486  G4double rsup(DBL_MAX), rinf(0.);
487 
488  //for a ThreeVector, choose the best unit for the biggest value
489  G4double value = std::max(std::max(std::fabs(a.Value[0]),
490  std::fabs(a.Value[1])),
491  std::fabs(a.Value[2]));
492 
493  for (size_t k=0; k<List.size(); k++)
494  {
495  G4double unit = List[k]->GetValue();
496  if (!(value!=DBL_MAX))
497  {if(unit>umax) {umax=unit; ksup=k;}}
498  else if (value<=DBL_MIN)
499  {if(unit<umin) {umin=unit; kinf=k;}}
500  else
501  {
502  G4double ratio = value/unit;
503  if ((ratio>=1.)&&(ratio<rsup)) {rsup=ratio; ksup=k;}
504  if ((ratio< 1.)&&(ratio>rinf)) {rinf=ratio; kinf=k;}
505  }
506  }
507 
508  G4int index=ksup;
509  if(index==-1) { index=kinf; }
510  if(index==-1) { index=0; }
511 
512  for (G4int j=0; j<a.nbOfVals; j++)
513  { flux << a.Value[j]/(List[index]->GetValue()) << " "; }
514 
515  std::ios::fmtflags oldform = flux.flags();
516 
517  flux.setf(std::ios::left,std::ios::adjustfield);
518  flux << std::setw(len) << List[index]->GetSymbol();
519  flux.flags(oldform);
520 
521  return flux;
522 }
523 
524 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......