Warning, file /geant4/examples/advanced/dna/moleculardna/src/PrimaryGeneratorSourceGRASCSV.cc was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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
0055 ("TWO-STAGE PARTICLE PHASE SPACE OUTPUT FILE: PARTICLE PHASE-SPACE INFORMATION")
0056 != std::string::npos)
0057 {
0058 for (G4int i = 0; i < 18; i++) {
0059 std::getline(fInputFile, line);
0060
0061 G4cout << line << G4endl;
0062 }
0063 break;
0064 }
0065 }
0066
0067 fnParticles = totalevents;
0068 }
0069 else {
0070 G4cout << "*** Warning: can't open particle source file in reading mode ***" << G4endl;
0071 }
0072 }
0073
0074
0075
0076 PrimaryGeneratorSourceGRASCSV::~PrimaryGeneratorSourceGRASCSV()
0077 {
0078
0079 if (fInputFile.is_open()) {
0080 fInputFile.close();
0081 }
0082 fPrimaryList.clear();
0083 }
0084
0085
0086
0087 G4double PrimaryGeneratorSourceGRASCSV::RecomputeNParticles()
0088 {
0089
0090
0091 return fnParticles;
0092 }
0093
0094
0095
0096 Primary* PrimaryGeneratorSourceGRASCSV::GetPrimary()
0097 {
0098 if (fInputFile.peek() == EOF || fEndOfFile == true) return nullptr;
0099
0100 if (fPrimaryList.size() == 0) {
0101
0102 G4int num = fBufferSize;
0103 if (num > fnParticles) {
0104 num = fnParticles;
0105 }
0106
0107 for (G4int i = 0; i < num; i++) {
0108 if (fInputFile.peek() == EOF) {
0109
0110 fEndOfFile = true;
0111 break;
0112 }
0113 G4String line = "";
0114 std::getline(fInputFile, line);
0115 if (line.find
0116 ("TWO-STAGE PARTICLE PHASE SPACE OUTPUT FILE: PARTICLE PHASE-SPACE INFORMATION")
0117 != std::string::npos)
0118 {
0119 const G4int lines = 18;
0120 for (G4int j = 0; j < lines; j++) {
0121 std::getline(fInputFile, line);
0122 }
0123 i--;
0124 continue;
0125 }
0126 if (line.find("Block") != std::string::npos || line.find("File") != std::string::npos
0127 || line.find("End") != std::string::npos || line.find("*") != std::string::npos)
0128 {
0129
0130 i--;
0131 continue;
0132 }
0133 std::stringstream ss(line);
0134 std::vector<G4String> tempVect;
0135 G4String tempString = "";
0136 while (ss >> tempString)
0137 tempVect.push_back(tempString);
0138
0139 try {
0140
0141 G4ThreeVector pos = G4ThreeVector(std::stod(tempVect.at(10)),
0142 std::stod(tempVect.at(11)),
0143 std::stod(tempVect.at(12)));
0144 G4ThreeVector momDir = G4ThreeVector(std::stod(tempVect.at(16)),
0145 std::stod(tempVect.at(17)),
0146 std::stod(tempVect.at(18)));
0147 G4int PDGEncoding = std::stoi(tempVect.at(2));
0148 G4double KE = 0;
0149 KE = std::stod(tempVect.at(8));
0150
0151
0152 auto* primary = new Primary();
0153 primary->SetName(PDGEncoding);
0154 primary->SetPosition(pos);
0155 primary->SetMomentumDirection(momDir);
0156 primary->SetEnergy(KE);
0157
0158
0159 fPrimaryList.push_back(primary);
0160 }
0161 catch (const std::invalid_argument&) {
0162 G4cerr << "Phase Space reading error: invalid_argument" << G4endl;
0163 }
0164 catch (const std::out_of_range&) {
0165 G4cerr << "Phase Space reading error: out_of_range" << G4endl;
0166 }
0167 }
0168 }
0169
0170
0171 Primary* primary = nullptr;
0172 if (fPrimaryList.size() > 0) {
0173 primary = fPrimaryList.front();
0174 fPrimaryList.pop_front();
0175 }
0176 return primary;
0177 }