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