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