Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BrachyUserScoreWriter.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 /*
28 // Code developed by:
29 // S.Guatelli, susanna@uow.edu.au
30 //
31 Original code from geant4/examples/extended/runAndEvent/RE03, by M. Asai
32 */
33 #include <map>
34 #include <fstream>
35 #include "BrachyUserScoreWriter.hh"
36 #include "G4SystemOfUnits.hh"
37 
38 #ifdef ANALYSIS_USE
39 #include "BrachyAnalysisManager.hh"
40 #endif
41 
43 #include "G4SDParticleFilter.hh"
44 #include "G4VPrimitiveScorer.hh"
45 #include "G4VScoringMesh.hh"
46 // The default output is
47 // voxelX, voxelY, voxelZ, edep
48 // The BrachyUserScoreWriter allows to change the format of the output file.
49 // in the specific case:
50 // xx (mm) yy(mm) zz(mm) edep(keV)
51 // The same information is stored in a ntuple, in the
52 // brachytherapy.root file
53 
56 {
57 }
58 
60 {;}
61 
63  const G4String & fileName,
64  const G4String & option)
65 {
66 if(verboseLevel > 0)
67  {G4cout << "BrachyUserScorer-defined DumpQuantityToFile() method is invoked."
68  << G4endl;
69  }
70 
71 // change the option string into lowercase to the case-insensitive.
72 G4String opt = option;
73 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
74 
75 // confirm the option
76 if(opt.size() == 0) opt = "csv";
77 
78 // open the file
79 std::ofstream ofile(fileName);
80 
81 if(!ofile)
82 {
83  G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
84  return;
85 }
86  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
87 
88 // retrieve the map
89 MeshScoreMap fSMap = fScoringMesh -> GetScoreMap();
90 
91 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
92 
93 if(msMapItr == fSMap.end())
94  {
95  G4cerr << "ERROR : DumpToFile : Unknown quantity, \""<< psName
96  << "\"." << G4endl;
97  return;
98  }
99 
100 std::map<G4int, G4StatDouble*> * score = msMapItr -> second-> GetMap();
101 
102 ofile << "# primitive scorer name: " << msMapItr -> first << G4endl;
103 //
104 // Write quantity in the ASCII output file and in brachytherapy.root
105 //
106 ofile << std::setprecision(16); // for double value with 8 bytes
107 
108 for(int x = 0; x < fNMeshSegments[0]; x++) {
109  for(int y = 0; y < fNMeshSegments[1]; y++) {
110  for(int z = 0; z < fNMeshSegments[2]; z++){
111  G4int numberOfVoxel_x = fNMeshSegments[0];
112  G4int numberOfVoxel_y = fNMeshSegments[1];
113  G4int numberOfVoxel_z =fNMeshSegments[2];
114  // If the voxel width is changed in the macro file,
115  // the voxel width variable must be updated
116  G4double voxelWidth = 0.25 *mm;
117  //
118  G4double xx = ( - numberOfVoxel_x + 1+ 2*x )* voxelWidth/2;
119  G4double yy = ( - numberOfVoxel_y + 1+ 2*y )* voxelWidth/2;
120  G4double zz = ( - numberOfVoxel_z + 1+ 2*z )* voxelWidth/2;
121  G4int idx = GetIndex(x, y, z);
122  std::map<G4int, G4StatDouble*>::iterator value = score -> find(idx);
123 
124  if (value != score -> end())
125  {
126  // Print in the ASCII output file the information
127 
128  ofile << xx << " " << yy << " " << zz <<" "
129  <<(value->second->sum_wx())/keV << G4endl;
130 
131 #ifdef ANALYSIS_USE
132  // Save the same information in the output analysis file
134 
135  if(zz> -0.125 *mm && zz < 0.125*mm) analysis -> FillH2WithEnergyDeposition(xx,yy, (value->second->sum_wx())/keV);
136 #endif
137 }}}}
138 
139 ofile << std::setprecision(6);
140 
141 // Close the output ASCII file
142 ofile.close();
143 }
const G4String & GetWorldName() const
static constexpr double mm
Definition: G4SIunits.hh:115
static BrachyAnalysisManager * GetInstance()
tuple x
Definition: test.py:50
static constexpr double second
Definition: G4SIunits.hh:157
int G4int
Definition: G4Types.hh:78
void DumpQuantityToFile(const G4String &psName, const G4String &fileName, const G4String &option)
G4VScoringMesh * fScoringMesh
G4int fNMeshSegments[3]
G4GLOB_DLL std::ostream G4cout
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
std::ofstream ofile
Definition: clparse.cc:45
tuple z
Definition: test.py:28
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
static constexpr double keV
Definition: G4SIunits.hh:216
std::map< G4String, RunScore * > MeshScoreMap
G4GLOB_DLL std::ostream G4cerr