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 // G4OrderedTable class implementation 26 // G4OrderedTable class implementation 27 // 27 // 28 // Author: M.Maire (LAPP), September 1996 28 // Author: M.Maire (LAPP), September 1996 29 // Revisions: H.Kurashige (Kobe Univ.), Januar 29 // Revisions: H.Kurashige (Kobe Univ.), January-September 2001 30 // ------------------------------------------- 30 // -------------------------------------------------------------------- 31 31 32 #include "G4OrderedTable.hh" 32 #include "G4OrderedTable.hh" 33 #include "G4DataVector.hh" 33 #include "G4DataVector.hh" 34 34 35 #include <fstream> 35 #include <fstream> 36 #include <iomanip> 36 #include <iomanip> 37 #include <iostream> 37 #include <iostream> 38 38 39 // ------------------------------------------- 39 // -------------------------------------------------------------------- >> 40 G4OrderedTable::G4OrderedTable() >> 41 : std::vector<G4DataVector*>() >> 42 {} >> 43 >> 44 // -------------------------------------------------------------------- 40 G4OrderedTable::G4OrderedTable(std::size_t cap 45 G4OrderedTable::G4OrderedTable(std::size_t cap) 41 : std::vector<G4DataVector*>(cap, (G4DataVec << 46 : std::vector<G4DataVector*>(cap, (G4DataVector*) (0)) 42 {} 47 {} 43 48 44 // ------------------------------------------- 49 // -------------------------------------------------------------------- >> 50 G4OrderedTable::~G4OrderedTable() {} >> 51 >> 52 // -------------------------------------------------------------------- 45 void G4OrderedTable::clearAndDestroy() 53 void G4OrderedTable::clearAndDestroy() 46 { 54 { 47 G4DataVector* a = nullptr; 55 G4DataVector* a = nullptr; 48 while(!empty()) << 56 while(size() > 0) 49 { 57 { 50 a = back(); 58 a = back(); 51 pop_back(); 59 pop_back(); 52 for(auto i = cbegin(); i != cend(); ++i) 60 for(auto i = cbegin(); i != cend(); ++i) 53 { 61 { 54 if(*i == a) 62 if(*i == a) 55 { 63 { 56 erase(i); 64 erase(i); 57 --i; 65 --i; 58 } 66 } 59 } 67 } 60 << 68 if(a != nullptr) 61 delete a; << 69 { >> 70 delete a; >> 71 } 62 } 72 } 63 } 73 } 64 74 65 // ------------------------------------------- 75 // -------------------------------------------------------------------- 66 G4bool G4OrderedTable::Store(const G4String& f 76 G4bool G4OrderedTable::Store(const G4String& fileName, G4bool ascii) 67 { 77 { 68 std::ofstream fOut; 78 std::ofstream fOut; 69 79 70 // open output file // 80 // open output file // 71 if(!ascii) 81 if(!ascii) 72 { 82 { 73 fOut.open(fileName, std::ios::out | std::i 83 fOut.open(fileName, std::ios::out | std::ios::binary); 74 } 84 } 75 else 85 else 76 { 86 { 77 fOut.open(fileName, std::ios::out); 87 fOut.open(fileName, std::ios::out); 78 } 88 } 79 89 80 // check if the file has been opened success 90 // check if the file has been opened successfully 81 if(!fOut) 91 if(!fOut) 82 { 92 { 83 #ifdef G4VERBOSE 93 #ifdef G4VERBOSE 84 G4cerr << "G4OrderedTable::::Store():"; 94 G4cerr << "G4OrderedTable::::Store():"; 85 G4cerr << " Cannot open file: " << fileNam 95 G4cerr << " Cannot open file: " << fileName << G4endl; 86 #endif 96 #endif 87 fOut.close(); 97 fOut.close(); 88 return false; 98 return false; 89 } 99 } 90 100 91 auto tableSize = G4int(size()); // Number o << 101 G4int tableSize = G4int(size()); // Number of elements 92 if(!ascii) 102 if(!ascii) 93 { 103 { 94 fOut.write((char*) (&tableSize), sizeof ta 104 fOut.write((char*) (&tableSize), sizeof tableSize); 95 } 105 } 96 else 106 else 97 { 107 { 98 fOut << tableSize << G4endl; 108 fOut << tableSize << G4endl; 99 } 109 } 100 110 101 G4int vType = G4DataVector::T_G4DataVector; 111 G4int vType = G4DataVector::T_G4DataVector; // Data Vector 102 for(const auto itr : *this) << 112 for(auto itr = cbegin(); itr != cend(); ++itr) 103 { 113 { 104 if(!ascii) 114 if(!ascii) 105 { 115 { 106 fOut.write((char*) (&vType), sizeof vTyp 116 fOut.write((char*) (&vType), sizeof vType); 107 } 117 } 108 else 118 else 109 { 119 { 110 fOut << vType << G4endl; 120 fOut << vType << G4endl; 111 } 121 } 112 itr->Store(fOut, ascii); << 122 (*itr)->Store(fOut, ascii); 113 } 123 } 114 fOut.close(); 124 fOut.close(); 115 return true; 125 return true; 116 } 126 } 117 127 118 // ------------------------------------------- 128 // -------------------------------------------------------------------- 119 G4bool G4OrderedTable::Retrieve(const G4String 129 G4bool G4OrderedTable::Retrieve(const G4String& fileName, G4bool ascii) 120 { 130 { 121 std::ifstream fIn; 131 std::ifstream fIn; 122 // open input file // 132 // open input file // 123 if(!ascii) 133 if(!ascii) 124 { 134 { 125 fIn.open(fileName, std::ios::in | std::ios 135 fIn.open(fileName, std::ios::in | std::ios::binary); 126 } 136 } 127 else 137 else 128 { 138 { 129 fIn.open(fileName, std::ios::in); 139 fIn.open(fileName, std::ios::in); 130 } 140 } 131 141 132 // check if the file has been opened success 142 // check if the file has been opened successfully 133 if(!fIn) 143 if(!fIn) 134 { 144 { 135 #ifdef G4VERBOSE 145 #ifdef G4VERBOSE 136 G4cerr << "G4OrderedTable::Retrieve():"; 146 G4cerr << "G4OrderedTable::Retrieve():"; 137 G4cerr << " Cannot open file: " << fileNam 147 G4cerr << " Cannot open file: " << fileName << G4endl; 138 #endif 148 #endif 139 fIn.close(); 149 fIn.close(); 140 return false; 150 return false; 141 } 151 } 142 152 143 // clear 153 // clear 144 clearAndDestroy(); 154 clearAndDestroy(); 145 155 146 // Number of elements 156 // Number of elements 147 G4int tableSize = 0; 157 G4int tableSize = 0; 148 if(!ascii) 158 if(!ascii) 149 { 159 { 150 fIn.read((char*) (&tableSize), sizeof tabl 160 fIn.read((char*) (&tableSize), sizeof tableSize); 151 } 161 } 152 else 162 else 153 { 163 { 154 fIn >> tableSize; 164 fIn >> tableSize; 155 } 165 } 156 if(tableSize <= 0) 166 if(tableSize <= 0) 157 { 167 { 158 #ifdef G4VERBOSE 168 #ifdef G4VERBOSE 159 G4cerr << "G4OrderedTable::Retrieve():"; 169 G4cerr << "G4OrderedTable::Retrieve():"; 160 G4cerr << " Invalid table size: " << table 170 G4cerr << " Invalid table size: " << tableSize << G4endl; 161 #endif 171 #endif 162 return false; 172 return false; 163 } 173 } 164 reserve(tableSize); 174 reserve(tableSize); 165 175 166 // Physics Vector 176 // Physics Vector 167 for(G4int idx = 0; idx < tableSize; ++idx) 177 for(G4int idx = 0; idx < tableSize; ++idx) 168 { 178 { 169 G4int vType = 0; 179 G4int vType = 0; 170 if(!ascii) 180 if(!ascii) 171 { 181 { 172 fIn.read((char*) (&vType), sizeof vType) 182 fIn.read((char*) (&vType), sizeof vType); 173 } 183 } 174 else 184 else 175 { 185 { 176 fIn >> vType; 186 fIn >> vType; 177 } 187 } 178 if(vType != G4DataVector::T_G4DataVector) 188 if(vType != G4DataVector::T_G4DataVector) 179 { 189 { 180 #ifdef G4VERBOSE 190 #ifdef G4VERBOSE 181 G4cerr << "G4OrderedTable::Retrieve():"; 191 G4cerr << "G4OrderedTable::Retrieve():"; 182 G4cerr << " Illegal Data Vector type: " 192 G4cerr << " Illegal Data Vector type: " << vType << " in "; 183 G4cerr << fileName << G4endl; 193 G4cerr << fileName << G4endl; 184 #endif 194 #endif 185 fIn.close(); 195 fIn.close(); 186 return false; 196 return false; 187 } 197 } 188 198 189 auto* pVec = new G4DataVector; << 199 G4DataVector* pVec = new G4DataVector; 190 200 191 if(!(pVec->Retrieve(fIn, ascii))) 201 if(!(pVec->Retrieve(fIn, ascii))) 192 { 202 { 193 #ifdef G4VERBOSE 203 #ifdef G4VERBOSE 194 G4cerr << "G4OrderedTable::Retrieve(): " 204 G4cerr << "G4OrderedTable::Retrieve(): "; 195 G4cerr << " Error in retreiving " << idx 205 G4cerr << " Error in retreiving " << idx 196 << "-th Physics Vector from file: 206 << "-th Physics Vector from file: "; 197 G4cerr << fileName << G4endl; 207 G4cerr << fileName << G4endl; 198 #endif 208 #endif 199 fIn.close(); 209 fIn.close(); 200 delete pVec; 210 delete pVec; 201 return false; 211 return false; 202 } 212 } 203 213 204 // add a PhysicsVector to this OrderedTabl 214 // add a PhysicsVector to this OrderedTable 205 push_back(pVec); 215 push_back(pVec); 206 } 216 } 207 fIn.close(); 217 fIn.close(); 208 return true; 218 return true; 209 } 219 } 210 220 211 // ------------------------------------------- 221 // -------------------------------------------------------------------- 212 std::ostream& operator<<(std::ostream& out, G4 222 std::ostream& operator<<(std::ostream& out, G4OrderedTable& right) 213 { 223 { 214 // Printout Data Vector 224 // Printout Data Vector 215 std::size_t i = 0; 225 std::size_t i = 0; 216 for(const auto itr : right) << 226 for(auto itr = right.cbegin(); itr != right.cend(); ++itr) 217 { 227 { 218 out << std::setw(8) << i << "-th Vector 228 out << std::setw(8) << i << "-th Vector "; 219 out << ": Type " << G4DataVector::T_G4D 229 out << ": Type " << G4DataVector::T_G4DataVector << G4endl; 220 out << *itr; << 230 out << *(*itr); 221 i += 1; 231 i += 1; 222 } 232 } 223 out << G4endl; 233 out << G4endl; 224 return out; 234 return out; 225 } 235 } 226 236