Geant4 11.1.1
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 ()=default
 
 G4OrderedTable (std::size_t cap)
 
virtual ~G4OrderedTable ()=default
 
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 ( )
default

◆ G4OrderedTable() [2/2]

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

Definition at line 40 of file G4OrderedTable.cc.

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

◆ ~G4OrderedTable()

virtual G4OrderedTable::~G4OrderedTable ( )
virtualdefault

Member Function Documentation

◆ clearAndDestroy()

void G4OrderedTable::clearAndDestroy ( )

Definition at line 45 of file G4OrderedTable.cc.

46{
47 G4DataVector* a = nullptr;
48 while(!empty())
49 {
50 a = back();
51 pop_back();
52 for(auto i = cbegin(); i != cend(); ++i)
53 {
54 if(*i == a)
55 {
56 erase(i);
57 --i;
58 }
59 }
60
61 delete a;
62 }
63}

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

◆ Retrieve()

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

Definition at line 119 of file G4OrderedTable.cc.

120{
121 std::ifstream fIn;
122 // open input file //
123 if(!ascii)
124 {
125 fIn.open(fileName, std::ios::in | std::ios::binary);
126 }
127 else
128 {
129 fIn.open(fileName, std::ios::in);
130 }
131
132 // check if the file has been opened successfully
133 if(!fIn)
134 {
135#ifdef G4VERBOSE
136 G4cerr << "G4OrderedTable::Retrieve():";
137 G4cerr << " Cannot open file: " << fileName << G4endl;
138#endif
139 fIn.close();
140 return false;
141 }
142
143 // clear
145
146 // Number of elements
147 G4int tableSize = 0;
148 if(!ascii)
149 {
150 fIn.read((char*) (&tableSize), sizeof tableSize);
151 }
152 else
153 {
154 fIn >> tableSize;
155 }
156 if(tableSize <= 0)
157 {
158#ifdef G4VERBOSE
159 G4cerr << "G4OrderedTable::Retrieve():";
160 G4cerr << " Invalid table size: " << tableSize << G4endl;
161#endif
162 return false;
163 }
164 reserve(tableSize);
165
166 // Physics Vector
167 for(G4int idx = 0; idx < tableSize; ++idx)
168 {
169 G4int vType = 0;
170 if(!ascii)
171 {
172 fIn.read((char*) (&vType), sizeof vType);
173 }
174 else
175 {
176 fIn >> vType;
177 }
179 {
180#ifdef G4VERBOSE
181 G4cerr << "G4OrderedTable::Retrieve():";
182 G4cerr << " Illegal Data Vector type: " << vType << " in ";
183 G4cerr << fileName << G4endl;
184#endif
185 fIn.close();
186 return false;
187 }
188
189 auto* pVec = new G4DataVector;
190
191 if(!(pVec->Retrieve(fIn, ascii)))
192 {
193#ifdef G4VERBOSE
194 G4cerr << "G4OrderedTable::Retrieve(): ";
195 G4cerr << " Error in retreiving " << idx
196 << "-th Physics Vector from file: ";
197 G4cerr << fileName << G4endl;
198#endif
199 fIn.close();
200 delete pVec;
201 return false;
202 }
203
204 // add a PhysicsVector to this OrderedTable
205 push_back(pVec);
206 }
207 fIn.close();
208 return true;
209}
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
void clearAndDestroy()

◆ Store()

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

Definition at line 66 of file G4OrderedTable.cc.

67{
68 std::ofstream fOut;
69
70 // open output file //
71 if(!ascii)
72 {
73 fOut.open(fileName, std::ios::out | std::ios::binary);
74 }
75 else
76 {
77 fOut.open(fileName, std::ios::out);
78 }
79
80 // check if the file has been opened successfully
81 if(!fOut)
82 {
83#ifdef G4VERBOSE
84 G4cerr << "G4OrderedTable::::Store():";
85 G4cerr << " Cannot open file: " << fileName << G4endl;
86#endif
87 fOut.close();
88 return false;
89 }
90
91 G4int tableSize = G4int(size()); // Number of elements
92 if(!ascii)
93 {
94 fOut.write((char*) (&tableSize), sizeof tableSize);
95 }
96 else
97 {
98 fOut << tableSize << G4endl;
99 }
100
101 G4int vType = G4DataVector::T_G4DataVector; // Data Vector
102 for(const auto itr : *this)
103 {
104 if(!ascii)
105 {
106 fOut.write((char*) (&vType), sizeof vType);
107 }
108 else
109 {
110 fOut << vType << G4endl;
111 }
112 itr->Store(fOut, ascii);
113 }
114 fOut.close();
115 return true;
116}

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 212 of file G4OrderedTable.cc.

213{
214 // Printout Data Vector
215 std::size_t i = 0;
216 for(const auto itr : right)
217 {
218 out << std::setw(8) << i << "-th Vector ";
219 out << ": Type " << G4DataVector::T_G4DataVector << G4endl;
220 out << *itr;
221 i += 1;
222 }
223 out << G4endl;
224 return out;
225}

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