File indexing completed on 2025-02-25 09:22:35
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #include "GRScoreWriter.hh"
0044
0045 #include <map>
0046 #include <fstream>
0047
0048 #include "G4MultiFunctionalDetector.hh"
0049 #include "G4SDParticleFilter.hh"
0050 #include "G4VPrimitiveScorer.hh"
0051 #include "G4VScoringMesh.hh"
0052
0053 GRScoreWriter::GRScoreWriter()
0054 {;}
0055
0056 GRScoreWriter::~GRScoreWriter()
0057 {;}
0058
0059 void GRScoreWriter::DumpQuantityToFile(const G4String& psName,
0060 const G4String& fileName,
0061 const G4String& option) {
0062
0063
0064 G4String opt = option;
0065 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
0066
0067
0068 if(opt.size() == 0) opt = "csv";
0069 if(opt.find("csv") == std::string::npos &&
0070 opt.find("sequence") == std::string::npos) {
0071 G4cerr << "ERROR : DumpToFile : Unknown option -> "
0072 << option << G4endl;
0073 return;
0074 }
0075
0076
0077 std::ofstream ofile(fileName);
0078 if(!ofile) {
0079 G4cerr << "ERROR : DumpToFile : File open error -> "
0080 << fileName << G4endl;
0081 return;
0082 }
0083 ofile << "# Mesh or volume name: " << fScoringMesh->GetWorldName() << G4endl;
0084
0085 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
0086
0087 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
0088
0089
0090 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
0091 if(msMapItr == fSMap.end()) {
0092 G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
0093 << psName << "\"." << G4endl;
0094 return;
0095 }
0096
0097 std::map<G4int, G4StatDouble*> * score = msMapItr->second->GetMap();
0098 ofile << "# Primitive scorer name: " << msMapItr->first << G4endl;
0099 if(fact!=1.0)
0100 { ofile << "# Multiplication factor : " << fact << G4endl; }
0101
0102 G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
0103 G4String unit = fScoringMesh->GetPSUnit(psName);
0104 G4String divisionAxisNames[3];
0105 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
0106 ofile << "# First three integer entries: index of a cell of a mesh, just one cell for a volume tally." << G4endl;
0107 ofile << "# Forth entry: sum of scores." << G4endl;
0108 ofile << "# Fifth entry: sum of squared scores." << G4endl;
0109 ofile << "# Sixth entry: number of events with non-zero effect." << G4endl;
0110 ofile << "# Seventh entry: relative statistical error in %." << G4endl << G4endl;
0111
0112 ofile << "# i" << divisionAxisNames[0]
0113 << ", i" << divisionAxisNames[1]
0114 << ", i" << divisionAxisNames[2];
0115
0116 ofile << ", total(value) ";
0117 if(unit.size() > 0) ofile << "[" << unit << "]";
0118 ofile << ", total(value^2), number of entries, relative error (%)" << G4endl;
0119
0120
0121 if(opt.find("sequence") != std::string::npos) {
0122 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
0123 << G4endl;
0124 }
0125
0126
0127 long count = 0;
0128 ofile << std::setprecision(16);
0129 for(int x = 0; x < fNMeshSegments[0]; x++) {
0130 for(int y = 0; y < fNMeshSegments[1]; y++) {
0131 for(int z = 0; z < fNMeshSegments[2]; z++) {
0132 G4int idx = GetIndex(x, y, z);
0133
0134 if(opt.find("csv") != std::string::npos)
0135 ofile << x << "," << y << "," << z << ",";
0136
0137 std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
0138 if(value == score->end()) {
0139 ofile << 0. << "," << 0. << "," << 0;
0140 } else {
0141 G4double x1 = value->second->sum_wx()/unitValue*fact;
0142 G4double x2 = value->second->sum_wx2()/unitValue/unitValue*fact*fact;
0143 G4int n = value->second->n();
0144
0145
0146 G4double rms = value->second->rms()/unitValue*fact;
0147
0148 G4double relError = rms*std::sqrt(n)*100./x1;
0149 ofile << x1 << ", " << x2 << ", " << n << ", " << relError;
0150 ofile << G4endl;
0151 G4double factor = relError*relError/100.;
0152 if (factor > 1.)
0153 {
0154 G4cout << "# Mesh or volume name: " << fScoringMesh->GetWorldName()
0155 << " -- # Primitive scorer name: " << msMapItr->first << G4endl
0156 << " bin " << x << "," << y << "," << z << " : statistical error " << relError << "(%)" << G4endl
0157 << " to reduce the statistical error below 10%, increase number of events approximately "
0158 << factor << " times." << G4endl;
0159 }
0160 }
0161
0162 if(opt.find("csv") != std::string::npos) {
0163 ofile << G4endl;
0164 } else if(opt.find("sequence") != std::string::npos) {
0165 ofile << " ";
0166 if(count++%5 == 4) ofile << G4endl;
0167 }
0168
0169 }
0170 }
0171 }
0172 ofile << std::setprecision(6);
0173
0174
0175 ofile.close();
0176 }
0177
0178 void GRScoreWriter::DumpAllQuantitiesToFile(const G4String& fileName,
0179 const G4String& option) {
0180
0181
0182 G4String opt = option;
0183 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
0184
0185
0186 if(opt.size() == 0) opt = "csv";
0187 if(opt.find("csv") == std::string::npos &&
0188 opt.find("sequence") == std::string::npos) {
0189 G4cerr << "ERROR : DumpToFile : Unknown option -> "
0190 << option << G4endl;
0191 return;
0192 }
0193
0194
0195 std::ofstream ofile(fileName);
0196 if(!ofile) {
0197 G4cerr << "ERROR : DumpToFile : File open error -> "
0198 << fileName << G4endl;
0199 return;
0200 }
0201 ofile << "# Mesh or volume name: " << fScoringMesh->GetWorldName() << G4endl;
0202 if(fact!=1.0)
0203 { ofile << "# Multiplication factor : " << fact << G4endl; }
0204 ofile << "# First three integer entries: index of a cell of a mesh, just one cell for a volume tally." << G4endl;
0205 ofile << "# Forth entry: sum of scores." << G4endl;
0206 ofile << "# Fifth entry: sum of squared scores." << G4endl;
0207 ofile << "# Sixth entry: number of events with non-zero effect." << G4endl;
0208 ofile << "# Seventh entry: relative statistical error in %." << G4endl << G4endl;
0209
0210
0211 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
0212 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
0213 MeshScoreMap::const_iterator msMapItr = fSMap.begin();
0214 std::map<G4int, G4StatDouble*> * score;
0215 for(; msMapItr != fSMap.end(); msMapItr++) {
0216
0217 G4String psname = msMapItr->first;
0218
0219 score = msMapItr->second->GetMap();
0220 ofile << "# Primitive scorer name: " << msMapItr->first << G4endl;
0221
0222 G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
0223 G4String unit = fScoringMesh->GetPSUnit(psname);
0224 G4String divisionAxisNames[3];
0225 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
0226
0227 ofile << "# i" << divisionAxisNames[0]
0228 << ", i" << divisionAxisNames[1]
0229 << ", i" << divisionAxisNames[2];
0230
0231 ofile << ", total(value) ";
0232 if(unit.size() > 0) ofile << "[" << unit << "]";
0233 ofile << ", total(value^2), number of entries, relative error (%)" << G4endl;
0234
0235
0236
0237 if(opt.find("sequence") != std::string::npos) {
0238 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
0239 << G4endl;
0240 }
0241
0242
0243 long count = 0;
0244 ofile << std::setprecision(16);
0245 for(int x = 0; x < fNMeshSegments[0]; x++) {
0246 for(int y = 0; y < fNMeshSegments[1]; y++) {
0247 for(int z = 0; z < fNMeshSegments[2]; z++) {
0248 G4int idx = GetIndex(x, y, z);
0249
0250 if(opt.find("csv") != std::string::npos)
0251 ofile << x << "," << y << "," << z << ",";
0252
0253 std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
0254 if(value == score->end()) {
0255 ofile << 0. << "," << 0. << "," << 0;
0256 } else {
0257 G4double x1 = value->second->sum_wx()/unitValue*fact;
0258 G4double x2 = value->second->sum_wx2()/unitValue/unitValue*fact*fact;
0259 G4int n = value->second->n();
0260
0261
0262 G4double rms = value->second->rms()/unitValue*fact;
0263
0264 G4double relError = rms*std::sqrt(n)*100./x1;
0265 ofile << x1 << ", " << x2 << ", " << n << ", " << relError;
0266 ofile << G4endl;
0267 G4double factor = relError*relError/100.;
0268 if (factor > 1.)
0269 {
0270 G4cout << "# Mesh or volume name: " << fScoringMesh->GetWorldName()
0271 << " -- # Primitive scorer name: " << msMapItr->first << G4endl
0272 << " bin " << x << "," << y << "," << z << " : statistical error " << relError << "(%)" << G4endl
0273 << " to reduce the statistical error below 10%, increase number of events approximately "
0274 << factor << " times." << G4endl;
0275 }
0276 }
0277
0278 if(opt.find("csv") != std::string::npos) {
0279 ofile << G4endl;
0280 } else if(opt.find("sequence") != std::string::npos) {
0281 ofile << " ";
0282 if(count++%5 == 4) ofile << G4endl;
0283 }
0284
0285 }
0286 }
0287 }
0288 ofile << std::setprecision(6);
0289
0290 }
0291
0292
0293 ofile.close();
0294
0295 }
0296
0297