Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4UnitsTable class implementation << 27 // 26 // 28 // Author: M.Maire, 17.05.1998 - First version << 27 // $Id$ 29 // Revisions: G.Cosmo, 06.03.2001 - Migrated t << 28 // 30 // ------------------------------------------- << 29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 30 // >> 31 // 17-05-98: first version, M.Maire >> 32 // 05-08-98: angstrom,microbarn,picobarn,petaelectronvolt, M.Maire >> 33 // 13-10-98: units and symbols printed in fixed length, M.Maire >> 34 // 01-03-01: parsec, M.Maire >> 35 // 06-03-01: migration to STL vectors, G.Cosmo >> 36 // 06-05-02: BestUnit operator<< flux instead of G4cout (mma) >> 37 // 12-08-05: cm2/g ("Surface/Mass") (mma) >> 38 // 30-06-05: um for micrometer (mma) >> 39 // 07-02-06: GeV/cm MeV/cm keV/cm eV/cm ("Energy/Length") (mma) >> 40 // 15-02-06: g/cm2 ("Mass/Surface") >> 41 // MeV*cm2/g ..etc.. ("Energy*Surface/Mass") >> 42 // 18-08-06: remove symbol mum (mma) >> 43 // 06-05-08: V/m ("Electric field") (mma) >> 44 // 09-08-10: new category "Solid angle" (mma) >> 45 // >> 46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 47 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 31 48 32 #include <iomanip> 49 #include <iomanip> 33 #include <sstream> 50 #include <sstream> 34 51 35 #include "G4SystemOfUnits.hh" << 36 #include "G4Threading.hh" << 37 #include "G4UnitsTable.hh" 52 #include "G4UnitsTable.hh" >> 53 #include "G4SystemOfUnits.hh" 38 54 39 G4ThreadLocal G4UnitsTable* G4UnitDefinition:: << 55 G4UnitsTable G4UnitDefinition::theUnitsTable; 40 G4ThreadLocal G4bool G4UnitDefinition::unitsTa << 41 << 42 #ifdef G4MULTITHREADED << 43 G4UnitsTable* G4UnitDefinition::pUnitsTableSha << 44 << 45 // ------------------------------------------- << 46 << 47 G4UnitsTable::~G4UnitsTable() << 48 { << 49 for(const auto itr : *this) << 50 { << 51 delete itr; << 52 } << 53 clear(); << 54 } << 55 << 56 #endif << 57 << 58 // ------------------------------------------- << 59 56 60 G4UnitDefinition::G4UnitDefinition(const G4Str << 57 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 58 >> 59 G4UnitDefinition::G4UnitDefinition(const G4String& name, >> 60 const G4String& symbol, 61 const G4Str 61 const G4String& category, G4double value) 62 : Name(name) << 62 : Name(name),SymbolName(symbol),Value(value) 63 , SymbolName(symbol) << 63 { 64 , Value(value) << 64 // Does the Category objet already exist ? >> 65 // >> 66 size_t nbCat = theUnitsTable.size(); >> 67 size_t i = 0; >> 68 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; } >> 69 if (i == nbCat) >> 70 { theUnitsTable.push_back( new G4UnitsCategory(category)); } >> 71 CategoryIndex = i; >> 72 >> 73 // Insert this Unit in the Units table >> 74 // >> 75 (theUnitsTable[CategoryIndex]->GetUnitsList()).push_back(this); >> 76 >> 77 // Update string max length for name and symbol >> 78 // >> 79 theUnitsTable[i]->UpdateNameMxLen((G4int)name.length()); >> 80 theUnitsTable[i]->UpdateSymbMxLen((G4int)symbol.length()); >> 81 } >> 82 >> 83 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 84 >> 85 G4UnitDefinition::~G4UnitDefinition() 65 { 86 { 66 if(pUnitsTable == nullptr) << 67 { << 68 if(unitsTableDestroyed) << 69 { << 70 G4Exception("G4UnitDefinition::G4UnitDef << 71 FatalException, "G4UnitsTabl << 72 } << 73 pUnitsTable = new G4UnitsTable; << 74 #ifdef G4MULTITHREADED << 75 if(G4Threading::IsMasterThread()) << 76 { << 77 pUnitsTableShadow = pUnitsTable; << 78 } << 79 #endif << 80 } << 81 << 82 // Does the Category objet already exist ? << 83 // << 84 std::size_t nbCat = pUnitsTable->size(); << 85 std::size_t i = 0; << 86 while((i < nbCat) && ((*pUnitsTable)[i]->Get << 87 { << 88 ++i; << 89 } << 90 if(i == nbCat) << 91 { << 92 pUnitsTable->push_back(new G4UnitsCategory << 93 } << 94 CategoryIndex = i; << 95 << 96 // Insert this Unit in the Units table << 97 // << 98 ((*pUnitsTable)[CategoryIndex]->GetUnitsList << 99 << 100 // Update string max length for name and sym << 101 // << 102 (*pUnitsTable)[i]->UpdateNameMxLen((G4int) n << 103 (*pUnitsTable)[i]->UpdateSymbMxLen((G4int) s << 104 } 87 } 105 88 106 // ------------------------------------------- << 89 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 107 << 90 108 G4UnitDefinition::G4UnitDefinition(const G4Uni 91 G4UnitDefinition::G4UnitDefinition(const G4UnitDefinition& right) 109 { 92 { 110 *this = right; << 93 *this = right; 111 } 94 } 112 95 113 // ------------------------------------------- << 96 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 114 << 97 115 G4UnitDefinition& G4UnitDefinition::operator=( 98 G4UnitDefinition& G4UnitDefinition::operator=(const G4UnitDefinition& right) 116 { 99 { 117 if(this != &right) << 100 if (this != &right) 118 { << 101 { 119 Name = right.Name; << 102 Name = right.Name; 120 SymbolName = right.SymbolName; << 103 SymbolName = right.SymbolName; 121 Value = right.Value; << 104 Value = right.Value; 122 CategoryIndex = right.CategoryIndex; << 105 CategoryIndex = right.CategoryIndex; 123 } << 106 } 124 return *this; 107 return *this; 125 } 108 } 126 109 127 // ------------------------------------------- << 110 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 128 << 111 129 G4bool G4UnitDefinition::operator==(const G4Un << 112 G4int G4UnitDefinition::operator==(const G4UnitDefinition& right) const 130 { 113 { 131 return (this == (G4UnitDefinition*) &right); << 114 return (this == (G4UnitDefinition *) &right); 132 } 115 } 133 116 134 // ------------------------------------------- << 117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 135 << 118 136 G4bool G4UnitDefinition::operator!=(const G4Un << 119 G4int G4UnitDefinition::operator!=(const G4UnitDefinition &right) const 137 { 120 { 138 return (this != (G4UnitDefinition*) &right); << 121 return (this != (G4UnitDefinition *) &right); 139 } 122 } 140 123 141 // ------------------------------------------- << 124 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 142 << 125 143 G4UnitsTable& G4UnitDefinition::GetUnitsTable( 126 G4UnitsTable& G4UnitDefinition::GetUnitsTable() 144 { 127 { 145 if(pUnitsTable == nullptr) << 128 if(theUnitsTable.size()==0) { BuildUnitsTable(); } 146 { << 129 return theUnitsTable; 147 pUnitsTable = new G4UnitsTable; << 148 } << 149 if(pUnitsTable->empty()) << 150 { << 151 BuildUnitsTable(); << 152 } << 153 #ifdef G4MULTITHREADED << 154 if(G4Threading::IsMasterThread() && pUnitsTa << 155 { << 156 pUnitsTableShadow = pUnitsTable; << 157 } << 158 #endif << 159 return *pUnitsTable; << 160 } 130 } 161 << 131 162 // ------------------------------------------- << 132 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 163 << 133 164 G4bool G4UnitDefinition::IsUnitDefined(const G << 165 { << 166 G4String name, symbol; << 167 for(std::size_t i = 0; i < (GetUnitsTable()) << 168 { << 169 G4UnitsContainer& units = (*pUnitsTable)[i << 170 for(auto& unit : units) << 171 { << 172 name = unit->GetName(); << 173 symbol = unit->GetSymbol(); << 174 if(str == name || str == symbol) << 175 { << 176 return true; << 177 } << 178 } << 179 } << 180 return false; << 181 } << 182 << 183 // ------------------------------------------- << 184 << 185 G4double G4UnitDefinition::GetValueOf(const G4 134 G4double G4UnitDefinition::GetValueOf(const G4String& str) 186 { 135 { 187 G4String name, symbol; << 136 G4String name,symbol; 188 for(std::size_t i = 0; i < (GetUnitsTable()) << 137 for (size_t i=0;i<(GetUnitsTable()).size();i++) 189 { << 138 { 190 G4UnitsContainer& units = (*pUnitsTable)[i << 139 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList(); 191 for(auto& unit : units) << 140 for (size_t j=0;j<units.size();j++) 192 { << 141 { 193 name = unit->GetName(); << 142 name=units[j]->GetName(); symbol=units[j]->GetSymbol(); 194 symbol = unit->GetSymbol(); << 143 if(str==name||str==symbol) 195 if(str == name || str == symbol) << 144 { return units[j]->GetValue(); } 196 { << 145 } 197 return unit->GetValue(); << 146 } 198 } << 199 } << 200 } << 201 std::ostringstream message; 147 std::ostringstream message; 202 message << "The unit '" << str << "' does no << 148 message << "The unit '" << str << "' does not exist in the Units Table."; 203 G4Exception("G4UnitDefinition::GetValueOf()" << 149 G4Exception("G4UnitDefinition::GetValueOf()", "InvalidUnit", 204 message); << 150 JustWarning, message, "Returning Value = 0."); 205 return 0.; << 151 return 0.; 206 } 152 } 207 153 208 // ------------------------------------------- << 154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 209 << 155 210 G4String G4UnitDefinition::GetCategory(const G 156 G4String G4UnitDefinition::GetCategory(const G4String& str) 211 { 157 { 212 G4String name, symbol; << 158 G4String name,symbol; 213 for(std::size_t i = 0; i < (GetUnitsTable()) << 159 for (size_t i=0;i<(GetUnitsTable()).size();i++) 214 { << 160 { 215 G4UnitsContainer& units = (*pUnitsTable)[i << 161 G4UnitsContainer& units = theUnitsTable[i]->GetUnitsList(); 216 for(auto& unit : units) << 162 for (size_t j=0;j<units.size();j++) 217 { << 163 { 218 name = unit->GetName(); << 164 name=units[j]->GetName(); symbol=units[j]->GetSymbol(); 219 symbol = unit->GetSymbol(); << 165 if(str==name||str==symbol) 220 if(str == name || str == symbol) << 166 { return theUnitsTable[i]->GetName(); } 221 { << 167 } 222 return (*pUnitsTable)[i]->GetName(); << 168 } 223 } << 224 } << 225 } << 226 std::ostringstream message; 169 std::ostringstream message; 227 message << "The unit '" << str << "' does no << 170 message << "The unit '" << str << "' does not exist in the Units Table."; 228 G4Exception("G4UnitDefinition::GetCategory() << 171 G4Exception("G4UnitDefinition::GetCategory()", "InvalidUnit", 229 message); << 172 JustWarning, message, "Returning Value = 0."); 230 name = "None"; << 173 name = "None"; 231 return name; << 174 return name; 232 } 175 } 233 176 234 // ------------------------------------------- << 177 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 235 << 178 236 void G4UnitDefinition::PrintDefinition() 179 void G4UnitDefinition::PrintDefinition() 237 { 180 { 238 G4int nameL = (*pUnitsTable)[CategoryIndex]- << 181 G4int nameL = theUnitsTable[CategoryIndex]->GetNameMxLen(); 239 G4int symbL = (*pUnitsTable)[CategoryIndex]- << 182 G4int symbL = theUnitsTable[CategoryIndex]->GetSymbMxLen(); 240 G4cout << std::setw(nameL) << Name << " (" < << 183 G4cout << std::setw(nameL) << Name << " (" 241 << ") = " << Value << G4endl; << 184 << std::setw(symbL) << SymbolName << ") = " << Value << G4endl; 242 } 185 } 243 186 244 // ------------------------------------------- << 187 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 245 << 188 246 void G4UnitDefinition::BuildUnitsTable() 189 void G4UnitDefinition::BuildUnitsTable() 247 { 190 { 248 // Length << 191 //Length 249 new G4UnitDefinition("parsec", "pc", "Length << 192 new G4UnitDefinition( "parsec","pc" ,"Length",parsec); 250 new G4UnitDefinition("kilometer", "km", "Len << 193 new G4UnitDefinition( "kilometer","km" ,"Length",kilometer); 251 new G4UnitDefinition("meter", "m", "Length", << 194 new G4UnitDefinition( "meter","m" ,"Length",meter); 252 new G4UnitDefinition("centimeter", "cm", "Le << 195 new G4UnitDefinition("centimeter","cm" ,"Length",centimeter); 253 new G4UnitDefinition("millimeter", "mm", "Le << 196 new G4UnitDefinition("millimeter","mm" ,"Length",millimeter); 254 new G4UnitDefinition("micrometer", "um", "Le << 197 new G4UnitDefinition("micrometer","um" ,"Length",micrometer); 255 new G4UnitDefinition("nanometer", "nm", "Len << 198 new G4UnitDefinition( "nanometer","nm" ,"Length",nanometer); 256 new G4UnitDefinition("angstrom", "Ang", "Len << 199 new G4UnitDefinition( "angstrom","Ang" ,"Length",angstrom); 257 new G4UnitDefinition("fermi", "fm", "Length" << 200 new G4UnitDefinition( "fermi","fm" ,"Length",fermi); 258 << 201 259 // Surface << 202 //Surface 260 new G4UnitDefinition("kilometer2", "km2", "S << 203 new G4UnitDefinition( "kilometer2","km2" ,"Surface",kilometer2); 261 new G4UnitDefinition("meter2", "m2", "Surfac << 204 new G4UnitDefinition( "meter2","m2" ,"Surface",meter2); 262 new G4UnitDefinition("centimeter2", "cm2", " << 205 new G4UnitDefinition("centimeter2","cm2" ,"Surface",centimeter2); 263 new G4UnitDefinition("millimeter2", "mm2", " << 206 new G4UnitDefinition("millimeter2","mm2" ,"Surface",millimeter2); 264 new G4UnitDefinition("barn", "barn", "Surfac << 207 new G4UnitDefinition( "barn","barn" ,"Surface",barn); 265 new G4UnitDefinition("millibarn", "mbarn", " << 208 new G4UnitDefinition( "millibarn","mbarn" ,"Surface",millibarn); 266 new G4UnitDefinition("microbarn", "mubarn", << 209 new G4UnitDefinition( "microbarn","mubarn" ,"Surface",microbarn); 267 new G4UnitDefinition("nanobarn", "nbarn", "S << 210 new G4UnitDefinition( "nanobarn","nbarn" ,"Surface",nanobarn); 268 new G4UnitDefinition("picobarn", "pbarn", "S << 211 new G4UnitDefinition( "picobarn","pbarn" ,"Surface",picobarn); 269 << 212 270 // Volume << 213 //Volume 271 new G4UnitDefinition("kilometer3", "km3", "V << 214 new G4UnitDefinition( "kilometer3","km3" ,"Volume",kilometer3); 272 new G4UnitDefinition("meter3", "m3", "Volume << 215 new G4UnitDefinition( "meter3","m3" ,"Volume",meter3); 273 new G4UnitDefinition("centimeter3", "cm3", " << 216 new G4UnitDefinition("centimeter3","cm3" ,"Volume",centimeter3); 274 new G4UnitDefinition("millimeter3", "mm3", " << 217 new G4UnitDefinition("millimeter3","mm3" ,"Volume",millimeter3); 275 << 218 276 new G4UnitDefinition("liter", "L", "Volume", << 219 //Angle 277 new G4UnitDefinition("dL", "dL", "Volume", d << 220 new G4UnitDefinition( "radian","rad" ,"Angle",radian); 278 new G4UnitDefinition("cL", "cL", "Volume", c << 221 new G4UnitDefinition("milliradian","mrad" ,"Angle",milliradian); 279 new G4UnitDefinition("mL", "mL", "Volume", m << 222 new G4UnitDefinition( "degree","deg" ,"Angle",degree); 280 << 223 281 // Angle << 224 //Solid angle 282 new G4UnitDefinition("radian", "rad", "Angle << 225 new G4UnitDefinition( "steradian","sr" ,"Solid angle",steradian); 283 new G4UnitDefinition("milliradian", "mrad", << 226 new G4UnitDefinition("millisteradian","msr" ,"Solid angle",steradian*0.001); 284 new G4UnitDefinition("degree", "deg", "Angle << 227 285 << 228 //Time 286 // Solid angle << 229 new G4UnitDefinition( "second","s" ,"Time",second); 287 new G4UnitDefinition("steradian", "sr", "Sol << 230 new G4UnitDefinition("millisecond","ms" ,"Time",millisecond); 288 new G4UnitDefinition("millisteradian", "msr" << 231 new G4UnitDefinition("microsecond","mus" ,"Time",microsecond); 289 steradian * 0.001); << 232 new G4UnitDefinition( "nanosecond","ns" ,"Time",nanosecond); 290 << 233 new G4UnitDefinition( "picosecond","ps" ,"Time",picosecond); 291 // Time << 234 292 new G4UnitDefinition("second", "s", "Time", << 235 //Frequency 293 new G4UnitDefinition("millisecond", "ms", "T << 236 new G4UnitDefinition( "hertz","Hz" ,"Frequency",hertz); 294 new G4UnitDefinition("microsecond", "us", "T << 237 new G4UnitDefinition("kilohertz","kHz" ,"Frequency",kilohertz); 295 new G4UnitDefinition("nanosecond", "ns", "Ti << 238 new G4UnitDefinition("megahertz","MHz" ,"Frequency",megahertz); 296 new G4UnitDefinition("picosecond", "ps", "Ti << 239 297 new G4UnitDefinition("minute", "min", "Time" << 240 //Electric charge 298 new G4UnitDefinition("hour", "h", "Time" << 241 new G4UnitDefinition( "eplus","e+" ,"Electric charge",eplus); 299 new G4UnitDefinition("day", "d", "Time" << 242 new G4UnitDefinition("coulomb","C" ,"Electric charge",coulomb); 300 new G4UnitDefinition("year", "y", "Time" << 243 301 << 244 //Energy 302 // Frequency << 245 new G4UnitDefinition( "electronvolt","eV" ,"Energy",electronvolt); 303 new G4UnitDefinition("hertz", "Hz", "Frequen << 246 new G4UnitDefinition("kiloelectronvolt","keV","Energy",kiloelectronvolt); 304 new G4UnitDefinition("kilohertz", "kHz", "Fr << 247 new G4UnitDefinition("megaelectronvolt","MeV","Energy",megaelectronvolt); 305 new G4UnitDefinition("megahertz", "MHz", "Fr << 248 new G4UnitDefinition("gigaelectronvolt","GeV","Energy",gigaelectronvolt); >> 249 new G4UnitDefinition("teraelectronvolt","TeV","Energy",teraelectronvolt); >> 250 new G4UnitDefinition("petaelectronvolt","PeV","Energy",petaelectronvolt); >> 251 new G4UnitDefinition( "joule","J" ,"Energy",joule); >> 252 >> 253 // Energy/Length >> 254 new G4UnitDefinition( "GeV/cm", "GeV/cm","Energy/Length", GeV/cm); >> 255 new G4UnitDefinition( "MeV/cm", "MeV/cm","Energy/Length", MeV/cm); >> 256 new G4UnitDefinition( "keV/cm", "keV/cm","Energy/Length", keV/cm); >> 257 new G4UnitDefinition( "eV/cm", "eV/cm","Energy/Length", eV/cm); 306 258 307 // Velocity << 259 //Mass 308 new G4UnitDefinition("cm/ns", "cm/ns", "Velo << 260 new G4UnitDefinition("milligram","mg","Mass",milligram); 309 new G4UnitDefinition("mm/ns", "mm/ns", "Velo << 261 new G4UnitDefinition( "gram","g" ,"Mass",gram); 310 new G4UnitDefinition("cm/us", "cm/us", "Velo << 262 new G4UnitDefinition( "kilogram","kg","Mass",kilogram); 311 new G4UnitDefinition("km/s" , "km/s" , "Velo << 263 312 new G4UnitDefinition("cm/ms", "cm/ms", "Velo << 264 //Volumic Mass 313 new G4UnitDefinition( "m/s" , "m/s" , "Velo << 265 new G4UnitDefinition( "g/cm3", "g/cm3","Volumic Mass", g/cm3); 314 new G4UnitDefinition("cm/s" , "cm/s" , "Velo << 266 new G4UnitDefinition("mg/cm3","mg/cm3","Volumic Mass",mg/cm3); 315 new G4UnitDefinition("mm/s" , "mm/s" , "Velo << 267 new G4UnitDefinition("kg/m3", "kg/m3", "Volumic Mass",kg/m3); 316 << 268 317 // Electric charge << 269 // Mass/Surface 318 new G4UnitDefinition("eplus", "e+", "Electri << 270 new G4UnitDefinition( "g/cm2", "g/cm2","Mass/Surface", g/cm2); 319 new G4UnitDefinition("coulomb", "C", "Electr << 271 new G4UnitDefinition( "mg/cm2", "mg/cm2","Mass/Surface", mg/cm2); 320 << 272 new G4UnitDefinition( "kg/cm2", "kg/cm2","Mass/Surface", kg/cm2); 321 // Energy << 273 322 new G4UnitDefinition("electronvolt", "eV", " << 274 // Surface/Mass 323 new G4UnitDefinition("kiloelectronvolt", "ke << 275 new G4UnitDefinition( "cm2/g", "cm2/g","Surface/Mass", cm2/g); 324 new G4UnitDefinition("megaelectronvolt", "Me << 276 325 new G4UnitDefinition("gigaelectronvolt", "Ge << 277 // Energy.Surface/Mass 326 new G4UnitDefinition("teraelectronvolt", "Te << 278 new G4UnitDefinition( "eV*cm2/g", " eV*cm2/g","Energy*Surface/Mass", eV*cm2/g); 327 new G4UnitDefinition("petaelectronvolt", "Pe << 279 new G4UnitDefinition("keV*cm2/g", "keV*cm2/g","Energy*Surface/Mass",keV*cm2/g); 328 new G4UnitDefinition("millielectronVolt", "m << 280 new G4UnitDefinition("MeV*cm2/g", "MeV*cm2/g","Energy*Surface/Mass",MeV*cm2/g); 329 new G4UnitDefinition("joule", "J", "Energy", << 281 new G4UnitDefinition("GeV*cm2/g", "GeV*cm2/g","Energy*Surface/Mass",GeV*cm2/g); 330 << 282 331 //Momentum << 283 //Power 332 new G4UnitDefinition( "eV/c", "eV/c", "Mome << 284 new G4UnitDefinition("watt","W","Power",watt); 333 new G4UnitDefinition("keV/c", "keV/c", "Mome << 285 334 new G4UnitDefinition("MeV/c", "MeV/c", "Mome << 286 //Force 335 new G4UnitDefinition("GeV/c", "GeV/c", "Mome << 287 new G4UnitDefinition("newton","N","Force",newton); 336 new G4UnitDefinition("TeV/c", "TeV/c", "Mome << 288 337 << 289 //Pressure 338 // Energy/Length << 290 new G4UnitDefinition( "pascal","Pa" ,"Pressure",pascal); 339 new G4UnitDefinition("GeV/cm", "GeV/cm", "En << 291 new G4UnitDefinition( "bar","bar","Pressure",bar); 340 new G4UnitDefinition("MeV/cm", "MeV/cm", "En << 292 new G4UnitDefinition("atmosphere","atm","Pressure",atmosphere); 341 new G4UnitDefinition("keV/cm", "keV/cm", "En << 293 342 new G4UnitDefinition("eV/cm", "eV/cm", "Ener << 294 //Electric current 343 << 295 new G4UnitDefinition( "ampere","A" ,"Electric current",ampere); 344 // Mass << 296 new G4UnitDefinition("milliampere","mA" ,"Electric current",milliampere); 345 new G4UnitDefinition("milligram", "mg", "Mas << 297 new G4UnitDefinition("microampere","muA","Electric current",microampere); 346 new G4UnitDefinition("gram", "g", "Mass", gr << 298 new G4UnitDefinition( "nanoampere","nA" ,"Electric current",nanoampere); 347 new G4UnitDefinition("kilogram", "kg", "Mass << 299 348 << 300 //Electric potential 349 // Volumic Mass << 301 new G4UnitDefinition( "volt","V" ,"Electric potential",volt); 350 new G4UnitDefinition("g/cm3", "g/cm3", "Volu << 302 new G4UnitDefinition("kilovolt","kV","Electric potential",kilovolt); 351 new G4UnitDefinition("mg/cm3", "mg/cm3", "Vo << 303 new G4UnitDefinition("megavolt","MV","Electric potential",megavolt); 352 new G4UnitDefinition("kg/m3", "kg/m3", "Volu << 304 353 << 305 //Electric field 354 // Mass/Surface << 306 new G4UnitDefinition( "volt/m","V/m","Electric field",volt/m); 355 new G4UnitDefinition("g/cm2", "g/cm2", "Mass << 307 356 new G4UnitDefinition("mg/cm2", "mg/cm2", "Ma << 308 //Magnetic flux 357 new G4UnitDefinition("kg/cm2", "kg/cm2", "Ma << 309 new G4UnitDefinition("weber","Wb","Magnetic flux",weber); 358 << 310 359 // Surface/Mass << 311 //Magnetic flux density 360 new G4UnitDefinition("cm2/g", "cm2/g", "Surf << 312 new G4UnitDefinition( "tesla","T" ,"Magnetic flux density",tesla); 361 << 313 new G4UnitDefinition("kilogauss","kG","Magnetic flux density",kilogauss); 362 // Energy.Surface/Mass << 314 new G4UnitDefinition( "gauss","G" ,"Magnetic flux density",gauss); 363 new G4UnitDefinition("eV*cm2/g", " eV*cm2/g" << 315 364 eV * cm2 / g); << 316 //Temperature 365 new G4UnitDefinition("keV*cm2/g", "keV*cm2/g << 317 new G4UnitDefinition("kelvin","K","Temperature",kelvin); 366 keV * cm2 / g); << 318 367 new G4UnitDefinition("MeV*cm2/g", "MeV*cm2/g << 319 //Amount of substance 368 MeV * cm2 / g); << 320 new G4UnitDefinition("mole","mol","Amount of substance",mole); 369 new G4UnitDefinition("GeV*cm2/g", "GeV*cm2/g << 321 370 GeV * cm2 / g); << 322 //Activity 371 << 323 new G4UnitDefinition("becquerel","Bq","Activity",becquerel); 372 // Power << 324 new G4UnitDefinition( "curie","Ci","Activity",curie); 373 new G4UnitDefinition("watt", "W", "Power", w << 325 374 << 326 //Dose 375 // Force << 327 new G4UnitDefinition("gray","Gy","Dose",gray); 376 new G4UnitDefinition("newton", "N", "Force", << 377 << 378 // Pressure << 379 new G4UnitDefinition("pascal", "Pa", "Pressu << 380 new G4UnitDefinition("bar", "bar", "Pressure << 381 new G4UnitDefinition("atmosphere", "atm", "P << 382 << 383 // Electric current << 384 new G4UnitDefinition("ampere", "A", "Electri << 385 new G4UnitDefinition("milliampere", "mA", "E << 386 new G4UnitDefinition("microampere", "muA", " << 387 new G4UnitDefinition("nanoampere", "nA", "El << 388 << 389 // Electric potential << 390 new G4UnitDefinition("volt", "V", "Electric << 391 new G4UnitDefinition("kilovolt", "kV", "Elec << 392 new G4UnitDefinition("megavolt", "MV", "Elec << 393 << 394 // Electric field << 395 new G4UnitDefinition("volt/m", "V/m", "Elect << 396 new G4UnitDefinition("kilovolt/m", "kV/m", " << 397 new G4UnitDefinition("megavolt/m", "MV/m", " << 398 << 399 // Magnetic flux << 400 new G4UnitDefinition("weber", "Wb", "Magneti << 401 << 402 // Magnetic flux density << 403 new G4UnitDefinition("tesla", "T", "Magnetic << 404 new G4UnitDefinition("kilogauss", "kG", "Mag << 405 new G4UnitDefinition("gauss", "G", "Magnetic << 406 << 407 // Temperature << 408 new G4UnitDefinition("kelvin", "K", "Tempera << 409 << 410 // Amount of substance << 411 new G4UnitDefinition("mole", "mol", "Amount << 412 new G4UnitDefinition("g/mole", "g/mol", "Mol << 413 << 414 // Activity << 415 new G4UnitDefinition("becquerel", "Bq", "Act << 416 new G4UnitDefinition("curie", "Ci", "Activit << 417 << 418 // Dose << 419 new G4UnitDefinition("gray", "Gy", "Dose", g << 420 } 328 } 421 329 422 // ------------------------------------------- << 330 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 423 << 331 424 void G4UnitDefinition::PrintUnitsTable() 332 void G4UnitDefinition::PrintUnitsTable() 425 { 333 { 426 G4cout << "\n ----- The Table of Un 334 G4cout << "\n ----- The Table of Units ----- \n"; 427 if(pUnitsTable == nullptr) << 335 for(size_t i=0;i<theUnitsTable.size();i++) 428 { 336 { 429 pUnitsTable = new G4UnitsTable; << 337 theUnitsTable[i]->PrintCategory(); 430 } << 431 for(std::size_t i = 0; i < pUnitsTable->size << 432 { << 433 (*pUnitsTable)[i]->PrintCategory(); << 434 } 338 } 435 } 339 } 436 340 437 // ------------------------------------------- << 341 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 438 342 439 void G4UnitDefinition::ClearUnitsTable() 343 void G4UnitDefinition::ClearUnitsTable() 440 { 344 { 441 #ifdef G4MULTITHREADED << 345 for (size_t i=0;i<theUnitsTable.size();i++) 442 delete pUnitsTable; << 443 pUnitsTable = nullptr; << 444 if(G4Threading::IsMasterThread()) << 445 { << 446 pUnitsTableShadow = nullptr; << 447 } << 448 #else << 449 for(std::size_t i = 0; i < pUnitsTable->size << 450 { 346 { 451 delete(*pUnitsTable)[i]; << 347 delete theUnitsTable[i]; 452 } 348 } 453 pUnitsTable->clear(); << 349 theUnitsTable.clear(); 454 #endif << 455 unitsTableDestroyed = true; << 456 } 350 } 457 351 458 // ------------------------------------------- << 352 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 459 << 353 460 G4UnitsCategory::G4UnitsCategory(const G4Strin 354 G4UnitsCategory::G4UnitsCategory(const G4String& name) 461 : Name(name) << 355 : Name(name),UnitsList(),NameMxLen(0),SymbMxLen(0) 462 {} << 356 { 463 << 357 } 464 // ------------------------------------------- << 465 358 >> 359 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... >> 360 466 G4UnitsCategory::~G4UnitsCategory() 361 G4UnitsCategory::~G4UnitsCategory() 467 { 362 { 468 for(auto& i : UnitsList) << 363 for(size_t i=0;i<UnitsList.size();i++) 469 { 364 { 470 delete i; << 365 delete UnitsList[i]; 471 } 366 } 472 UnitsList.clear(); 367 UnitsList.clear(); 473 } 368 } 474 369 475 // ------------------------------------------- << 370 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 476 << 371 477 G4UnitsCategory::G4UnitsCategory(const G4Units 372 G4UnitsCategory::G4UnitsCategory(const G4UnitsCategory& right) 478 { 373 { 479 *this = right; 374 *this = right; 480 } 375 } 481 376 482 // ------------------------------------------- << 377 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 483 << 378 484 G4UnitsCategory& G4UnitsCategory::operator=(co 379 G4UnitsCategory& G4UnitsCategory::operator=(const G4UnitsCategory& right) 485 { 380 { 486 if(this != &right) << 381 if (this != &right) 487 { << 382 { 488 Name = right.Name; << 383 Name = right.Name; 489 UnitsList = right.UnitsList; << 384 UnitsList = right.UnitsList; 490 NameMxLen = right.NameMxLen; << 385 NameMxLen = right.NameMxLen; 491 SymbMxLen = right.SymbMxLen; << 386 SymbMxLen = right.SymbMxLen; 492 } << 387 } 493 return *this; 388 return *this; 494 } 389 } 495 390 496 // ------------------------------------------- << 391 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 497 << 392 498 G4bool G4UnitsCategory::operator==(const G4Uni << 393 G4int G4UnitsCategory::operator==(const G4UnitsCategory& right) const 499 { 394 { 500 return (this == (G4UnitsCategory*) &right); << 395 return (this == (G4UnitsCategory *) &right); 501 } 396 } 502 397 503 // ------------------------------------------- << 398 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 504 << 399 505 G4bool G4UnitsCategory::operator!=(const G4Uni << 400 G4int G4UnitsCategory::operator!=(const G4UnitsCategory &right) const 506 { 401 { 507 return (this != (G4UnitsCategory*) &right); << 402 return (this != (G4UnitsCategory *) &right); 508 } 403 } 509 404 510 // ------------------------------------------- << 405 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 511 << 406 512 void G4UnitsCategory::PrintCategory() 407 void G4UnitsCategory::PrintCategory() 513 { 408 { 514 G4cout << "\n category: " << Name << G4endl 409 G4cout << "\n category: " << Name << G4endl; 515 for(auto& i : UnitsList) << 410 for(size_t i=0;i<UnitsList.size();i++) 516 { << 411 { UnitsList[i]->PrintDefinition(); } 517 i->PrintDefinition(); << 518 } << 519 } 412 } 520 413 521 // ------------------------------------------- << 414 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 522 << 415 523 G4BestUnit::G4BestUnit(G4double value, const G 416 G4BestUnit::G4BestUnit(G4double value, const G4String& category) 524 : nbOfVals(1) 417 : nbOfVals(1) 525 { 418 { 526 // find the category << 419 // find the category 527 G4UnitsTable& theUnitsTable = G4UnitDefiniti << 420 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable(); 528 std::size_t nbCat = theUnitsTable. << 421 size_t nbCat = theUnitsTable.size(); 529 std::size_t i = 0; << 422 size_t i = 0; 530 while((i < nbCat) && (theUnitsTable[i]->GetN << 423 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; } 531 { << 424 if (i == nbCat) 532 ++i; << 425 { 533 } << 426 G4cout << " G4BestUnit: the category " << category 534 if(i == nbCat) << 427 << " does not exist !!" << G4endl; 535 { << 428 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", 536 G4cout << " G4BestUnit: the category " << << 429 FatalException, "Missing unit category !") ; 537 << G4endl; << 430 } 538 G4Exception("G4BestUnit::G4BestUnit()", "I << 431 // 539 "Missing unit category !"); << 432 Value[0] = value; 540 } << 433 Value[1] = 0.; 541 << 434 Value[2] = 0.; 542 Value[0] = value; << 435 IndexOfCategory = i; 543 Value[1] = 0.; << 544 Value[2] = 0.; << 545 Category = category; << 546 IndexOfCategory = i; << 547 } 436 } 548 437 549 // ------------------------------------------- << 438 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 550 << 439 551 G4BestUnit::G4BestUnit(const G4ThreeVector& va 440 G4BestUnit::G4BestUnit(const G4ThreeVector& value, const G4String& category) 552 : nbOfVals(3) 441 : nbOfVals(3) 553 { 442 { 554 // find the category << 443 // find the category 555 G4UnitsTable& theUnitsTable = G4UnitDefiniti << 444 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable(); 556 std::size_t nbCat = theUnitsTable. << 445 size_t nbCat = theUnitsTable.size(); 557 std::size_t i = 0; << 446 size_t i = 0; 558 while((i < nbCat) && (theUnitsTable[i]->GetN << 447 while ((i<nbCat)&&(theUnitsTable[i]->GetName()!=category)) { i++; } 559 { << 448 if (i == nbCat) 560 ++i; << 449 { 561 } << 450 G4cerr << " G4BestUnit: the category " << category 562 if(i == nbCat) << 451 << " does not exist." << G4endl; 563 { << 452 G4Exception("G4BestUnit::G4BestUnit()", "InvalidCall", 564 G4cerr << " G4BestUnit: the category " << << 453 FatalException, "Missing unit category !") ; 565 << G4endl; << 454 } 566 G4Exception("G4BestUnit::G4BestUnit()", "I << 455 // 567 "Missing unit category !"); << 456 Value[0] = value.x(); 568 } << 457 Value[1] = value.y(); 569 << 458 Value[2] = value.z(); 570 Value[0] = value.x(); << 459 IndexOfCategory = i; 571 Value[1] = value.y(); << 460 } 572 Value[2] = value.z(); << 461 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 573 Category = category; << 462 574 IndexOfCategory = i; << 463 G4BestUnit::~G4BestUnit() 575 } << 464 {} 576 465 577 // ------------------------------------------- << 466 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 578 467 579 G4BestUnit::operator G4String() const << 468 G4BestUnit::operator G4String () const 580 { 469 { 581 std::ostringstream oss; 470 std::ostringstream oss; 582 oss << *this; 471 oss << *this; 583 return oss.str(); 472 return oss.str(); 584 } 473 } 585 474 586 // ------------------------------------------- << 475 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 587 << 476 588 std::ostream& operator<<(std::ostream& flux, c << 477 std::ostream& operator<<(std::ostream& flux, G4BestUnit a) 589 { 478 { 590 G4UnitsTable& theUnitsTable = G4UnitDefiniti 479 G4UnitsTable& theUnitsTable = G4UnitDefinition::GetUnitsTable(); 591 G4UnitsContainer& List = theUnitsTable[a.Ind << 480 G4UnitsContainer& List = theUnitsTable[a.IndexOfCategory] 592 G4int len = theUnitsTable[a.Ind << 481 ->GetUnitsList(); 593 << 482 G4int len = theUnitsTable[a.IndexOfCategory]->GetSymbMxLen(); 594 G4long ksup(-1), kinf(-1); << 483 >> 484 G4int ksup(-1), kinf(-1); 595 G4double umax(0.), umin(DBL_MAX); 485 G4double umax(0.), umin(DBL_MAX); 596 G4double rsup(DBL_MAX), rinf(0.); 486 G4double rsup(DBL_MAX), rinf(0.); 597 487 598 // for a ThreeVector, choose the best unit f << 488 //for a ThreeVector, choose the best unit for the biggest value 599 G4double value = << 489 G4double value = std::max(std::max(std::fabs(a.Value[0]), 600 std::max(std::max(std::fabs(a.Value[0]), s << 490 std::fabs(a.Value[1])), 601 std::fabs(a.Value[2])); << 491 std::fabs(a.Value[2])); 602 << 492 603 //special treatement for Energy. << 493 for (size_t k=0; k<List.size(); k++) 604 if ((a.Category == "Energy") && (value == 0. << 494 { 605 for (G4int j = 0; j < a.nbOfVals; ++j) { << 495 G4double unit = List[k]->GetValue(); 606 flux << a.Value[j] << " "; << 496 if (!(value!=DBL_MAX)) 607 } << 497 {if(unit>umax) {umax=unit; ksup=k;}} 608 std::ios::fmtflags oldform = flux.flags(); << 498 else if (value<=DBL_MIN) 609 flux.setf(std::ios::left, std::ios::adjust << 499 {if(unit<umin) {umin=unit; kinf=k;}} 610 flux << std::setw(len) << "eV"; << 500 else 611 flux.flags(oldform); << 501 { 612 return flux; << 502 G4double ratio = value/unit; 613 } << 503 if ((ratio>=1.)&&(ratio<rsup)) {rsup=ratio; ksup=k;} 614 << 504 if ((ratio< 1.)&&(ratio>rinf)) {rinf=ratio; kinf=k;} 615 //here, value != 0. << 505 } 616 for(std::size_t k = 0; k < List.size(); ++k) << 506 } 617 { << 507 618 G4double unit = List[k]->GetValue(); << 508 G4int index=ksup; 619 if(!(value != DBL_MAX)) << 509 if(index==-1) { index=kinf; } 620 { << 510 if(index==-1) { index=0; } 621 if(unit > umax) << 511 622 { << 512 for (G4int j=0; j<a.nbOfVals; j++) 623 umax = unit; << 513 { flux << a.Value[j]/(List[index]->GetValue()) << " "; } 624 ksup = k; << 625 } << 626 } << 627 else if(value <= DBL_MIN) << 628 { << 629 if(unit < umin) << 630 { << 631 umin = unit; << 632 kinf = k; << 633 } << 634 } << 635 else << 636 { << 637 G4double ratio = value / unit; << 638 if((ratio >= 1.) && (ratio < rsup)) << 639 { << 640 rsup = ratio; << 641 ksup = k; << 642 } << 643 if((ratio < 1.) && (ratio > rinf)) << 644 { << 645 rinf = ratio; << 646 kinf = k; << 647 } << 648 } << 649 } << 650 << 651 G4long index = ksup; << 652 if(index == -1) << 653 { << 654 index = kinf; << 655 } << 656 if(index == -1) << 657 { << 658 index = 0; << 659 } << 660 << 661 for(G4int j = 0; j < a.nbOfVals; ++j) << 662 { << 663 flux << a.Value[j] / (List[index]->GetValu << 664 } << 665 514 666 std::ios::fmtflags oldform = flux.flags(); 515 std::ios::fmtflags oldform = flux.flags(); 667 516 668 flux.setf(std::ios::left, std::ios::adjustfi << 517 flux.setf(std::ios::left,std::ios::adjustfield); 669 flux << std::setw(len) << List[index]->GetSy << 518 flux << std::setw(len) << List[index]->GetSymbol(); 670 flux.flags(oldform); 519 flux.flags(oldform); 671 520 672 return flux; 521 return flux; 673 } << 522 } 674 << 675 // ------------------------------------------- << 676 << 677 #ifdef G4MULTITHREADED << 678 << 679 void G4UnitsTable::Synchronize() << 680 { << 681 G4UnitsTable* orig = &(G4UnitDefinition::Get << 682 if(this == orig) << 683 { << 684 return; << 685 } << 686 << 687 for(const auto category : *orig) << 688 { << 689 G4String catName = category->GetN << 690 G4UnitsContainer* units = &(category->Ge << 691 for(const auto unit : *units) << 692 { << 693 if(!Contains(unit, catName)) << 694 { << 695 new G4UnitDefinition(unit->GetName(), << 696 unit->GetValue()) << 697 } << 698 } << 699 } << 700 } << 701 << 702 // ------------------------------------------- << 703 << 704 G4bool G4UnitsTable::Contains(const G4UnitDefi << 705 const G4String& << 706 { << 707 for(const auto category : *this) << 708 { << 709 G4String catName = category->GetName(); << 710 if(catName != categoryName) << 711 { << 712 continue; << 713 } << 714 G4UnitsContainer* units = &(category->GetU << 715 for(const auto ucItr : *units) << 716 { << 717 if(ucItr->GetName() == unit->GetName() & << 718 ucItr->GetSymbol() == unit->GetSymbol << 719 { << 720 return true; << 721 } << 722 } << 723 } << 724 return false; << 725 } << 726 523 727 #endif << 524 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo...... 728 525