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
00084
00085 void RunAction::EndOfRunAction(const G4Run*)
00086 {
00087 G4double meanEd = 0., meanEd2 = 0., rmsEd = 0., error_rel_sum = 0.;
00088 if (m_nbEntry) {
00089 meanEd = m_sumEdep/m_nbEntry; meanEd2 = m_sumEdep2/m_nbEntry;
00090 G4double variance = meanEd2 - meanEd*meanEd;
00091 if (variance > 0.) rmsEd = std::sqrt(variance);
00092 error_rel_sum = rmsEd/(meanEd*std::sqrt(double(m_nbEntry)));
00093 }
00094
00095
00096 std::ios::fmtflags mode = G4cout.flags();
00097 G4cout.setf(std::ios::fixed,std::ios::floatfield);
00098 G4int prec = G4cout.precision(3);
00099
00100
00101
00102 G4cout
00103 << "\n ---> Nb of non-empty events = " << m_nbEntry
00104 << "\n ---> Mean Edep per event = " << G4BestUnit(meanEd,"Energy")
00105 << "\n ---> Total Energy deposited = " << G4BestUnit(m_sumEdep,"Energy")
00106 << " +- " << G4BestUnit(m_sumEdep*error_rel_sum,"Energy")
00107 << " (-> " << 100*error_rel_sum << " %)"
00108 << G4endl;
00109
00110
00111
00112 G4double mass = m_detector->GetAstronaut()->GetLogicalVolume()->GetMass();
00113
00114
00115
00116 G4double dose = m_sumEdep/mass;
00117 G4cout.setf(mode,std::ios::floatfield);
00118 G4cout.precision(5);
00119 G4cout << " ---> Dose = " << G4BestUnit(dose,"Dose")
00120 << " +- " << G4BestUnit(dose*error_rel_sum,"Dose") << "\n"
00121 << G4endl;
00122
00123
00124 G4cout.setf(mode,std::ios::floatfield);
00125 G4cout.precision(prec);
00126
00127
00128
00129 for (G4int k=0; k<m_nbSlices; k++) m_depthDose[k] /= mass;
00130
00131 for (G4int k=0; k<m_nbSlices; k++) m_depthDoseSD[k] /= mass;
00132
00133
00134
00135
00136
00137 WriteDepthDose("depthDose");
00138
00139
00140 G4cout.setf(mode,std::ios::floatfield);
00141 G4cout.precision(prec);
00142 }
00143
00144
00145
00146 #include <fstream>
00147
00148 void RunAction::WriteDepthDose(G4String name)
00149 {
00150 G4String fileName = name + ".ascii";
00151 std::ofstream File(fileName, std::ios::out);
00152
00153 G4String fileNameSD = name + "SD"+ ".ascii";
00154 std::ofstream FileSD(fileNameSD, std::ios::out);
00155
00156 std::ios::fmtflags mode = File.flags();
00157 File.setf( std::ios::scientific, std::ios::floatfield );
00158 G4int prec = File.precision(4);
00159
00160 std::ios::fmtflags modeSD = FileSD.flags();
00161 FileSD.setf( std::ios::scientific, std::ios::floatfield );
00162 G4int precSD = FileSD.precision(4);
00163
00164 File << " Longitudinal depth dose distribution \n "
00165 << "\n zLocal (mm)\t m_depthDose(gray) \n" << G4endl;
00166
00167 FileSD << " Longitudinal depth dose distribution \n "
00168 << "\n zLocal (mm)\t m_depthDose(gray) \n" << G4endl;
00169
00170 G4double zLocal;
00171 for (G4int k=0; k<m_nbSlices; k++) {
00172 zLocal = (k+0.5)*m_sliceWidth;
00173 File << " " << zLocal/mm
00174 << "\t " << m_depthDose[k]/gray << G4endl;
00175 }
00176
00177
00178 for (G4int k=0; k<m_nbSlices; k++) {
00179 zLocal = (k+0.5)*m_sliceWidth;
00180 FileSD << " " << zLocal/mm
00181 << "\t " << m_depthDoseSD[k]/gray << G4endl;
00182 }
00183
00184
00185 File.setf(mode,std::ios::floatfield);
00186 File.precision(prec);
00187
00188 FileSD.setf(modeSD,std::ios::floatfield);
00189 FileSD.precision(precSD);
00190
00191 }
00192
00193
00194