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

#include <G4VScoreWriter.hh>

Public Member Functions

 G4VScoreWriter ()=default
 
virtual ~G4VScoreWriter ()=default
 
virtual void DumpQuantityToFile (const G4String &psName, const G4String &fileName, const G4String &option)
 
virtual void DumpAllQuantitiesToFile (const G4String &fileName, const G4String &option)
 
void SetScoringMesh (G4VScoringMesh *sm)
 
void SetVerboseLevel (G4int vl)
 
void SetFactor (G4double val=1.0)
 
G4double GetFactor () const
 

Protected Member Functions

G4int GetIndex (G4int x, G4int y, G4int z) const
 

Protected Attributes

G4int fNMeshSegments [3] = {0, 0, 0}
 
G4VScoringMeshfScoringMesh = nullptr
 
G4int verboseLevel = 0
 
G4double fact = 1.0
 

Detailed Description

Definition at line 40 of file G4VScoreWriter.hh.

Constructor & Destructor Documentation

◆ G4VScoreWriter()

G4VScoreWriter::G4VScoreWriter ( )
default

◆ ~G4VScoreWriter()

virtual G4VScoreWriter::~G4VScoreWriter ( )
virtualdefault

Member Function Documentation

◆ DumpAllQuantitiesToFile()

void G4VScoreWriter::DumpAllQuantitiesToFile ( const G4String fileName,
const G4String option 
)
virtual

Definition at line 159 of file G4VScoreWriter.cc.

161{
162 // change the option string into lowercase to the case-insensitive.
163 G4String opt = option;
164 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
165
166 // confirm the option
167 if(opt.empty())
168 opt = "csv";
169 if(opt.find("csv") == std::string::npos &&
170 opt.find("sequence") == std::string::npos)
171 {
172 G4cerr << "ERROR : DumpToFile : Unknown option -> " << option << G4endl;
173 return;
174 }
175
176 // open the file
177 std::ofstream ofile(fileName);
178 if(!ofile)
179 {
180 G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
181 return;
182 }
183 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
184 if(fact != 1.0)
185 {
186 ofile << "# multiplied factor : " << fact << std::endl;
187 }
188
189 // retrieve the map
190 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
191 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
192 // NOLINTNEXTLINE(modernize-use-auto): Explicitly want a const_iterator
193 MeshScoreMap::const_iterator msMapItr = fSMap.begin();
194 std::map<G4int, G4StatDouble*>* score;
195 for(; msMapItr != fSMap.end(); msMapItr++)
196 {
197 G4String psname = msMapItr->first;
198
199 score = msMapItr->second->GetMap();
200 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
201
202 G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
203 G4String unit = fScoringMesh->GetPSUnit(psname);
204 G4String divisionAxisNames[3];
205 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
206 // index order
207 ofile << "# i" << divisionAxisNames[0] << ", i" << divisionAxisNames[1]
208 << ", i" << divisionAxisNames[2];
209 // unit of scored value
210 ofile << ", total(value) ";
211 if(!unit.empty())
212 ofile << "[" << unit << "]";
213 ofile << ", total(val^2), entry" << G4endl;
214
215 // "sequence" option: write header info
216 if(opt.find("sequence") != std::string::npos)
217 {
218 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " "
219 << fNMeshSegments[2] << G4endl;
220 }
221
222 // write quantity
223 long count = 0;
224 ofile << std::setprecision(16); // for double value with 8 bytes
225 for(int x = 0; x < fNMeshSegments[0]; x++)
226 {
227 for(int y = 0; y < fNMeshSegments[1]; y++)
228 {
229 for(int z = 0; z < fNMeshSegments[2]; z++)
230 {
231 G4int idx = GetIndex(x, y, z);
232
233 if(opt.find("csv") != std::string::npos)
234 ofile << x << "," << y << "," << z << ",";
235
236 auto value = score->find(idx);
237 if(value == score->end())
238 {
239 ofile << 0. << "," << 0. << "," << 0;
240 }
241 else
242 {
243 ofile << (value->second->sum_wx()) / unitValue * fact << ","
244 << (value->second->sum_wx2()) / unitValue / unitValue * fact *
245 fact
246 << "," << value->second->n();
247 }
248
249 if(opt.find("csv") != std::string::npos)
250 {
251 ofile << G4endl;
252 }
253 else if(opt.find("sequence") != std::string::npos)
254 {
255 ofile << " ";
256 if(count++ % 5 == 4)
257 ofile << G4endl;
258 }
259
260 } // z
261 } // y
262 } // x
263 ofile << std::setprecision(6);
264
265 } // for(; msMapItr ....)
266
267 // close the file
268 ofile.close();
269}
double G4double
Definition: G4Types.hh:83
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4int fNMeshSegments[3]
G4VScoringMesh * fScoringMesh
G4int GetIndex(G4int x, G4int y, G4int z) const
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

Referenced by G4ScoringManager::DumpAllQuantitiesToFile().

◆ DumpQuantityToFile()

void G4VScoreWriter::DumpQuantityToFile ( const G4String psName,
const G4String fileName,
const G4String option 
)
virtual

Definition at line 45 of file G4VScoreWriter.cc.

