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