CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Alignment/CocoaModel/src/ALIUnitsTable.cc

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