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