Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4OrderedTable.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// G4OrderedTable class implementation
27//
28// Author: M.Maire (LAPP), September 1996
29// Revisions: H.Kurashige (Kobe Univ.), January-September 2001
30// --------------------------------------------------------------------
31
32#include "G4OrderedTable.hh"
33#include "G4DataVector.hh"
34
35#include <fstream>
36#include <iomanip>
37#include <iostream>
38
39// --------------------------------------------------------------------
41 : std::vector<G4DataVector*>()
42{}
43
44// --------------------------------------------------------------------
46 : std::vector<G4DataVector*>(cap, (G4DataVector*) (0))
47{}
48
49// --------------------------------------------------------------------
51
52// --------------------------------------------------------------------
54{
55 G4DataVector* a = nullptr;
56 while(size() > 0)
57 {
58 a = back();
59 pop_back();
60 for(auto i = cbegin(); i != cend(); ++i)
61 {
62 if(*i == a)
63 {
64 erase(i);
65 --i;
66 }
67 }
68 if(a != nullptr)
69 {
70 delete a;
71 }
72 }
73}
74
75// --------------------------------------------------------------------
77{
78 std::ofstream fOut;
79
80 // open output file //
81 if(!ascii)
82 {
83 fOut.open(fileName, std::ios::out | std::ios::binary);
84 }
85 else
86 {
87 fOut.open(fileName, std::ios::out);
88 }
89
90 // check if the file has been opened successfully
91 if(!fOut)
92 {
93#ifdef G4VERBOSE
94 G4cerr << "G4OrderedTable::::Store():";
95 G4cerr << " Cannot open file: " << fileName << G4endl;
96#endif
97 fOut.close();
98 return false;
99 }
100
101 G4int tableSize = G4int(size()); // Number of elements
102 if(!ascii)
103 {
104 fOut.write((char*) (&tableSize), sizeof tableSize);
105 }
106 else
107 {
108 fOut << tableSize << G4endl;
109 }
110
111 G4int vType = G4DataVector::T_G4DataVector; // Data Vector
112 for(auto itr = cbegin(); itr != cend(); ++itr)
113 {
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 // open input file //
133 if(!ascii)
134 {
135 fIn.open(fileName, std::ios::in | std::ios::binary);
136 }
137 else
138 {
139 fIn.open(fileName, std::ios::in);
140 }
141
142 // check if the file has been opened successfully
143 if(!fIn)
144 {
145#ifdef G4VERBOSE
146 G4cerr << "G4OrderedTable::Retrieve():";
147 G4cerr << " Cannot open file: " << fileName << G4endl;
148#endif
149 fIn.close();
150 return false;
151 }
152
153 // clear
155
156 // Number of elements
157 G4int tableSize = 0;
158 if(!ascii)
159 {
160 fIn.read((char*) (&tableSize), sizeof tableSize);
161 }
162 else
163 {
164 fIn >> tableSize;
165 }
166 if(tableSize <= 0)
167 {
168#ifdef G4VERBOSE
169 G4cerr << "G4OrderedTable::Retrieve():";
170 G4cerr << " Invalid table size: " << tableSize << G4endl;
171#endif
172 return false;
173 }
174 reserve(tableSize);
175
176 // Physics Vector
177 for(G4int idx = 0; idx < tableSize; ++idx)
178 {
179 G4int vType = 0;
180 if(!ascii)
181 {
182 fIn.read((char*) (&vType), sizeof vType);
183 }
184 else
185 {
186 fIn >> vType;
187 }
189 {
190#ifdef G4VERBOSE
191 G4cerr << "G4OrderedTable::Retrieve():";
192 G4cerr << " Illegal Data Vector type: " << vType << " in ";
193 G4cerr << fileName << G4endl;
194#endif
195 fIn.close();
196 return false;
197 }
198
199 G4DataVector* pVec = new G4DataVector;
200
201 if(!(pVec->Retrieve(fIn, ascii)))
202 {
203#ifdef G4VERBOSE
204 G4cerr << "G4OrderedTable::Retrieve(): ";
205 G4cerr << " Error in retreiving " << idx
206 << "-th Physics Vector from file: ";
207 G4cerr << fileName << G4endl;
208#endif
209 fIn.close();
210 delete pVec;
211 return false;
212 }
213
214 // add a PhysicsVector to this OrderedTable
215 push_back(pVec);
216 }
217 fIn.close();
218 return true;
219}
220
221// --------------------------------------------------------------------
222std::ostream& operator<<(std::ostream& out, G4OrderedTable& right)
223{
224 // Printout Data Vector
225 std::size_t i = 0;
226 for(auto itr = right.cbegin(); itr != right.cend(); ++itr)
227 {
228 out << std::setw(8) << i << "-th Vector ";
229 out << ": Type " << G4DataVector::T_G4DataVector << G4endl;
230 out << *(*itr);
231 i += 1;
232 }
233 out << G4endl;
234 return out;
235}
std::ostream & operator<<(std::ostream &out, G4OrderedTable &right)
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4bool Retrieve(std::ifstream &fIn, G4bool ascii=false)
Definition: G4DataVector.cc:79
void clearAndDestroy()
G4bool Store(const G4String &filename, G4bool ascii=false)
virtual ~G4OrderedTable()
G4bool Retrieve(const G4String &filename, G4bool ascii=false)