Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 92820 2015-09-17 15:22:14Z 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 
47 DicomRunAction* DicomRunAction::fInstance = 0;
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
50 
51 DicomRunAction* DicomRunAction::Instance() { return fInstance; }
52 
53 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
56 : G4UserRunAction(), fDcmrun(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  fInstance = this;
62 }
63 
64 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 {
68  fSDName.clear();
69  fInstance = 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 fDcmrun = 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  if( DoseDeposit && DoseDeposit->GetMap()->size() != 0 ) {
176  std::ostream *myout = &G4cout;
177  PrintHeader(myout);
178  std::map<G4int,G4double*>::iterator itr =DoseDeposit->GetMap()->begin();
179  for(; itr != DoseDeposit->GetMap()->end(); itr++) {
180  fileout << itr->first
181  << " " << *(itr->second)/CLHEP::gray
182  << G4endl;
183  G4cout << " " << itr->first
184  << " " << std::setprecision(6)
185  << *(itr->second)/CLHEP::gray << " Gy"
186  << G4endl;
187  }
188  G4cout << "============================================="<<G4endl;
189  } else {
190  G4Exception("DicomRunAction", "000", JustWarning,
191  "DoseDeposit HitsMap is either a null pointer of the HitsMap was empty");
192  }
193  fileout.close();
194  G4cout << " closed file " << fname << " for dose output" << G4endl;
195 
196  }
197  }
198 
199  G4cout << "Finished : End of Run Action " << aRun->GetRunID() << G4endl;
200 
201 }
202 
203 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
204 void DicomRunAction::PrintHeader(std::ostream *out)
205 {
206  std::vector<G4String> vecScoreName;
207  vecScoreName.push_back("DoseDeposit");
208 
209  // head line
210  //
211  std::string vname;
212  *out << std::setw(10) << "Voxel" << " |";
213  for (std::vector<G4String>::iterator it = vecScoreName.begin();
214  it != vecScoreName.end(); it++) {
215  //vname = FillString((*it),
216  // ' ',
217  // FieldValue+1,
218  // false);
219  // *out << vname << '|';
220  *out << std::setw(fFieldValue) << (*it) << " |";
221  }
222  *out << G4endl;
223 }
224 
225 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
226 std::string DicomRunAction::FillString(const std::string &name,
227  char c, G4int n, G4bool back)
228 {
229  std::string fname("");
230  G4int k = n - name.size();
231  if (k > 0) {
232  if (back) {
233  fname = name;
234  fname += std::string(k,c);
235  }
236  else {
237  fname = std::string(k,c);
238  fname += name;
239  }
240  }
241  else {
242  fname = name;
243  }
244  return fname;
245 }
virtual void EndOfRunAction(const G4Run *)
const XML_Char * name
Definition: expat.h:151
void PrintHeader(std::ostream *out)
Definition of the DicomRunAction class.
virtual G4Run * GenerateRun()
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
static constexpr double gray
Definition: G4SIunits.hh:309
G4bool IsMaster() const
G4int GetRunID() const
Definition: G4Run.hh:76
Definition: G4Run.hh:46
virtual void BeginOfRunAction(const G4Run *)
const G4int n
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
std::map< G4int, T * > * GetMap() const
Definition: G4THitsMap.hh:99
virtual ~DicomRunAction()
Destructor.
#define G4endl
Definition: G4ios.hh:61
static DicomRunAction * Instance()
string fname
Definition: test.py:308
G4THitsMap< G4double > * GetHitsMap(G4int i) const
Definition: DicomRun.hh:71
tuple c
Definition: test.py:13
std::string FillString(const std::string &name, char c, G4int n, G4bool back=true)
static constexpr double gray