Geant4-11
G4VScoreWriter.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//
28
29#include "G4VScoreWriter.hh"
30
32#include "G4SDParticleFilter.hh"
33#include "G4VPrimitiveScorer.hh"
34#include "G4VScoringMesh.hh"
35
36#include <map>
37#include <fstream>
38
40 : fScoringMesh(nullptr)
41 , verboseLevel(0)
42 , fact(1.0)
43{
45}
46
48
50{
53}
54
56 const G4String& fileName,
57 const G4String& option)
58{
59 // change the option string into lowercase to the case-insensitive.
60 G4String opt = option;
61 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
62
63 // confirm the option
64 if(opt.size() == 0)
65 opt = "csv";
66 if(opt.find("csv") == std::string::npos &&
67 opt.find("sequence") == std::string::npos)
68 {
69 G4cerr << "ERROR : DumpToFile : Unknown option -> " << option << G4endl;
70 return;
71 }
72
73 // open the file
74 std::ofstream ofile(fileName);
75 if(!ofile)
76 {
77 G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
78 return;
79 }
80 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
81
82 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
83 // retrieve the map
84 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
85
86 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
87 if(msMapItr == fSMap.end())
88 {
89 G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" << psName << "\"."
90 << G4endl;
91 return;
92 }
93
94 std::map<G4int, G4StatDouble*>* score = msMapItr->second->GetMap();
95 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
96 if(fact != 1.0)
97 {
98 ofile << "# multiplied factor : " << fact << std::endl;
99 }
100
101 G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
102 G4String unit = fScoringMesh->GetPSUnit(psName);
103 G4String divisionAxisNames[3];
104 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
105 // index order
106 ofile << "# i" << divisionAxisNames[0] << ", i" << divisionAxisNames[1]
107 << ", i" << divisionAxisNames[2];
108 // unit of scored value
109 ofile << ", total(value) ";
110 if(unit.size() > 0)
111 ofile << "[" << unit << "]";
112 ofile << ", total(val^2), entry" << G4endl;
113
114 // "sequence" option: write header info
115 if(opt.find("sequence") != std::string::npos)
116 {
117 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " "
118 << fNMeshSegments[2] << G4endl;
119 }
120
121 // write quantity
122 long count = 0;
123 ofile << std::setprecision(16); // for double value with 8 bytes
124 for(int x = 0; x < fNMeshSegments[0]; x++)
125 {
126 for(int y = 0; y < fNMeshSegments[1]; y++)
127 {
128 for(int z = 0; z < fNMeshSegments[2]; z++)
129 {
130 G4int idx = GetIndex(x, y, z);
131
132 if(opt.find("csv") != std::string::npos)
133 ofile << x << "," << y << "," << z << ",";
134
135 std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
136 if(value == score->end())
137 {
138 ofile << 0. << "," << 0. << "," << 0;
139 }
140 else
141 {
142 ofile << (value->second->sum_wx()) / unitValue * fact << ","
143 << (value->second->sum_wx2()) / unitValue / unitValue * fact *
144 fact
145 << "," << value->second->n();
146 }
147
148 if(opt.find("csv") != std::string::npos)
149 {
150 ofile << G4endl;
151 }
152 else if(opt.find("sequence") != std::string::npos)
153 {
154 ofile << " ";
155 if(count++ % 5 == 4)
156 ofile << G4endl;
157 }
158
159 } // z
160 } // y
161 } // x
162 ofile << std::setprecision(6);
163
164 // close the file
165 ofile.close();
166}
167
169 const G4String& option)
170{
171 // change the option string into lowercase to the case-insensitive.
172 G4String opt = option;
173 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
174
175 // confirm the option
176 if(opt.size() == 0)
177 opt = "csv";
178 if(opt.find("csv") == std::string::npos &&
179 opt.find("sequence") == std::string::npos)
180 {
181 G4cerr << "ERROR : DumpToFile : Unknown option -> " << option << G4endl;
182 return;
183 }
184
185 // open the file
186 std::ofstream ofile(fileName);
187 if(!ofile)
188 {
189 G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
190 return;
191 }
192 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
193 if(fact != 1.0)
194 {
195 ofile << "# multiplied factor : " << fact << std::endl;
196 }
197
198 // retrieve the map
199 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
200 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
201 MeshScoreMap::const_iterator msMapItr = fSMap.begin();
202 std::map<G4int, G4StatDouble*>* score;
203 for(; msMapItr != fSMap.end(); msMapItr++)
204 {
205 G4String psname = msMapItr->first;
206
207 score = msMapItr->second->GetMap();
208 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
209
210 G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
211 G4String unit = fScoringMesh->GetPSUnit(psname);
212 G4String divisionAxisNames[3];
213 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
214 // index order
215 ofile << "# i" << divisionAxisNames[0] << ", i" << divisionAxisNames[1]
216 << ", i" << divisionAxisNames[2];
217 // unit of scored value
218 ofile << ", total(value) ";
219 if(unit.size() > 0)
220 ofile << "[" << unit << "]";
221 ofile << ", total(val^2), entry" << G4endl;
222
223 // "sequence" option: write header info
224 if(opt.find("sequence") != std::string::npos)
225 {
226 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " "
227 << fNMeshSegments[2] << G4endl;
228 }
229
230 // write quantity
231 long count = 0;
232 ofile << std::setprecision(16); // for double value with 8 bytes
233 for(int x = 0; x < fNMeshSegments[0]; x++)
234 {
235 for(int y = 0; y < fNMeshSegments[1]; y++)
236 {
237 for(int z = 0; z < fNMeshSegments[2]; z++)
238 {
239 G4int idx = GetIndex(x, y, z);
240
241 if(opt.find("csv") != std::string::npos)
242 ofile << x << "," << y << "," << z << ",";
243
244 std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
245 if(value == score->end())
246 {
247 ofile << 0. << "," << 0. << "," << 0;
248 }
249 else
250 {
251 ofile << (value->second->sum_wx()) / unitValue * fact << ","
252 << (value->second->sum_wx2()) / unitValue / unitValue * fact *
253 fact
254 << "," << value->second->n();
255 }
256
257 if(opt.find("csv") != std::string::npos)
258 {
259 ofile << G4endl;
260 }
261 else if(opt.find("sequence") != std::string::npos)
262 {
263 ofile << " ";
264 if(count++ % 5 == 4)
265 ofile << G4endl;
266 }
267
268 } // z
269 } // y
270 } // x
271 ofile << std::setprecision(6);
272
273 } // for(; msMapItr ....)
274
275 // close the file
276 ofile.close();
277}
278
280{
281 // return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1];
282 return x * fNMeshSegments[1] * fNMeshSegments[2] + y * fNMeshSegments[2] + z;
283}
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
virtual ~G4VScoreWriter()
G4int fNMeshSegments[3]
G4VScoringMesh * fScoringMesh
virtual void DumpQuantityToFile(const G4String &psName, const G4String &fileName, const G4String &option)
virtual void DumpAllQuantitiesToFile(const G4String &fileName, const G4String &option)
G4int GetIndex(G4int x, G4int y, G4int z) const
void SetScoringMesh(G4VScoringMesh *sm)
void GetNumberOfSegments(G4int nSegment[3])
G4double GetPSUnitValue(const G4String &psname)
G4String GetPSUnit(const G4String &psname)
const G4String & GetWorldName() const
std::map< G4String, RunScore * > MeshScoreMap
void GetDivisionAxisNames(G4String divisionAxisNames[3])
MeshScoreMap GetScoreMap() const
std::ofstream ofile
Definition: clparse.cc:44
G4bool transform(G4String &input, const G4String &type)