Geant4_10
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 #include "BrachyAnalysisManager.hh"
39 #include "G4SDParticleFilter.hh"
40 #include "G4VPrimitiveScorer.hh"
41 #include "G4VScoringMesh.hh"
42 // The default output is
43 // voxelX, voxelY, voxelZ, edep
44 // The BrachyUserScoreWriter allows to change the format of the output file.
45 // in the specific case:
46 // xx (mm) yy(mm) zz(mm) edep(keV)
47 // The same information is stored in a ntuple, in the
48 // brachytherapy.root file
49 
52 {
53 analysis = analysis_manager;
54 }
55 
57 {;}
58 
60  const G4String & fileName,
61  const G4String & option)
62 {
63 if(verboseLevel > 0)
64  {G4cout << "BrachyUserScorer-defined DumpQuantityToFile() method is invoked."
65  << G4endl;
66  }
67 
68 // change the option string into lowercase to the case-insensitive.
69 G4String opt = option;
70 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
71 
72 // confirm the option
73 if(opt.size() == 0) opt = "csv";
74 
75 // open the file
76 std::ofstream ofile(fileName);
77 
78 if(!ofile)
79 {
80  G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
81  return;
82 }
83  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
84 
85 // retrieve the map
86 MeshScoreMap fSMap = fScoringMesh -> GetScoreMap();
87 
88 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
89 
90 if(msMapItr == fSMap.end())
91  {
92  G4cerr << "ERROR : DumpToFile : Unknown quantity, \""<< psName
93  << "\"." << G4endl;
94  return;
95  }
96 
97 std::map<G4int, G4double*> * score = msMapItr -> second-> GetMap();
98 
99 ofile << "# primitive scorer name: " << msMapItr -> first << G4endl;
100 //
101 // Write quantity in the ASCII output file and in brachytherapy.root
102 //
103 ofile << std::setprecision(16); // for double value with 8 bytes
104 
105 for(int x = 0; x < fNMeshSegments[0]; x++) {
106  for(int y = 0; y < fNMeshSegments[1]; y++) {
107  for(int z = 0; z < fNMeshSegments[2]; z++){
108  G4int numberOfVoxel = fNMeshSegments[0];
109  // If the voxel width is changed in the macro file,
110  // the voxel width variable must be updated
111  G4double voxelWidth = 1. *mm;
112  //
113  G4double xx = ( - numberOfVoxel + 1+ 2*x )* voxelWidth/2;
114  G4double yy = ( - numberOfVoxel + 1+ 2*y )* voxelWidth/2;
115  G4double zz = ( - numberOfVoxel + 1+ 2*z )* voxelWidth/2;
116  G4int idx = GetIndex(x, y, z);
117  std::map<G4int, G4double*>::iterator value = score -> find(idx);
118 
119  if (value != score -> end())
120  {
121  // Print in the ASCII output file the information
122  ofile << xx << " " << yy << " " << zz <<" "
123  <<*(value->second)/keV << G4endl;
124 
125 #ifdef ANALYSIS_USE
126  // Save the same information in the output analysis file
127  analysis -> FillNtupleWithEnergyDeposition(xx, yy, zz, *(value->second)/keV);
128 #endif
129 }}}}
130 
131 ofile << std::setprecision(6);
132 
133 // Close the output ASCII file
134 ofile.close();
135 }
Double_t yy
Definition: macro.C:11
const G4String & GetWorldName() const
std::map< G4String, G4THitsMap< G4double > * > MeshScoreMap
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
Double_t xx
Definition: macro.C:10
BrachyUserScoreWriter(BrachyAnalysisManager *analysis)
tuple x
Definition: test.py:50
int G4int
Definition: G4Types.hh:78
void DumpQuantityToFile(const G4String &psName, const G4String &fileName, const G4String &option)
G4VScoringMesh * fScoringMesh
Double_t y
Definition: plot.C:279
G4int fNMeshSegments[3]
G4GLOB_DLL std::ostream G4cout
G4int GetIndex(G4int x, G4int y, G4int z) const
Double_t zz
Definition: macro.C:12
std::ofstream ofile
Definition: clparse.cc:45
G4int first
tuple z
Definition: test.py:28
const XML_Char int const XML_Char * value
Definition: expat.h:331
#define G4endl
Definition: G4ios.hh:61
double G4double
Definition: G4Types.hh:76
G4GLOB_DLL std::ostream G4cerr