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