Geant4  10.00.p01
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 74809 2013-10-22 09:49:26Z 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 = reRun->GetHitsMap(fSDName[i]+"/DoseDeposit");
111 
112  if( DoseDeposit && DoseDeposit->GetMap()->size() != 0 ) {
113  std::map<G4int,G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
114  for(; itr != DoseDeposit->GetMap()->end(); itr++) {
115  if(!IsMaster()) { local_total_dose += *(itr->second); }
116  total_dose += *(itr->second);
117  }
118  }
119 
120  }
121 
122  if (IsMaster())
123  {
124  G4cout
125  << "\n--------------------End of Global Run-----------------------"
126  << " \n The run was " << nofEvents << " events " << G4endl;
127  G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose/gray << " Gy" << std::endl;
128  G4cout << " TOTAL DOSE : \t" << total_dose/gray << " Gy" << std::endl;
129 
130  }
131  else
132  {
133  G4cout
134  << "\n--------------------End of Local Run------------------------"
135  << " \n The run was " << nofEvents << G4endl;
136  G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose/gray << " Gy" << std::endl;
137  G4cout << " TOTAL DOSE : \t" << total_dose/gray << " Gy" << std::endl;
138 
139  }
140 
141  if(IsMaster()) {
142  G4cout << " ###### EndOfRunAction ###### " << G4endl;
143  //- DicomRun object.
144  const DicomRun* re02Run = static_cast<const DicomRun*>(aRun);
145  //--- Dump all scored quantities involved in DicomRun.
146 
147  for ( G4int i = 0; i < (G4int)fSDName.size(); i++ ){
148  //
149  //---------------------------------------------
150  // Dump accumulated quantities for this RUN.
151  // (Display only central region of x-y plane)
152  // 0 ConcreteSD/DoseDeposit
153  //---------------------------------------------
154  G4THitsMap<G4double>* DoseDeposit = re02Run->GetHitsMap(fSDName[i]+"/DoseDeposit");
155 
156  G4cout << "=============================================================" <<G4endl;
157  G4cout << " Number of event processed : "<< aRun->GetNumberOfEvent() << G4endl;
158  G4cout << "=============================================================" <<G4endl;
159 
160  std::ofstream fileout;
161  G4String fname = "dicom.out";
162  fileout.open(fname);
163  G4cout << " opened file " << fname << " for dose output" << G4endl;
164 
165 
166  if( DoseDeposit && DoseDeposit->GetMap()->size() != 0 ) {
167  std::ostream *myout = &G4cout;
168  PrintHeader(myout);
169  std::map<G4int,G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
170  for(; itr != DoseDeposit->GetMap()->end(); itr++) {
171  fileout << itr->first
172  << " " << *(itr->second)/CLHEP::gray
173  << G4endl;
174  G4cout << " " << itr->first
175  << " " << std::setprecision(6) << *(itr->second)/CLHEP::gray << " Gy"
176  << G4endl;
177  }
178  G4cout << "============================================="<<G4endl;
179  } else {
180  G4Exception("DicomRunAction", "000", JustWarning,
181  "DoseDeposit HitsMap is either a null pointer of the HitsMap was empty");
182  }
183  fileout.close();
184  G4cout << " closed file " << fname << " for dose output" << G4endl;
185 
186  }
187  }
188 
189  G4cout << "Finished : End of Run Action " << aRun->GetRunID() << G4endl;
190 
191 }
192 
193 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
194 void DicomRunAction::PrintHeader(std::ostream *out)
195 {
196  std::vector<G4String> vecScoreName;
197  vecScoreName.push_back("DoseDeposit");
198 
199  // head line
200  //
201  std::string vname;
202  *out << std::setw(10) << "Voxel" << " |";
203  for (std::vector<G4String>::iterator it = vecScoreName.begin();
204  it != vecScoreName.end(); it++) {
205  //vname = FillString((*it),
206 // ' ',
207 // FieldValue+1,
208 // false);
209 // *out << vname << '|';
210  *out << std::setw(fFieldValue) << (*it) << " |";
211  }
212  *out << G4endl;
213 }
214 
215 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
216 std::string DicomRunAction::FillString(const std::string &name,
217  char c, G4int n, G4bool back)
218 {
219  std::string fname("");
220  G4int k = n - name.size();
221  if (k > 0) {
222  if (back) {
223  fname = name;
224  fname += std::string(k,c);
225  }
226  else {
227  fname = std::string(k,c);
228  fname += name;
229  }
230  }
231  else {
232  fname = name;
233  }
234  return fname;
235 }
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:74
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