48{
49 // change the option string into lowercase to the case-insensitive.
50 G4String opt = option;
51 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
52
53 // confirm the option
54 if(opt.empty())
55 opt = "csv";
56 if(opt.find("csv") == std::string::npos &&
57 opt.find("sequence") == std::string::npos)
58 {
59 G4cerr << "ERROR : DumpToFile : Unknown option -> " << option << G4endl;
60 return;
61 }
62
63 // open the file
64 std::ofstream ofile(fileName);
65 if(!ofile)
66 {
67 G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
68 return;
69 }
70 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
71
72 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
73 // retrieve the map
74 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
75
76 // NOLINTNEXTLINE(modernize-use-auto): Explicitly want a const_iterator
77 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
78 if(msMapItr == fSMap.end())
79 {
80 G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" << psName << "\"."
81 << G4endl;
82 return;
83 }
84
85 std::map<G4int, G4StatDouble*>* score = msMapItr->second->GetMap();
86 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
87 if(fact != 1.0)
88 {
89 ofile << "# multiplied factor : " << fact << std::endl;
90 }
91
92 G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
93 G4String unit = fScoringMesh->GetPSUnit(psName);
94 G4String divisionAxisNames[3];
95 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
96 // index order
97 ofile << "# i" << divisionAxisNames[0] << ", i" << divisionAxisNames[1]
98 << ", i" << divisionAxisNames[2];
99 // unit of scored value
100 ofile << ", total(value) ";
101 if(!unit.empty())
102 ofile << "[" << unit << "]";
103 ofile << ", total(val^2), entry" << G4endl;
104
105 // "sequence" option: write header info
106 if(opt.find("sequence") != std::string::npos)
107 {
108 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " "
109 << fNMeshSegments[2] << G4endl;
110 }
111
112 // write quantity
113 long count = 0;
114 ofile << std::setprecision(16); // for double value with 8 bytes
115 for(int x = 0; x < fNMeshSegments[0]; x++)
116 {
117 for(int y = 0; y < fNMeshSegments[1]; y++)
118 {
119 for(int z = 0; z < fNMeshSegments[2]; z++)
120 {
121 G4int idx = GetIndex(x, y, z);
122
123 if(opt.find("csv") != std::string::npos)
124 ofile << x << "," << y << "," << z << ",";
125
126 auto value = score->find(idx);
127 if(value == score->end())
128 {
129 ofile << 0. << "," << 0. << "," << 0;
130 }
131 else
132 {
133 ofile << (value->second->sum_wx()) / unitValue * fact << ","
134 << (value->second->sum_wx2()) / unitValue / unitValue * fact *
135 fact
136 << "," << value->second->n();
137 }
138
139 if(opt.find("csv") != std::string::npos)
140 {
141 ofile << G4endl;
142 }
143 else if(opt.find("sequence") != std::string::npos)
144 {
145 ofile << " ";
146 if(count++ % 5 == 4)
147 ofile << G4endl;
148 }
149
150 } // z
151 } // y
152 } // x
153 ofile << std::setprecision(6);
154
155 // close the file
156 ofile.close();
157}

Referenced by G4ScoringManager::DumpQuantityToFile().

◆ GetFactor()

G4double G4VScoreWriter::GetFactor ( ) const
inline

Definition at line 60 of file G4VScoreWriter.hh.

60{ return fact; }

Referenced by G4ScoringManager::GetFactor().

◆ GetIndex()

G4int G4VScoreWriter::GetIndex ( G4int  x,
G4int  y,
G4int  z 
) const
protected

Definition at line 271 of file G4VScoreWriter.cc.

272{
273 return x * fNMeshSegments[1] * fNMeshSegments[2] + y * fNMeshSegments[2] + z;
274}

Referenced by DumpAllQuantitiesToFile(), and DumpQuantityToFile().

◆ SetFactor()

void G4VScoreWriter::SetFactor ( G4double  val = 1.0)
inline

Definition at line 59 of file G4VScoreWriter.hh.

59{ fact = val; }

Referenced by G4ScoringManager::SetFactor().

◆ SetScoringMesh()

void G4VScoreWriter::SetScoringMesh ( G4VScoringMesh sm)

Definition at line 38 of file G4VScoreWriter.cc.

39{
40 // Should be checked for nullptr!
43}
void GetNumberOfSegments(G4int nSegment[3])

Referenced by G4ScoringManager::DumpAllQuantitiesToFile(), and G4ScoringManager::DumpQuantityToFile().

◆ SetVerboseLevel()

void G4VScoreWriter::SetVerboseLevel ( G4int  vl)
inline

Member Data Documentation

◆ fact

G4double G4VScoreWriter::fact = 1.0
protected

◆ fNMeshSegments

G4int G4VScoreWriter::fNMeshSegments[3] = {0, 0, 0}
protected

◆ fScoringMesh

G4VScoringMesh* G4VScoreWriter::fScoringMesh = nullptr
protected

Definition at line 68 of file G4VScoreWriter.hh.

Referenced by DumpAllQuantitiesToFile(), DumpQuantityToFile(), and SetScoringMesh().

◆ verboseLevel

G4int G4VScoreWriter::verboseLevel = 0
protected

Definition at line 69 of file G4VScoreWriter.hh.

Referenced by SetVerboseLevel().


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