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