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