File indexing completed on 2025-02-23 09:22:19
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 #include "Dose.hh"
0031
0032 #include "G4SystemOfUnits.hh"
0033
0034 #include "DetectorConstruction.hh"
0035 #include "DoseAccumulable.hh"
0036 #include "DoseMessenger.hh"
0037 #include "VoxelizedSensitiveDetector.hh"
0038
0039 #include <fstream>
0040
0041 namespace RadioBio
0042 {
0043
0044 #define width 15L
0045
0046
0047
0048 Dose::Dose()
0049 {
0050
0051 fPath = "RadioBio_Dose.out";
0052
0053
0054 fMessenger = new DoseMessenger(this);
0055
0056
0057 Initialize();
0058 }
0059
0060
0061
0062 Dose::~Dose()
0063 {
0064 delete fMessenger;
0065 }
0066
0067
0068
0069 void Dose::Initialize()
0070 {
0071 if (fVerboseLevel > 0) G4cout << "Dose::Initialize() called" << G4endl;
0072
0073 G4int VoxelNumber = VoxelizedSensitiveDetector::GetInstance()->GetTotalVoxelNumber();
0074
0075 fEnDep.resize(VoxelNumber);
0076 fDose.resize(VoxelNumber);
0077 fInitialized = true;
0078 }
0079
0080
0081
0082 void Dose::Compute()
0083 {
0084
0085 if (!fCalculationEnabled) {
0086 if (fVerboseLevel > 0) {
0087 G4cout << "Dose::Compute() called but skipped as calculation not enabled" << G4endl;
0088 }
0089 return;
0090 }
0091
0092 if (fCalculated) return;
0093
0094 if (fVerboseLevel > 0) G4cout << "Dose::Compute() called" << G4endl;
0095
0096 auto voxSensDet = VoxelizedSensitiveDetector::GetInstance();
0097
0098 if (voxSensDet == nullptr)
0099 G4Exception("Dose::Compute", "VoxNotInit", FatalException,
0100 "Calculating dose without voxelized geometry pointer!");
0101
0102 G4double voxelMass = voxSensDet->GetVoxelMass();
0103
0104 for (G4int v = 0; v < voxSensDet->GetTotalVoxelNumber(); v++) {
0105 if (fVerboseLevel > 1) G4cout << "Calculating Dose for voxel number " << v << G4endl;
0106
0107
0108 fDose[v] = fEnDep[v] / voxelMass;
0109 }
0110
0111 fCalculated = true;
0112 }
0113
0114
0115
0116 void Dose::Store()
0117 {
0118
0119 if (!fCalculationEnabled) {
0120 if (fVerboseLevel > 0) {
0121 G4cout << "Dose::Store() called but skipped as calculation not enabled" << G4endl;
0122 }
0123 return;
0124 }
0125
0126 if (fSaved == true)
0127 G4Exception("Dose::Store", "DoseOverwrite", JustWarning,
0128 "Overwriting Dose file. For multiple runs, change filename.");
0129
0130 Compute();
0131
0132 std::ofstream ofs(fPath);
0133
0134 if (ofs.is_open()) {
0135 ofs << "x_index" << std::setw(width) << "y_index" << std::setw(width) << "z_index"
0136 << std::setw(width) << "Dose (Gy)" << G4endl;
0137
0138 auto voxSensDet = VoxelizedSensitiveDetector::GetInstance();
0139
0140 for (G4int i = 0; i < voxSensDet->GetVoxelNumberAlongX(); i++)
0141 for (G4int j = 0; j < voxSensDet->GetVoxelNumberAlongY(); j++)
0142 for (G4int k = 0; k < voxSensDet->GetVoxelNumberAlongZ(); k++) {
0143 G4int v = voxSensDet->GetThisVoxelNumber(i, j, k);
0144 ofs << i << std::setw(width) << j << std::setw(width) << k << std::setw(width)
0145 << fDose[v] / gray << G4endl;
0146 }
0147 }
0148 if (fVerboseLevel > 0) {
0149 G4cout << "Dose: Dose written to " << fPath << G4endl;
0150 }
0151 fSaved = true;
0152 }
0153
0154
0155
0156 void Dose::AddFromAccumulable(G4VAccumulable* GenAcc)
0157 {
0158 DoseAccumulable* acc = (DoseAccumulable*)GenAcc;
0159 AddEnergyDeposit(acc->GetEnDeposit());
0160 fCalculated = false;
0161 }
0162
0163
0164
0165 void Dose::Reset()
0166 {
0167 if (fVerboseLevel > 1) {
0168 G4cout << "Dose::Reset(): ";
0169 }
0170 fEnDep = 0.0;
0171 fDose = 0.0;
0172 fCalculated = false;
0173 }
0174
0175
0176
0177 void Dose::PrintParameters()
0178 {
0179 G4cout << "*******************************************" << G4endl
0180 << "****** Parameters of the class Dose *******" << G4endl
0181 << "*******************************************" << G4endl;
0182 PrintVirtualParameters();
0183 }
0184
0185
0186
0187 }