Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4PhysicsTable class implementation 27 // 28 // Author: G.Cosmo, 2 December 1995 29 // First implementation based on object model 30 // Revisions: 31 // - 1st March 1996, K.Amako: modified 32 // - 24th February 2001, H.Kurashige: migration to STL vectors 33 // -------------------------------------------------------------------- 34 35 #include <fstream> 36 #include <iomanip> 37 #include <iostream> 38 39 #include "G4PhysicsFreeVector.hh" 40 #include "G4PhysicsLinearVector.hh" 41 #include "G4PhysicsLogVector.hh" 42 #include "G4PhysicsTable.hh" 43 #include "G4PhysicsVector.hh" 44 #include "G4PhysicsVectorType.hh" 45 46 // -------------------------------------------------------------------- 47 G4PhysicsTable::G4PhysicsTable(std::size_t cap) 48 { 49 reserve(cap); 50 vecFlag.reserve(cap); 51 } 52 53 // -------------------------------------------------------------------- 54 G4PhysicsTable::~G4PhysicsTable() 55 { 56 G4PhysCollection::clear(); 57 vecFlag.clear(); 58 } 59 60 // -------------------------------------------------------------------- 61 void G4PhysicsTable::resize(std::size_t siz, G4PhysicsVector* vec) 62 { 63 G4PhysCollection::resize(siz, vec); 64 vecFlag.resize(siz, true); 65 } 66 67 // -------------------------------------------------------------------- 68 G4bool G4PhysicsTable::StorePhysicsTable(const G4String& fileName, G4bool ascii) 69 { 70 std::ofstream fOut; 71 72 // open output file 73 if(!ascii) 74 { 75 fOut.open(fileName, std::ios::out | std::ios::binary); 76 } 77 else 78 { 79 fOut.open(fileName, std::ios::out); 80 } 81 82 // check if the file has been opened successfully 83 if(!fOut.is_open()) 84 { 85 #ifdef G4VERBOSE 86 G4cerr << "G4PhysicsTable::StorePhysicsTable():"; 87 G4cerr << " Cannot open file: " << fileName << G4endl; 88 #endif 89 fOut.close(); 90 return false; 91 } 92 93 // Number of elements 94 std::size_t tableSize = size(); 95 if(!ascii) 96 { 97 fOut.write((char*) (&tableSize), sizeof tableSize); 98 } 99 else 100 { 101 fOut << tableSize << G4endl; 102 } 103 104 // Physics Vector 105 for(const auto itr : *this) 106 { 107 G4int vType = itr->GetType(); 108 if(!ascii) 109 { 110 fOut.write((char*) (&vType), sizeof vType); 111 } 112 else 113 { 114 fOut << vType << G4endl; 115 } 116 itr->Store(fOut, ascii); 117 } 118 fOut.close(); 119 return true; 120 } 121 122 // -------------------------------------------------------------------- 123 G4bool G4PhysicsTable::ExistPhysicsTable(const G4String& fileName) const 124 { 125 std::ifstream fIn; 126 G4bool value = true; 127 // open input file 128 fIn.open(fileName, std::ios::in); 129 130 // check if the file has been opened successfully 131 if(!fIn) 132 { 133 value = false; 134 } 135 fIn.close(); 136 return value; 137 } 138 139 // -------------------------------------------------------------------- 140 G4bool G4PhysicsTable::RetrievePhysicsTable(const G4String& fileName, 141 G4bool ascii, G4bool spline) 142 { 143 std::ifstream fIn; 144 // open input file 145 if(ascii) 146 { 147 fIn.open(fileName, std::ios::in | std::ios::binary); 148 } 149 else 150 { 151 fIn.open(fileName, std::ios::in); 152 } 153 154 // check if the file has been opened successfully 155 if(!fIn.is_open()) 156 { 157 #ifdef G4VERBOSE 158 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 159 G4cerr << " Cannot open file: " << fileName << G4endl; 160 #endif 161 fIn.close(); 162 return false; 163 } 164 165 // clear 166 clearAndDestroy(); 167 168 // Number of elements 169 std::size_t tableSize = 0; 170 if(!ascii) 171 { 172 fIn.read((char*) (&tableSize), sizeof tableSize); 173 } 174 else 175 { 176 fIn >> tableSize; 177 } 178 reserve(tableSize); 179 vecFlag.clear(); 180 181 // Physics Vector 182 for(std::size_t idx = 0; idx < tableSize; ++idx) 183 { 184 G4int vType = 0; 185 if(!ascii) 186 { 187 fIn.read((char*) (&vType), sizeof vType); 188 } 189 else 190 { 191 fIn >> vType; 192 } 193 G4PhysicsVector* pVec = CreatePhysicsVector(vType, spline); 194 if(pVec == nullptr) 195 { 196 #ifdef G4VERBOSE 197 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 198 G4cerr << " Illegal Physics Vector type: " << vType << " in: "; 199 G4cerr << fileName << G4endl; 200 #endif 201 fIn.close(); 202 return false; 203 } 204 205 if(!(pVec->Retrieve(fIn, ascii))) 206 { 207 #ifdef G4VERBOSE 208 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():"; 209 G4cerr << " Rrror in retreiving " << idx 210 << "-th Physics Vector from file: "; 211 G4cerr << fileName << G4endl; 212 #endif 213 fIn.close(); 214 return false; 215 } 216 217 // add a PhysicsVector to this PhysicsTable 218 G4PhysCollection::push_back(pVec); 219 vecFlag.push_back(true); 220 } 221 fIn.close(); 222 return true; 223 } 224 225 // -------------------------------------------------------------------- 226 std::ostream& operator<<(std::ostream& out, G4PhysicsTable& right) 227 { 228 // Printout Physics Vector 229 std::size_t i = 0; 230 for(auto itr = right.cbegin(); itr != right.cend(); ++itr) 231 { 232 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)->GetType()); 239 out << ": Flag "; 240 if(right.GetFlag(i)) 241 { 242 out << " T"; 243 } 244 else 245 { 246 out << " F"; 247 } 248 out << G4endl; 249 out << *(*itr); 250 ++i; 251 } 252 out << G4endl; 253 return out; 254 } 255 256 // -------------------------------------------------------------------- 257 void G4PhysicsTable::ResetFlagArray() 258 { 259 size_t tableSize = G4PhysCollection::size(); 260 vecFlag.clear(); 261 for(std::size_t idx = 0; idx < tableSize; ++idx) 262 { 263 vecFlag.push_back(true); 264 } 265 } 266 267 // -------------------------------------------------------------------- 268 G4PhysicsVector* G4PhysicsTable::CreatePhysicsVector(G4int type, G4bool spline) 269 { 270 G4PhysicsVector* pVector = nullptr; 271 switch(type) 272 { 273 case T_G4PhysicsLinearVector: 274 pVector = new G4PhysicsLinearVector(spline); 275 break; 276 277 case T_G4PhysicsLogVector: 278 pVector = new G4PhysicsLogVector(spline); 279 break; 280 281 default: 282 pVector = new G4PhysicsVector(spline); 283 break; 284 } 285 return pVector; 286 } 287