Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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