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