Geant4-11
G4PhysicsTable.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// G4PhysicsTable class implementation
27//
28// Author: G.Cosmo, 2 December 1995
29// First implementation based on object model
30// Revisions:
31// - 1st March 1996, K.Amako: modified
32// - 24th February 2001, H.Kurashige: migration to STL vectors
33// --------------------------------------------------------------------
34
35#include <fstream>
36#include <iomanip>
37#include <iostream>
38
41#include "G4PhysicsLogVector.hh"
42#include "G4PhysicsTable.hh"
43#include "G4PhysicsVector.hh"
45
46// --------------------------------------------------------------------
49{}
50
51// --------------------------------------------------------------------
52G4PhysicsTable::G4PhysicsTable(std::size_t cap)
53 : G4PhysCollection()
54{
55 reserve(cap);
56 vecFlag.reserve(cap);
57}
58
59// --------------------------------------------------------------------
61{
62 G4PhysCollection::clear();
63 vecFlag.clear();
64}
65
66// --------------------------------------------------------------------
67void G4PhysicsTable::resize(std::size_t siz, G4PhysicsVector* vec)
68{
69 G4PhysCollection::resize(siz, vec);
70 vecFlag.resize(siz, true);
71}
72
73// --------------------------------------------------------------------
75{
76 std::ofstream fOut;
77
78 // open output file
79 if(!ascii)
80 {
81 fOut.open(fileName, std::ios::out | std::ios::binary);
82 }
83 else
84 {
85 fOut.open(fileName, std::ios::out);
86 }
87
88 // check if the file has been opened successfully
89 if(!fOut.is_open())
90 {
91#ifdef G4VERBOSE
92 G4cerr << "G4PhysicsTable::StorePhysicsTable():";
93 G4cerr << " Cannot open file: " << fileName << G4endl;
94#endif
95 fOut.close();
96 return false;
97 }
98
99 // Number of elements
100 std::size_t tableSize = size();
101 if(!ascii)
102 {
103 fOut.write((char*) (&tableSize), sizeof tableSize);
104 }
105 else
106 {
107 fOut << tableSize << G4endl;
108 }
109
110 // Physics Vector
111 for(auto itr = cbegin(); itr != cend(); ++itr)
112 {
113 G4int vType = (*itr)->GetType();
114 if(!ascii)
115 {
116 fOut.write((char*) (&vType), sizeof vType);
117 }
118 else
119 {
120 fOut << vType << G4endl;
121 }
122 (*itr)->Store(fOut, ascii);
123 }
124 fOut.close();
125 return true;
126}
127
128// --------------------------------------------------------------------
130{
131 std::ifstream fIn;
132 G4bool value = true;
133 // open input file
134 fIn.open(fileName, std::ios::in);
135
136 // check if the file has been opened successfully
137 if(!fIn)
138 {
139 value = false;
140 }
141 fIn.close();
142 return value;
143}
144
145// --------------------------------------------------------------------
147 G4bool ascii, G4bool spline)
148{
149 std::ifstream fIn;
150 // open input file
151 if(ascii)
152 {
153 fIn.open(fileName, std::ios::in | std::ios::binary);
154 }
155 else
156 {
157 fIn.open(fileName, std::ios::in);
158 }
159
160 // check if the file has been opened successfully
161 if(!fIn.is_open())
162 {
163#ifdef G4VERBOSE
164 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
165 G4cerr << " Cannot open file: " << fileName << G4endl;
166#endif
167 fIn.close();
168 return false;
169 }
170
171 // clear
173
174 // Number of elements
175 std::size_t tableSize = 0;
176 if(!ascii)
177 {
178 fIn.read((char*) (&tableSize), sizeof tableSize);
179 }
180 else
181 {
182 fIn >> tableSize;
183 }
184 reserve(tableSize);
185 vecFlag.clear();
186
187 // Physics Vector
188 for(std::size_t idx = 0; idx < tableSize; ++idx)
189 {
190 G4int vType = 0;
191 if(!ascii)
192 {
193 fIn.read((char*) (&vType), sizeof vType);
194 }
195 else
196 {
197 fIn >> vType;
198 }
199 G4PhysicsVector* pVec = CreatePhysicsVector(vType, spline);
200 if(pVec == nullptr)
201 {
202#ifdef G4VERBOSE
203 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
204 G4cerr << " Illegal Physics Vector type: " << vType << " in: ";
205 G4cerr << fileName << G4endl;
206#endif
207 fIn.close();
208 return false;
209 }
210
211 if(!(pVec->Retrieve(fIn, ascii)))
212 {
213#ifdef G4VERBOSE
214 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
215 G4cerr << " Rrror in retreiving " << idx
216 << "-th Physics Vector from file: ";
217 G4cerr << fileName << G4endl;
218#endif
219 fIn.close();
220 return false;
221 }
222
223 // add a PhysicsVector to this PhysicsTable
224 G4PhysCollection::push_back(pVec);
225 vecFlag.push_back(true);
226 }
227 fIn.close();
228 return true;
229}
230
231// --------------------------------------------------------------------
232std::ostream& operator<<(std::ostream& out, G4PhysicsTable& right)
233{
234 // Printout Physics Vector
235 std::size_t i = 0;
236 for(auto itr = right.cbegin(); itr != right.cend(); ++itr)
237 {
238 out << std::setw(8) << i << "-th Vector ";
239 out << ": Type " << G4int((*itr)->GetType());
240 out << ": Flag ";
241 if(right.GetFlag(i))
242 {
243 out << " T";
244 }
245 else
246 {
247 out << " F";
248 }
249 out << G4endl;
250 out << *(*itr);
251 ++i;
252 }
253 out << G4endl;
254 return out;
255}
256
257// --------------------------------------------------------------------
259{
260 size_t tableSize = G4PhysCollection::size();
261 vecFlag.clear();
262 for(std::size_t idx = 0; idx < tableSize; ++idx)
263 {
264 vecFlag.push_back(true);
265 }
266}
267
268// --------------------------------------------------------------------
270{
271 G4PhysicsVector* pVector = nullptr;
272 switch(type)
273 {
275 pVector = new G4PhysicsLinearVector(spline);
276 break;
277
279 pVector = new G4PhysicsLogVector(spline);
280 break;
281
282 default:
283 pVector = new G4PhysicsVector(spline);
284 break;
285 }
286 return pVector;
287}
@ T_G4PhysicsLinearVector
@ T_G4PhysicsLogVector
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
void clearAndDestroy()
G4bool GetFlag(std::size_t i) const
G4FlagCollection vecFlag
G4bool RetrievePhysicsTable(const G4String &filename, G4bool ascii=false, G4bool spline=false)
G4bool ExistPhysicsTable(const G4String &fileName) const
G4bool StorePhysicsTable(const G4String &filename, G4bool ascii=false)
void resize(std::size_t, G4PhysicsVector *vec=(G4PhysicsVector *)(0))
G4PhysicsVector * CreatePhysicsVector(G4int type, G4bool spline)
std::vector< G4PhysicsVector * > G4PhysCollection
virtual ~G4PhysicsTable()
G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)