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