[814] | 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 | // |
---|
[1340] | 27 | // $Id: G4VScoreWriter.cc,v 1.10 2010/07/27 01:44:54 akimura Exp $ |
---|
| 28 | // GEANT4 tag $Name: $ |
---|
[814] | 29 | // |
---|
| 30 | |
---|
| 31 | #include "G4VScoreWriter.hh" |
---|
| 32 | |
---|
| 33 | #include "G4MultiFunctionalDetector.hh" |
---|
| 34 | #include "G4SDParticleFilter.hh" |
---|
| 35 | #include "G4VPrimitiveScorer.hh" |
---|
| 36 | #include "G4VScoringMesh.hh" |
---|
| 37 | |
---|
| 38 | #include <map> |
---|
| 39 | #include <fstream> |
---|
| 40 | |
---|
| 41 | G4VScoreWriter::G4VScoreWriter() |
---|
| 42 | : fScoringMesh(0), verboseLevel(0) { |
---|
| 43 | ; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | G4VScoreWriter::~G4VScoreWriter() { |
---|
| 47 | } |
---|
| 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 | |
---|
| 56 | // change the option string into lowercase to the case-insensitive. |
---|
| 57 | G4String opt = option; |
---|
| 58 | std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); |
---|
| 59 | |
---|
| 60 | // confirm the option |
---|
| 61 | if(opt.size() == 0) opt = "csv"; |
---|
| 62 | if(opt.find("csv") == std::string::npos && |
---|
| 63 | opt.find("sequence") == std::string::npos) { |
---|
| 64 | G4cerr << "ERROR : DumpToFile : Unknown option -> " |
---|
| 65 | << option << G4endl; |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | // open the file |
---|
| 70 | std::ofstream ofile(fileName); |
---|
| 71 | if(!ofile) { |
---|
| 72 | G4cerr << "ERROR : DumpToFile : File open error -> " |
---|
| 73 | << fileName << G4endl; |
---|
| 74 | return; |
---|
| 75 | } |
---|
| 76 | ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | // retrieve the map |
---|
| 80 | MeshScoreMap fSMap = fScoringMesh->GetScoreMap(); |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | MeshScoreMap::const_iterator msMapItr = fSMap.find(psName); |
---|
| 84 | if(msMapItr == fSMap.end()) { |
---|
| 85 | G4cerr << "ERROR : DumpToFile : Unknown quantity, \"" |
---|
| 86 | << psName << "\"." << G4endl; |
---|
| 87 | return; |
---|
| 88 | } |
---|
[1340] | 89 | |
---|
| 90 | |
---|
[814] | 91 | std::map<G4int, G4double*> * score = msMapItr->second->GetMap(); |
---|
[1340] | 92 | ofile << "# primitive scorer name: " << msMapItr->first << std::endl; |
---|
[814] | 93 | |
---|
[1340] | 94 | |
---|
| 95 | G4double unitValue = fScoringMesh->GetPSUnitValue(psName); |
---|
| 96 | G4String unit = fScoringMesh->GetPSUnit(psName); |
---|
| 97 | G4String divisionAxisNames[3]; |
---|
| 98 | fScoringMesh->GetDivisionAxisNames(divisionAxisNames); |
---|
| 99 | // index order |
---|
| 100 | ofile << "# i" << divisionAxisNames[0] |
---|
| 101 | << ", i" << divisionAxisNames[1] |
---|
| 102 | << ", i" << divisionAxisNames[2]; |
---|
| 103 | // unit of scored value |
---|
| 104 | ofile << ", value "; |
---|
| 105 | if(unit.size() > 0) ofile << "[" << unit << "]"; |
---|
| 106 | ofile << G4endl; |
---|
| 107 | |
---|
[814] | 108 | // "sequence" option: write header info |
---|
| 109 | if(opt.find("sequence") != std::string::npos) { |
---|
| 110 | ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] |
---|
| 111 | << G4endl; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | // write quantity |
---|
| 115 | long count = 0; |
---|
| 116 | ofile << std::setprecision(16); // for double value with 8 bytes |
---|
| 117 | for(int x = 0; x < fNMeshSegments[0]; x++) { |
---|
| 118 | for(int y = 0; y < fNMeshSegments[1]; y++) { |
---|
| 119 | for(int z = 0; z < fNMeshSegments[2]; z++) { |
---|
| 120 | G4int idx = GetIndex(x, y, z); |
---|
| 121 | |
---|
| 122 | if(opt.find("csv") != std::string::npos) |
---|
| 123 | ofile << x << "," << y << "," << z << ","; |
---|
| 124 | |
---|
| 125 | std::map<G4int, G4double*>::iterator value = score->find(idx); |
---|
| 126 | if(value == score->end()) { |
---|
| 127 | ofile << 0.; |
---|
| 128 | } else { |
---|
[1340] | 129 | ofile << *(value->second)/unitValue; |
---|
[814] | 130 | } |
---|
| 131 | |
---|
| 132 | if(opt.find("csv") != std::string::npos) { |
---|
| 133 | ofile << G4endl; |
---|
| 134 | } else if(opt.find("sequence") != std::string::npos) { |
---|
| 135 | ofile << " "; |
---|
| 136 | if(count++%5 == 4) ofile << G4endl; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | } // z |
---|
| 140 | } // y |
---|
| 141 | } // x |
---|
| 142 | ofile << std::setprecision(6); |
---|
| 143 | |
---|
| 144 | // close the file |
---|
| 145 | ofile.close(); |
---|
| 146 | |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void G4VScoreWriter::DumpAllQuantitiesToFile(G4String & fileName, G4String & option) { |
---|
| 150 | |
---|
| 151 | // change the option string into lowercase to the case-insensitive. |
---|
| 152 | G4String opt = option; |
---|
| 153 | std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); |
---|
| 154 | |
---|
| 155 | // confirm the option |
---|
| 156 | if(opt.size() == 0) opt = "csv"; |
---|
| 157 | if(opt.find("csv") == std::string::npos && |
---|
| 158 | opt.find("sequence") == std::string::npos) { |
---|
| 159 | G4cerr << "ERROR : DumpToFile : Unknown option -> " |
---|
| 160 | << option << G4endl; |
---|
| 161 | return; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // open the file |
---|
| 165 | std::ofstream ofile(fileName); |
---|
| 166 | if(!ofile) { |
---|
| 167 | G4cerr << "ERROR : DumpToFile : File open error -> " |
---|
| 168 | << fileName << G4endl; |
---|
| 169 | return; |
---|
| 170 | } |
---|
| 171 | ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; |
---|
| 172 | |
---|
| 173 | // retrieve the map |
---|
| 174 | MeshScoreMap fSMap = fScoringMesh->GetScoreMap(); |
---|
| 175 | MeshScoreMap::const_iterator msMapItr = fSMap.begin(); |
---|
| 176 | std::map<G4int, G4double*> * score; |
---|
| 177 | for(; msMapItr != fSMap.end(); msMapItr++) { |
---|
[1340] | 178 | |
---|
| 179 | G4String psname = msMapItr->first; |
---|
| 180 | |
---|
[814] | 181 | score = msMapItr->second->GetMap(); |
---|
[1340] | 182 | ofile << "# primitive scorer name: " << msMapItr->first << std::endl; |
---|
[814] | 183 | |
---|
[1340] | 184 | G4double unitValue = fScoringMesh->GetPSUnitValue(psname); |
---|
| 185 | G4String unit = fScoringMesh->GetPSUnit(psname); |
---|
| 186 | G4String divisionAxisNames[3]; |
---|
| 187 | fScoringMesh->GetDivisionAxisNames(divisionAxisNames); |
---|
| 188 | // index order |
---|
| 189 | ofile << "# i" << divisionAxisNames[0] |
---|
| 190 | << ", i" << divisionAxisNames[1] |
---|
| 191 | << ", i" << divisionAxisNames[2]; |
---|
| 192 | // unit of scored value |
---|
| 193 | ofile << ", value "; |
---|
| 194 | if(unit.size() > 0) ofile << "[" << unit << "]"; |
---|
| 195 | ofile << G4endl; |
---|
| 196 | |
---|
| 197 | |
---|
[814] | 198 | // "sequence" option: write header info |
---|
| 199 | if(opt.find("sequence") != std::string::npos) { |
---|
| 200 | ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] |
---|
| 201 | << G4endl; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | // write quantity |
---|
| 205 | long count = 0; |
---|
| 206 | ofile << std::setprecision(16); // for double value with 8 bytes |
---|
| 207 | for(int x = 0; x < fNMeshSegments[0]; x++) { |
---|
| 208 | for(int y = 0; y < fNMeshSegments[1]; y++) { |
---|
| 209 | for(int z = 0; z < fNMeshSegments[2]; z++) { |
---|
| 210 | G4int idx = GetIndex(x, y, z); |
---|
| 211 | |
---|
| 212 | if(opt.find("csv") != std::string::npos) |
---|
| 213 | ofile << x << "," << y << "," << z << ","; |
---|
| 214 | |
---|
| 215 | std::map<G4int, G4double*>::iterator value = score->find(idx); |
---|
| 216 | if(value == score->end()) { |
---|
| 217 | ofile << 0.; |
---|
| 218 | } else { |
---|
[1340] | 219 | ofile << *(value->second)/unitValue; |
---|
[814] | 220 | } |
---|
| 221 | |
---|
| 222 | if(opt.find("csv") != std::string::npos) { |
---|
| 223 | ofile << G4endl; |
---|
| 224 | } else if(opt.find("sequence") != std::string::npos) { |
---|
| 225 | ofile << " "; |
---|
| 226 | if(count++%5 == 4) ofile << G4endl; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | } // z |
---|
| 230 | } // y |
---|
| 231 | } // x |
---|
| 232 | ofile << std::setprecision(6); |
---|
| 233 | |
---|
| 234 | } // for(; msMapItr ....) |
---|
| 235 | |
---|
| 236 | // close the file |
---|
| 237 | ofile.close(); |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const { |
---|
[850] | 242 | //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1]; |
---|
| 243 | return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z; |
---|
[814] | 244 | } |
---|
| 245 | |
---|