source: trunk/source/global/management/src/G4PhysicsTable.cc

Last change on this file was 1340, checked in by garnier, 15 years ago

update ti head

File size: 7.8 KB
Line 
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: G4PhysicsTable.cc,v 1.17 2010/11/01 13:55:53 gcosmo Exp $
28// GEANT4 tag $Name: global-V09-03-22 $
29//
30//
31// ------------------------------------------------------------
32// GEANT 4 class implementation
33//
34// G4PhysicsTable
35//
36// ------------------------------------------------------------
37
38#include <iostream>
39#include <fstream>
40#include <iomanip>
41
42#include "G4PhysicsVector.hh"
43#include "G4PhysicsTable.hh"
44#include "G4PhysicsVectorType.hh"
45#include "G4LPhysicsFreeVector.hh"
46#include "G4PhysicsLogVector.hh"
47#include "G4PhysicsFreeVector.hh"
48#include "G4PhysicsOrderedFreeVector.hh"
49#include "G4PhysicsLinearVector.hh"
50#include "G4PhysicsLnVector.hh"
51
52G4PhysicsTable::G4PhysicsTable()
53 : G4PhysCollection()
54{
55}
56
57G4PhysicsTable::G4PhysicsTable(size_t cap)
58 : G4PhysCollection()
59{
60 reserve(cap);
61 vecFlag.reserve(cap);
62}
63
64G4PhysicsTable::G4PhysicsTable(const G4PhysicsTable& right)
65 : G4PhysCollection()
66{
67 *this = right;
68}
69
70G4PhysicsTable& G4PhysicsTable::operator=(const G4PhysicsTable& right)
71{
72 if (this != &right)
73 {
74 size_t idx = 0;
75 for (G4PhysCollection::const_iterator itr=right.begin();
76 itr!=right.end(); ++itr )
77 {
78 G4PhysCollection::push_back(*itr);
79 vecFlag.push_back(right.GetFlag(idx));
80 idx +=1;
81 }
82 }
83 return *this;
84}
85
86G4PhysicsTable::~G4PhysicsTable()
87{
88 G4PhysCollection::clear();
89 vecFlag.clear();
90}
91
92void G4PhysicsTable::resize(size_t siz, G4PhysicsVector* vec)
93{
94 G4PhysCollection::resize(siz, vec);
95 vecFlag.resize(siz, true);
96}
97
98G4bool G4PhysicsTable::StorePhysicsTable(const G4String& fileName,
99 G4bool ascii)
100{
101 std::ofstream fOut;
102
103 // open output file //
104 if (!ascii)
105 { fOut.open(fileName, std::ios::out|std::ios::binary); }
106 else
107 { fOut.open(fileName, std::ios::out); }
108
109 // check if the file has been opened successfully
110 if (!fOut)
111 {
112#ifdef G4VERBOSE
113 G4cerr << "G4PhysicsTable::StorePhysicsTable():";
114 G4cerr << " Cannot open file: " << fileName << G4endl;
115#endif
116 fOut.close();
117 return false;
118 }
119
120 // Number of elements
121 size_t tableSize = size();
122 if (!ascii)
123 {
124 fOut.write( (char*)(&tableSize), sizeof tableSize);
125 }
126 else
127 {
128 fOut << tableSize << G4endl;
129 }
130
131 // Physics Vector
132 for (G4PhysicsTableIterator itr=begin(); itr!=end(); ++itr)
133 {
134 G4int vType = (*itr)->GetType();
135 if (!ascii)
136 {
137 fOut.write( (char*)(&vType), sizeof vType);
138 }
139 else
140 {
141 fOut << vType << G4endl;
142 }
143 (*itr)->Store(fOut,ascii);
144 }
145 fOut.close();
146 return true;
147}
148
149
150G4bool G4PhysicsTable::ExistPhysicsTable(const G4String& fileName) const
151{
152 std::ifstream fIn;
153 G4bool value=true;
154 // open input file
155 fIn.open(fileName,std::ios::in);
156
157 // check if the file has been opened successfully
158 if (!fIn)
159 {
160 value = false;
161 }
162 fIn.close();
163 return value;
164}
165
166G4bool G4PhysicsTable::RetrievePhysicsTable(const G4String& fileName,
167 G4bool ascii)
168{
169 std::ifstream fIn;
170 // open input file
171 if (ascii)
172 { fIn.open(fileName,std::ios::in|std::ios::binary); }
173 else
174 { fIn.open(fileName,std::ios::in);}
175
176 // check if the file has been opened successfully
177 if (!fIn)
178 {
179#ifdef G4VERBOSE
180 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
181 G4cerr << " Cannot open file: " << fileName << G4endl;
182#endif
183 fIn.close();
184 return false;
185 }
186
187 // clear
188 clearAndDestroy();
189
190 // Number of elements
191 G4int tableSize=0;
192 if (!ascii)
193 {
194 fIn.read((char*)(&tableSize), sizeof tableSize);
195 }
196 else
197 {
198 fIn >> tableSize;
199 }
200 if (tableSize<=0)
201 {
202#ifdef G4VERBOSE
203 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
204 G4cerr << " Invalid table size: " << tableSize << G4endl;
205#endif
206 return false;
207 }
208 reserve(tableSize);
209 vecFlag.clear();
210
211 // Physics Vector
212 for (G4int idx=0; idx<tableSize; ++idx)
213 {
214 G4int vType=0;
215 if (!ascii)
216 {
217 fIn.read( (char*)(&vType), sizeof vType);
218 }
219 else
220 {
221 fIn >> vType;
222 }
223 G4PhysicsVector* pVec = CreatePhysicsVector(vType);
224 if (pVec==0)
225 {
226#ifdef G4VERBOSE
227 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
228 G4cerr << " Illegal Physics Vector type: " << vType << " in: ";
229 G4cerr << fileName << G4endl;
230#endif
231 fIn.close();
232 return false;
233 }
234
235 if (! (pVec->Retrieve(fIn,ascii)) )
236 {
237#ifdef G4VERBOSE
238 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
239 G4cerr << " Rrror in retreiving " << idx
240 << "-th Physics Vector from file: ";
241 G4cerr << fileName << G4endl;
242#endif
243 fIn.close();
244 return false;
245 }
246
247 // add a PhysicsVector to this PhysicsTable
248 G4PhysCollection::push_back(pVec);
249 vecFlag.push_back(true);
250
251 }
252 fIn.close();
253 return true;
254}
255
256std::ostream& operator<<(std::ostream& out,
257 G4PhysicsTable& right)
258{
259 // Printout Physics Vector
260 size_t i=0;
261 for (G4PhysicsTableIterator itr=right.begin(); itr!=right.end(); ++itr)
262 {
263 out << std::setw(8) << i << "-th Vector ";
264 out << ": Type " << G4int((*itr)->GetType()) ;
265 out << ": Flag ";
266 if (right.GetFlag(i))
267 {
268 out << " T";
269 }
270 else
271 {
272 out << " F";
273 }
274 out << G4endl;
275 out << *(*itr);
276 i +=1;
277 }
278 out << G4endl;
279 return out;
280}
281
282void G4PhysicsTable::ResetFlagArray()
283{
284 size_t tableSize = G4PhysCollection::size();
285 vecFlag.clear();
286 for (size_t idx=0; idx<tableSize; idx++)
287 {
288 vecFlag.push_back(true);
289 }
290}
291
292G4PhysicsVector* G4PhysicsTable::CreatePhysicsVector(G4int type)
293{
294 G4PhysicsVector* pVector=0;
295 switch (type)
296 {
297 case T_G4PhysicsLinearVector:
298 pVector = new G4PhysicsLinearVector();
299 break;
300
301 case T_G4PhysicsLogVector:
302 pVector = new G4PhysicsLogVector();
303 break;
304
305 case T_G4PhysicsLnVector:
306 pVector = new G4PhysicsLnVector();
307 break;
308
309 case T_G4PhysicsFreeVector:
310 pVector = new G4PhysicsFreeVector();
311 break;
312
313 case T_G4PhysicsOrderedFreeVector:
314 pVector = new G4PhysicsOrderedFreeVector();
315 break;
316
317 case T_G4LPhysicsFreeVector:
318 pVector = new G4LPhysicsFreeVector();
319 break;
320
321 default:
322 break;
323 }
324 return pVector;
325}
Note: See TracBrowser for help on using the repository browser.