File indexing completed on 2026-09-20 08:30:42
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
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 #include "Run.hh"
0041
0042 #include "PrimaryGeneratorAction.hh"
0043
0044 #include "G4Material.hh"
0045 #include "G4SystemOfUnits.hh"
0046 #include "G4UnitsTable.hh"
0047
0048
0049
0050 Run::Run(const DetectorConstruction* detector)
0051 : G4Run(),
0052 fDetector(detector),
0053 fParticle(0),
0054 fEkin(0.),
0055 fEdeposit(0.),
0056 fEdeposit2(0.),
0057 fTrackLen(0.),
0058 fTrackLen2(0.),
0059 fProjRange(0.),
0060 fProjRange2(0.),
0061 fPenetration(0.),
0062 fPenetration2(0.),
0063 fNbOfSteps(0),
0064 fNbOfSteps2(0),
0065 fStepSize(0.),
0066 fStepSize2(0.)
0067 {}
0068
0069
0070
0071 Run::~Run() {}
0072
0073
0074
0075 void Run::SetPrimary(G4ParticleDefinition* particle, G4double energy)
0076 {
0077 fParticle = particle;
0078 fEkin = energy;
0079 }
0080
0081
0082
0083 void Run::AddEdep(G4double e)
0084 {
0085 fEdeposit += e;
0086 fEdeposit2 += e * e;
0087 }
0088
0089
0090
0091 void Run::AddTrackLength(G4double t)
0092 {
0093 fTrackLen += t;
0094 fTrackLen2 += t * t;
0095 }
0096
0097
0098
0099 void Run::AddProjRange(G4double x)
0100 {
0101 fProjRange += x;
0102 fProjRange2 += x * x;
0103 }
0104
0105
0106
0107 void Run::AddPenetration(G4double x)
0108 {
0109 fPenetration += x;
0110 fPenetration2 += x * x;
0111 }
0112
0113
0114
0115 void Run::AddStepSize(G4int nb, G4double st)
0116 {
0117 fNbOfSteps += nb;
0118 fNbOfSteps2 += nb * nb;
0119 fStepSize += st;
0120 fStepSize2 += st * st;
0121 }
0122
0123
0124
0125 void Run::Merge(const G4Run* run)
0126 {
0127 const Run* localRun = static_cast<const Run*>(run);
0128
0129
0130 fParticle = localRun->fParticle;
0131 fEkin = localRun->fEkin;
0132
0133
0134 fEdeposit += localRun->fEdeposit;
0135 fEdeposit2 += localRun->fEdeposit2;
0136 fTrackLen += localRun->fTrackLen;
0137 fTrackLen2 += localRun->fTrackLen2;
0138 fProjRange += localRun->fProjRange;
0139 fProjRange2 += localRun->fProjRange2;
0140 fPenetration += localRun->fPenetration;
0141 fPenetration2 += localRun->fPenetration2;
0142 fNbOfSteps += localRun->fNbOfSteps;
0143 fNbOfSteps2 += localRun->fNbOfSteps2;
0144 fStepSize += localRun->fStepSize;
0145 fStepSize2 += localRun->fStepSize2;
0146
0147 G4Run::Merge(run);
0148 }
0149
0150
0151
0152 void Run::EndOfRun()
0153 {
0154 std::ios::fmtflags mode = G4cout.flags();
0155 G4cout.setf(std::ios::fixed, std::ios::floatfield);
0156 G4int prec = G4cout.precision(2);
0157
0158
0159 G4Material* material = fDetector->GetAbsorMaterial();
0160 G4double density = material->GetDensity();
0161 G4String partName = fParticle->GetParticleName();
0162
0163 G4cout << "\n ======================= run summary ====================\n";
0164 G4cout << "\n The run is " << numberOfEvent << " " << partName << " of "
0165 << G4BestUnit(fEkin, "Energy") << " through a sphere of radius "
0166 << G4BestUnit(fDetector->GetAbsorRadius(), "Length") << "of " << material->GetName()
0167 << " (density: " << G4BestUnit(density, "Volumic Mass") << ")" << G4endl;
0168
0169 if (numberOfEvent == 0) {
0170 G4cout.setf(mode, std::ios::floatfield);
0171 G4cout.precision(prec);
0172 return;
0173 }
0174
0175
0176 fTrackLen /= numberOfEvent;
0177 fTrackLen2 /= numberOfEvent;
0178 G4double rmsTrack = fTrackLen2 - fTrackLen * fTrackLen;
0179
0180 if (rmsTrack > 0.)
0181 rmsTrack = std::sqrt(rmsTrack);
0182 else
0183 rmsTrack = 0.;
0184
0185 G4cout.precision(3);
0186 G4cout << "\n Track length of primary track = " << G4BestUnit(fTrackLen, "Length") << " +- "
0187 << G4BestUnit(rmsTrack, "Length");
0188
0189
0190 fProjRange /= numberOfEvent;
0191 fProjRange2 /= numberOfEvent;
0192 G4double rmsProj = fProjRange2 - fProjRange * fProjRange;
0193 if (rmsProj > 0.)
0194 rmsProj = std::sqrt(rmsProj);
0195 else
0196 rmsProj = 0.;
0197
0198 G4cout << "\n Projected range = " << G4BestUnit(fProjRange, "Length") << " +- "
0199 << G4BestUnit(rmsProj, "Length");
0200
0201
0202 fPenetration /= numberOfEvent;
0203 fPenetration2 /= numberOfEvent;
0204 G4double rmsPene = fPenetration2 - fPenetration * fPenetration;
0205 if (rmsPene > 0.)
0206 rmsPene = std::sqrt(rmsPene);
0207 else
0208 rmsPene = 0.;
0209
0210 G4cout << "\n Penetration = " << G4BestUnit(fPenetration, "Length") << " +- "
0211 << G4BestUnit(rmsPene, "Length") << G4endl;
0212
0213
0214
0215
0216 FILE* myFile;
0217 myFile = fopen("range.txt", "a");
0218 fprintf(myFile, "%e %e %e %e %e %e %e\n", fEkin / eV, fTrackLen / nm, rmsTrack / nm,
0219 fProjRange / nm, rmsProj / nm, fPenetration / nm, rmsPene / nm);
0220 fclose(myFile);
0221
0222
0223 G4cout.setf(mode, std::ios::floatfield);
0224 G4cout.precision(prec);
0225 }