Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4OrderedTable Class Reference

#include <G4OrderedTable.hh>

+ Inheritance diagram for G4OrderedTable:

Public Member Functions

 G4OrderedTable ()
 
 G4OrderedTable (std::size_t cap)
 
virtual ~G4OrderedTable ()
 
void clearAndDestroy ()
 
G4bool Store (const G4String &filename, G4bool ascii=false)
 
G4bool Retrieve (const G4String &filename, G4bool ascii=false)
 

Friends

std::ostream & operator<< (std::ostream &out, G4OrderedTable &table)
 

Detailed Description

Definition at line 43 of file G4OrderedTable.hh.

Constructor & Destructor Documentation

◆ G4OrderedTable() [1/2]

G4OrderedTable::G4OrderedTable ( )

Definition at line 40 of file G4OrderedTable.cc.

41 : std::vector<G4DataVector*>()
42{}

◆ G4OrderedTable() [2/2]

G4OrderedTable::G4OrderedTable ( std::size_t  cap)
explicit

Definition at line 45 of file G4OrderedTable.cc.

46 : std::vector<G4DataVector*>(cap, (G4DataVector*) (0))
47{}

◆ ~G4OrderedTable()

G4OrderedTable::~G4OrderedTable ( )
virtual

Definition at line 50 of file G4OrderedTable.cc.

50{}

Member Function Documentation

◆ clearAndDestroy()

void G4OrderedTable::clearAndDestroy ( )

Definition at line 53 of file G4OrderedTable.cc.

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}

Referenced by Retrieve(), and G4SandiaTable::~G4SandiaTable().

◆ Retrieve()

G4bool G4OrderedTable::Retrieve ( const G4String filename,
G4bool  ascii = false 
)

Definition at line 129 of file G4OrderedTable.cc.

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}
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()

◆ Store()

G4bool G4OrderedTable::Store ( const G4String filename,
G4bool  ascii = false 
)

Definition at line 76 of file G4OrderedTable.cc.

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}

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  out,
G4OrderedTable table 
)
friend

Definition at line 222 of file G4OrderedTable.cc.

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}

The documentation for this class was generated from the following files: