Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-10 02:20:13

0001 
0002 #include "Alignment/CocoaModel/interface/ALIUnitsTable.h"
0003 #include <CLHEP/Units/SystemOfUnits.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   using namespace CLHEP;
0118   //Length
0119   std::make_shared<ALIUnitDefinition>("kilometer", "km", "Length", kilometer);
0120   std::make_shared<ALIUnitDefinition>("meter", "m", "Length", meter);
0121   std::make_shared<ALIUnitDefinition>("centimeter", "cm", "Length", centimeter);
0122   std::make_shared<ALIUnitDefinition>("millimeter", "mm", "Length", millimeter);
0123   std::make_shared<ALIUnitDefinition>("micrometer", "mum", "Length", micrometer);
0124   std::make_shared<ALIUnitDefinition>("nanometer", "nm", "Length", nanometer);
0125   std::make_shared<ALIUnitDefinition>("angstrom", "Ang", "Length", angstrom);
0126   std::make_shared<ALIUnitDefinition>("fermi", "fm", "Length", fermi);
0127 
0128   //Surface
0129   std::make_shared<ALIUnitDefinition>("kilometer2", "km2", "Surface", kilometer2);
0130   std::make_shared<ALIUnitDefinition>("meter2", "m2", "Surface", meter2);
0131   std::make_shared<ALIUnitDefinition>("centimeter2", "cm2", "Surface", centimeter2);
0132   std::make_shared<ALIUnitDefinition>("millimeter2", "mm2", "Surface", millimeter2);
0133   std::make_shared<ALIUnitDefinition>("barn", "barn", "Surface", barn);
0134   std::make_shared<ALIUnitDefinition>("millibarn", "mbarn", "Surface", millibarn);
0135   std::make_shared<ALIUnitDefinition>("microbarn", "mubarn", "Surface", microbarn);
0136   std::make_shared<ALIUnitDefinition>("nanobarn", "nbarn", "Surface", nanobarn);
0137   std::make_shared<ALIUnitDefinition>("picobarn", "pbarn", "Surface", picobarn);
0138 
0139   //Volume
0140   std::make_shared<ALIUnitDefinition>("kilometer3", "km3", "Volume", kilometer3);
0141   std::make_shared<ALIUnitDefinition>("meter3", "m3", "Volume", meter3);
0142   std::make_shared<ALIUnitDefinition>("centimeter3", "cm3", "Volume", centimeter3);
0143   std::make_shared<ALIUnitDefinition>("millimeter3", "mm3", "Volume", millimeter3);
0144 
0145   //Angle
0146   std::make_shared<ALIUnitDefinition>("radian", "rad", "Angle", radian);
0147   std::make_shared<ALIUnitDefinition>("milliradian", "mrad", "Angle", milliradian);
0148   std::make_shared<ALIUnitDefinition>("milliradian", "murad", "Angle", 0.001 * milliradian);
0149   std::make_shared<ALIUnitDefinition>("steradian", "sr", "Angle", steradian);
0150   std::make_shared<ALIUnitDefinition>("degree", "deg", "Angle", degree);
0151 
0152   //Time
0153   std::make_shared<ALIUnitDefinition>("second", "s", "Time", second);
0154   std::make_shared<ALIUnitDefinition>("millisecond", "ms", "Time", millisecond);
0155   std::make_shared<ALIUnitDefinition>("microsecond", "mus", "Time", microsecond);
0156   std::make_shared<ALIUnitDefinition>("nanosecond", "ns", "Time", nanosecond);
0157   std::make_shared<ALIUnitDefinition>("picosecond", "ps", "Time", picosecond);
0158 
0159   //Frequency
0160   std::make_shared<ALIUnitDefinition>("hertz", "Hz", "Frequency", hertz);
0161   std::make_shared<ALIUnitDefinition>("kilohertz", "kHz", "Frequency", kilohertz);
0162   std::make_shared<ALIUnitDefinition>("megahertz", "MHz", "Frequency", megahertz);
0163 
0164   //Electric charge
0165   std::make_shared<ALIUnitDefinition>("eplus", "e+", "Electric charge", eplus);
0166   std::make_shared<ALIUnitDefinition>("coulomb", "C", "Electric charge", coulomb);
0167 
0168   //Energy
0169   std::make_shared<ALIUnitDefinition>("electronvolt", "eV", "Energy", electronvolt);
0170   std::make_shared<ALIUnitDefinition>("kiloelectronvolt", "keV", "Energy", kiloelectronvolt);
0171   std::make_shared<ALIUnitDefinition>("megaelectronvolt", "MeV", "Energy", megaelectronvolt);
0172   std::make_shared<ALIUnitDefinition>("gigaelectronvolt", "GeV", "Energy", gigaelectronvolt);
0173   std::make_shared<ALIUnitDefinition>("teraelectronvolt", "TeV", "Energy", teraelectronvolt);
0174   std::make_shared<ALIUnitDefinition>("petaelectronvolt", "PeV", "Energy", petaelectronvolt);
0175   std::make_shared<ALIUnitDefinition>("joule", "J", "Energy", joule);
0176 
0177   //Mass
0178   std::make_shared<ALIUnitDefinition>("milligram", "mg", "Mass", milligram);
0179   std::make_shared<ALIUnitDefinition>("gram", "g", "Mass", gram);
0180   std::make_shared<ALIUnitDefinition>("kilogram", "kg", "Mass", kilogram);
0181 
0182   //Volumic Mass
0183   std::make_shared<ALIUnitDefinition>("g/cm3", "g/cm3", "Volumic Mass", g / cm3);
0184   std::make_shared<ALIUnitDefinition>("mg/cm3", "mg/cm3", "Volumic Mass", mg / cm3);
0185   std::make_shared<ALIUnitDefinition>("kg/m3", "kg/m3", "Volumic Mass", kg / m3);
0186 
0187   //Power
0188   std::make_shared<ALIUnitDefinition>("watt", "W", "Power", watt);
0189 
0190   //Force
0191   std::make_shared<ALIUnitDefinition>("newton", "N", "Force", newton);
0192 
0193   //Pressure
0194   std::make_shared<ALIUnitDefinition>("pascal", "Pa", "Pressure", pascal);
0195   std::make_shared<ALIUnitDefinition>("bar", "bar", "Pressure", bar);
0196   std::make_shared<ALIUnitDefinition>("atmosphere", "atm", "Pressure", atmosphere);
0197 
0198   //Electric current
0199   std::make_shared<ALIUnitDefinition>("ampere", "A", "Electric current", ampere);
0200   std::make_shared<ALIUnitDefinition>("milliampere", "mA", "Electric current", milliampere);
0201   std::make_shared<ALIUnitDefinition>("microampere", "muA", "Electric current", microampere);
0202   std::make_shared<ALIUnitDefinition>("nanoampere", "nA", "Electric current", nanoampere);
0203 
0204   //Electric potential
0205   std::make_shared<ALIUnitDefinition>("volt", "V", "Electric potential", volt);
0206   std::make_shared<ALIUnitDefinition>("kilovolt", "kV", "Electric potential", kilovolt);
0207   std::make_shared<ALIUnitDefinition>("megavolt", "MV", "Electric potential", megavolt);
0208 
0209   //Magnetic flux
0210   std::make_shared<ALIUnitDefinition>("weber", "Wb", "Magnetic flux", weber);
0211 
0212   //Magnetic flux density
0213   std::make_shared<ALIUnitDefinition>("tesla", "T", "Magnetic flux density", tesla);
0214   std::make_shared<ALIUnitDefinition>("kilogauss", "kG", "Magnetic flux density", kilogauss);
0215   std::make_shared<ALIUnitDefinition>("gauss", "G", "Magnetic flux density", gauss);
0216 
0217   //Temperature
0218   std::make_shared<ALIUnitDefinition>("kelvin", "K", "Temperature", kelvin);
0219 
0220   //Amount of substance
0221   std::make_shared<ALIUnitDefinition>("mole", "mol", "Amount of substance", mole);
0222 
0223   //Activity
0224   std::make_shared<ALIUnitDefinition>("becquerel", "Bq", "Activity", becquerel);
0225   std::make_shared<ALIUnitDefinition>("curie", "Ci", "Activity", curie);
0226 
0227   //Dose
0228   std::make_shared<ALIUnitDefinition>("gray", "Gy", "Dose", gray);
0229 }
0230 
0231 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0232 
0233 void ALIUnitDefinition::PrintUnitsTable() {
0234   std::cout << "\n          ----- The Table of Units ----- \n";
0235   for (size_t i = 0; i < theUnitsTable.size(); i++)
0236     theUnitsTable[i]->PrintCategory();
0237 }
0238 
0239 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0240 
0241 ALIUnitsCategory::ALIUnitsCategory(ALIstring name) : Name(name), NameMxLen(0), SymbMxLen(0) {
0242   UnitsList = *(new ALIUnitsContainer);
0243 }
0244 
0245 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0246 
0247 ALIUnitsCategory::~ALIUnitsCategory() {}
0248 
0249 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0250 
0251 ALIUnitsCategory::ALIUnitsCategory(ALIUnitsCategory& right) { *this = right; }
0252 
0253 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0254 
0255 ALIUnitsCategory& ALIUnitsCategory::operator=(const ALIUnitsCategory& right) {
0256   if (this != &right) {
0257     Name = right.Name;
0258     UnitsList = right.UnitsList;
0259     NameMxLen = right.NameMxLen;
0260     SymbMxLen = right.SymbMxLen;
0261   }
0262   return *this;
0263 }
0264 
0265 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0266 
0267 ALIint ALIUnitsCategory::operator==(const ALIUnitsCategory& right) const { return (this == &right); }
0268 
0269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0270 
0271 ALIint ALIUnitsCategory::operator!=(const ALIUnitsCategory& right) const { return (this != &right); }
0272 
0273 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0274 
0275 void ALIUnitsCategory::PrintCategory() {
0276   std::cout << "\n  category: " << Name << std::endl;
0277   for (size_t i = 0; i < UnitsList.size(); i++)
0278     UnitsList[i]->PrintDefinition();
0279 }
0280 
0281 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0282 
0283 ALIBestUnit::ALIBestUnit(ALIdouble value, ALIstring category) {
0284   // find the category
0285   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0286   size_t nbCat = theUnitsTable.size();
0287   size_t i = 0;
0288   while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
0289     i++;
0290   if (i == nbCat) {
0291     std::cout << " ALIBestUnit: the category " << category << " does not exist !!" << std::endl;
0292     std::cerr << "Missing unit category !" << std::endl;
0293     abort();
0294   }
0295   //
0296   IndexOfCategory = i;
0297   nbOfVals = 1;
0298   Value[0] = value;
0299   Value[1] = 0.;
0300   Value[2] = 0.;
0301 }
0302 
0303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0304 
0305 ALIBestUnit::ALIBestUnit(const CLHEP::Hep3Vector& value, ALIstring category) {
0306   // find the category
0307   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0308   size_t nbCat = theUnitsTable.size();
0309   size_t i = 0;
0310   while ((i < nbCat) && (theUnitsTable[i]->GetName() != category))
0311     i++;
0312   if (i == nbCat) {
0313     std::cerr << " ALIBestUnit: the category " << category << " does not exist." << std::endl;
0314     std::cerr << "Unit category not existing !" << std::endl;
0315     abort();
0316   }
0317   //
0318   IndexOfCategory = i;
0319   nbOfVals = 3;
0320   Value[0] = value.x();
0321   Value[1] = value.y();
0322   Value[2] = value.z();
0323 }
0324 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0325 
0326 ALIBestUnit::~ALIBestUnit() {}
0327 
0328 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0329 
0330 std::ostream& operator<<(std::ostream& flux, ALIBestUnit a) {
0331   ALIUnitsTable& theUnitsTable = ALIUnitDefinition::GetUnitsTable();
0332   ALIUnitsContainer& List = theUnitsTable[a.IndexOfCategory]->GetUnitsList();
0333   ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
0334 
0335   ALIint ksup(-1), kinf(-1);
0336   ALIdouble umax(0.), umin(1.E12);
0337   ALIdouble rsup(1.E12), rinf(0.);
0338 
0339   //for a ThreeVector, choose the best unit for the biggest value
0340   ALIdouble value = std::max(std::max(std::abs(a.Value[0]), std::abs(a.Value[1])), std::abs(a.Value[2]));
0341 
0342   for (size_t k = 0; k < List.size(); k++) {
0343     ALIdouble unit = List[k]->GetValue();
0344     if (value == 1.E12) {
0345       if (unit > umax) {
0346         umax = unit;
0347         ksup = k;
0348       }
0349     } else if (value <= -1.E12) {
0350       if (unit < umin) {
0351         umin = unit;
0352         kinf = k;
0353       }
0354     }
0355 
0356     else {
0357       ALIdouble ratio = value / unit;
0358       if ((ratio >= 1.) && (ratio < rsup)) {
0359         rsup = ratio;
0360         ksup = k;
0361       }
0362       if ((ratio < 1.) && (ratio > rinf)) {
0363         rinf = ratio;
0364         kinf = k;
0365       }
0366     }
0367   }
0368 
0369   ALIint index = ksup;
0370   if (index == -1)
0371     index = kinf;
0372   if (index == -1)
0373     index = 0;
0374 
0375   for (ALIint j = 0; j < a.nbOfVals; j++) {
0376     flux << a.Value[j] / (List[index]->GetValue()) << " ";
0377   }
0378 
0379 #ifdef ALIUSE_STD_NAMESPACE
0380   std::ios::fmtflags oldform = std::cout.flags();
0381 #else
0382   //    ALIint oldform = std::cout.flags();
0383 #endif
0384 
0385   flux.setf(std::ios::left, std::ios::adjustfield);
0386   flux << std::setw(len) << List[index]->GetSymbol();
0387   //??  flux.flags(oldform);
0388 
0389   return flux;
0390 }
0391 
0392 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....