Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 99154 2016-09-07 08:06:30Z 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(nullptr), 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, G4StatDouble*> * 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 << ", total(value) ";
106  if(unit.size() > 0) ofile << "[" << unit << "]";
107  ofile << ", total(val^2), entry" << 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, G4StatDouble*>::iterator value = score->find(idx);
127  if(value == score->end()) {
128  ofile << 0. << "," << 0. << "," << 0;
129  } else {
130  ofile << (value->second->sum_wx())/unitValue << ","
131  << (value->second->sum_wx2())/unitValue/unitValue << ","
132  << value->second->n();
133  }
134 
135  if(opt.find("csv") != std::string::npos) {
136  ofile << G4endl;
137  } else if(opt.find("sequence") != std::string::npos) {
138  ofile << " ";
139  if(count++%5 == 4) ofile << G4endl;
140  }
141 
142  } // z
143  } // y
144  } // x
145  ofile << std::setprecision(6);
146 
147  // close the file
148  ofile.close();
149 
150 }
151 
153  const G4String& option) {
154 
155  // change the option string into lowercase to the case-insensitive.
156  G4String opt = option;
157  std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
158 
159  // confirm the option
160  if(opt.size() == 0) opt = "csv";
161  if(opt.find("csv") == std::string::npos &&
162  opt.find("sequence") == std::string::npos) {
163  G4cerr << "ERROR : DumpToFile : Unknown option -> "
164  << option << G4endl;
165  return;
166  }
167 
168  // open the file
169  std::ofstream ofile(fileName);
170  if(!ofile) {
171  G4cerr << "ERROR : DumpToFile : File open error -> "
172  << fileName << G4endl;
173  return;
174  }
175  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
176 
177  // retrieve the map
179  MeshScoreMap::const_iterator msMapItr = fSMap.begin();
180  std::map<G4int, G4StatDouble*> * score;
181  for(; msMapItr != fSMap.end(); msMapItr++) {
182 
183  G4String psname = msMapItr->first;
184 
185  score = msMapItr->second->GetMap();
186  ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
187 
188  G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
189  G4String unit = fScoringMesh->GetPSUnit(psname);
190  G4String divisionAxisNames[3];
191  fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
192  // index order
193  ofile << "# i" << divisionAxisNames[0]
194  << ", i" << divisionAxisNames[1]
195  << ", i" << divisionAxisNames[2];
196  // unit of scored value
197  ofile << ", total(value) ";
198  if(unit.size() > 0) ofile << "[" << unit << "]";
199  ofile << ", total(val^2), entry" << G4endl;
200 
201 
202  // "sequence" option: write header info
203  if(opt.find("sequence") != std::string::npos) {
204  ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
205  << G4endl;
206  }
207 
208  // write quantity
209  long count = 0;
210  ofile << std::setprecision(16); // for double value with 8 bytes
211  for(int x = 0; x < fNMeshSegments[0]; x++) {
212  for(int y = 0; y < fNMeshSegments[1]; y++) {
213  for(int z = 0; z < fNMeshSegments[2]; z++) {
214  G4int idx = GetIndex(x, y, z);
215 
216  if(opt.find("csv") != std::string::npos)
217  ofile << x << "," << y << "," << z << ",";
218 
219  std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
220  if(value == score->end()) {
221  ofile << 0. << "," << 0. << "," << 0;
222  } else {
223  ofile << (value->second->sum_wx())/unitValue << ","
224  << (value->second->sum_wx2())/unitValue/unitValue << ","
225  << value->second->n();
226  }
227 
228  if(opt.find("csv") != std::string::npos) {
229  ofile << G4endl;
230  } else if(opt.find("sequence") != std::string::npos) {
231  ofile << " ";
232  if(count++%5 == 4) ofile << G4endl;
233  }
234 
235  } // z
236  } // y
237  } // x
238  ofile << std::setprecision(6);
239 
240  } // for(; msMapItr ....)
241 
242  // close the file
243  ofile.close();
244 
245 }
246 
248  //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1];
249  return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z;
250 }
251 
void GetNumberOfSegments(G4int nSegment[3])
const G4String & GetWorldName() const
G4int first(char) const
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)
G4int fNMeshSegments[3]
const XML_Char int const XML_Char * value
Definition: expat.h:331
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
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
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
virtual ~G4VScoreWriter()
std::map< G4String, RunScore * > MeshScoreMap
G4GLOB_DLL std::ostream G4cerr
virtual void DumpQuantityToFile(const G4String &psName, const G4String &fileName, const G4String &option)