ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BrachyUserScoreWriter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file BrachyUserScoreWriter.cc
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 
38 
39 #ifdef ANALYSIS_USE
40 #include "BrachyAnalysisManager.hh"
41 #endif
42 
44 #include "G4SDParticleFilter.hh"
45 #include "G4VPrimitiveScorer.hh"
46 #include "G4VScoringMesh.hh"
47 // The default output is
48 // voxelX, voxelY, voxelZ, edep
49 // The BrachyUserScoreWriter allows to change the format of the output file.
50 // in the specific case:
51 // xx (mm) yy(mm) zz(mm) edep(keV)
52 // The same information is stored in a ntuple, in the
53 // brachytherapy.root file
54 
57 {
58 }
59 
61 {;}
62 
64  const G4String & fileName,
65  const G4String & option)
66 {
67 using MeshScoreMap = G4VScoringMesh::MeshScoreMap;
68 
69 if(verboseLevel > 0)
70  {G4cout << "BrachyUserScorer-defined DumpQuantityToFile() method is invoked."
71  << G4endl;
72  }
73 
74 // change the option string into lowercase to the case-insensitive.
75 G4String opt = option;
76 std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
77 
78 // confirm the option
79 if(opt.size() == 0) opt = "csv";
80 
81 // open the file
82 std::ofstream ofile(fileName);
83 
84 if(!ofile)
85 {
86  G4cerr << "ERROR : DumpToFile : File open error -> " << fileName << G4endl;
87  return;
88 }
89  ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
90 
91 // retrieve the map
92 MeshScoreMap fSMap = fScoringMesh -> GetScoreMap();
93 
94 MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
95 
96 if(msMapItr == fSMap.end())
97  {
98  G4cerr << "ERROR : DumpToFile : Unknown quantity, \""<< psName
99  << "\"." << G4endl;
100  return;
101  }
102 
103 std::map<G4int, G4StatDouble*> * score = msMapItr -> second-> GetMap();
104 
105 ofile << "# primitive scorer name: " << msMapItr -> first << G4endl;
106 //
107 // Write quantity in the ASCII output file and in brachytherapy.root
108 //
109 ofile << std::setprecision(16); // for double value with 8 bytes
110 
111 for(int x = 0; x < fNMeshSegments[0]; x++) {
112  for(int y = 0; y < fNMeshSegments[1]; y++) {
113  for(int z = 0; z < fNMeshSegments[2]; z++){
114  G4int numberOfVoxel_x = fNMeshSegments[0];
115  G4int numberOfVoxel_y = fNMeshSegments[1];
116  G4int numberOfVoxel_z =fNMeshSegments[2];
117  // If the voxel width is changed in the macro file,
118  // the voxel width variable must be updated
119  G4double voxelWidth = 0.25 *CLHEP::mm;
120  //
121  G4double xx = ( - numberOfVoxel_x + 1+ 2*x )* voxelWidth/2;
122  G4double yy = ( - numberOfVoxel_y + 1+ 2*y )* voxelWidth/2;
123  G4double zz = ( - numberOfVoxel_z + 1+ 2*z )* voxelWidth/2;
124  G4int idx = GetIndex(x, y, z);
125  std::map<G4int, G4StatDouble*>::iterator value = score -> find(idx);
126 
127  if (value != score -> end())
128  {
129  // Print in the ASCII output file the information
130 
131  ofile << xx << " " << yy << " " << zz <<" "
132  <<(value->second->sum_wx())/CLHEP::keV << G4endl;
133 
134 #ifdef ANALYSIS_USE
135  // Save the same information in the output analysis file
137 
138  if(zz> -0.125 *CLHEP::mm && zz < 0.125*CLHEP::mm)
139  analysis -> FillH2WithEnergyDeposition(xx,yy,
140  (value->second->sum_wx())/CLHEP::keV);
141 #endif
142 }}}}
143 
144 ofile << std::setprecision(6);
145 
146 // Close the output ASCII file
147 ofile.close();
148 }