[833] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
| 27 | // $Id: G4OrderedTable.cc,v 1.6 2007/11/13 17:35:06 gcosmo Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
| 29 | // |
---|
| 30 | // |
---|
| 31 | // ------------------------------------------------------------ |
---|
| 32 | // GEANT 4 class implementation |
---|
| 33 | // |
---|
| 34 | // G4OrderedTable |
---|
| 35 | // |
---|
| 36 | // ------------------------------------------------------------ |
---|
| 37 | |
---|
| 38 | #include "G4DataVector.hh" |
---|
| 39 | #include "G4OrderedTable.hh" |
---|
| 40 | #include <iostream> |
---|
| 41 | #include <fstream> |
---|
| 42 | #include <iomanip> |
---|
| 43 | |
---|
| 44 | G4OrderedTable::G4OrderedTable() |
---|
| 45 | : std::vector<G4DataVector*>() |
---|
| 46 | { |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | G4OrderedTable::G4OrderedTable(size_t cap) |
---|
| 50 | : std::vector<G4DataVector*>(cap, (G4DataVector*)(0) ) |
---|
| 51 | { |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | G4OrderedTable::~G4OrderedTable() |
---|
| 55 | { |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | G4bool G4OrderedTable::Store(const G4String& fileName, |
---|
| 59 | G4bool ascii) |
---|
| 60 | { |
---|
| 61 | std::ofstream fOut; |
---|
| 62 | |
---|
| 63 | // open output file // |
---|
| 64 | if (!ascii) |
---|
| 65 | { fOut.open(fileName, std::ios::out|std::ios::binary); } |
---|
| 66 | else |
---|
| 67 | { fOut.open(fileName, std::ios::out); } |
---|
| 68 | |
---|
| 69 | // check if the file has been opened successfully |
---|
| 70 | if (!fOut) |
---|
| 71 | { |
---|
| 72 | #ifdef G4VERBOSE |
---|
| 73 | G4cerr << "G4OrderedTable::::Store "; |
---|
| 74 | G4cerr << " Can not open file " << fileName << G4endl; |
---|
| 75 | #endif |
---|
| 76 | fOut.close(); |
---|
| 77 | return false; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // Number of elements |
---|
| 81 | size_t tableSize = size(); |
---|
| 82 | if (!ascii) |
---|
| 83 | { |
---|
| 84 | fOut.write( (char*)(&tableSize), sizeof tableSize); |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | fOut << tableSize << G4endl; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | // Data Vector |
---|
| 92 | G4int vType = G4DataVector::T_G4DataVector; |
---|
| 93 | for (G4OrderedTableIterator itr=begin(); itr!=end(); ++itr) |
---|
| 94 | { |
---|
| 95 | if (!ascii) |
---|
| 96 | { |
---|
| 97 | fOut.write( (char*)(&vType), sizeof vType); |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | { |
---|
| 101 | fOut << vType << G4endl; |
---|
| 102 | } |
---|
| 103 | (*itr)->Store(fOut,ascii); |
---|
| 104 | } |
---|
| 105 | fOut.close(); |
---|
| 106 | return true; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | G4bool G4OrderedTable::Retrieve(const G4String& fileName, |
---|
| 112 | G4bool ascii) |
---|
| 113 | { |
---|
| 114 | std::ifstream fIn; |
---|
| 115 | // open input file // |
---|
| 116 | if (ascii) |
---|
| 117 | { fIn.open(fileName,std::ios::in|std::ios::binary); } |
---|
| 118 | else |
---|
| 119 | { fIn.open(fileName,std::ios::in); } |
---|
| 120 | |
---|
| 121 | // check if the file has been opened successfully |
---|
| 122 | if (!fIn) |
---|
| 123 | { |
---|
| 124 | #ifdef G4VERBOSE |
---|
| 125 | G4cerr << "G4OrderedTable::Retrieve "; |
---|
| 126 | G4cerr << " Can not open file " << fileName << G4endl; |
---|
| 127 | #endif |
---|
| 128 | fIn.close(); |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // clear |
---|
| 133 | clearAndDestroy(); |
---|
| 134 | |
---|
| 135 | // Number of elements |
---|
| 136 | size_t tableSize=0; |
---|
| 137 | if (!ascii) |
---|
| 138 | { |
---|
| 139 | fIn.read((char*)(&tableSize), sizeof tableSize); |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | fIn >> tableSize; |
---|
| 144 | } |
---|
| 145 | reserve(tableSize); |
---|
| 146 | |
---|
| 147 | // Physics Vector |
---|
| 148 | for (size_t idx=0; idx<tableSize; ++idx) |
---|
| 149 | { |
---|
| 150 | G4int vType=0; |
---|
| 151 | if (!ascii) |
---|
| 152 | { |
---|
| 153 | fIn.read( (char*)(&vType), sizeof vType); |
---|
| 154 | } |
---|
| 155 | else |
---|
| 156 | { |
---|
| 157 | fIn >> vType; |
---|
| 158 | } |
---|
| 159 | if (vType != G4DataVector::T_G4DataVector) |
---|
| 160 | { |
---|
| 161 | #ifdef G4VERBOSE |
---|
| 162 | G4cerr << "G4OrderedTable::Retrieve "; |
---|
| 163 | G4cerr << " illegal Data Vector type " << vType << " in "; |
---|
| 164 | G4cerr << fileName << G4endl; |
---|
| 165 | #endif |
---|
| 166 | fIn.close(); |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | G4DataVector* pVec = new G4DataVector; |
---|
| 171 | |
---|
| 172 | if (! (pVec->Retrieve(fIn,ascii)) ) |
---|
| 173 | { |
---|
| 174 | #ifdef G4VERBOSE |
---|
| 175 | G4cerr << "G4OrderedTable::Retrieve "; |
---|
| 176 | G4cerr << " error in retreiving " << idx |
---|
| 177 | << "-th Physics Vector from file "; |
---|
| 178 | G4cerr << fileName << G4endl; |
---|
| 179 | #endif |
---|
| 180 | fIn.close(); |
---|
| 181 | return false; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // add a PhysicsVector to this OrderedTable |
---|
| 185 | push_back(pVec); |
---|
| 186 | } |
---|
| 187 | fIn.close(); |
---|
| 188 | return true; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | std::ostream& operator<<(std::ostream& out, |
---|
| 192 | G4OrderedTable& right) |
---|
| 193 | { |
---|
| 194 | // Printout Data Vector |
---|
| 195 | size_t i=0; |
---|
| 196 | for (G4OrderedTableIterator itr=right.begin(); itr!=right.end(); ++itr) |
---|
| 197 | { |
---|
| 198 | out << std::setw(8) << i << "-th Vector "; |
---|
| 199 | out << ": Type " << G4DataVector::T_G4DataVector << G4endl; |
---|
| 200 | out << *(*itr); |
---|
| 201 | i +=1; |
---|
| 202 | } |
---|
| 203 | out << G4endl; |
---|
| 204 | return out; |
---|
| 205 | } |
---|