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