CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/Alignment/CocoaToDDL/src/CocoaUnitsTable.cc

Go to the documentation of this file.
00001  
00002 #include "Alignment/CocoaToDDL/interface/CocoaUnitsTable.h"
00003 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00004 
00005 #include <iomanip>
00006 #include <math.h>
00007 
00008 CocoaUnitsTable      CocoaUnitDefinition::theUnitsTable;
00009 
00010 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00011  
00012 CocoaUnitDefinition::CocoaUnitDefinition(const ALIstring& name, const ALIstring& symbol,
00013                                    const ALIstring& category, ALIdouble value)
00014   : Name(name),SymbolName(symbol),Value(value)                             
00015 {
00016     //
00017     //does the Category objet already exist ?
00018     size_t nbCat = theUnitsTable.size();
00019     size_t i = 0;
00020     while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) i++;
00021     if (i == nbCat) theUnitsTable.push_back( new CocoaUnitsCategory(category));
00022     CategoryIndex = i;
00023     //
00024     //insert this Unit in the Unitstable
00025     (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this);
00026     
00027     //update string max length for name and symbol
00028     theUnitsTable[i]->UpdateNameMxLen((ALIint)name.length());
00029     theUnitsTable[i]->UpdateSymbMxLen((ALIint)symbol.length());
00030 }
00031 
00032 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00033  
00034 CocoaUnitDefinition::~CocoaUnitDefinition()
00035 {
00036   for (size_t i=0;i<theUnitsTable.size();i++)
00037   {
00038     delete theUnitsTable[i];
00039   }
00040 }
00041 
00042 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00043  
00044 CocoaUnitDefinition::CocoaUnitDefinition(const CocoaUnitDefinition& right)
00045 {
00046     *this = right;
00047 }
00048 
00049 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00050  
00051 CocoaUnitDefinition& CocoaUnitDefinition::operator=(const CocoaUnitDefinition& right)
00052 {
00053   if (this != &right)
00054     {
00055       Name          = right.Name;
00056       SymbolName    = right.SymbolName;
00057       Value         = right.Value;
00058       CategoryIndex = right.CategoryIndex;
00059     }
00060   return *this;
00061 }
00062 
00063 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00064  
00065 ALIint CocoaUnitDefinition::operator==(const CocoaUnitDefinition& right) const
00066 {
00067   return (this == (CocoaUnitDefinition *) &right);
00068 }
00069 
00070 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00071  
00072 ALIint CocoaUnitDefinition::operator!=(const CocoaUnitDefinition &right) const
00073 {
00074   return (this != (CocoaUnitDefinition *) &right);
00075 }
00076 
00077 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00078  
00079 CocoaUnitsTable& CocoaUnitDefinition::GetUnitsTable()
00080 {
00081   return theUnitsTable;
00082 }
00083  
00084 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00085  
00086 ALIdouble CocoaUnitDefinition::GetValueOf(const ALIstring& str)
00087 {
00088   if(theUnitsTable.size()==0) BuildUnitsTable();
00089   ALIstring name,symbol;
00090   for (size_t i=0;i<theUnitsTable.size();i++)
00091      { CocoaUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
00092        for (size_t j=0;j<units.size();j++)
00093           { name=units[j]->GetName(); symbol=units[j]->GetSymbol();
00094             if(str==name||str==symbol) 
00095                return units[j]->GetValue();
00096           }
00097      }
00098   std::cout << "Warning from CocoaUnitDefinition::GetValueOf(" << str << ")."
00099        << " The unit " << str << " does not exist in UnitsTable."
00100        << " Return Value = 0." << std::endl;     
00101   return 0.;             
00102 }
00103 
00104 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00105   
00106 ALIstring CocoaUnitDefinition::GetCategory(const ALIstring& str)
00107 {
00108   if(theUnitsTable.size()==0) BuildUnitsTable();
00109   ALIstring name,symbol;
00110   for (size_t i=0;i<theUnitsTable.size();i++)
00111      { CocoaUnitsContainer& units = theUnitsTable[i]->GetUnitsList();
00112        for (size_t j=0;j<units.size();j++)
00113           { name=units[j]->GetName(); symbol=units[j]->GetSymbol();
00114             if(str==name||str==symbol) 
00115                return theUnitsTable[i]->GetName();
00116           }
00117      }
00118   std::cout << "Warning from CocoaUnitDefinition::GetCategory(" << str << ")."
00119        << " The unit " << str << " does not exist in UnitsTable."
00120        << " Return category = None" << std::endl;
00121   name = "None";     
00122   return name;             
00123 }
00124 
00125 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00126  
00127 void CocoaUnitDefinition::PrintDefinition()
00128 {
00129   ALIint nameL = theUnitsTable[CategoryIndex]->GetNameMxLen();
00130   ALIint symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen();
00131   std::cout << std::setw(nameL) << Name << " (" 
00132          << std::setw(symbL) << SymbolName << ") = " << Value << std::endl;
00133 }
00134 
00135 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00136  
00137 void CocoaUnitDefinition::BuildUnitsTable()
00138 {
00139  //Length
00140  new CocoaUnitDefinition(     "meter","m"       ,"Length",meter);
00141  new CocoaUnitDefinition("centimeter","cm"      ,"Length",centimeter); 
00142  new CocoaUnitDefinition("millimeter","mm"      ,"Length",millimeter);
00143  // new CocoaUnitDefinition("micrometer","um"      ,"Length",micrometer);
00144  new CocoaUnitDefinition("micrometer","mum"     ,"Length",micrometer);
00145  new CocoaUnitDefinition( "nanometer","nm"      ,"Length",nanometer);
00146  new CocoaUnitDefinition(  "angstrom","Ang"     ,"Length",angstrom);    
00147  new CocoaUnitDefinition(     "fermi","fm"      ,"Length",fermi);
00148  
00149  //Surface
00150  new CocoaUnitDefinition( "kilometer2","km2"    ,"Surface",kilometer2);
00151  new CocoaUnitDefinition(     "meter2","m2"     ,"Surface",meter2);
00152  new CocoaUnitDefinition("centimeter2","cm2"    ,"Surface",centimeter2); 
00153  new CocoaUnitDefinition("millimeter2","mm2"    ,"Surface",millimeter2);
00154  new CocoaUnitDefinition(       "barn","barn"   ,"Surface",barn);
00155  new CocoaUnitDefinition(  "millibarn","mbarn"  ,"Surface",millibarn);   
00156  new CocoaUnitDefinition(  "microbarn","mubarn" ,"Surface",microbarn);
00157  new CocoaUnitDefinition(   "nanobarn","nbarn"  ,"Surface",nanobarn);
00158  new CocoaUnitDefinition(   "picobarn","pbarn"  ,"Surface",picobarn);
00159  
00160  //Volume
00161  new CocoaUnitDefinition( "kilometer3","km3"    ,"Volume",kilometer3);
00162  new CocoaUnitDefinition(     "meter3","m3"     ,"Volume",meter3);
00163  new CocoaUnitDefinition("centimeter3","cm3"    ,"Volume",centimeter3); 
00164  new CocoaUnitDefinition("millimeter3","mm3"    ,"Volume",millimeter3);
00165 
00166  //Angle
00167  new CocoaUnitDefinition(     "radian","rad"    ,"Angle",radian);
00168  new CocoaUnitDefinition("milliradian","mrad"   ,"Angle",milliradian); 
00169  new CocoaUnitDefinition(  "steradian","sr"     ,"Angle",steradian);
00170  new CocoaUnitDefinition(     "degree","deg"    ,"Angle",degree);
00171  
00172  //Time
00173  new CocoaUnitDefinition(     "second","s"      ,"Time",second);
00174  new CocoaUnitDefinition("millisecond","ms"     ,"Time",millisecond);
00175  new CocoaUnitDefinition("microsecond","mus"    ,"Time",microsecond);
00176  new CocoaUnitDefinition( "nanosecond","ns"     ,"Time",nanosecond);
00177  new CocoaUnitDefinition( "picosecond","ps"     ,"Time",picosecond);
00178  
00179  //Frequency
00180  new CocoaUnitDefinition(    "hertz","Hz"       ,"Frequency",hertz);
00181  new CocoaUnitDefinition("kilohertz","kHz"      ,"Frequency",kilohertz);
00182  new CocoaUnitDefinition("megahertz","MHz"      ,"Frequency",megahertz);
00183  
00184  //Electric charge
00185  new CocoaUnitDefinition(  "eplus","e+"         ,"Electric charge",eplus);
00186  new CocoaUnitDefinition("coulomb","C"          ,"Electric charge",coulomb); 
00187  
00188  //Energy
00189  new CocoaUnitDefinition(    "electronvolt","eV" ,"Energy",electronvolt);
00190  new CocoaUnitDefinition("kiloelectronvolt","keV","Energy",kiloelectronvolt);
00191  new CocoaUnitDefinition("megaelectronvolt","MeV","Energy",megaelectronvolt);
00192  new CocoaUnitDefinition("gigaelectronvolt","GeV","Energy",gigaelectronvolt);
00193  new CocoaUnitDefinition("teraelectronvolt","TeV","Energy",teraelectronvolt);
00194  new CocoaUnitDefinition("petaelectronvolt","PeV","Energy",petaelectronvolt);
00195  new CocoaUnitDefinition(           "joule","J"  ,"Energy",joule);
00196  
00197  //Mass
00198  new CocoaUnitDefinition("milligram","mg","Mass",milligram);
00199  new CocoaUnitDefinition(     "gram","g" ,"Mass",gram);
00200  new CocoaUnitDefinition( "kilogram","kg","Mass",kilogram);
00201  
00202  //Volumic Mass
00203  new CocoaUnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3);
00204  new CocoaUnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3);
00205  new CocoaUnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3);
00206  
00207  //Power
00208  new CocoaUnitDefinition("watt","W","Power",watt);
00209  
00210  //Force
00211  new CocoaUnitDefinition("newton","N","Force",newton);
00212  
00213  //Pressure
00214  new CocoaUnitDefinition(    "pascal","Pa" ,"Pressure",pascal);
00215  new CocoaUnitDefinition(       "bar","bar","Pressure",bar); 
00216  new CocoaUnitDefinition("atmosphere","atm","Pressure",atmosphere);
00217  
00218  //Electric current
00219  new CocoaUnitDefinition(     "ampere","A"  ,"Electric current",ampere);
00220  new CocoaUnitDefinition("milliampere","mA" ,"Electric current",milliampere);
00221  new CocoaUnitDefinition("microampere","muA","Electric current",microampere);
00222  new CocoaUnitDefinition( "nanoampere","nA" ,"Electric current",nanoampere);   
00223  
00224  //Electric potential
00225  new CocoaUnitDefinition(    "volt","V" ,"Electric potential",volt); 
00226  new CocoaUnitDefinition("kilovolt","kV","Electric potential",kilovolt);
00227  new CocoaUnitDefinition("megavolt","MV","Electric potential",megavolt);
00228  
00229  //Magnetic flux
00230  new CocoaUnitDefinition("weber","Wb","Magnetic flux",weber);
00231  
00232  //Magnetic flux density
00233  new CocoaUnitDefinition(    "tesla","T" ,"Magnetic flux density",tesla);
00234  new CocoaUnitDefinition("kilogauss","kG","Magnetic flux density",kilogauss);
00235  new CocoaUnitDefinition(    "gauss","G" ,"Magnetic flux density",gauss);
00236  
00237  //Temperature
00238  new CocoaUnitDefinition("kelvin","K","Temperature",kelvin);
00239  
00240  //Amount of substance
00241  new CocoaUnitDefinition("mole","mol","Amount of substance",mole);
00242  
00243  //Activity
00244  new CocoaUnitDefinition("becquerel","Bq","Activity",becquerel);
00245  new CocoaUnitDefinition(    "curie","Ci","Activity",curie);
00246  
00247  //Dose
00248  new CocoaUnitDefinition("gray","Gy","Dose",gray);                          
00249 }
00250 
00251 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00252  
00253 void CocoaUnitDefinition::PrintUnitsTable()
00254 {
00255   std::cout << "\n          ----- The Table of Units ----- \n";
00256   for(size_t i=0;i<theUnitsTable.size();i++)
00257   {
00258     theUnitsTable[i]->PrintCategory();
00259   }
00260 }
00261 
00262 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00263    
00264 CocoaUnitsCategory::CocoaUnitsCategory(const ALIstring& name)
00265   : Name(name),UnitsList(),NameMxLen(0),SymbMxLen(0)
00266 {
00267 }
00268 
00269 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00270  
00271 CocoaUnitsCategory::~CocoaUnitsCategory()
00272 {
00273 }
00274 
00275 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00276  
00277 CocoaUnitsCategory::CocoaUnitsCategory(const CocoaUnitsCategory& right)
00278 {
00279   *this = right;
00280 }
00281 
00282 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00283  
00284 CocoaUnitsCategory& CocoaUnitsCategory::operator=(const CocoaUnitsCategory& right)
00285 {
00286   if (this != &right)
00287     {
00288       Name      = right.Name;
00289       UnitsList = right.UnitsList;
00290       NameMxLen = right.NameMxLen;
00291       SymbMxLen = right.SymbMxLen;
00292     }
00293   return *this;
00294 }
00295 
00296 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00297  
00298 ALIint CocoaUnitsCategory::operator==(const CocoaUnitsCategory& right) const
00299 {
00300   return (this == (CocoaUnitsCategory *) &right);
00301 }
00302 
00303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00304  
00305 ALIint CocoaUnitsCategory::operator!=(const CocoaUnitsCategory &right) const
00306 {
00307   return (this != (CocoaUnitsCategory *) &right);
00308 }
00309 
00310 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00311  
00312 void CocoaUnitsCategory::PrintCategory()
00313 {
00314   std::cout << "\n  category: " << Name << std::endl;
00315   for(size_t i=0;i<UnitsList.size();i++)
00316       UnitsList[i]->PrintDefinition();
00317 }
00318 
00319 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00320        
00321 CocoaBestUnit::CocoaBestUnit(ALIdouble value, const ALIstring& category)
00322 {
00323  // find the category
00324     CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
00325     if( theUnitsTable.size() == 0 ) CocoaUnitDefinition::BuildUnitsTable(); //t should be done somewhere else
00326     size_t nbCat = theUnitsTable.size();
00327     size_t i = 0;
00328     while
00329      ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) i++;
00330     if (i == nbCat) 
00331        { std::cout << " CocoaBestUnit: the category " << category 
00332                    << " does not exist !!" << nbCat << std::endl;
00333        std::exception();//"Missing unit category !");
00334        }  
00335   //
00336     IndexOfCategory = i;
00337     nbOfVals = 1;
00338     Value[0] = value; Value[1] = 0.; Value[2] = 0.;
00339 
00340     //COCOA internal units are in meters, not mm as in CLHEP
00341     if(category == "Length" ) {
00342       Value[0] *= 1000.;
00343       Value[1] *= 1000.;
00344       Value[2] *= 1000.;
00345     }
00346 }
00347 
00348 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00349        
00350 CocoaBestUnit::CocoaBestUnit(const CLHEP::Hep3Vector& value, const ALIstring& category)
00351 {
00352  // find the category
00353     CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
00354     size_t nbCat = theUnitsTable.size();
00355     size_t i = 0;
00356     while
00357      ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) i++;
00358     if (i == nbCat) 
00359        { std::cerr << " CocoaBestUnit: the category " << category 
00360                 << " does not exist." << std::endl;
00361        std::exception();//"Unit category not existing !");
00362        }  
00363   //
00364     IndexOfCategory = i;
00365     nbOfVals = 3;
00366     Value[0] = value.x();
00367     Value[1] = value.y();
00368     Value[2] = value.z();
00369 }
00370 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00371  
00372 CocoaBestUnit::~CocoaBestUnit()
00373 {}
00374 
00375 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00376  
00377 std::ostream& operator<<(std::ostream& flux, CocoaBestUnit a)
00378 {
00379   CocoaUnitsTable& theUnitsTable = CocoaUnitDefinition::GetUnitsTable();
00380   CocoaUnitsContainer& List = theUnitsTable[a.IndexOfCategory]
00381                            ->GetUnitsList();
00382   ALIint len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen();
00383                            
00384   ALIint    ksup(-1), kinf(-1);
00385   ALIdouble umax(0.), umin(ALI_DBL_MAX);
00386   ALIdouble rsup(ALI_DBL_MAX), rinf(0.);
00387 
00388   //for a ThreeVector, choose the best unit for the biggest value 
00389   ALIdouble value = std::max(std::max(fabs(a.Value[0]),fabs(a.Value[1])),
00390                               fabs(a.Value[2]));
00391 
00392   for (size_t k=0; k<List.size(); k++)
00393      {
00394        ALIdouble unit = List[k]->GetValue();
00395             if (value==ALI_DBL_MAX) {if(unit>umax) {umax=unit; ksup=k;}}
00396        else if (value<=ALI_DBL_MIN) {if(unit<umin) {umin=unit; kinf=k;}}
00397        
00398        else { ALIdouble ratio = value/unit;
00399               if ((ratio>=1.)&&(ratio<rsup)) {rsup=ratio; ksup=k;}
00400               if ((ratio< 1.)&&(ratio>rinf)) {rinf=ratio; kinf=k;}
00401             } 
00402      }
00403          
00404   ALIint index=ksup; if(index==-1) index=kinf; if(index==-1) index=0;
00405   
00406   for (ALIint j=0; j<a.nbOfVals; j++) 
00407      {flux << a.Value[j]/(List[index]->GetValue()) << " ";}
00408 
00409   std::ios::fmtflags oldform = flux.flags();
00410 
00411   flux.setf(std::ios::left,std::ios::adjustfield);
00412   flux << std::setw(len) << List[index]->GetSymbol();       
00413   flux.flags(oldform);
00414 
00415   return flux;
00416 }       
00417 
00418 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
00419