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 #ifdef G4ANALYSIS_USE
00014 #include "AIDA/AIDA.h"
00015 #endif
00016
00017 #include <iomanip>
00018
00019
00020
00021 RunAction::RunAction(DetectorConstruction* det)
00022 :m_detector(det)
00023 {
00024 BookHistoFile( "histograms", "root");
00025 }
00026
00027
00028
00029 RunAction::~RunAction()
00030 {
00031 SaveHistoFile();
00032 }
00033
00034
00035
00036 void RunAction::BeginOfRunAction(const G4Run* run)
00037 {
00038 G4cout << "### Run " << run->GetRunID() << " start." << G4endl;
00039
00040
00041
00042 m_sumEdep = m_sumEdep2 = 0.;
00043 m_nbEntry = 0;
00044
00045
00046
00047 m_nbSlices = 100;
00048 m_astronautHeight = m_detector->GetAstronautHeight();
00049 m_sliceWidth = m_astronautHeight/m_nbSlices;
00050 m_depthDose.resize(m_nbSlices, 0.);
00051 }
00052
00053
00054
00055 void RunAction::SumDepthDose(G4double zLocal, G4double edep)
00056 {
00057
00058 if (zLocal > m_astronautHeight) {
00059 G4cout << "\n --> warning from RunAction::SumDepthDose() : "
00060 << " zLocal beyond astronaut height : " << zLocal/mm << G4endl;
00061 return;
00062 }
00063
00064
00065 G4int k = int(zLocal/m_sliceWidth);
00066
00067
00068 m_depthDose[k] += edep;
00069 }
00070
00071
00072
00073 void RunAction::EndOfRunAction(const G4Run*)
00074 {
00075 G4double meanEd = 0., meanEd2 = 0., rmsEd = 0., error_rel_sum = 0.;
00076 if (m_nbEntry) {
00077 meanEd = m_sumEdep/m_nbEntry; meanEd2 = m_sumEdep2/m_nbEntry;
00078 G4double variance = meanEd2 - meanEd*meanEd;
00079 if (variance > 0.) rmsEd = std::sqrt(variance);
00080 error_rel_sum = rmsEd/(meanEd*std::sqrt(double(m_nbEntry)));
00081 }
00082
00083
00084 std::ios::fmtflags mode = G4cout.flags();
00085 G4cout.setf(std::ios::fixed,std::ios::floatfield);
00086 G4int prec = G4cout.precision(3);
00087
00088
00089
00090 G4cout
00091 << "\n ---> Nb of non-empty events = " << m_nbEntry
00092 << "\n ---> Mean Edep per event = " << G4BestUnit(meanEd,"Energy")
00093 << "\n ---> Total Energy deposited = " << G4BestUnit(m_sumEdep,"Energy")
00094 << " +- " << G4BestUnit(m_sumEdep*error_rel_sum,"Energy")
00095 << " (-> " << 100*error_rel_sum << " %)"
00096 << G4endl;
00097
00098
00099
00100 G4double mass = m_detector->GetAstronaut()->GetLogicalVolume()->GetMass();
00101
00102
00103
00104 G4double dose = m_sumEdep/mass;
00105 G4cout.setf(mode,std::ios::floatfield);
00106 G4cout.precision(5);
00107 G4cout << " ---> Dose = " << G4BestUnit(dose,"Dose")
00108 << " +- " << G4BestUnit(dose*error_rel_sum,"Dose") << "\n"
00109 << G4endl;
00110
00111
00112 G4cout.setf(mode,std::ios::floatfield);
00113 G4cout.precision(prec);
00114
00115
00116
00117 for (G4int k=0; k<m_nbSlices; k++) m_depthDose[k] /= mass;
00118
00119
00120
00121
00122
00123
00124 G4cout.setf(mode,std::ios::floatfield);
00125 G4cout.precision(prec);
00126 }
00127
00128
00129
00130 #include <fstream>
00131
00132 void RunAction::WriteDepthDose(G4String name)
00133 {
00134 G4String fileName = name + ".ascii";
00135 std::ofstream File(fileName, std::ios::out);
00136
00137 std::ios::fmtflags mode = File.flags();
00138 File.setf( std::ios::scientific, std::ios::floatfield );
00139 G4int prec = File.precision(4);
00140
00141 File << " Longitudinal depth dose distribution \n "
00142 << "\n zLocal (mm)\t m_depthDose(gray) \n" << G4endl;
00143
00144 G4double zLocal;
00145 for (G4int k=0; k<m_nbSlices; k++) {
00146 zLocal = (k+0.5)*m_sliceWidth;
00147 File << " " << zLocal/mm
00148 << "\t " << m_depthDose[k]/gray << G4endl;
00149 }
00150
00151
00152 File.setf(mode,std::ios::floatfield);
00153 File.precision(prec);
00154 }
00155
00156
00157
00158 void RunAction::BookHistoFile(G4String fileName, G4String type)
00159 {
00160 histoFile = fileName + "." + type;
00161 m_af = 0; m_tree = 0;
00162 m_histo[0] = m_histo[1] = 0;
00163
00164 #ifdef G4ANALYSIS_USE
00165
00166
00167 if ((type != "root") && (type != "hbook")) {
00168 G4cout << "\n----> Error in BookHisto. Type = " << type
00169 << " not recognized" << G4endl;
00170 return;
00171 }
00172
00173
00174 m_af = AIDA_createAnalysisFactory();
00175
00176 if (m_af) {
00177
00178 AIDA::ITreeFactory* tf = m_af->createTreeFactory();
00179
00180
00181 G4bool readOnly = false;
00182 G4bool createNew = true;
00183 G4String options = "export=root";
00184 m_tree = tf->create(histoFile,type,readOnly,createNew, options);
00185 delete tf;
00186
00187 if (m_tree) {
00188
00189 AIDA::IHistogramFactory* hf = m_af->createHistogramFactory(*m_tree);
00190
00191
00192 m_histo[0]=hf->createHistogram1D
00193 ("1","total energy deposit in astronaut",100,0.,500*MeV);
00194
00195 delete hf;
00196 G4cout << "\n----> Histogram m_tree is opened in " << histoFile << G4endl;
00197 }
00198 }
00199 #endif
00200 }
00201
00202
00203
00204 void RunAction::SaveHistoFile()
00205 {
00206 #ifdef G4ANALYSIS_USE
00207 m_tree->commit();
00208 m_tree->close();
00209 G4cout << "\n----> Histogram m_tree is saved in " << histoFile << G4endl;
00210
00211 delete m_tree;
00212 delete m_af;
00213 #endif
00214 }
00215
00216