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