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 // G4PhysicsTable class implementation 26 // G4PhysicsTable class implementation 27 // 27 // 28 // Author: G.Cosmo, 2 December 1995 28 // Author: G.Cosmo, 2 December 1995 29 // First implementation based on objec 29 // First implementation based on object model 30 // Revisions: 30 // Revisions: 31 // - 1st March 1996, K.Amako: modified 31 // - 1st March 1996, K.Amako: modified 32 // - 24th February 2001, H.Kurashige: migratio 32 // - 24th February 2001, H.Kurashige: migration to STL vectors 33 // ------------------------------------------- 33 // -------------------------------------------------------------------- 34 34 35 #include <fstream> 35 #include <fstream> 36 #include <iomanip> 36 #include <iomanip> 37 #include <iostream> 37 #include <iostream> 38 38 >> 39 #include "G4LPhysicsFreeVector.hh" 39 #include "G4PhysicsFreeVector.hh" 40 #include "G4PhysicsFreeVector.hh" 40 #include "G4PhysicsLinearVector.hh" 41 #include "G4PhysicsLinearVector.hh" >> 42 #include "G4PhysicsLnVector.hh" 41 #include "G4PhysicsLogVector.hh" 43 #include "G4PhysicsLogVector.hh" >> 44 #include "G4PhysicsOrderedFreeVector.hh" 42 #include "G4PhysicsTable.hh" 45 #include "G4PhysicsTable.hh" 43 #include "G4PhysicsVector.hh" 46 #include "G4PhysicsVector.hh" 44 #include "G4PhysicsVectorType.hh" 47 #include "G4PhysicsVectorType.hh" 45 48 46 // ------------------------------------------- 49 // -------------------------------------------------------------------- >> 50 G4PhysicsTable::G4PhysicsTable() >> 51 : G4PhysCollection() >> 52 {} >> 53 >> 54 // -------------------------------------------------------------------- 47 G4PhysicsTable::G4PhysicsTable(std::size_t cap 55 G4PhysicsTable::G4PhysicsTable(std::size_t cap) >> 56 : G4PhysCollection() 48 { 57 { 49 reserve(cap); 58 reserve(cap); 50 vecFlag.reserve(cap); 59 vecFlag.reserve(cap); 51 } 60 } 52 61 53 // ------------------------------------------- 62 // -------------------------------------------------------------------- 54 G4PhysicsTable::~G4PhysicsTable() 63 G4PhysicsTable::~G4PhysicsTable() 55 { 64 { 56 G4PhysCollection::clear(); 65 G4PhysCollection::clear(); 57 vecFlag.clear(); 66 vecFlag.clear(); 58 } 67 } 59 68 60 // ------------------------------------------- 69 // -------------------------------------------------------------------- 61 void G4PhysicsTable::resize(std::size_t siz, G 70 void G4PhysicsTable::resize(std::size_t siz, G4PhysicsVector* vec) 62 { 71 { 63 G4PhysCollection::resize(siz, vec); 72 G4PhysCollection::resize(siz, vec); 64 vecFlag.resize(siz, true); 73 vecFlag.resize(siz, true); 65 } 74 } 66 75 67 // ------------------------------------------- 76 // -------------------------------------------------------------------- 68 G4bool G4PhysicsTable::StorePhysicsTable(const 77 G4bool G4PhysicsTable::StorePhysicsTable(const G4String& fileName, G4bool ascii) 69 { 78 { 70 std::ofstream fOut; 79 std::ofstream fOut; 71 80 72 // open output file 81 // open output file 73 if(!ascii) 82 if(!ascii) 74 { 83 { 75 fOut.open(fileName, std::ios::out | std::i 84 fOut.open(fileName, std::ios::out | std::ios::binary); 76 } 85 } 77 else 86 else 78 { 87 { 79 fOut.open(fileName, std::ios::out); 88 fOut.open(fileName, std::ios::out); 80 } 89 } 81 90 82 // check if the file has been opened success 91 // check if the file has been opened successfully 83 if(!fOut.is_open()) << 92 if(!fOut) 84 { 93 { 85 #ifdef G4VERBOSE 94 #ifdef G4VERBOSE 86 G4cerr << "G4PhysicsTable::StorePhysicsTab 95 G4cerr << "G4PhysicsTable::StorePhysicsTable():"; 87 G4cerr << " Cannot open file: " << fileNam 96 G4cerr << " Cannot open file: " << fileName << G4endl; 88 #endif 97 #endif 89 fOut.close(); 98 fOut.close(); 90 return false; 99 return false; 91 } 100 } 92 101 93 // Number of elements 102 // Number of elements 94 std::size_t tableSize = size(); 103 std::size_t tableSize = size(); 95 if(!ascii) 104 if(!ascii) 96 { 105 { 97 fOut.write((char*) (&tableSize), sizeof ta 106 fOut.write((char*) (&tableSize), sizeof tableSize); 98 } 107 } 99 else 108 else 100 { 109 { 101 fOut << tableSize << G4endl; 110 fOut << tableSize << G4endl; 102 } 111 } 103 112 104 // Physics Vector 113 // Physics Vector 105 for(const auto itr : *this) << 114 for(auto itr = cbegin(); itr != cend(); ++itr) 106 { 115 { 107 G4int vType = itr->GetType(); << 116 G4int vType = (*itr)->GetType(); 108 if(!ascii) 117 if(!ascii) 109 { 118 { 110 fOut.write((char*) (&vType), sizeof vTyp 119 fOut.write((char*) (&vType), sizeof vType); 111 } 120 } 112 else 121 else 113 { 122 { 114 fOut << vType << G4endl; 123 fOut << vType << G4endl; 115 } 124 } 116 itr->Store(fOut, ascii); << 125 (*itr)->Store(fOut, ascii); 117 } 126 } 118 fOut.close(); 127 fOut.close(); 119 return true; 128 return true; 120 } 129 } 121 130 122 // ------------------------------------------- 131 // -------------------------------------------------------------------- 123 G4bool G4PhysicsTable::ExistPhysicsTable(const 132 G4bool G4PhysicsTable::ExistPhysicsTable(const G4String& fileName) const 124 { 133 { 125 std::ifstream fIn; 134 std::ifstream fIn; 126 G4bool value = true; 135 G4bool value = true; 127 // open input file 136 // open input file 128 fIn.open(fileName, std::ios::in); 137 fIn.open(fileName, std::ios::in); 129 138 130 // check if the file has been opened success 139 // check if the file has been opened successfully 131 if(!fIn) 140 if(!fIn) 132 { 141 { 133 value = false; 142 value = false; 134 } 143 } 135 fIn.close(); 144 fIn.close(); 136 return value; 145 return value; 137 } 146 } 138 147 139 // ------------------------------------------- 148 // -------------------------------------------------------------------- 140 G4bool G4PhysicsTable::RetrievePhysicsTable(co 149 G4bool G4PhysicsTable::RetrievePhysicsTable(const G4String& fileName, 141 G4 << 150 G4bool ascii) 142 { 151 { 143 std::ifstream fIn; 152 std::ifstream fIn; 144 // open input file 153 // open input file 145 if(ascii) 154 if(ascii) 146 { 155 { 147 fIn.open(fileName, std::ios::in | std::ios 156 fIn.open(fileName, std::ios::in | std::ios::binary); 148 } 157 } 149 else 158 else 150 { 159 { 151 fIn.open(fileName, std::ios::in); 160 fIn.open(fileName, std::ios::in); 152 } 161 } 153 162 154 // check if the file has been opened success 163 // check if the file has been opened successfully 155 if(!fIn.is_open()) << 164 if(!fIn) 156 { 165 { 157 #ifdef G4VERBOSE 166 #ifdef G4VERBOSE 158 G4cerr << "G4PhysicsTable::RetrievePhysics 167 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 159 G4cerr << " Cannot open file: " << fileNam 168 G4cerr << " Cannot open file: " << fileName << G4endl; 160 #endif 169 #endif 161 fIn.close(); 170 fIn.close(); 162 return false; 171 return false; 163 } 172 } 164 173 165 // clear 174 // clear 166 clearAndDestroy(); 175 clearAndDestroy(); 167 176 168 // Number of elements 177 // Number of elements 169 std::size_t tableSize = 0; 178 std::size_t tableSize = 0; 170 if(!ascii) 179 if(!ascii) 171 { 180 { 172 fIn.read((char*) (&tableSize), sizeof tabl 181 fIn.read((char*) (&tableSize), sizeof tableSize); 173 } 182 } 174 else 183 else 175 { 184 { 176 fIn >> tableSize; 185 fIn >> tableSize; 177 } 186 } 178 reserve(tableSize); 187 reserve(tableSize); 179 vecFlag.clear(); 188 vecFlag.clear(); 180 189 181 // Physics Vector 190 // Physics Vector 182 for(std::size_t idx = 0; idx < tableSize; ++ 191 for(std::size_t idx = 0; idx < tableSize; ++idx) 183 { 192 { 184 G4int vType = 0; 193 G4int vType = 0; 185 if(!ascii) 194 if(!ascii) 186 { 195 { 187 fIn.read((char*) (&vType), sizeof vType) 196 fIn.read((char*) (&vType), sizeof vType); 188 } 197 } 189 else 198 else 190 { 199 { 191 fIn >> vType; 200 fIn >> vType; 192 } 201 } 193 G4PhysicsVector* pVec = CreatePhysicsVecto << 202 G4PhysicsVector* pVec = CreatePhysicsVector(vType); 194 if(pVec == nullptr) 203 if(pVec == nullptr) 195 { 204 { 196 #ifdef G4VERBOSE 205 #ifdef G4VERBOSE 197 G4cerr << "G4PhysicsTable::RetrievePhysi 206 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 198 G4cerr << " Illegal Physics Vector type: 207 G4cerr << " Illegal Physics Vector type: " << vType << " in: "; 199 G4cerr << fileName << G4endl; 208 G4cerr << fileName << G4endl; 200 #endif 209 #endif 201 fIn.close(); 210 fIn.close(); 202 return false; 211 return false; 203 } 212 } 204 213 205 if(!(pVec->Retrieve(fIn, ascii))) 214 if(!(pVec->Retrieve(fIn, ascii))) 206 { 215 { 207 #ifdef G4VERBOSE 216 #ifdef G4VERBOSE 208 G4cerr << "G4PhysicsTable::RetrievePhysi 217 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 209 G4cerr << " Rrror in retreiving " << idx 218 G4cerr << " Rrror in retreiving " << idx 210 << "-th Physics Vector from file: 219 << "-th Physics Vector from file: "; 211 G4cerr << fileName << G4endl; 220 G4cerr << fileName << G4endl; 212 #endif 221 #endif 213 fIn.close(); 222 fIn.close(); 214 return false; 223 return false; 215 } 224 } 216 225 217 // add a PhysicsVector to this PhysicsTabl 226 // add a PhysicsVector to this PhysicsTable 218 G4PhysCollection::push_back(pVec); 227 G4PhysCollection::push_back(pVec); 219 vecFlag.push_back(true); 228 vecFlag.push_back(true); 220 } 229 } 221 fIn.close(); 230 fIn.close(); 222 return true; 231 return true; 223 } 232 } 224 233 225 // ------------------------------------------- 234 // -------------------------------------------------------------------- 226 std::ostream& operator<<(std::ostream& out, G4 235 std::ostream& operator<<(std::ostream& out, G4PhysicsTable& right) 227 { 236 { 228 // Printout Physics Vector 237 // Printout Physics Vector 229 std::size_t i = 0; 238 std::size_t i = 0; 230 for(auto itr = right.cbegin(); itr != right. 239 for(auto itr = right.cbegin(); itr != right.cend(); ++itr) 231 { 240 { 232 out << std::setw(8) << i << "-th Vector 241 out << std::setw(8) << i << "-th Vector "; 233 if (nullptr == (*itr)) { << 234 out << "empty" << G4endl; << 235 ++i; << 236 continue; << 237 } << 238 out << ": Type " << G4int((*itr)->GetTy 242 out << ": Type " << G4int((*itr)->GetType()); 239 out << ": Flag "; 243 out << ": Flag "; 240 if(right.GetFlag(i)) 244 if(right.GetFlag(i)) 241 { 245 { 242 out << " T"; 246 out << " T"; 243 } 247 } 244 else 248 else 245 { 249 { 246 out << " F"; 250 out << " F"; 247 } 251 } 248 out << G4endl; 252 out << G4endl; 249 out << *(*itr); 253 out << *(*itr); 250 ++i; 254 ++i; 251 } 255 } 252 out << G4endl; 256 out << G4endl; 253 return out; 257 return out; 254 } 258 } 255 259 256 // ------------------------------------------- 260 // -------------------------------------------------------------------- 257 void G4PhysicsTable::ResetFlagArray() 261 void G4PhysicsTable::ResetFlagArray() 258 { 262 { 259 size_t tableSize = G4PhysCollection::size(); 263 size_t tableSize = G4PhysCollection::size(); 260 vecFlag.clear(); 264 vecFlag.clear(); 261 for(std::size_t idx = 0; idx < tableSize; ++ 265 for(std::size_t idx = 0; idx < tableSize; ++idx) 262 { 266 { 263 vecFlag.push_back(true); 267 vecFlag.push_back(true); 264 } 268 } 265 } 269 } 266 270 267 // ------------------------------------------- 271 // -------------------------------------------------------------------- 268 G4PhysicsVector* G4PhysicsTable::CreatePhysics << 272 G4PhysicsVector* G4PhysicsTable::CreatePhysicsVector(G4int type) 269 { 273 { 270 G4PhysicsVector* pVector = nullptr; 274 G4PhysicsVector* pVector = nullptr; 271 switch(type) 275 switch(type) 272 { 276 { 273 case T_G4PhysicsLinearVector: 277 case T_G4PhysicsLinearVector: 274 pVector = new G4PhysicsLinearVector(spli << 278 pVector = new G4PhysicsLinearVector(); 275 break; 279 break; 276 280 277 case T_G4PhysicsLogVector: 281 case T_G4PhysicsLogVector: 278 pVector = new G4PhysicsLogVector(spline) << 282 pVector = new G4PhysicsLogVector(); >> 283 break; >> 284 >> 285 case T_G4PhysicsLnVector: >> 286 pVector = new G4PhysicsLogVector(); >> 287 break; >> 288 >> 289 case T_G4PhysicsFreeVector: >> 290 pVector = new G4PhysicsFreeVector(); >> 291 break; >> 292 >> 293 case T_G4PhysicsOrderedFreeVector: >> 294 pVector = new G4PhysicsOrderedFreeVector(); >> 295 break; >> 296 >> 297 case T_G4LPhysicsFreeVector: >> 298 pVector = new G4PhysicsFreeVector(); 279 break; 299 break; 280 300 281 default: 301 default: 282 pVector = new G4PhysicsVector(spline); << 283 break; 302 break; 284 } 303 } 285 return pVector; 304 return pVector; 286 } 305 } 287 306