File indexing completed on 2025-02-23 09:21:46
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include "DicomRunAction.hh"
0032
0033 #include "DicomRun.hh"
0034
0035
0036 #include "G4RunManager.hh"
0037 #include "G4SystemOfUnits.hh"
0038 #include "G4THitsMap.hh"
0039 #include "G4UnitsTable.hh"
0040
0041 #include <fstream>
0042 #include <iomanip>
0043
0044
0045
0046 DicomRunAction::DicomRunAction() : G4UserRunAction(), fDcmrun(0), fFieldValue(14)
0047 {
0048
0049
0050 fSDName.push_back(G4String("phantomSD"));
0051 }
0052
0053
0054
0055 DicomRunAction::~DicomRunAction()
0056 {
0057 fSDName.clear();
0058 }
0059
0060
0061 G4Run* DicomRunAction::GenerateRun()
0062 {
0063
0064
0065
0066
0067 return fDcmrun = new DicomRun(fSDName);
0068 }
0069
0070
0071 void DicomRunAction::BeginOfRunAction(const G4Run* aRun)
0072 {
0073 G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
0074
0075 G4RunManager::GetRunManager()->SetRandomNumberStore(true);
0076 G4RunManager::GetRunManager()->SetRandomNumberStorePerEvent(false);
0077 G4RunManager::GetRunManager()->SetRandomNumberStoreDir(G4String("dicom-run-")
0078 + std::to_string(aRun->GetRunID()));
0079
0080 G4int progress = aRun->GetNumberOfEventToBeProcessed() / 100;
0081 progress = (progress < 1) ? 1 : progress;
0082 G4RunManager::GetRunManager()->SetPrintProgress(progress);
0083 }
0084
0085
0086 void DicomRunAction::EndOfRunAction(const G4Run* aRun)
0087 {
0088 G4int nofEvents = aRun->GetNumberOfEvent();
0089
0090 static G4double local_total_dose = 0;
0091 G4double total_dose = 0;
0092
0093 const DicomRun* reRun = static_cast<const DicomRun*>(aRun);
0094
0095 for (G4int i = 0; i < (G4int)fSDName.size(); i++) {
0096
0097
0098
0099
0100
0101
0102 G4THitsMap<G4double>* DoseDeposit = reRun->GetHitsMap(fSDName[i] + "/DoseDeposit");
0103
0104 if (DoseDeposit && DoseDeposit->GetMap()->size() != 0) {
0105 std::map<G4int, G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
0106 for (; itr != DoseDeposit->GetMap()->end(); itr++) {
0107 if (!IsMaster()) {
0108 local_total_dose += *(itr->second);
0109 }
0110 total_dose += *(itr->second);
0111 }
0112 }
0113 }
0114
0115 if (IsMaster()) {
0116 G4cout << "--------------------End of Global Run-----------------------" << G4endl;
0117 G4cout << " The run was " << nofEvents << " events " << G4endl;
0118 G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose / gray << " Gy" << G4endl;
0119 G4cout << " TOTAL DOSE : \t" << total_dose / gray << " Gy" << G4endl;
0120 }
0121 else {
0122 G4cout << "--------------------End of Local Run------------------------" << G4endl;
0123 G4cout << " The run was " << nofEvents << G4endl;
0124 G4cout << "LOCAL TOTAL DOSE : \t" << local_total_dose / gray << " Gy" << G4endl;
0125 G4cout << " TOTAL DOSE : \t" << total_dose / gray << " Gy" << G4endl;
0126 }
0127
0128 if (IsMaster()) {
0129 G4cout << " ###### EndOfRunAction ###### " << G4endl;
0130
0131 const DicomRun* re02Run = static_cast<const DicomRun*>(aRun);
0132
0133
0134 for (G4int i = 0; i < (G4int)fSDName.size(); i++) {
0135
0136
0137
0138
0139
0140
0141 G4THitsMap<G4double>* DoseDeposit = re02Run->GetHitsMap(fSDName[i] + "/DoseDeposit");
0142
0143 G4cout << "=============================================================" << G4endl;
0144 G4cout << " Number of event processed : " << aRun->GetNumberOfEvent() << G4endl;
0145 G4cout << "=============================================================" << G4endl;
0146
0147 std::ofstream fileout;
0148 G4String fname = "dicom.out";
0149 fileout.open(fname);
0150 G4cout << " opened file " << fname << " for dose output" << G4endl;
0151
0152 if (DoseDeposit && DoseDeposit->GetMap()->size() != 0) {
0153 std::ostream* myout = &G4cout;
0154 PrintHeader(myout);
0155 std::map<G4int, G4double*>::iterator itr = DoseDeposit->GetMap()->begin();
0156 for (; itr != DoseDeposit->GetMap()->end(); itr++) {
0157 fileout << itr->first << " " << *(itr->second) / CLHEP::gray << G4endl;
0158
0159
0160
0161
0162 }
0163 G4cout << "=============================================" << G4endl;
0164 }
0165 else {
0166 G4Exception("DicomRunAction", "000", JustWarning,
0167 "DoseDeposit HitsMap is either a null pointer of the HitsMap was empty");
0168 }
0169 fileout.close();
0170 G4cout << " closed file " << fname << " for dose output" << G4endl;
0171 }
0172 }
0173
0174 G4cout << "Finished : End of Run Action " << aRun->GetRunID() << G4endl;
0175 }
0176
0177
0178 void DicomRunAction::PrintHeader(std::ostream* out)
0179 {
0180 std::vector<G4String> vecScoreName;
0181 vecScoreName.push_back("DoseDeposit");
0182
0183
0184
0185 std::string vname;
0186 *out << std::setw(10) << "Voxel"
0187 << " |";
0188 for (std::vector<G4String>::iterator it = vecScoreName.begin(); it != vecScoreName.end(); it++) {
0189
0190
0191
0192
0193
0194 *out << std::setw(fFieldValue) << (*it) << " |";
0195 }
0196 *out << G4endl;
0197 }
0198
0199
0200 std::string DicomRunAction::FillString(const std::string& name, char c, G4int n, G4bool back)
0201 {
0202 std::string fname("");
0203 G4int k = G4int(n - name.size());
0204 if (k > 0) {
0205 if (back) {
0206 fname = name;
0207 fname += std::string(k, c);
0208 }
0209 else {
0210 fname = std::string(k, c);
0211 fname += name;
0212 }
0213 }
0214 else {
0215 fname = name;
0216 }
0217 return fname;
0218 }