Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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