Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:52:35

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file Dose.cc
0027 /// \brief Implementation of the RadioBio::Dose class
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0046 
0047 Dose::Dose()
0048 {
0049   // Default output filename
0050   fPath = "RadioBio_Dose.out";
0051 
0052   // Create the messenger
0053   fMessenger = new DoseMessenger(this);
0054 
0055   // Initialize the class
0056   Initialize();
0057 }
0058 
0059 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0060 
0061 Dose::~Dose()
0062 {
0063   delete fMessenger;
0064 }
0065 
0066 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0080 
0081 void Dose::Compute()
0082 {
0083   // Skip Dose computation if calculation not enabled.
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     // compute total Dose
0107     fDose[v] = fEnDep[v] / voxelMass;
0108   }
0109 
0110   fCalculated = true;
0111 }
0112 
0113 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0114 
0115 void Dose::Store()
0116 {
0117   // Skip Dose store if calculation not enabled.
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0154 
0155 void Dose::AddFromAccumulable(G4VAccumulable* GenAcc)
0156 {
0157   DoseAccumulable* acc = (DoseAccumulable*)GenAcc;
0158   AddEnergyDeposit(acc->GetEnDeposit());
0159   fCalculated = false;
0160 }
0161 
0162 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0175 
0176 void Dose::PrintParameters()
0177 {
0178   G4cout << "*******************************************" << G4endl
0179          << "****** Parameters of the class Dose *******" << G4endl
0180          << "*******************************************" << G4endl;
0181   PrintVirtualParameters();
0182 }
0183 
0184 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0185 
0186 }  // namespace RadioBio