00001
00002
00003
00004
00005
00006 #include "RunAction.hh"
00007 #include "DetectorConstruction.hh"
00008
00009 #include "G4Run.hh"
00010 #include "G4RunManager.hh"
00011 #include "G4UnitsTable.hh"
00012
00013 #include <iomanip>
00014
00015
00016
00017 RunAction::RunAction(DetectorConstruction* det)
00018 :m_detector(det)
00019 { }
00020
00021
00022
00023 RunAction::~RunAction()
00024 { }
00025
00026
00027
00028 void RunAction::BeginOfRunAction(const G4Run* run)
00029 {
00030 G4cout << "### Run " << run->GetRunID() << " start." << G4endl;
00031
00032
00033
00034 m_sumEdep = m_sumEdep2 = 0.;
00035 m_nbEntry = 0;
00036
00037
00038
00039 m_nbSlices = 100;
00040 m_astronautHeight = m_detector->GetAstronautHeight();
00041 m_sliceWidth = m_astronautHeight/m_nbSlices;
00042 m_depthDose.resize(m_nbSlices, 0.);
00043 m_depthDoseSD.resize(m_nbSlices, 0.);
00044
00045 }
00046
00047
00048
00049 void RunAction::SumDepthDose(G4double zLocal, G4double edep)
00050 {
00051
00052 if (zLocal > m_astronautHeight) {
00053 G4cout << "\n --> warning from RunAction::SumDepthDose() : "
00054 << " zLocal beyond astronaut height : " << zLocal/mm << G4endl;
00055 return;
00056 }
00057
00058
00059 G4int k = int(zLocal/m_sliceWidth);
00060
00061
00062 m_depthDose[k] += edep;
00063 }
00064
00065
00066 void RunAction::SumDepthDoseSD(G4double zLocal, G4double edep)
00067 {
00068
00069 if (zLocal > m_astronautHeight) {
00070 G4cout << "\n --> warning from RunAction::SumDepthDose() : "
00071 << " zLocal beyond astronaut height : " << zLocal/mm << G4endl;
00072 return;
00073 }
00074
00075
00076 G4int k = int(zLocal/m_sliceWidth);
00077
00078
00079 m_depthDoseSD[k] += edep;
00080 }
00081
00082
00083 void RunAction::SumDepthDoseSD(G4int k, G4double edep)
00084 {
00085
00086 if (k >= m_nbSlices) {
00087 G4cout << "\n --> warning from RunAction::SumDepthDoseSD() : "
00088 << " k beyond nb slices : " << k << G4endl;
00089 return;
00090 }
00091
00092
00093 m_depthDoseSD[k] += edep;
00094 }
00095
00096
00097 G4int RunAction::GetSliceNumber(G4double zLocal) const
00098 {
00099
00100 G4int k = int(zLocal/m_sliceWidth);
00101
00102
00103 if (k >= m_nbSlices) {
00104 G4cout << "\n --> warning from RunAction::GetSliceNumber() : "
00105 << " k beyond nb slices : " << k << " returning -1" << G4endl;
00106 k = -1;
00107 }
00108
00109 return k;
00110 }
00111
00112
00113
00114 void RunAction::EndOfRunAction(const G4Run*)
00115 {
00116 G4double meanEd = 0., meanEd2 = 0., rmsEd = 0., error_rel_sum = 0.;
00117 if (m_nbEntry) {
00118 meanEd = m_sumEdep/m_nbEntry; meanEd2 = m_sumEdep2/m_nbEntry;
00119 G4double variance = meanEd2 - meanEd*meanEd;
00120 if (variance > 0.) rmsEd = std::sqrt(variance);
00121 error_rel_sum = rmsEd/(meanEd*std::sqrt(double(m_nbEntry)));
00122 }
00123
00124
00125 std::ios::fmtflags mode = G4cout.flags();
00126 G4cout.setf(std::ios::fixed,std::ios::floatfield);
00127 G4int prec = G4cout.precision(3);
00128
00129
00130
00131 G4cout
00132 << "\n ---> Nb of non-empty events = " << m_nbEntry
00133 << "\n ---> Mean Edep per event = " << G4BestUnit(meanEd,"Energy")
00134 << "\n ---> Total Energy deposited = " << G4BestUnit(m_sumEdep,"Energy")
00135 << " +- " << G4BestUnit(m_sumEdep*error_rel_sum,"Energy")
00136 << " (-> " << 100*error_rel_sum << " %)"
00137 << G4endl;
00138
00139
00140
00141 G4double mass = m_detector->GetAstronaut()->GetLogicalVolume()->GetMass();
00142
00143
00144
00145 G4double dose = m_sumEdep/mass;
00146 G4cout.setf(mode,std::ios::floatfield);
00147 G4cout.precision(5);
00148 G4cout << " ---> Dose = " << G4BestUnit(dose,"Dose")
00149 << " +- " << G4BestUnit(dose*error_rel_sum,"Dose") << "\n"
00150 << G4endl;
00151
00152
00153 G4cout.setf(mode,std::ios::floatfield);
00154 G4cout.precision(prec);
00155
00156
00157
00158 for (G4int k=0; k<m_nbSlices; k++) m_depthDose[k] /= mass;
00159
00160 for (G4int k=0; k<m_nbSlices; k++) m_depthDoseSD[k] /= mass;
00161
00162
00163
00164
00165
00166 WriteDepthDose("depthDose");
00167
00168
00169 G4cout.setf(mode,std::ios::floatfield);
00170 G4cout.precision(prec);
00171 }
00172
00173
00174
00175 #include <fstream>
00176
00177 void RunAction::WriteDepthDose(G4String name)
00178 {
00179 G4String fileName = name + ".ascii";
00180 std::ofstream File(fileName, std::ios::out);
00181
00182 G4String fileNameSD = name + "SD"+ ".ascii";
00183 std::ofstream FileSD(fileNameSD, std::ios::out);
00184
00185 std::ios::fmtflags mode = File.flags();
00186 File.setf( std::ios::scientific, std::ios::floatfield );
00187 G4int prec = File.precision(4);
00188
00189 std::ios::fmtflags modeSD = FileSD.flags();
00190 FileSD.setf( std::ios::scientific, std::ios::floatfield );
00191 G4int precSD = FileSD.precision(4);
00192
00193 File << " Longitudinal depth dose distribution \n "
00194 << "\n zLocal (mm)\t m_depthDose(gray) \n" << G4endl;
00195
00196 FileSD << " Longitudinal depth dose distribution \n "
00197 << "\n zLocal (mm)\t m_depthDose(gray) \n" << G4endl;
00198
00199 G4double zLocal;
00200 for (G4int k=0; k<m_nbSlices; k++) {
00201 zLocal = (k+0.5)*m_sliceWidth;
00202 File << " " << zLocal/mm
00203 << "\t " << m_depthDose[k]/gray << G4endl;
00204 }
00205
00206
00207 for (G4int k=0; k<m_nbSlices; k++) {
00208 zLocal = (k+0.5)*m_sliceWidth;
00209 FileSD << " " << zLocal/mm
00210 << "\t " << m_depthDoseSD[k]/gray << G4endl;
00211 }
00212
00213
00214 File.setf(mode,std::ios::floatfield);
00215 File.precision(prec);
00216
00217 FileSD.setf(modeSD,std::ios::floatfield);
00218 FileSD.precision(precSD);
00219
00220 }
00221
00222
00223