Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4OrderedTable class implementation << 27 // 23 // 28 // Author: M.Maire (LAPP), September 1996 << 24 // $Id: G4OrderedTable.cc,v 1.2 2002/04/19 07:10:32 gcosmo Exp $ 29 // Revisions: H.Kurashige (Kobe Univ.), Januar << 25 // GEANT4 tag $Name: geant4-05-00 $ 30 // ------------------------------------------- << 26 // >> 27 // >> 28 // ------------------------------------------------------------ >> 29 // GEANT 4 class implementation >> 30 // >> 31 // G4OrderedTable >> 32 // >> 33 // ------------------------------------------------------------ 31 34 32 #include "G4OrderedTable.hh" << 33 #include "G4DataVector.hh" 35 #include "G4DataVector.hh" >> 36 #include "G4OrderedTable.hh" >> 37 #include "g4std/iostream" >> 38 #include "g4std/fstream" >> 39 #include "g4std/iomanip" 34 40 35 #include <fstream> << 36 #include <iomanip> << 37 #include <iostream> << 38 << 39 // ------------------------------------------- << 40 G4OrderedTable::G4OrderedTable(std::size_t cap << 41 : std::vector<G4DataVector*>(cap, (G4DataVec << 42 {} << 43 << 44 // ------------------------------------------- << 45 void G4OrderedTable::clearAndDestroy() << 46 { << 47 G4DataVector* a = nullptr; << 48 while(!empty()) << 49 { << 50 a = back(); << 51 pop_back(); << 52 for(auto i = cbegin(); i != cend(); ++i) << 53 { << 54 if(*i == a) << 55 { << 56 erase(i); << 57 --i; << 58 } << 59 } << 60 41 61 delete a; << 42 G4OrderedTable::G4OrderedTable() 62 } << 43 : G4std::vector<G4DataVector*>() >> 44 { 63 } 45 } 64 46 65 // ------------------------------------------- << 47 G4OrderedTable::G4OrderedTable(size_t capacity) 66 G4bool G4OrderedTable::Store(const G4String& f << 48 : G4std::vector<G4DataVector*>(capacity, (G4DataVector*)(0) ) 67 { 49 { 68 std::ofstream fOut; << 50 } 69 51 >> 52 G4OrderedTable::~G4OrderedTable() >> 53 { >> 54 } >> 55 >> 56 G4bool G4OrderedTable::Store(const G4String& fileName, >> 57 G4bool ascii) >> 58 { >> 59 G4std::ofstream fOut; >> 60 70 // open output file // 61 // open output file // 71 if(!ascii) << 62 #ifdef G4USE_STD_NAMESPACE 72 { << 63 if (!ascii) 73 fOut.open(fileName, std::ios::out | std::i << 64 fOut.open(fileName, G4std::ios::out|G4std::ios::binary); 74 } << 75 else 65 else 76 { << 66 #endif 77 fOut.open(fileName, std::ios::out); << 67 fOut.open(fileName, G4std::ios::out); 78 } << 79 68 80 // check if the file has been opened success << 69 // check if the file has been opened successfully 81 if(!fOut) << 70 if (!fOut) { 82 { << 71 #ifdef G4VERBOSE 83 #ifdef G4VERBOSE << 72 G4cerr << "G4OrderedTable::::Store "; 84 G4cerr << "G4OrderedTable::::Store():"; << 73 G4cerr << " Can not open file " << fileName << G4endl; 85 G4cerr << " Cannot open file: " << fileNam << 86 #endif 74 #endif 87 fOut.close(); 75 fOut.close(); 88 return false; 76 return false; 89 } 77 } 90 78 91 auto tableSize = G4int(size()); // Number o << 79 // Number of elements 92 if(!ascii) << 80 size_t tableSize = size(); 93 { << 81 if (!ascii){ 94 fOut.write((char*) (&tableSize), sizeof ta << 82 fOut.write( (char*)(&tableSize), sizeof tableSize); 95 } << 83 } else { 96 else << 97 { << 98 fOut << tableSize << G4endl; 84 fOut << tableSize << G4endl; 99 } 85 } 100 86 101 G4int vType = G4DataVector::T_G4DataVector; << 87 // Data Vector 102 for(const auto itr : *this) << 88 G4OrderedTableIterator itr; 103 { << 89 G4int vType = G4DataVector::T_G4DataVector; 104 if(!ascii) << 90 for (itr=begin(); itr!=end(); ++itr) { 105 { << 91 if (!ascii){ 106 fOut.write((char*) (&vType), sizeof vTyp << 92 fOut.write( (char*)(&vType), sizeof vType); 107 } << 93 } else { 108 else << 109 { << 110 fOut << vType << G4endl; 94 fOut << vType << G4endl; 111 } 95 } 112 itr->Store(fOut, ascii); << 96 (*itr)->Store(fOut,ascii); 113 } 97 } 114 fOut.close(); 98 fOut.close(); 115 return true; 99 return true; 116 } 100 } 117 101 118 // ------------------------------------------- << 102 119 G4bool G4OrderedTable::Retrieve(const G4String << 103 >> 104 G4bool G4OrderedTable::Retrieve(const G4String& fileName, >> 105 G4bool ascii) 120 { 106 { 121 std::ifstream fIn; << 107 G4std::ifstream fIn; 122 // open input file // 108 // open input file // 123 if(!ascii) << 109 #ifdef G4USE_STD_NAMESPACE 124 { << 110 if (ascii) 125 fIn.open(fileName, std::ios::in | std::ios << 111 fIn.open(fileName,G4std::ios::in|G4std::ios::binary); 126 } << 127 else 112 else 128 { << 113 #endif 129 fIn.open(fileName, std::ios::in); << 114 fIn.open(fileName,G4std::ios::in); 130 } << 131 115 132 // check if the file has been opened success << 116 // check if the file has been opened successfully 133 if(!fIn) << 117 if (!fIn) { 134 { << 118 #ifdef G4VERBOSE 135 #ifdef G4VERBOSE << 119 G4cerr << "G4OrderedTable::Retrieve "; 136 G4cerr << "G4OrderedTable::Retrieve():"; << 120 G4cerr << " Can not open file " << fileName << G4endl; 137 G4cerr << " Cannot open file: " << fileNam << 138 #endif 121 #endif 139 fIn.close(); 122 fIn.close(); 140 return false; 123 return false; 141 } 124 } 142 125 143 // clear << 126 // clear 144 clearAndDestroy(); 127 clearAndDestroy(); 145 << 128 146 // Number of elements 129 // Number of elements 147 G4int tableSize = 0; << 130 size_t tableSize; 148 if(!ascii) << 131 if (!ascii){ 149 { << 132 fIn.read((char*)(&tableSize), sizeof tableSize); 150 fIn.read((char*) (&tableSize), sizeof tabl << 133 } else { 151 } << 152 else << 153 { << 154 fIn >> tableSize; 134 fIn >> tableSize; 155 } 135 } 156 if(tableSize <= 0) << 136 reserve(tableSize); 157 { << 158 #ifdef G4VERBOSE << 159 G4cerr << "G4OrderedTable::Retrieve():"; << 160 G4cerr << " Invalid table size: " << table << 161 #endif << 162 return false; << 163 } << 164 reserve(tableSize); << 165 137 166 // Physics Vector 138 // Physics Vector 167 for(G4int idx = 0; idx < tableSize; ++idx) << 139 for (size_t idx=0; idx<tableSize; ++idx) { 168 { << 140 G4int vType; 169 G4int vType = 0; << 141 if (!ascii){ 170 if(!ascii) << 142 fIn.read( (char*)(&vType), sizeof vType); 171 { << 143 } else { 172 fIn.read((char*) (&vType), sizeof vType) << 144 fIn >> vType; 173 } << 145 } 174 else << 146 if (vType != G4DataVector::T_G4DataVector) { 175 { << 147 #ifdef G4VERBOSE 176 fIn >> vType; << 148 G4cerr << "G4OrderedTable::Retrieve "; 177 } << 149 G4cerr << " illegal Data Vector type " << vType << " in "; 178 if(vType != G4DataVector::T_G4DataVector) << 179 { << 180 #ifdef G4VERBOSE << 181 G4cerr << "G4OrderedTable::Retrieve():"; << 182 G4cerr << " Illegal Data Vector type: " << 183 G4cerr << fileName << G4endl; 150 G4cerr << fileName << G4endl; 184 #endif << 151 #endif 185 fIn.close(); 152 fIn.close(); 186 return false; 153 return false; 187 } 154 } 188 155 189 auto* pVec = new G4DataVector; << 156 G4DataVector* pVec = new G4DataVector; 190 157 191 if(!(pVec->Retrieve(fIn, ascii))) << 158 if (! (pVec->Retrieve(fIn,ascii)) ){ 192 { << 159 #ifdef G4VERBOSE 193 #ifdef G4VERBOSE << 160 G4cerr << "G4OrderedTable::Retrieve "; 194 G4cerr << "G4OrderedTable::Retrieve(): " << 161 G4cerr << " error in retreiving " << idx << "-th Physics Vector from file "; 195 G4cerr << " Error in retreiving " << idx << 196 << "-th Physics Vector from file: << 197 G4cerr << fileName << G4endl; 162 G4cerr << fileName << G4endl; 198 #endif << 163 #endif 199 fIn.close(); 164 fIn.close(); 200 delete pVec; << 201 return false; 165 return false; 202 } 166 } 203 167 204 // add a PhysicsVector to this OrderedTabl 168 // add a PhysicsVector to this OrderedTable 205 push_back(pVec); 169 push_back(pVec); 206 } << 170 } 207 fIn.close(); 171 fIn.close(); 208 return true; 172 return true; 209 } 173 } 210 174 211 // ------------------------------------------- << 175 G4std::ostream& operator<<(G4std::ostream& out, 212 std::ostream& operator<<(std::ostream& out, G4 << 176 G4OrderedTable& right) 213 { 177 { 214 // Printout Data Vector 178 // Printout Data Vector 215 std::size_t i = 0; << 179 G4OrderedTableIterator itr; 216 for(const auto itr : right) << 180 size_t i=0; 217 { << 181 for (itr=right.begin(); itr!=right.end(); ++itr) { 218 out << std::setw(8) << i << "-th Vector << 182 out << G4std::setw(8) << i << "-th Vector "; 219 out << ": Type " << G4DataVector::T_G4D 183 out << ": Type " << G4DataVector::T_G4DataVector << G4endl; 220 out << *itr; << 184 out << *(*itr); 221 i += 1; << 185 i +=1; 222 } 186 } 223 out << G4endl; 187 out << G4endl; 224 return out; << 188 return out; 225 } 189 } >> 190 >> 191 226 192