Geant4  9.6.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 
34 #include <map>
35 #include <fstream>
36 
37 #include "BrachyUserScoreWriter.hh"
38 #include "G4SystemOfUnits.hh"
39 #include "BrachyAnalysis.hh"
41 #include "G4SDParticleFilter.hh"
42 #include "G4VPrimitiveScorer.hh"
43 #include "G4VScoringMesh.hh"
44 
45 // The default output is
46 // voxelX, voxelY, voxelZ, edep
47 // The BrachyUserScoreWriter allows to change the format of the output file.
48 // in the specific case:
49 // xx (mm) yy(mm) zz(mm) edep(keV)
50 
51 // The same information is stored in a ntuple, in the
52 // brachytherapy.root file
53 
55 {;}
56 
58 {;}
59 
60 void BrachyUserScoreWriter::DumpQuantityToFile(const G4String & psName, const G4String & fileName, const G4String & option)
61 {
62  if(verboseLevel > 0) {
63  G4cout << "BrachyUserScorer-defined DumpQuantityToFile() method is invoked."
64  << G4endl; }
65 
66  // change the option string into lowercase to the case-insensitive.
67  G4String opt = option;
68  std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
69 
70  // confirm the option
71  if(opt.size() == 0) opt = "csv";
72 
73  // open the file
74  std::ofstream ofile(fileName);
75  if(!ofile) {
76  G4cerr << "ERROR : DumpToFile : File open error -> "
77  << fileName << G4endl;
78  return;
79  }
80  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
81 
82  // retrieve the map
83  MeshScoreMap fSMap = fScoringMesh -> GetScoreMap();
84 
85  MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
86  if(msMapItr == fSMap.end()) {
87  G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
88  << psName << "\"." << G4endl;
89  return;
90  }
91  std::map<G4int, G4double*> * score = msMapItr -> second-> GetMap();
92  ofile << "# primitive scorer name: " << msMapItr -> first << G4endl;
93 
94 
95  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
96 
97  //
98  // Open a ROOT output file
99  //
100 
101  analysisManager -> OpenFile("brachytherapy");
102 
103  //
104  // Creating ntuple
105  //
106  analysisManager -> CreateNtuple("EnergyDeposition", "Edep(keV) in the phantom");
107  analysisManager -> CreateNtupleDColumn("xx");
108  analysisManager -> CreateNtupleDColumn("yy");
109  analysisManager -> CreateNtupleDColumn("zz");
110  analysisManager -> CreateNtupleDColumn("edep");
111  analysisManager -> FinishNtuple();
112 
113  //
114  // Write quantity in the ASCII output file and in brachytherapy.root
115  //
116  ofile << std::setprecision(16); // for double value with 8 bytes
117  for(int x = 0; x < fNMeshSegments[0]; x++) {
118  for(int y = 0; y < fNMeshSegments[1]; y++) {
119  for(int z = 0; z < fNMeshSegments[2]; z++) {
120 
121  G4int numberOfVoxel = fNMeshSegments[0];
122 
123  // If the voxel width is changed in the macro file,
124  // the voxel width variable must be updated
125  G4double voxelWidth = 1. *mm;
126  //
127 
128  G4double xx = ( - numberOfVoxel + 1+ 2*x )* voxelWidth/2;
129  G4double yy = ( - numberOfVoxel + 1+ 2*y )* voxelWidth/2;
130  G4double zz = ( - numberOfVoxel + 1+ 2*z )* voxelWidth/2;
131 
132  G4int idx = GetIndex(x, y, z);
133 
134  std::map<G4int, G4double*>::iterator value = score -> find(idx);
135 
136  if (value != score -> end())
137  {
138  // Print in the ASCII output file the information
139  ofile << xx << " " << yy << " " << zz <<" " <<*(value->second)/keV << G4endl;
140 
141  // Save the same information in the output analysis file
142  analysisManager = G4AnalysisManager::Instance();
143  analysisManager -> FillNtupleDColumn(0, xx);
144  analysisManager -> FillNtupleDColumn(1, yy);
145  analysisManager -> FillNtupleDColumn(2, zz);
146  analysisManager -> FillNtupleDColumn(3, *(value->second)/keV);
147  analysisManager -> AddNtupleRow();
148  }
149  }
150  }
151  }
152  ofile << std::setprecision(6);
153 
154  // Close the output ASCII file
155  ofile.close();
156 
157  // Close the output brachytherapy.root
158  analysisManager -> Write();
159  analysisManager -> CloseFile();
160 }
161