00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "G4DataVector.hh"
00038 #include "G4OrderedTable.hh"
00039 #include <iostream>
00040 #include <fstream>
00041 #include <iomanip>
00042
00043 G4OrderedTable::G4OrderedTable()
00044 : std::vector<G4DataVector*>()
00045 {
00046 }
00047
00048 G4OrderedTable::G4OrderedTable(size_t cap)
00049 : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) )
00050 {
00051 }
00052
00053 G4OrderedTable::~G4OrderedTable()
00054 {
00055 }
00056
00057 G4bool G4OrderedTable::Store(const G4String& fileName,
00058 G4bool ascii)
00059 {
00060 std::ofstream fOut;
00061
00062
00063 if (!ascii)
00064 { fOut.open(fileName, std::ios::out|std::ios::binary); }
00065 else
00066 { fOut.open(fileName, std::ios::out); }
00067
00068
00069 if (!fOut)
00070 {
00071 #ifdef G4VERBOSE
00072 G4cerr << "G4OrderedTable::::Store():";
00073 G4cerr << " Cannot open file: " << fileName << G4endl;
00074 #endif
00075 fOut.close();
00076 return false;
00077 }
00078
00079
00080 size_t tableSize = size();
00081 if (!ascii)
00082 {
00083 fOut.write( (char*)(&tableSize), sizeof tableSize);
00084 }
00085 else
00086 {
00087 fOut << tableSize << G4endl;
00088 }
00089
00090
00091 G4int vType = G4DataVector::T_G4DataVector;
00092 for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr)
00093 {
00094 if (!ascii)
00095 {
00096 fOut.write( (char*)(&vType), sizeof vType);
00097 }
00098 else
00099 {
00100 fOut << vType << G4endl;
00101 }
00102 (*itr)->Store(fOut,ascii);
00103 }
00104 fOut.close();
00105 return true;
00106 }
00107
00108
00109
00110 G4bool G4OrderedTable::Retrieve(const G4String& fileName,
00111 G4bool ascii)
00112 {
00113 std::ifstream fIn;
00114
00115 if (ascii)
00116 { fIn.open(fileName,std::ios::in|std::ios::binary); }
00117 else
00118 { fIn.open(fileName,std::ios::in); }
00119
00120
00121 if (!fIn)
00122 {
00123 #ifdef G4VERBOSE
00124 G4cerr << "G4OrderedTable::Retrieve():";
00125 G4cerr << " Cannot open file: " << fileName << G4endl;
00126 #endif
00127 fIn.close();
00128 return false;
00129 }
00130
00131
00132 clearAndDestroy();
00133
00134
00135 G4int tableSize=0;
00136 if (!ascii)
00137 {
00138 fIn.read((char*)(&tableSize), sizeof tableSize);
00139 }
00140 else
00141 {
00142 fIn >> tableSize;
00143 }
00144 if (tableSize<=0)
00145 {
00146 #ifdef G4VERBOSE
00147 G4cerr << "G4OrderedTable::Retrieve():";
00148 G4cerr << " Invalid table size: " << tableSize << G4endl;
00149 #endif
00150 return false;
00151 }
00152 reserve(tableSize);
00153
00154
00155 for (G4int idx=0; idx<tableSize; ++idx)
00156 {
00157 G4int vType=0;
00158 if (!ascii)
00159 {
00160 fIn.read( (char*)(&vType), sizeof vType);
00161 }
00162 else
00163 {
00164 fIn >> vType;
00165 }
00166 if (vType != G4DataVector::T_G4DataVector)
00167 {
00168 #ifdef G4VERBOSE
00169 G4cerr << "G4OrderedTable::Retrieve():";
00170 G4cerr << " Illegal Data Vector type: " << vType << " in ";
00171 G4cerr << fileName << G4endl;
00172 #endif
00173 fIn.close();
00174 return false;
00175 }
00176
00177 G4DataVector* pVec = new G4DataVector;
00178
00179 if (! (pVec->Retrieve(fIn,ascii)) )
00180 {
00181 #ifdef G4VERBOSE
00182 G4cerr << "G4OrderedTable::Retrieve(): ";
00183 G4cerr << " Rrror in retreiving " << idx
00184 << "-th Physics Vector from file: ";
00185 G4cerr << fileName << G4endl;
00186 #endif
00187 fIn.close();
00188 delete pVec;
00189 return false;
00190 }
00191
00192
00193 push_back(pVec);
00194 }
00195 fIn.close();
00196 return true;
00197 }
00198
00199 std::ostream& operator<<(std::ostream& out,
00200 G4OrderedTable& right)
00201 {
00202
00203 size_t i=0;
00204 for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr)
00205 {
00206 out << std::setw(8) << i << "-th Vector ";
00207 out << ": Type " << G4DataVector::T_G4DataVector << G4endl;
00208 out << *(*itr);
00209 i +=1;
00210 }
00211 out << G4endl;
00212 return out;
00213 }