Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:28

0001 
0002 #include "Alignment/CocoaModel/interface/ALIUnitsTable.h"
0003 #include "CLHEP/Units/GlobalSystemOfUnits.h"
0004 
0005 #include <iomanip>
0006 #include <cstdlib>
0007 #include <cmath>  // include floating-point std::abs functions
0008 
0009 ALIUnitsTable ALIUnitDefinition::theUnitsTable;
0010 
0011 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0012 
0013 ALIUnitDefinition::ALIUnitDefinition(ALIstring name, ALIstring symbol, ALIstring category, ALIdouble value)
0014     : Name(name), SymbolName(symbol), Value(value) {
0015   //
0016   //does the Category objet already exist ?
0017   size_t nbCat = theUnitsTable.size();
0018   size_t i = 0;
0019   while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
0020     i++;
0021   if (i == nbCat)
0022     theUnitsTable.push_back(new ALIUnitsCategory(category));
0023   CategoryIndex = i;
0024   //
0025   //insert this Unit in the Unitstable
0026   (theUnitsTable[CategoryIndex]->GetUnitsList()).emplace_back(shared_from_this());
0027 
0028   //update ALIstring max length for name and symbol
0029   theUnitsTable[i]->UpdateNameMxLen((ALIint)name.length());
0030   theUnitsTable[i]->UpdateSymbMxLen((ALIint)symbol.length());
0031 }
0032 
0033 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0034 
0035 ALIUnitDefinition::~ALIUnitDefinition() {}
0036 
0037 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0038 
0039 ALIUnitDefinition::ALIUnitDefinition(ALIUnitDefinition& right)
0040     : std::enable_shared_from_this<ALIUnitDefinition>(right) {
0041   *this = right;
0042 }
0043 
0044 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0045 
0046 ALIUnitDefinition& ALIUnitDefinition::operator=(const ALIUnitDefinition& right) {
0047   if (this != &right) {
0048     Name = right.Name;
0049     SymbolName = right.SymbolName;
0050     Value = right.Value;
0051     CategoryIndex = right.CategoryIndex;
0052   }
0053   return *this;
0054 }
0055 
0056 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0057 
0058 ALIint ALIUnitDefinition::operator==(const ALIUnitDefinition& right) const { return (this == &right); }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0061 
0062 ALIint ALIUnitDefinition::operator!=(const ALIUnitDefinition& right) const { return (this != &right); }
0063 
0064 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0065 ALIdouble ALIUnitDefinition::GetValueOf(ALIstring stri) {
0066   if (theUnitsTable.empty())
0067     BuildUnitsTable();
0068   ALIstring name, symbol;
0069   for (size_t i = 0; i < theUnitsTable.size(); i++) {
0070     ALIUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
0071     for (size_t j = 0; j < units.size(); j++) {
0072       name = units[j]->GetName();
0073       symbol = units[j]->GetSymbol();
0074       if (stri == name || stri == symbol)
0075         return units[j]->GetValue();
0076     }
0077   }
0078   std::cout << "Warning from ALIUnitDefinition::GetValueOf(" << stri << ")."
0079             << " The unit " << stri << " does not exist in UnitsTable."
0080             << " Return Value = 0." << std::endl;
0081   return 0.;
0082 }
0083 
0084 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0085 
0086 ALIstring ALIUnitDefinition::GetCategory(ALIstring stri) {
0087   if (theUnitsTable.empty())
0088     BuildUnitsTable();
0089   ALIstring name, symbol;
0090   for (size_t i = 0; i < theUnitsTable.size(); i++) {
0091     ALIUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
0092     for (size_t j = 0; j < units.size(); j++) {
0093       name = units[j]->GetName();
0094       symbol = units[j]->GetSymbol();
0095       if (stri == name || stri == symbol)
0096         return theUnitsTable[i]->GetName();
0097     }
0098   }
0099   std::cout << "Warning from ALIUnitDefinition::GetCategory(" << stri << ")."
0100             << " The unit " << stri << " does not exist in UnitsTable."
0101             << " Return category = None" << std::endl;
0102   name = "None";
0103   return name;
0104 }
0105 
0106 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0107 
0108 void ALIUnitDefinition::PrintDefinition() {
0109   ALIint nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
0110   ALIint symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
0111   std::cout << std::setw(nameL) << Name << " (" << std::setw(symbL) << SymbolName << ") = " << Value << std::endl;
0112 }
0113 
0114 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0115 
0116 void ALIUnitDefinition::BuildUnitsTable() {
0117   //Length
0118   std::make_shared<ALIUnitDefinition>("kilometer", "km", "Length", kilometer);
0119   std::make_shared<ALIUnitDefinition>("meter", "m", "Length", meter);
0120   std::make_shared<ALIUnitDefinition>("centimeter", "cm", "Length", centimeter);
0121   std::make_shared<ALIUnitDefinition>("millimeter", "mm", "Length", millimeter);
0122   std::make_shared<ALIUnitDefinition>("micrometer", "mum", "Length", micrometer);
0123   std::make_shared<ALIUnitDefinition>("nanometer", "nm", "Length", nanometer);
0124   std::make_shared<ALIUnitDefinition>("angstrom", "Ang", "Length", angstrom);
0125   std::make_shared<ALIUnitDefinition>("fermi", "fm", "Length", fermi);
0126 
0127   //Surface
0128   std::make_shared<ALIUnitDefinition>("kilometer2", "km2", "Surface", kilometer2);
0129   std::make_shared<ALIUnitDefinition>("meter2", "m2", "Surface", meter2);
0130   std::make_shared<ALIUnitDefinition>("centimeter2", "cm2", "Surface", centimeter2);
0131   std::make_shared<ALIUnitDefinition>("millimeter2", "mm2", "Surface", millimeter2);
0132   std::make_shared<ALIUnitDefinition>("barn", "barn", "Surface", barn);
0133   std::make_shared<ALIUnitDefinition>("millibarn", "mbarn", "Surface", millibarn);
0134   std::make_shared<ALIUnitDefinition>("microbarn", "mubarn", "Surface", microbarn);
0135   std::make_shared<ALIUnitDefinition>("nanobarn", "nbarn", "Surface", nanobarn);
0136   std::make_shared<ALIUnitDefinition>("picobarn", "pbarn", "Surface", picobarn);
0137 
0138   //Volume
0139   std::make_shared<ALIUnitDefinition>("kilometer3", "km3", "Volume", kilometer3);
0140   std::make_shared<ALIUnitDefinition>("meter3", "m3", "Volume", meter3);
0141   std::make_shared<ALIUnitDefinition>("centimeter3", "cm3", "Volume", centimeter3);
0142   std::make_shared<ALIUnitDefinition>("millimeter3", "mm3", "Volume", millimeter3);
0143 
0144   //Angle
0145   std::make_shared<ALIUnitDefinition>("radian", "rad", "Angle", radian);
0146   std::make_shared<ALIUnitDefinition>("milliradian", "mrad", "Angle", milliradian);
0147   std::make_shared<ALIUnitDefinition>("milliradian", "murad", "Angle", 0.001 * milliradian);
0148   std::make_shared<ALIUnitDefinition>("steradian", "sr", "Angle", steradian);
0149   std::make_shared<ALIUnitDefinition>("degree", "deg", "Angle", degree);
0150 
0151   //Time
0152   std::make_shared<ALIUnitDefinition>("second", "s", "Time", second);
0153   std::make_shared<ALIUnitDefinition>("millisecond", "ms", "Time", millisecond);
0154   std::make_shared<ALIUnitDefinition>("microsecond", "mus", "Time", microsecond);
0155   std::make_shared<ALIUnitDefinition>("nanosecond", "ns", "Time", nanosecond);
0156   std::make_shared<ALIUnitDefinition>("picosecond", "ps", "Time", picosecond);
0157 
0158   //Frequency
0159   std::make_shared<ALIUnitDefinition>("hertz", "Hz", "Frequency", hertz);
0160   std::make_shared<ALIUnitDefinition>("kilohertz", "kHz", "Frequency", kilohertz);
0161   std::make_shared<ALIUnitDefinition>("megahertz", "MHz", "Frequency", megahertz);
0162 
0163   //Electric charge
0164   std::make_shared<ALIUnitDefinition>("eplus", "e+", "Electric charge", eplus);
0165   std::make_shared<ALIUnitDefinition>("coulomb", "C", "Electric charge", coulomb);
0166 
0167   //Energy
0168   std::make_shared<ALIUnitDefinition>("electronvolt", "eV", "Energy", electronvolt);
0169   std::make_shared<ALIUnitDefinition>("kiloelectronvolt", "keV", "Energy", kiloelectronvolt);
0170   std::make_shared<ALIUnitDefinition>("megaelectronvolt", "MeV", "Energy", megaelectronvolt);
0171   std::make_shared<ALIUnitDefinition>("gigaelectronvolt", "GeV", "Energy", gigaelectronvolt);
0172   std::make_shared<ALIUnitDefinition>("teraelectronvolt", "TeV", "Energy", teraelectronvolt);
0173   std::make_shared<ALIUnitDefinition>("petaelectronvolt", "PeV", "Energy", petaelectronvolt);
0174   std::make_shared<ALIUnitDefinition>("joule", "J", "Energy", joule);
0175 
0176   //Mass
0177   std::make_shared<ALIUnitDefinition>("milligram", "mg", "Mass", milligram);
0178   std::make_shared<ALIUnitDefinition>("gram", "g", "Mass", gram);
0179   std::make_shared<ALIUnitDefinition>("kilogram", "kg", "Mass", kilogram);
0180 
0181   //Volumic Mass
0182   std::make_shared<ALIUnitDefinition>("g/cm3", "g/cm3", "Volumic Mass", g / cm3);
0183   std::make_shared<ALIUnitDefinition>("mg/cm3", "mg/cm3", "Volumic Mass", mg / cm3);
0184   std::make_shared<ALIUnitDefinition>("kg/m3", "kg/m3", "Volumic Mass", kg / m3);
0185 
0186   //Power
0187   std::make_shared<ALIUnitDefinition>("watt", "W", "Power", watt);
0188 
0189   //Force
0190   std::make_shared<ALIUnitDefinition>("newton", "N", "Force", newton);
0191 
0192   //Pressure
0193   std::make_shared<ALIUnitDefinition>("pascal", "Pa", "Pressure", pascal);
0194   std::make_shared<ALIUnitDefinition>("bar", "bar", "Pressure", bar);
0195   std::make_shared<ALIUnitDefinition>("atmosphere", "atm", "Pressure", atmosphere);
0196 
0197   //Electric current
0198   std::make_shared<ALIUnitDefinition>("ampere", "A", "Electric current", ampere);
0199   std::make_shared<ALIUnitDefinition>("milliampere", "mA", "Electric current", milliampere);
0200   std::make_shared<ALIUnitDefinition>("microampere", "muA", "Electric current", microampere);
0201   std::make_shared<ALIUnitDefinition>("nanoampere", "nA", "Electric current", nanoampere);
0202 
0203   //Electric potential
0204   std::make_shared<ALIUnitDefinition>("volt", "V", "Electric potential", volt);
0205   std::make_shared<ALIUnitDefinition>("kilovolt", "kV", "Electric potential", kilovolt);
0206   std::make_shared<ALIUnitDefinition>("megavolt", "MV", "Electric potential", megavolt);
0207 
0208   //Magnetic flux
0209   std::make_shared<ALIUnitDefinition>("weber", "Wb", "Magnetic flux", weber);
0210 
0211   //Magnetic flux density
0212   std::make_shared<ALIUnitDefinition>("tesla", "T", "Magnetic flux density", tesla);
0213   std::make_shared<ALIUnitDefinition>("kilogauss", "kG", "Magnetic flux density", kilogauss);
0214   std::make_shared<ALIUnitDefinition>("gauss", "G", "Magnetic flux density", gauss);
0215 
0216   //Temperature
0217   std::make_shared<ALIUnitDefinition>("kelvin", "K", "Temperature", kelvin);
0218 
0219   //Amount of substance
0220   std::make_shared<ALIUnitDefinition>("mole", "mol", "Amount of substance", mole);
0221 
0222   //Activity
0223   std::make_shared<ALIUnitDefinition>("becquerel", "Bq", "Activity", becquerel);
0224   std::make_shared<ALIUnitDefinition>("curie", "Ci", "Activity", curie);
0225 
0226   //Dose
0227   std::make_shared<ALIUnitDefinition>("gray", "Gy", "Dose", gray);
0228 }
0229 
0230 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0231 
0232 void ALIUnitDefinition::PrintUnitsTable() {
0233   std::cout << "\n          ----- The Table of Units ----- \n";
0234   for (size_t i = 0; i < theUnitsTable.size(); i++)
0235     theUnitsTable[i]->PrintCategory();
0236 }
0237 
0238 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0239 
0240 ALIUnitsCategory::ALIUnitsCategory(ALIstring name) : Name(name), NameMxLen(0), SymbMxLen(0) {
0241   UnitsList = *(new ALIUnitsContainer);
0242 }
0243 
0244 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0245 
0246 ALIUnitsCategory::~ALIUnitsCategory() {}
0247 
0248 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0249 
0250 ALIUnitsCategory::ALIUnitsCategory(ALIUnitsCategory& right) { *this = right; }
0251 
0252 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0253 
0254 ALIUnitsCategory& ALIUnitsCategory::operator=(const ALIUnitsCategory& right) {
0255   if (this != &right) {
0256     Name = right.Name;
0257     UnitsList = right.UnitsList;
0258     NameMxLen = right.NameMxLen;
0259     SymbMxLen = right.SymbMxLen;
0260   }
0261   return *this;
0262 }
0263 
0264 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0265 
0266 ALIint ALIUnitsCategory::operator==(const ALIUnitsCategory& right) const { return (this == &right); }
0267 
0268 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0269 
0270 ALIint ALIUnitsCategory::operator!=(const ALIUnitsCategory& right) const { return (this != &right); }
0271 
0272 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0273 
0274 void ALIUnitsCategory::PrintCategory() {
0275   std::cout << "\n  category: " << Name << std::endl;
0276   for (size_t i = 0; i < UnitsList.size(); i++)
0277     UnitsList[i]->PrintDefinition();
0278 }
0279 
0280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0281 
0282 ALIBestUnit::ALIBestUnit(ALIdouble value, ALIstring category) {
0283   // find the category
0284   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0285   size_t nbCat = theUnitsTable.size();
0286   size_t i = 0;
0287   while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
0288     i++;
0289   if (i == nbCat) {
0290     std::cout << " ALIBestUnit: the category " << category << " does not exist !!" << std::endl;
0291     std::cerr << "Missing unit category !" << std::endl;
0292     abort();
0293   }
0294   //
0295   IndexOfCategory = i;
0296   nbOfVals = 1;
0297   Value[0] = value;
0298   Value[1] = 0.;
0299   Value[2] = 0.;
0300 }
0301 
0302 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0303 
0304 ALIBestUnit::ALIBestUnit(const CLHEP::Hep3Vector& value, ALIstring category) {
0305   // find the category
0306   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0307   size_t nbCat = theUnitsTable.size();
0308   size_t i = 0;
0309   while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
0310     i++;
0311   if (i == nbCat) {
0312     std::cerr << " ALIBestUnit: the category " << category << " does not exist." << std::endl;
0313     std::cerr << "Unit category not existing !" << std::endl;
0314     abort();
0315   }
0316   //
0317   IndexOfCategory = i;
0318   nbOfVals = 3;
0319   Value[0] = value.x();
0320   Value[1] = value.y();
0321   Value[2] = value.z();
0322 }
0323 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0324 
0325 ALIBestUnit::~ALIBestUnit() {}
0326 
0327 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0328 
0329 std::ostream& operator<<(std::ostream& flux, ALIBestUnit a) {
0330   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0331   ALIUnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
0332   ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
0333 
0334   ALIint ksup(-1), kinf(-1);
0335   ALIdouble umax(0.), umin(1.E12);
0336   ALIdouble rsup(1.E12), rinf(0.);
0337 
0338   //for a ThreeVector, choose the best unit for the biggest value
0339   ALIdouble value = std::max(std::max(std::abs(a.Value[0]), std::abs(a.Value[1])), std::abs(a.Value[2]));
0340 
0341   for (size_t k = 0; k < List.size(); k++) {
0342     ALIdouble unit = List[k]->GetValue();
0343     if (value == 1.E12) {
0344       if (unit > umax) {
0345         umax = unit;
0346         ksup = k;
0347       }
0348     } else if (value <= -1.E12) {
0349       if (unit < umin) {
0350         umin = unit;
0351         kinf = k;
0352       }
0353     }
0354 
0355     else {
0356       ALIdouble ratio = value / unit;
0357       if ((ratio >= 1.) && (ratio < rsup)) {
0358         rsup = ratio;
0359         ksup = k;
0360       }
0361       if ((ratio < 1.) && (ratio > rinf)) {
0362         rinf = ratio;
0363         kinf = k;
0364       }
0365     }
0366   }
0367 
0368   ALIint index = ksup;
0369   if (index == -1)
0370     index = kinf;
0371   if (index == -1)
0372     index = 0;
0373 
0374   for (ALIint j = 0; j < a.nbOfVals; j++) {
0375     flux << a.Value[j] / (List[index]->GetValue()) << " ";
0376   }
0377 
0378 #ifdef ALIUSE_STD_NAMESPACE
0379   std::ios::fmtflags oldform = std::cout.flags();
0380 #else
0381   //    ALIint oldform = std::cout.flags();
0382 #endif
0383 
0384   flux.setf(std::ios::left, std::ios::adjustfield);
0385   flux << std::setw(len) << List[index]->GetSymbol();
0386   //??  flux.flags(oldform);
0387 
0388   return flux;
0389 }
0390 
0391 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....