[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 | // |
---|
[850] | 27 | // $Id: G4VScoreWriter.cc,v 1.5 2008/03/04 23:19:09 taso Exp $ |
---|
[1228] | 28 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
[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 | } |
---|
| 89 | std::map<G4int, G4double*> * score = msMapItr->second->GetMap(); |
---|
| 90 | ofile << "# primitive scorer name: " << msMapItr->first << G4endl; |
---|
| 91 | |
---|
| 92 | // "sequence" option: write header info |
---|
| 93 | if(opt.find("sequence") != std::string::npos) { |
---|
| 94 | ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] |
---|
| 95 | << G4endl; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | // write quantity |
---|
| 99 | long count = 0; |
---|
| 100 | ofile << std::setprecision(16); // for double value with 8 bytes |
---|
| 101 | for(int x = 0; x < fNMeshSegments[0]; x++) { |
---|
| 102 | for(int y = 0; y < fNMeshSegments[1]; y++) { |
---|
| 103 | for(int z = 0; z < fNMeshSegments[2]; z++) { |
---|
| 104 | G4int idx = GetIndex(x, y, z); |
---|
| 105 | |
---|
| 106 | if(opt.find("csv") != std::string::npos) |
---|
| 107 | ofile << x << "," << y << "," << z << ","; |
---|
| 108 | |
---|
| 109 | std::map<G4int, G4double*>::iterator value = score->find(idx); |
---|
| 110 | if(value == score->end()) { |
---|
| 111 | ofile << 0.; |
---|
| 112 | } else { |
---|
| 113 | ofile << *(value->second); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if(opt.find("csv") != std::string::npos) { |
---|
| 117 | ofile << G4endl; |
---|
| 118 | } else if(opt.find("sequence") != std::string::npos) { |
---|
| 119 | ofile << " "; |
---|
| 120 | if(count++%5 == 4) ofile << G4endl; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | } // z |
---|
| 124 | } // y |
---|
| 125 | } // x |
---|
| 126 | ofile << std::setprecision(6); |
---|
| 127 | |
---|
| 128 | // close the file |
---|
| 129 | ofile.close(); |
---|
| 130 | |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void G4VScoreWriter::DumpAllQuantitiesToFile(G4String & fileName, G4String & option) { |
---|
| 134 | |
---|
| 135 | // change the option string into lowercase to the case-insensitive. |
---|
| 136 | G4String opt = option; |
---|
| 137 | std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower)); |
---|
| 138 | |
---|
| 139 | // confirm the option |
---|
| 140 | if(opt.size() == 0) opt = "csv"; |
---|
| 141 | if(opt.find("csv") == std::string::npos && |
---|
| 142 | opt.find("sequence") == std::string::npos) { |
---|
| 143 | G4cerr << "ERROR : DumpToFile : Unknown option -> " |
---|
| 144 | << option << G4endl; |
---|
| 145 | return; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | // open the file |
---|
| 149 | std::ofstream ofile(fileName); |
---|
| 150 | if(!ofile) { |
---|
| 151 | G4cerr << "ERROR : DumpToFile : File open error -> " |
---|
| 152 | << fileName << G4endl; |
---|
| 153 | return; |
---|
| 154 | } |
---|
| 155 | ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl; |
---|
| 156 | |
---|
| 157 | // retrieve the map |
---|
| 158 | MeshScoreMap fSMap = fScoringMesh->GetScoreMap(); |
---|
| 159 | MeshScoreMap::const_iterator msMapItr = fSMap.begin(); |
---|
| 160 | std::map<G4int, G4double*> * score; |
---|
| 161 | for(; msMapItr != fSMap.end(); msMapItr++) { |
---|
| 162 | score = msMapItr->second->GetMap(); |
---|
| 163 | ofile << "# primitive scorer name: " << msMapItr->first << G4endl; |
---|
| 164 | |
---|
| 165 | // "sequence" option: write header info |
---|
| 166 | if(opt.find("sequence") != std::string::npos) { |
---|
| 167 | ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2] |
---|
| 168 | << G4endl; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | // write quantity |
---|
| 172 | long count = 0; |
---|
| 173 | ofile << std::setprecision(16); // for double value with 8 bytes |
---|
| 174 | for(int x = 0; x < fNMeshSegments[0]; x++) { |
---|
| 175 | for(int y = 0; y < fNMeshSegments[1]; y++) { |
---|
| 176 | for(int z = 0; z < fNMeshSegments[2]; z++) { |
---|
| 177 | G4int idx = GetIndex(x, y, z); |
---|
| 178 | |
---|
| 179 | if(opt.find("csv") != std::string::npos) |
---|
| 180 | ofile << x << "," << y << "," << z << ","; |
---|
| 181 | |
---|
| 182 | std::map<G4int, G4double*>::iterator value = score->find(idx); |
---|
| 183 | if(value == score->end()) { |
---|
| 184 | ofile << 0.; |
---|
| 185 | } else { |
---|
| 186 | ofile << *(value->second); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if(opt.find("csv") != std::string::npos) { |
---|
| 190 | ofile << G4endl; |
---|
| 191 | } else if(opt.find("sequence") != std::string::npos) { |
---|
| 192 | ofile << " "; |
---|
| 193 | if(count++%5 == 4) ofile << G4endl; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | } // z |
---|
| 197 | } // y |
---|
| 198 | } // x |
---|
| 199 | ofile << std::setprecision(6); |
---|
| 200 | |
---|
| 201 | } // for(; msMapItr ....) |
---|
| 202 | |
---|
| 203 | // close the file |
---|
| 204 | ofile.close(); |
---|
| 205 | |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const { |
---|
[850] | 209 | //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1]; |
---|
| 210 | return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z; |
---|
[814] | 211 | } |
---|
| 212 | |
---|