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