File indexing completed on 2025-02-23 09:22:01
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 "PrimaryGeneratorSourceGRASCSV.hh"
0032
0033
0034
0035 PrimaryGeneratorSourceGRASCSV::PrimaryGeneratorSourceGRASCSV(const G4String& filename)
0036 {
0037
0038 fEndOfFile = false;
0039
0040
0041 fInputFile.open(filename);
0042 if (fInputFile.is_open()) {
0043 G4cout << "*** Opening particle source file " << filename << " ***" << G4endl;
0044
0045 G4String line = "";
0046 G4int totalevents = 0;
0047 while (std::getline(fInputFile, line)) {
0048 if (line.find("TOTAL_EVENTS") != std::string::npos) {
0049
0050 std::stringstream ss(line);
0051 G4String tmp = "";
0052 ss >> tmp >> tmp >> totalevents;
0053 }
0054 if (line.find("TWO-STAGE PARTICLE PHASE SPACE OUTPUT FILE: PARTICLE PHASE-SPACE INFORMATION")
0055 != std::string::npos)
0056 {
0057 for (G4int i = 0; i < 18; i++) {
0058 std::getline(fInputFile, line);
0059
0060 G4cout << line << G4endl;
0061 }
0062 break;
0063 }
0064 }
0065
0066 fnParticles = totalevents;
0067 }
0068 else {
0069 G4cout << "*** Warning: can't open particle source file in reading mode ***" << G4endl;
0070 }
0071 }
0072
0073
0074
0075 PrimaryGeneratorSourceGRASCSV::~PrimaryGeneratorSourceGRASCSV()
0076 {
0077
0078 if (fInputFile.is_open()) {
0079 fInputFile.close();
0080 }
0081 fPrimaryList.clear();
0082 }
0083
0084
0085
0086 G4double PrimaryGeneratorSourceGRASCSV::RecomputeNParticles()
0087 {
0088
0089
0090 return fnParticles;
0091 }
0092
0093
0094
0095 Primary* PrimaryGeneratorSourceGRASCSV::GetPrimary()
0096 {
0097 if (fInputFile.peek() == EOF || fEndOfFile == true) return nullptr;
0098
0099 if (fPrimaryList.size() == 0) {
0100
0101 G4int num = fBufferSize;
0102 if (num > fnParticles) {
0103 num = fnParticles;
0104 }
0105
0106 for (G4int i = 0; i < num; i++) {
0107 if (fInputFile.peek() == EOF) {
0108
0109 fEndOfFile = true;
0110 break;
0111 }
0112 G4String line = "";
0113 std::getline(fInputFile, line);
0114 if (line.find("TWO-STAGE PARTICLE PHASE SPACE OUTPUT FILE: PARTICLE PHASE-SPACE INFORMATION")
0115 != std::string::npos)
0116 {
0117 const G4int lines = 18;
0118 for (G4int j = 0; j < lines; j++) {
0119 std::getline(fInputFile, line);
0120 }
0121 i--;
0122 continue;
0123 }
0124 if (line.find("Block") != std::string::npos || line.find("File") != std::string::npos
0125 || line.find("End") != std::string::npos || line.find("*") != std::string::npos)
0126 {
0127
0128 i--;
0129 continue;
0130 }
0131 std::stringstream ss(line);
0132 std::vector<G4String> tempVect;
0133 G4String tempString = "";
0134 while (ss >> tempString)
0135 tempVect.push_back(tempString);
0136
0137 try {
0138
0139 G4ThreeVector pos = G4ThreeVector(std::stod(tempVect.at(10)), std::stod(tempVect.at(11)),
0140 std::stod(tempVect.at(12)));
0141 G4ThreeVector momDir = G4ThreeVector(std::stod(tempVect.at(16)), std::stod(tempVect.at(17)),
0142 std::stod(tempVect.at(18)));
0143 G4int PDGEncoding = std::stoi(tempVect.at(2));
0144 G4double KE = 0;
0145 KE = std::stod(tempVect.at(8));
0146
0147
0148 auto* primary = new Primary();
0149 primary->SetName(PDGEncoding);
0150 primary->SetPosition(pos);
0151 primary->SetMomentumDirection(momDir);
0152 primary->SetEnergy(KE);
0153
0154
0155 fPrimaryList.push_back(primary);
0156 }
0157 catch (const std::invalid_argument&) {
0158 G4cerr << "Phase Space reading error: invalid_argument" << G4endl;
0159 }
0160 catch (const std::out_of_range&) {
0161 G4cerr << "Phase Space reading error: out_of_range" << G4endl;
0162 }
0163 }
0164 }
0165
0166
0167 Primary* primary = nullptr;
0168 if (fPrimaryList.size() > 0) {
0169 primary = fPrimaryList.front();
0170 fPrimaryList.pop_front();
0171 }
0172 return primary;
0173 }