00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "G4VScoreWriter.hh"
00031
00032 #include "G4MultiFunctionalDetector.hh"
00033 #include "G4SDParticleFilter.hh"
00034 #include "G4VPrimitiveScorer.hh"
00035 #include "G4VScoringMesh.hh"
00036
00037 #include <map>
00038 #include <fstream>
00039
00040 G4VScoreWriter::G4VScoreWriter()
00041 : fScoringMesh(0), verboseLevel(0) {
00042 fNMeshSegments[0] = fNMeshSegments[1] = fNMeshSegments[2] = 0;
00043 }
00044
00045 G4VScoreWriter::~G4VScoreWriter() {
00046 }
00047
00048 void G4VScoreWriter::SetScoringMesh(G4VScoringMesh * sm) {
00049 fScoringMesh = sm;
00050 fScoringMesh->GetNumberOfSegments(fNMeshSegments);
00051 }
00052
00053 void G4VScoreWriter::DumpQuantityToFile(const G4String& psName,
00054 const G4String& fileName,
00055 const G4String& option) {
00056
00057
00058 G4String opt = option;
00059 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
00060
00061
00062 if(opt.size() == 0) opt = "csv";
00063 if(opt.find("csv") == std::string::npos &&
00064 opt.find("sequence") == std::string::npos) {
00065 G4cerr << "ERROR : DumpToFile : Unknown option -> "
00066 << option << G4endl;
00067 return;
00068 }
00069
00070
00071 std::ofstream ofile(fileName);
00072 if(!ofile) {
00073 G4cerr << "ERROR : DumpToFile : File open error -> "
00074 << fileName << G4endl;
00075 return;
00076 }
00077 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
00078
00079
00080
00081 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
00082
00083
00084 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
00085 if(msMapItr == fSMap.end()) {
00086 G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
00087 << psName << "\"." << G4endl;
00088 return;
00089 }
00090
00091
00092 std::map<G4int, G4double*> * score = msMapItr->second->GetMap();
00093 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
00094
00095
00096 G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
00097 G4String unit = fScoringMesh->GetPSUnit(psName);
00098 G4String divisionAxisNames[3];
00099 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
00100
00101 ofile << "# i" << divisionAxisNames[0]
00102 << ", i" << divisionAxisNames[1]
00103 << ", i" << divisionAxisNames[2];
00104
00105 ofile << ", value ";
00106 if(unit.size() > 0) ofile << "[" << unit << "]";
00107 ofile << G4endl;
00108
00109
00110 if(opt.find("sequence") != std::string::npos) {
00111 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
00112 << G4endl;
00113 }
00114
00115
00116 long count = 0;
00117 ofile << std::setprecision(16);
00118 for(int x = 0; x < fNMeshSegments[0]; x++) {
00119 for(int y = 0; y < fNMeshSegments[1]; y++) {
00120 for(int z = 0; z < fNMeshSegments[2]; z++) {
00121 G4int idx = GetIndex(x, y, z);
00122
00123 if(opt.find("csv") != std::string::npos)
00124 ofile << x << "," << y << "," << z << ",";
00125
00126 std::map<G4int, G4double*>::iterator value = score->find(idx);
00127 if(value == score->end()) {
00128 ofile << 0.;
00129 } else {
00130 ofile << *(value->second)/unitValue;
00131 }
00132
00133 if(opt.find("csv") != std::string::npos) {
00134 ofile << G4endl;
00135 } else if(opt.find("sequence") != std::string::npos) {
00136 ofile << " ";
00137 if(count++%5 == 4) ofile << G4endl;
00138 }
00139
00140 }
00141 }
00142 }
00143 ofile << std::setprecision(6);
00144
00145
00146 ofile.close();
00147
00148 }
00149
00150 void G4VScoreWriter::DumpAllQuantitiesToFile(const G4String& fileName,
00151 const G4String& option) {
00152
00153
00154 G4String opt = option;
00155 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
00156
00157
00158 if(opt.size() == 0) opt = "csv";
00159 if(opt.find("csv") == std::string::npos &&
00160 opt.find("sequence") == std::string::npos) {
00161 G4cerr << "ERROR : DumpToFile : Unknown option -> "
00162 << option << G4endl;
00163 return;
00164 }
00165
00166
00167 std::ofstream ofile(fileName);
00168 if(!ofile) {
00169 G4cerr << "ERROR : DumpToFile : File open error -> "
00170 << fileName << G4endl;
00171 return;
00172 }
00173 ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
00174
00175
00176 MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
00177 MeshScoreMap::const_iterator msMapItr = fSMap.begin();
00178 std::map<G4int, G4double*> * score;
00179 for(; msMapItr != fSMap.end(); msMapItr++) {
00180
00181 G4String psname = msMapItr->first;
00182
00183 score = msMapItr->second->GetMap();
00184 ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
00185
00186 G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
00187 G4String unit = fScoringMesh->GetPSUnit(psname);
00188 G4String divisionAxisNames[3];
00189 fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
00190
00191 ofile << "# i" << divisionAxisNames[0]
00192 << ", i" << divisionAxisNames[1]
00193 << ", i" << divisionAxisNames[2];
00194
00195 ofile << ", value ";
00196 if(unit.size() > 0) ofile << "[" << unit << "]";
00197 ofile << G4endl;
00198
00199
00200
00201 if(opt.find("sequence") != std::string::npos) {
00202 ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
00203 << G4endl;
00204 }
00205
00206
00207 long count = 0;
00208 ofile << std::setprecision(16);
00209 for(int x = 0; x < fNMeshSegments[0]; x++) {
00210 for(int y = 0; y < fNMeshSegments[1]; y++) {
00211 for(int z = 0; z < fNMeshSegments[2]; z++) {
00212 G4int idx = GetIndex(x, y, z);
00213
00214 if(opt.find("csv") != std::string::npos)
00215 ofile << x << "," << y << "," << z << ",";
00216
00217 std::map<G4int, G4double*>::iterator value = score->find(idx);
00218 if(value == score->end()) {
00219 ofile << 0.;
00220 } else {
00221 ofile << *(value->second)/unitValue;
00222 }
00223
00224 if(opt.find("csv") != std::string::npos) {
00225 ofile << G4endl;
00226 } else if(opt.find("sequence") != std::string::npos) {
00227 ofile << " ";
00228 if(count++%5 == 4) ofile << G4endl;
00229 }
00230
00231 }
00232 }
00233 }
00234 ofile << std::setprecision(6);
00235
00236 }
00237
00238
00239 ofile.close();
00240
00241 }
00242
00243 G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const {
00244
00245 return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z;
00246 }
00247