Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 //
27 // $Id: G4PhysicsTable.cc 98864 2016-08-15 11:53:26Z gcosmo $
28 //
29 //
30 // ------------------------------------------------------------
31 // GEANT 4 class implementation
32 //
33 // G4PhysicsTable
34 //
35 // ------------------------------------------------------------
36 
37 #include <iostream>
38 #include <fstream>
39 #include <iomanip>
40 
41 #include "G4PhysicsVector.hh"
42 #include "G4PhysicsTable.hh"
43 #include "G4PhysicsVectorType.hh"
44 #include "G4LPhysicsFreeVector.hh"
45 #include "G4PhysicsLogVector.hh"
46 #include "G4PhysicsFreeVector.hh"
48 #include "G4PhysicsLinearVector.hh"
49 #include "G4PhysicsLnVector.hh"
50 
52  : G4PhysCollection()
53 {
54 }
55 
57  : G4PhysCollection()
58 {
59  reserve(cap);
60  vecFlag.reserve(cap);
61 }
62 
63 /*
64 G4PhysicsTable::G4PhysicsTable(const G4PhysicsTable& right)
65  : G4PhysCollection()
66 {
67  *this = right;
68 }
69 
70 G4PhysicsTable& 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 */
87 {
88  G4PhysCollection::clear();
89  vecFlag.clear();
90 }
91 
93 {
94  G4PhysCollection::resize(siz, vec);
95  vecFlag.resize(siz, true);
96 }
97 
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 
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 
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  size_t tableSize=0;
192  if (!ascii)
193  {
194  fIn.read((char*)(&tableSize), sizeof tableSize);
195  }
196  else
197  {
198  fIn >> tableSize;
199  }
200  reserve(tableSize);
201  vecFlag.clear();
202 
203  // Physics Vector
204  for (size_t idx=0; idx<tableSize; ++idx)
205  {
206  G4int vType=0;
207  if (!ascii)
208  {
209  fIn.read( (char*)(&vType), sizeof vType);
210  }
211  else
212  {
213  fIn >> vType;
214  }
215  G4PhysicsVector* pVec = CreatePhysicsVector(vType);
216  if (pVec==nullptr)
217  {
218 #ifdef G4VERBOSE
219  G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
220  G4cerr << " Illegal Physics Vector type: " << vType << " in: ";
221  G4cerr << fileName << G4endl;
222 #endif
223  fIn.close();
224  return false;
225  }
226 
227  if (! (pVec->Retrieve(fIn,ascii)) )
228  {
229 #ifdef G4VERBOSE
230  G4cerr << "G4PhysicsTable::RetrievePhysicsTable():";
231  G4cerr << " Rrror in retreiving " << idx
232  << "-th Physics Vector from file: ";
233  G4cerr << fileName << G4endl;
234 #endif
235  fIn.close();
236  return false;
237  }
238 
239  // add a PhysicsVector to this PhysicsTable
240  G4PhysCollection::push_back(pVec);
241  vecFlag.push_back(true);
242 
243  }
244  fIn.close();
245  return true;
246 }
247 
248 std::ostream& operator<<(std::ostream& out,
250 {
251  // Printout Physics Vector
252  size_t i=0;
253  for (G4PhysicsTableIterator itr=right.begin(); itr!=right.end(); ++itr)
254  {
255  out << std::setw(8) << i << "-th Vector ";
256  out << ": Type " << G4int((*itr)->GetType()) ;
257  out << ": Flag ";
258  if (right.GetFlag(i))
259  {
260  out << " T";
261  }
262  else
263  {
264  out << " F";
265  }
266  out << G4endl;
267  out << *(*itr);
268  i +=1;
269  }
270  out << G4endl;
271  return out;
272 }
273 
275 {
276  size_t tableSize = G4PhysCollection::size();
277  vecFlag.clear();
278  for (size_t idx=0; idx<tableSize; idx++)
279  {
280  vecFlag.push_back(true);
281  }
282 }
283 
285 {
286  G4PhysicsVector* pVector = nullptr;
287  switch (type)
288  {
290  pVector = new G4PhysicsLinearVector();
291  break;
292 
293  case T_G4PhysicsLogVector:
294  pVector = new G4PhysicsLogVector();
295  break;
296 
297  case T_G4PhysicsLnVector:
298  pVector = new G4PhysicsLogVector();
299  break;
300 
301  case T_G4PhysicsFreeVector:
302  pVector = new G4PhysicsFreeVector();
303  break;
304 
306  pVector = new G4PhysicsOrderedFreeVector();
307  break;
308 
310  pVector = new G4PhysicsFreeVector();
311  break;
312 
313  default:
314  break;
315  }
316  return pVector;
317 }
G4bool RetrievePhysicsTable(const G4String &filename, G4bool ascii=false)
G4FlagCollection vecFlag
int G4int
Definition: G4Types.hh:78
G4bool ExistPhysicsTable(const G4String &fileName) const
G4PhysicsTable::iterator G4PhysicsTableIterator
const XML_Char int const XML_Char * value
Definition: expat.h:331
bool G4bool
Definition: G4Types.hh:79
G4PhysicsVector * CreatePhysicsVector(G4int type)
void resize(size_t, G4PhysicsVector *vec=(G4PhysicsVector *)(0))
virtual G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
#define G4endl
Definition: G4ios.hh:61
G4bool StorePhysicsTable(const G4String &filename, G4bool ascii=false)
G4bool GetFlag(size_t i) const
virtual ~G4PhysicsTable()
void clearAndDestroy()
G4GLOB_DLL std::ostream G4cerr