Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
42#include "G4PhysicsLnVector.hh"
43#include "G4PhysicsLogVector.hh"
45#include "G4PhysicsTable.hh"
46#include "G4PhysicsVector.hh"
48
49// --------------------------------------------------------------------
51 : G4PhysCollection()
52{}
53
54// --------------------------------------------------------------------
55G4PhysicsTable::G4PhysicsTable(std::size_t cap)
56 : G4PhysCollection()
57{
58 reserve(cap);
59 vecFlag.reserve(cap);
60}
61
62// --------------------------------------------------------------------
64{
65 G4PhysCollection::clear();
66 vecFlag.clear();
67}
68
69// --------------------------------------------------------------------
70void G4PhysicsTable::resize(std::size_t siz, G4PhysicsVector* vec)
71{
72 G4PhysCollection::resize(siz, vec);
73 vecFlag.resize(siz, true);
74}
75
76// --------------------------------------------------------------------
78{
79 std::ofstream fOut;
80
81 // open output file
82 if(!ascii)
83 {
84 fOut.open(fileName, std::ios::out | std::ios::binary);
85 }
86 else
87 {
88 fOut.open(fileName, std::ios::out);
89 }
90
91 // check if the file has been opened successfully
92 if(!fOut)
93 {
94#ifdef G4VERBOSE
95 G4cerr << "G4PhysicsTable::StorePhysicsTable():";
96 G4cerr << " Cannot open file: " << fileName << G4endl;
97#endif
98 fOut.close();
99 return false;
100 }
101
102 // Number of elements
103 std::size_t tableSize = size();
104 if(!ascii)
105 {
106 fOut.write((char*) (&tableSize), sizeof tableSize);
107 }
108 else
109 {
110 fOut << tableSize << G4endl;
111 }
112
113 // Physics Vector
114 for(auto itr = cbegin(); itr != cend(); ++itr)
115 {
116 G4int vType = (*itr)->GetType();
117 if(!ascii)
118 {
119 fOut.write((char*) (&vType), sizeof vType);
120 }
121 else
122 {
123 fOut << vType << G4endl;
124 }
125 (*itr)->Store(fOut, ascii);
126 }
127 fOut.close();
128 return true;
129}
130
131// --------------------------------------------------------------------
133{
134 std::ifstream fIn;
135 G4bool value = true;
136 // open input file
137 fIn.open(fileName, std::ios::in);
138
139 // check if the file has been opened successfully
140 if(!fIn)
141 {
142 value = false;
143 }
144 fIn.close();
145 return value;
146}
147
148// --------------------------------------------------------------------
150 G4bool ascii)
151{
152 std::ifstream fIn;
153 // open input file
154 if(ascii)
155 {
156 fIn.open(fileName, std::ios::in | std::ios::binary);
157 }
158 else
159 {
160 fIn.open(fileName, std::ios::in);
161 }
162
163 // check if the file has been opened successfully
164 if(!fIn)
165 {
166#ifdef G4VERBOSE
167 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
168 G4cerr << " Cannot open file: " << fileName << G4endl;
169#endif
170 fIn.close();
171 return false;
172 }
173
174 // clear
176
177 // Number of elements
178 std::size_t tableSize = 0;
179 if(!ascii)
180 {
181 fIn.read((char*) (&tableSize), sizeof tableSize);
182 }
183 else
184 {
185 fIn >> tableSize;
186 }
187 reserve(tableSize);
188 vecFlag.clear();
189
190 // Physics Vector
191 for(std::size_t idx = 0; idx < tableSize; ++idx)
192 {
193 G4int vType = 0;
194 if(!ascii)
195 {
196 fIn.read((char*) (&vType), sizeof vType);
197 }
198 else
199 {
200 fIn >> vType;
201 }
203 if(pVec == nullptr)
204 {
205#ifdef G4VERBOSE
206 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
207 G4cerr << " Illegal Physics Vector type: " << vType << " in: ";
208 G4cerr << fileName << G4endl;
209#endif
210 fIn.close();
211 return false;
212 }
213
214 if(!(pVec->Retrieve(fIn, ascii)))
215 {
216#ifdef G4VERBOSE
217 G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
218 G4cerr << " Rrror in retreiving " << idx
219 << "-th Physics Vector from file: ";
220 G4cerr << fileName << G4endl;
221#endif
222 fIn.close();
223 return false;
224 }
225
226 // add a PhysicsVector to this PhysicsTable
227 G4PhysCollection::push_back(pVec);
228 vecFlag.push_back(true);
229 }
230 fIn.close();
231 return true;
232}
233
234// --------------------------------------------------------------------
235std::ostream& operator<<(std::ostream& out, G4PhysicsTable& right)
236{
237 // Printout Physics Vector
238 std::size_t i = 0;
239 for(auto itr = right.cbegin(); itr != right.cend(); ++itr)
240 {
241 out << std::setw(8) << i << "-th Vector ";
242 out << ": Type " << G4int((*itr)->GetType());
243 out << ": Flag ";
244 if(right.GetFlag(i))
245 {
246 out << " T";
247 }
248 else
249 {
250 out << " F";
251 }
252 out << G4endl;
253 out << *(*itr);
254 ++i;
255 }
256 out << G4endl;
257 return out;
258}
259
260// --------------------------------------------------------------------
262{
263 size_t tableSize = G4PhysCollection::size();
264 vecFlag.clear();
265 for(std::size_t idx = 0; idx < tableSize; ++idx)
266 {
267 vecFlag.push_back(true);
268 }
269}
270
271// --------------------------------------------------------------------
273{
274 G4PhysicsVector* pVector = nullptr;
275 switch(type)
276 {
278 pVector = new G4PhysicsLinearVector();
279 break;
280
282 pVector = new G4PhysicsLogVector();
283 break;
284
286 pVector = new G4PhysicsLogVector();
287 break;
288
290 pVector = new G4PhysicsFreeVector();
291 break;
292
294 pVector = new G4PhysicsOrderedFreeVector();
295 break;
296
298 pVector = new G4PhysicsFreeVector();
299 break;
300
301 default:
302 break;
303 }
304 return pVector;
305}
std::ostream & operator<<(std::ostream &out, G4PhysicsTable &right)
@ T_G4LPhysicsFreeVector
@ T_G4PhysicsOrderedFreeVector
@ T_G4PhysicsFreeVector
@ T_G4PhysicsLinearVector
@ T_G4PhysicsLogVector
@ T_G4PhysicsLnVector
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 ExistPhysicsTable(const G4String &fileName) const
G4bool StorePhysicsTable(const G4String &filename, G4bool ascii=false)
G4bool RetrievePhysicsTable(const G4String &filename, G4bool ascii=false)
void resize(std::size_t, G4PhysicsVector *vec=(G4PhysicsVector *)(0))
G4PhysicsVector * CreatePhysicsVector(G4int type)
virtual ~G4PhysicsTable()
virtual G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)