Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4VScoreWriter << 26 // 27 // ------------------------------------------- << 27 // $Id: G4VScoreWriter.cc,v 1.10 2010-07-27 01:44:54 akimura Exp $ >> 28 // GEANT4 tag $Name: geant4-09-04-patch-01 $ >> 29 // 28 30 29 #include "G4VScoreWriter.hh" 31 #include "G4VScoreWriter.hh" 30 32 31 #include "G4MultiFunctionalDetector.hh" 33 #include "G4MultiFunctionalDetector.hh" >> 34 #include "G4SDParticleFilter.hh" 32 #include "G4VPrimitiveScorer.hh" 35 #include "G4VPrimitiveScorer.hh" 33 #include "G4VScoringMesh.hh" 36 #include "G4VScoringMesh.hh" 34 37 35 #include <map> 38 #include <map> 36 #include <fstream> 39 #include <fstream> 37 40 38 void G4VScoreWriter::SetScoringMesh(G4VScoring << 41 G4VScoreWriter::G4VScoreWriter() 39 { << 42 : fScoringMesh(0), verboseLevel(0) { 40 // Should be checked for nullptr! << 43 ; 41 fScoringMesh = sm; << 42 fScoringMesh->GetNumberOfSegments(fNMeshSegm << 43 } 44 } 44 45 45 void G4VScoreWriter::DumpQuantityToFile(const << 46 G4VScoreWriter::~G4VScoreWriter() { 46 const << 47 } 47 const << 48 48 { << 49 void G4VScoreWriter::SetScoringMesh(G4VScoringMesh * sm) { >> 50 fScoringMesh = sm; >> 51 fScoringMesh->GetNumberOfSegments(fNMeshSegments); >> 52 } >> 53 >> 54 void G4VScoreWriter::DumpQuantityToFile(G4String & psName, G4String & fileName, G4String & option) { >> 55 49 // change the option string into lowercase t 56 // change the option string into lowercase to the case-insensitive. 50 G4String opt = option; 57 G4String opt = option; 51 std::transform(opt.begin(), opt.end(), opt.b 58 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); 52 59 53 // confirm the option 60 // confirm the option 54 if(opt.empty()) << 61 if(opt.size() == 0) opt = "csv"; 55 opt = "csv"; << 56 if(opt.find("csv") == std::string::npos && 62 if(opt.find("csv") == std::string::npos && 57 opt.find("sequence") == std::string::npos << 63 opt.find("sequence") == std::string::npos) { 58 { << 64 G4cerr << "ERROR : DumpToFile : Unknown option -> " 59 G4cerr << "ERROR : DumpToFile : Unknown op << 65 << option << G4endl; 60 return; 66 return; 61 } 67 } 62 68 63 // open the file 69 // open the file 64 std::ofstream ofile(fileName); 70 std::ofstream ofile(fileName); 65 if(!ofile) << 71 if(!ofile) { 66 { << 72 G4cerr << "ERROR : DumpToFile : File open error -> " 67 G4cerr << "ERROR : DumpToFile : File open << 73 << fileName << G4endl; 68 return; 74 return; 69 } 75 } 70 ofile << "# mesh name: " << fScoringMesh->Ge 76 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; 71 77 72 using MeshScoreMap = G4VScoringMesh::MeshSco << 78 73 // retrieve the map 79 // retrieve the map 74 MeshScoreMap fSMap = fScoringMesh->GetScoreM 80 MeshScoreMap fSMap = fScoringMesh->GetScoreMap(); >> 81 75 82 76 // NOLINTNEXTLINE(modernize-use-auto): Expli << 77 MeshScoreMap::const_iterator msMapItr = fSMa 83 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName); 78 if(msMapItr == fSMap.end()) << 84 if(msMapItr == fSMap.end()) { 79 { << 85 G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" 80 G4cerr << "ERROR : DumpToFile : Unknown qu << 86 << psName << "\"." << G4endl; 81 << G4endl; << 82 return; 87 return; 83 } 88 } 84 89 85 std::map<G4int, G4StatDouble*>* score = msMa << 90 >> 91 std::map<G4int, G4double*> * score = msMapItr->second->GetMap(); 86 ofile << "# primitive scorer name: " << msMa 92 ofile << "# primitive scorer name: " << msMapItr->first << std::endl; 87 if(fact != 1.0) << 93 88 { << 89 ofile << "# multiplied factor : " << fact << 90 } << 91 94 92 G4double unitValue = fScoringMesh->GetPSUnit 95 G4double unitValue = fScoringMesh->GetPSUnitValue(psName); 93 G4String unit = fScoringMesh->GetPSUnit << 96 G4String unit = fScoringMesh->GetPSUnit(psName); 94 G4String divisionAxisNames[3]; 97 G4String divisionAxisNames[3]; 95 fScoringMesh->GetDivisionAxisNames(divisionA 98 fScoringMesh->GetDivisionAxisNames(divisionAxisNames); 96 // index order 99 // index order 97 ofile << "# i" << divisionAxisNames[0] << ", << 100 ofile << "# i" << divisionAxisNames[0] 98 << ", i" << divisionAxisNames[2]; << 101 << ", i" << divisionAxisNames[1] >> 102 << ", i" << divisionAxisNames[2]; 99 // unit of scored value 103 // unit of scored value 100 ofile << ", total(value) "; << 104 ofile << ", value "; 101 if(!unit.empty()) << 105 if(unit.size() > 0) ofile << "[" << unit << "]"; 102 ofile << "[" << unit << "]"; << 106 ofile << G4endl; 103 ofile << ", total(val^2), entry" << G4endl; << 107 104 << 108 // "sequence" option: write header info 105 // "sequence" option: write header info << 109 if(opt.find("sequence") != std::string::npos) { 106 if(opt.find("sequence") != std::string::npos << 110 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] 107 { << 111 << G4endl; 108 ofile << fNMeshSegments[0] << " " << fNMes << 109 << fNMeshSegments[2] << G4endl; << 110 } 112 } 111 113 112 // write quantity 114 // write quantity 113 G4long count = 0; << 115 long count = 0; 114 ofile << std::setprecision(16); // for doub << 116 ofile << std::setprecision(16); // for double value with 8 bytes 115 for(G4int x = 0; x < fNMeshSegments[0]; x++) << 117 for(int x = 0; x < fNMeshSegments[0]; x++) { 116 { << 118 for(int y = 0; y < fNMeshSegments[1]; y++) { 117 for(G4int y = 0; y < fNMeshSegments[1]; y+ << 119 for(int z = 0; z < fNMeshSegments[2]; z++) { 118 { << 120 G4int idx = GetIndex(x, y, z); 119 for(G4int z = 0; z < fNMeshSegments[2]; << 121 120 { << 122 if(opt.find("csv") != std::string::npos) 121 G4int idx = GetIndex(x, y, z); << 123 ofile << x << "," << y << "," << z << ","; 122 << 124 123 if(opt.find("csv") != std::string::npo << 125 std::map<G4int, G4double*>::iterator value = score->find(idx); 124 ofile << x << "," << y << "," << z < << 126 if(value == score->end()) { 125 << 127 ofile << 0.; 126 auto value = score->find(idx); << 128 } else { 127 if(value == score->end()) << 129 ofile << *(value->second)/unitValue; 128 { << 130 } 129 ofile << 0. << "," << 0. << "," << 0 << 131 130 } << 132 if(opt.find("csv") != std::string::npos) { 131 else << 133 ofile << G4endl; 132 { << 134 } else if(opt.find("sequence") != std::string::npos) { 133 ofile << (value->second->sum_wx()) / << 135 ofile << " "; 134 << (value->second->sum_wx2()) << 136 if(count++%5 == 4) ofile << G4endl; 135 fact << 137 } 136 << "," << value->second->n(); << 138 137 } << 139 } // z 138 << 140 } // y 139 if(opt.find("csv") != std::string::npo << 141 } // x 140 { << 141 ofile << G4endl; << 142 } << 143 else if(opt.find("sequence") != std::s << 144 { << 145 ofile << " "; << 146 if(count++ % 5 == 4) << 147 ofile << G4endl; << 148 } << 149 << 150 } // z << 151 } // y << 152 } // x << 153 ofile << std::setprecision(6); 142 ofile << std::setprecision(6); 154 143 155 // close the file 144 // close the file 156 ofile.close(); 145 ofile.close(); >> 146 157 } 147 } 158 148 159 void G4VScoreWriter::DumpAllQuantitiesToFile(c << 149 void G4VScoreWriter::DumpAllQuantitiesToFile(G4String & fileName, G4String & option) { 160 c << 150 161 { << 162 // change the option string into lowercase t 151 // change the option string into lowercase to the case-insensitive. 163 G4String opt = option; 152 G4String opt = option; 164 std::transform(opt.begin(), opt.end(), opt.b << 153 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); 165 154 166 // confirm the option 155 // confirm the option 167 if(opt.empty()) << 156 if(opt.size() == 0) opt = "csv"; 168 opt = "csv"; << 169 if(opt.find("csv") == std::string::npos && 157 if(opt.find("csv") == std::string::npos && 170 opt.find("sequence") == std::string::npos << 158 opt.find("sequence") == std::string::npos) { 171 { << 159 G4cerr << "ERROR : DumpToFile : Unknown option -> " 172 G4cerr << "ERROR : DumpToFile : Unknown op << 160 << option << G4endl; 173 return; 161 return; 174 } 162 } 175 163 176 // open the file 164 // open the file 177 std::ofstream ofile(fileName); 165 std::ofstream ofile(fileName); 178 if(!ofile) << 166 if(!ofile) { 179 { << 167 G4cerr << "ERROR : DumpToFile : File open error -> " 180 G4cerr << "ERROR : DumpToFile : File open << 168 << fileName << G4endl; 181 return; 169 return; 182 } 170 } 183 ofile << "# mesh name: " << fScoringMesh->Ge 171 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; 184 if(fact != 1.0) << 185 { << 186 ofile << "# multiplied factor : " << fact << 187 } << 188 172 189 // retrieve the map 173 // retrieve the map 190 using MeshScoreMap = G4VScoringMesh::MeshSco << 191 MeshScoreMap fSMap = fScoringMesh->GetScoreM 174 MeshScoreMap fSMap = fScoringMesh->GetScoreMap(); 192 // NOLINTNEXTLINE(modernize-use-auto): Expli << 193 MeshScoreMap::const_iterator msMapItr = fSMa 175 MeshScoreMap::const_iterator msMapItr = fSMap.begin(); 194 std::map<G4int, G4StatDouble*>* score; << 176 std::map<G4int, G4double*> * score; 195 for(; msMapItr != fSMap.end(); msMapItr++) << 177 for(; msMapItr != fSMap.end(); msMapItr++) { 196 { << 178 197 G4String psname = msMapItr->first; 179 G4String psname = msMapItr->first; 198 180 199 score = msMapItr->second->GetMap(); 181 score = msMapItr->second->GetMap(); 200 ofile << "# primitive scorer name: " << ms 182 ofile << "# primitive scorer name: " << msMapItr->first << std::endl; 201 183 202 G4double unitValue = fScoringMesh->GetPSUn 184 G4double unitValue = fScoringMesh->GetPSUnitValue(psname); 203 G4String unit = fScoringMesh->GetPSUn << 185 G4String unit = fScoringMesh->GetPSUnit(psname); 204 G4String divisionAxisNames[3]; 186 G4String divisionAxisNames[3]; 205 fScoringMesh->GetDivisionAxisNames(divisio 187 fScoringMesh->GetDivisionAxisNames(divisionAxisNames); 206 // index order 188 // index order 207 ofile << "# i" << divisionAxisNames[0] << << 189 ofile << "# i" << divisionAxisNames[0] 208 << ", i" << divisionAxisNames[2]; << 190 << ", i" << divisionAxisNames[1] >> 191 << ", i" << divisionAxisNames[2]; 209 // unit of scored value 192 // unit of scored value 210 ofile << ", total(value) "; << 193 ofile << ", value "; 211 if(!unit.empty()) << 194 if(unit.size() > 0) ofile << "[" << unit << "]"; 212 ofile << "[" << unit << "]"; << 195 ofile << G4endl; 213 ofile << ", total(val^2), entry" << G4endl << 196 214 << 197 215 // "sequence" option: write header info << 198 // "sequence" option: write header info 216 if(opt.find("sequence") != std::string::np << 199 if(opt.find("sequence") != std::string::npos) { 217 { << 200 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] 218 ofile << fNMeshSegments[0] << " " << fNM << 201 << G4endl; 219 << fNMeshSegments[2] << G4endl; << 220 } 202 } 221 203 222 // write quantity 204 // write quantity 223 G4long count = 0; << 205 long count = 0; 224 ofile << std::setprecision(16); // for do << 206 ofile << std::setprecision(16); // for double value with 8 bytes 225 for(G4int x = 0; x < fNMeshSegments[0]; x+ << 207 for(int x = 0; x < fNMeshSegments[0]; x++) { 226 { << 208 for(int y = 0; y < fNMeshSegments[1]; y++) { 227 for(G4int y = 0; y < fNMeshSegments[1]; << 209 for(int z = 0; z < fNMeshSegments[2]; z++) { 228 { << 210 G4int idx = GetIndex(x, y, z); 229 for(G4int z = 0; z < fNMeshSegments[2] << 211 230 { << 212 if(opt.find("csv") != std::string::npos) 231 G4int idx = GetIndex(x, y, z); << 213 ofile << x << "," << y << "," << z << ","; 232 << 214 233 if(opt.find("csv") != std::string::n << 215 std::map<G4int, G4double*>::iterator value = score->find(idx); 234 ofile << x << "," << y << "," << z << 216 if(value == score->end()) { 235 << 217 ofile << 0.; 236 auto value = score->find(idx); << 218 } else { 237 if(value == score->end()) << 219 ofile << *(value->second)/unitValue; 238 { << 220 } 239 ofile << 0. << "," << 0. << "," << << 221 240 } << 222 if(opt.find("csv") != std::string::npos) { 241 else << 223 ofile << G4endl; 242 { << 224 } else if(opt.find("sequence") != std::string::npos) { 243 ofile << (value->second->sum_wx()) << 225 ofile << " "; 244 << (value->second->sum_wx2() << 226 if(count++%5 == 4) ofile << G4endl; 245 fact << 227 } 246 << "," << value->second->n() << 228 247 } << 229 } // z 248 << 230 } // y 249 if(opt.find("csv") != std::string::n << 231 } // x 250 { << 251 ofile << G4endl; << 252 } << 253 else if(opt.find("sequence") != std: << 254 { << 255 ofile << " "; << 256 if(count++ % 5 == 4) << 257 ofile << G4endl; << 258 } << 259 << 260 } // z << 261 } // y << 262 } // x << 263 ofile << std::setprecision(6); 232 ofile << std::setprecision(6); 264 233 265 } // for(; msMapItr ....) << 234 } // for(; msMapItr ....) 266 235 267 // close the file 236 // close the file 268 ofile.close(); 237 ofile.close(); >> 238 269 } 239 } 270 240 271 G4int G4VScoreWriter::GetIndex(G4int x, G4int << 241 G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const { 272 { << 242 //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1]; 273 return x * fNMeshSegments[1] * fNMeshSegment << 243 return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z; 274 } 244 } >> 245 275 246