File indexing completed on 2026-04-28 07:17:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "RunAction.hh"
0030
0031 #include "DetectorConstruction.hh"
0032 #include "PhysicsList.hh"
0033 #include "PrimaryGeneratorAction.hh"
0034 #include "StepMax.hh"
0035
0036 #include "G4Run.hh"
0037 #include "G4RunManager.hh"
0038 #include "G4SystemOfUnits.hh"
0039 #include "G4UnitsTable.hh"
0040 #include "G4ios.hh"
0041 #include "Randomize.hh"
0042
0043
0044
0045 RunAction::RunAction(DetectorConstruction* det, PhysicsList* phys, PrimaryGeneratorAction* kin)
0046 : G4UserRunAction(),
0047 fAnalysisManager(0),
0048 fDetector(det),
0049 fPhysics(phys),
0050 fKinematic(kin),
0051 fTallyEdep(new G4double[kMaxTally]),
0052 fProjRange(0.),
0053 fProjRange2(0.),
0054 fEdeptot(0.),
0055 fEniel(0.),
0056 fNbPrimarySteps(0),
0057 fRange(0)
0058 {
0059
0060 BookHisto();
0061 }
0062
0063
0064
0065 RunAction::~RunAction()
0066 {
0067 delete[] fTallyEdep;
0068 }
0069
0070
0071
0072 void RunAction::BeginOfRunAction(const G4Run* aRun)
0073 {
0074 G4cout << "### Run " << aRun->GetRunID() << " start." << G4endl;
0075
0076 if (!fAnalysisManager) {
0077 BookHisto();
0078 }
0079
0080 CLHEP::HepRandom::showEngineStatus();
0081
0082
0083
0084 fNbPrimarySteps = 0;
0085 fRange = 0;
0086 fProjRange = fProjRange2 = 0.;
0087 fEdeptot = fEniel = 0.;
0088 for (G4int j = 0; j < kMaxTally; ++j) {
0089 fTallyEdep[j] = 0.;
0090 }
0091 fKinematic->ResetEbeamCumul();
0092
0093 if (fAnalysisManager->IsActive()) {
0094 fAnalysisManager->OpenFile();
0095
0096
0097
0098 G4double length = fDetector->GetAbsorSizeX();
0099 G4double stepMax = fPhysics->GetStepMaxProcess()->GetMaxStep();
0100 G4int nbBins = 100;
0101 if (stepMax < DBL_MAX) {
0102 G4int nb = (G4int)(0.5 + length / stepMax);
0103 nbBins = std::min(std::max(nbBins, nb), 10000);
0104 }
0105 fAnalysisManager->SetH1(1, nbBins, 0., length, "mm");
0106 }
0107 }
0108
0109
0110
0111 void RunAction::EndOfRunAction(const G4Run* aRun)
0112 {
0113 G4int nbofEvents = aRun->GetNumberOfEvent();
0114 if (nbofEvents == 0) return;
0115
0116
0117
0118 const G4Material* material = fDetector->GetAbsorMaterial();
0119 G4double density = material->GetDensity();
0120
0121 G4String particle = fKinematic->GetParticleGun()->GetParticleDefinition()->GetParticleName();
0122 G4double energy = fKinematic->GetParticleGun()->GetParticleEnergy();
0123 G4cout << "\n The run consists of " << nbofEvents << " " << particle << " of "
0124 << G4BestUnit(energy, "Energy") << " through "
0125 << G4BestUnit(fDetector->GetAbsorSizeX(), "Length") << " of " << material->GetName()
0126 << " (density: " << G4BestUnit(density, "Volumic Mass") << ")" << G4endl;
0127
0128
0129
0130 if (fRange > 0) {
0131 fProjRange /= fRange;
0132 fProjRange2 /= fRange;
0133 }
0134 G4double rms = fProjRange2 - fProjRange * fProjRange;
0135 if (rms > 0.)
0136 rms = std::sqrt(rms);
0137 else
0138 rms = 0.;
0139
0140 G4double nstep = G4double(fNbPrimarySteps) / G4double(nbofEvents);
0141
0142 G4cout.precision(6);
0143 G4cout << "\n Projected Range= " << G4BestUnit(fProjRange, "Length")
0144 << " rms= " << G4BestUnit(rms, "Length") << G4endl;
0145 G4cout << " Mean number of primary steps = " << nstep << G4endl;
0146
0147
0148
0149 fEdeptot /= nbofEvents;
0150 G4cout << " Total energy deposit= " << G4BestUnit(fEdeptot, "Energy") << G4endl;
0151 fEniel /= nbofEvents;
0152 G4cout << " niel energy deposit = " << G4BestUnit(fEniel, "Energy") << G4endl;
0153
0154
0155
0156 G4int tallyNumber = fDetector->GetTallyNumber();
0157 if (tallyNumber > 0) {
0158 G4double Ebeam = fKinematic->GetEbeamCumul();
0159 G4cout << "\n---------------------------------------------------------\n";
0160 G4cout << " Cumulated Doses : \tEdep \tEdep/Ebeam \tDose" << G4endl;
0161 for (G4int j = 0; j < tallyNumber; ++j) {
0162 G4double Edep = fTallyEdep[j], ratio = 100 * Edep / Ebeam;
0163 G4double tallyMass = fDetector->GetTallyMass(j);
0164 G4double Dose = Edep / tallyMass;
0165 G4cout << " tally " << j << ": \t \t" << G4BestUnit(Edep, "Energy") << "\t" << ratio
0166 << " % \t" << G4BestUnit(Dose, "Dose") << G4endl;
0167 }
0168 G4cout << "\n---------------------------------------------------------\n";
0169 G4cout << G4endl;
0170 }
0171
0172 if (fAnalysisManager->IsActive()) {
0173
0174
0175 for (G4int j = 1; j < 3; ++j) {
0176 G4double binWidth = fAnalysisManager->GetH1Width(j);
0177 G4double fac = (mm / MeV) / (nbofEvents * binWidth);
0178 fAnalysisManager->ScaleH1(j, fac);
0179 }
0180 fAnalysisManager->ScaleH1(3, 1. / nbofEvents);
0181
0182
0183 fAnalysisManager->Write();
0184 fAnalysisManager->CloseFile();
0185 }
0186
0187
0188
0189 CLHEP::HepRandom::showEngineStatus();
0190 }
0191
0192
0193
0194 void RunAction::BookHisto()
0195 {
0196
0197 fAnalysisManager = G4AnalysisManager::Instance();
0198 fAnalysisManager->SetDefaultFileType("root");
0199 fAnalysisManager->SetFileName("testem7");
0200 fAnalysisManager->SetVerboseLevel(1);
0201 fAnalysisManager->SetActivation(true);
0202
0203
0204 const G4int kMaxHisto = 4;
0205 const G4String id[] = {"h0", "h1", "h2", "h3"};
0206 const G4String title[] = {
0207 "dummy",
0208 "Edep (MeV/mm) along absorber ",
0209 "Edep (MeV/mm) along absorber zoomed",
0210 "projectile range"
0211 };
0212
0213
0214 G4int nbins = 100;
0215 G4double vmin = 0.;
0216 G4double vmax = 100.;
0217
0218
0219
0220 for (G4int k = 0; k < kMaxHisto; ++k) {
0221 G4int ih = fAnalysisManager->CreateH1(id[k], title[k], nbins, vmin, vmax);
0222 G4bool activ = false;
0223 if (k == 1) activ = true;
0224 fAnalysisManager->SetH1Activation(ih, activ);
0225 }
0226 }
0227
0228