File indexing completed on 2025-12-16 09:29:53
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 #include "Run.hh"
0032
0033 #include "PrimaryGeneratorAction.hh"
0034
0035
0036 Run::Run(G4bool isP, G4int nbProjs, G4int nbS, G4int nbP, G4int resumeProjIndex)
0037 : G4Run(),
0038 isPIXE(isP),
0039 fProjectionIndex(0),
0040 fSliceIndex(0),
0041 fPixelIndex(0),
0042 fNbProjections(nbProjs),
0043 fNbSlices(nbS),
0044 fNbPixels(nbP),
0045 fResumeProjIndex(resumeProjIndex)
0046 {}
0047
0048
0049
0050 Run::~Run() = default;
0051
0052
0053
0054 void Run::Merge(const G4Run* run)
0055 {
0056 G4cout << "--------------Merge is called---------------" << G4endl;
0057 const Run* localRun = static_cast<const Run*>(run);
0058
0059 for (size_t i = 0; i != localRun->gammaAtExit.size(); i++) {
0060 gammaAtExit.push_back(localRun->gammaAtExit[i]);
0061 }
0062 for (size_t i = 0; i != localRun->gammaAtCreation.size(); i++) {
0063 gammaAtCreation.push_back(localRun->gammaAtCreation[i]);
0064 }
0065
0066 for (size_t i = 0; i != localRun->protonAtExit.size(); i++) {
0067 protonAtExit.push_back(localRun->protonAtExit[i]);
0068 }
0069
0070 G4Run::Merge(run);
0071 }
0072
0073
0074
0075 void Run::EndOfRun()
0076 {
0077
0078
0079 fProjectionIndex = GetCurrentProjection();
0080 fSliceIndex = GetCurrentSlice();
0081 fPixelIndex = GetCurrentPixel();
0082
0083 RunInfo runInfo((uint8_t)fProjectionIndex, (uint16_t)fSliceIndex, (uint16_t)fPixelIndex);
0084
0085 if (isPIXE) {
0086 runInfo.nbParticle = (uint32_t)gammaAtCreation.size();
0087 WriteFile("GammaAtCreation.dat", runInfo, gammaAtCreation);
0088
0089 runInfo.nbParticle = (uint32_t)gammaAtExit.size();
0090 WriteFile("GammaAtExit.dat", runInfo, gammaAtExit);
0091 }
0092 else {
0093 runInfo.nbParticle = (uint32_t)protonAtExit.size();
0094 WriteFile("ProtonAtExit.dat", runInfo, protonAtExit);
0095 }
0096
0097 ClearVecs();
0098 }
0099
0100
0101
0102 G4bool Run::GetIsPIXE()
0103 {
0104 return isPIXE;
0105 }
0106
0107
0108
0109 void Run::FillGammaAtExit(ParticleInfo gammaInfo)
0110 {
0111 gammaAtExit.push_back(gammaInfo);
0112 }
0113
0114 void Run::FillGammaAtCreation(ParticleInfo gammaInfo)
0115 {
0116 gammaAtCreation.push_back(gammaInfo);
0117 }
0118
0119 void Run::FillProtonAtExit(ParticleInfo protonInfo)
0120 {
0121 protonAtExit.push_back(protonInfo);
0122 }
0123
0124 void Run::ClearVecs()
0125 {
0126 if (gammaAtExit.size()) {
0127 gammaAtExit.clear();
0128 }
0129 if (gammaAtCreation.size()) {
0130 gammaAtCreation.clear();
0131 }
0132 if (protonAtExit.size()) {
0133 protonAtExit.clear();
0134 }
0135 }
0136
0137 void Run::WriteFile(const std::string fName, RunInfo& runInfo, std::vector<ParticleInfo>& vec)
0138 {
0139 std::ofstream ofs;
0140 if (runID) {
0141 ofs.open(fName.c_str(),
0142 std::ios::out | std::ios::app | std::ios::binary);
0143 }
0144 else {
0145
0146 ofs.open(fName.c_str(), std::ios::out | std::ios::trunc | std::ios::binary);
0147 }
0148 ofs.write((const char*)&runInfo, sizeof(RunInfo));
0149 if (vec.size()) {
0150 ofs.write((const char*)vec.data(), sizeof(ParticleInfo) * vec.size());
0151 }
0152
0153 ofs.close();
0154 }
0155 G4int Run::GetCurrentProjection()
0156 {
0157 if (fResumeProjIndex > fNbProjections - 1) {
0158 G4Exception("fResumeProjIndex", "Run::GetCurrentProjection()", FatalException,
0159 "To resume a simulation, the start of index of projection must be lower than the "
0160 "maximal index");
0161 }
0162 G4int projIndex = fResumeProjIndex + runID / (fNbSlices * fNbPixels);
0163 return projIndex;
0164 }
0165 G4int Run::GetCurrentSlice()
0166 {
0167 G4int remain = runID % (fNbSlices * fNbPixels);
0168 return remain / fNbPixels;
0169 }
0170
0171 G4int Run::GetCurrentPixel()
0172 {
0173 G4int remain = runID % (fNbSlices * fNbPixels);
0174 return remain % fNbPixels;
0175 }
0176
0177