Geant4  10.01.p03
DicomRunAction.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 // $Id: DicomRunAction.cc 84839 2014-10-21 13:44:55Z gcosmo $
27 //
30 //
31 
32 #include "DicomRunAction.hh"
33 #include "DicomRun.hh"
34 
35 //-- In order to obtain detector information.
36 #include <fstream>
37 #include <iomanip>
38 #include "G4THitsMap.hh"
39 
40 #include "G4UnitsTable.hh"
41 #include "G4SystemOfUnits.hh"
42 
43 #include "G4RunManager.hh"
44 
45 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
46 
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 : G4UserRunAction(), dcmrun(0), fFieldValue(14)
57 {
58  // - Prepare data member for DicomRun.
59  // vector represents a list of MultiFunctionalDetector names.
60  fSDName.push_back(G4String("phantomSD"));
61  fgInstance = this;
62 }
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 {
68  fSDName.clear();
69  fgInstance = 0;
70 }
71 
72 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
74 {
75  // Generate new RUN object, which is specially
76  // dedicated for MultiFunctionalDetector scheme.
77  // Detail description can be found in DicomRun.hh/cc.
78  //return new DicomRun(fSDName);
79  return dcmrun = new DicomRun(fSDName);
80 }
81 
82 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84 {
85  G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
86  //inform the runManager to save random number seed
88 }
89 
90 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
92 {
93  G4int nofEvents = aRun->GetNumberOfEvent();
94  if (nofEvents == 0) return;
95  //print
96  //
97 
98  static double local_total_dose = 0;
99  double total_dose = 0;
100 
101  const DicomRun* reRun = static_cast<const DicomRun*>(aRun);
102  //--- Dump all scored quantities involved in DicomRun.
103  for ( G4int i = 0; i < (G4int)fSDName.size(); i++ ){
104  //
105  //---------------------------------------------
106  // Dump accumulated quantities for this RUN.
107  // (Display only central region of x-y plane)
108  // 0 ConcreteSD/DoseDeposit
109  //---------------------------------------------
110  G4THitsMap<G4double>* DoseDeposit =
111  reRun->GetHitsMap(fSDName[i]+"/DoseDeposit");
112 
113  if( DoseDeposit && DoseDeposit->GetMap()->size() != 0 ) {
114  std::map<G4int,G4double*>::iterator itr =
115  DoseDeposit->GetMap()->begin();
116  for(; itr != DoseDeposit->GetMap()->end(); itr++) {
117  if(!IsMaster()) { local_total_dose += *(itr->second); }
118  total_dose += *(itr->second);
119  }
120  }
121 
122  }
123 
124  if (IsMaster())
125  {
126  G4cout
127  << "\n--------------------End of Global Run-----------------------"
128  << " \n The run was " << nofEvents << " events " << G4endl;
129  G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose/gray
130  << " Gy" << std::endl;
131  G4cout << " TOTAL DOSE : \t" << total_dose/gray
132  << " Gy" << std::endl;
133 
134  }
135  else
136  {
137  G4cout
138  << "\n--------------------End of Local Run------------------------"
139  << " \n The run was " << nofEvents << G4endl;
140  G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose/gray
141  << " Gy" << std::endl;
142  G4cout << " TOTAL DOSE : \t" << total_dose/gray
143  << " Gy" << std::endl;
144 
145  }
146 
147  if(IsMaster()) {
148  G4cout << " ###### EndOfRunAction ###### " << G4endl;
149  //- DicomRun object.
150  const DicomRun* re02Run = static_cast<const DicomRun*>(aRun);
151  //--- Dump all scored quantities involved in DicomRun.
152 
153  for ( G4int i = 0; i < (G4int)fSDName.size(); i++ ){
154  //
155  //---------------------------------------------
156  // Dump accumulated quantities for this RUN.
157  // (Display only central region of x-y plane)
158  // 0 ConcreteSD/DoseDeposit
159  //---------------------------------------------
160  G4THitsMap<G4double>* DoseDeposit =
161  re02Run->GetHitsMap(fSDName[i]+"/DoseDeposit");
162 
163  G4cout << "============================================================="
164  <<G4endl;
165  G4cout << " Number of event processed : "
166  << aRun->GetNumberOfEvent() << G4endl;
167  G4cout << "============================================================="
168  <<G4endl;
169 
170  std::ofstream fileout;
171  G4String fname = "dicom.out";
172  fileout.open(fname);
173  G4cout << " opened file " << fname << " for dose output" << G4endl;
174 
175 
176  if( DoseDeposit && DoseDeposit->GetMap()->size() != 0 ) {
177  std::ostream *myout = &G4cout;
178  PrintHeader(myout);
179  std::map<G4int,G4double*>::iterator itr =DoseDeposit->GetMap()->begin();
180  for(; itr != DoseDeposit->GetMap()->end(); itr++) {
181  fileout << itr->first
182  << " " << *(itr->second)/CLHEP::gray
183  << G4endl;
184  G4cout << " " << itr->first
185  << " " << std::setprecision(6)
186  << *(itr->second)/CLHEP::gray << " Gy"
187  << G4endl;
188  }
189  G4cout << "============================================="<<G4endl;
190  } else {
191  G4Exception("DicomRunAction", "000", JustWarning,
192  "DoseDeposit HitsMap is either a null pointer of the HitsMap was empty");
193  }
194  fileout.close();
195  G4cout << " closed file " << fname << " for dose output" << G4endl;
196 
197  }
198  }
199 
200  G4cout << "Finished : End of Run Action " << aRun->GetRunID() << G4endl;
201 
202 }
203 
204 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
205 void DicomRunAction::PrintHeader(std::ostream *out)
206 {
207  std::vector<G4String> vecScoreName;
208  vecScoreName.push_back("DoseDeposit");
209 
210  // head line
211  //
212  std::string vname;
213  *out << std::setw(10) << "Voxel" << " |";
214  for (std::vector<G4String>::iterator it = vecScoreName.begin();
215  it != vecScoreName.end(); it++) {
216  //vname = FillString((*it),
217  // ' ',
218  // FieldValue+1,
219  // false);
220  // *out << vname << '|';
221  *out << std::setw(fFieldValue) << (*it) << " |";
222  }
223  *out << G4endl;
224 }
225 
226 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
227 std::string DicomRunAction::FillString(const std::string &name,
228  char c, G4int n, G4bool back)
229 {
230  std::string fname("");
231  G4int k = n - name.size();
232  if (k > 0) {
233  if (back) {
234  fname = name;
235  fname += std::string(k,c);
236  }
237  else {
238  fname = std::string(k,c);
239  fname += name;
240  }
241  }
242  else {
243  fname = name;
244  }
245  return fname;
246 }
DicomRun class.
Definition: DicomRun.hh:50
virtual void EndOfRunAction(const G4Run *)
void PrintHeader(std::ostream *out)
Definition of the DicomRunAction class.
G4String name
Definition: TRTMaterials.hh:40
virtual G4Run * GenerateRun()
std::vector< G4String > fSDName
void SetRandomNumberStore(G4bool flag)
int G4int
Definition: G4Types.hh:78
Definition of the DicomRun class.
DicomRunAction()
Constructor.
G4GLOB_DLL std::ostream G4cout
G4int GetNumberOfEvent() const
Definition: G4Run.hh:79
bool G4bool
Definition: G4Types.hh:79
G4bool IsMaster() const
G4int GetRunID() const
Definition: G4Run.hh:76
Definition: G4Run.hh:46
virtual void BeginOfRunAction(const G4Run *)
const G4int n
static DicomRunAction * fgInstance
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static const double gray
Definition: G4SIunits.hh:276
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:68
virtual ~DicomRunAction()
Destructor.
#define G4endl
Definition: G4ios.hh:61
static DicomRunAction * Instance()
G4THitsMap< G4double > * GetHitsMap(G4int i) const
Definition: DicomRun.hh:71
std::string FillString(const std::string &name, char c, G4int n, G4bool back=true)
DicomRun * dcmrun