File indexing completed on 2026-04-17 07:51:38
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 #include "G4IAEAphspWriterStack.hh"
0037
0038 #include "globals.hh"
0039 #include "G4SystemOfUnits.hh"
0040 #include "G4ThreeVector.hh"
0041 #include "G4Step.hh"
0042 #include "G4IAEAphspWriter.hh"
0043
0044 #include <set>
0045 #include <vector>
0046
0047
0048
0049
0050 G4IAEAphspWriterStack::G4IAEAphspWriterStack(const G4String filename)
0051 {
0052 fFileName = filename;
0053 fZphspVec = new std::vector<G4double>;
0054 fIncrNumberVec = new std::vector<G4int>;
0055 fPassingTracksVec = new std::vector< std::set<G4int>* >;
0056 fPDGMtrx = new std::vector< std::vector<G4int>* >;
0057 fPosMtrx = new std::vector< std::vector<G4ThreeVector>* >;
0058 fMomMtrx = new std::vector< std::vector<G4ThreeVector>* >;
0059 fEneMtrx = new std::vector< std::vector<G4double>* >;
0060 fWtMtrx = new std::vector< std::vector<G4double>* >;
0061 fNstatMtrx = new std::vector< std::vector<G4int>* >;
0062
0063 G4cout << "G4IAEAphspWriterStack object constructed for files \""
0064 << fFileName << "_<zphsp>cm.IAEA*\"" << G4endl;
0065 }
0066
0067
0068
0069
0070 G4IAEAphspWriterStack::~G4IAEAphspWriterStack()
0071 {
0072 if (fZphspVec) delete fZphspVec;
0073 if (fIncrNumberVec) delete fIncrNumberVec;
0074 if (fPassingTracksVec) delete fPassingTracksVec;
0075 if (fPDGMtrx) delete fPDGMtrx;
0076 if (fPosMtrx) delete fPosMtrx;
0077 if (fMomMtrx) delete fMomMtrx;
0078 if (fEneMtrx) delete fEneMtrx;
0079 if (fWtMtrx) delete fWtMtrx;
0080 if (fNstatMtrx) delete fNstatMtrx;
0081 }
0082
0083
0084
0085
0086 void G4IAEAphspWriterStack::AddZphsp(const G4double zphsp)
0087 {
0088 fZphspVec->push_back(zphsp);
0089 G4cout << "G4IAEAphspWriterStack: Registered phase-space plane at z = "
0090 << zphsp/cm << " cm." << G4endl;
0091 }
0092
0093
0094
0095
0096 void G4IAEAphspWriterStack::ClearZphspVec()
0097 {
0098 G4cout << "G4IAEAphspWriterStack: Removing all registered phase-space planes!"
0099 << G4endl;
0100 fZphspVec->clear();
0101 }
0102
0103
0104
0105
0106 void G4IAEAphspWriterStack::SetDataFromWriter(const G4IAEAphspWriter* writer)
0107 {
0108 if (!writer) {
0109 G4ExceptionDescription msg;
0110 msg << "No G4IAEAphspWriter has been constructed!" << G4endl;
0111 G4Exception("G4IAEAphspWriterStack::SetDataFromWriter()",
0112 "IAEAphspWriterStack001", FatalException, msg );
0113 return;
0114 }
0115 else {
0116 fFileName = writer->GetFileName();
0117
0118 if (writer->GetZphspVec()->size() > 0) {
0119 (*fZphspVec) = *(writer->GetZphspVec());
0120
0121 G4cout << "G4IAEAphspWriterStack::fFileName = " << fFileName << G4endl;
0122 G4cout << "G4IAEAphspWriterStack::fZphspVec->size() = "
0123 << fZphspVec->size() << G4endl;
0124 }
0125 else {
0126 G4ExceptionDescription msg;
0127 msg << "No phsp plane z-coordinate has been defined!" << G4endl;
0128 G4Exception("G4IAEAphspWriterStack::SetDataFromWriter()",
0129 "IAEAphspWriterStack002", FatalErrorInArgument, msg );
0130 return;
0131 }
0132 }
0133 }
0134
0135
0136
0137
0138 void G4IAEAphspWriterStack::PrepareRun()
0139 {
0140 size_t nZphsps = fZphspVec->size();
0141
0142 fIncrNumberVec->reserve(nZphsps);
0143 fPassingTracksVec->reserve(nZphsps);
0144 fPDGMtrx->reserve(nZphsps);
0145 fPosMtrx->reserve(nZphsps);
0146 fMomMtrx->reserve(nZphsps);
0147 fEneMtrx->reserve(nZphsps);
0148 fWtMtrx->reserve(nZphsps);
0149 fNstatMtrx->reserve(nZphsps);
0150
0151 for (size_t ii = 0; ii < nZphsps; ii++) {
0152 fIncrNumberVec->push_back(0);
0153
0154 auto aSet = new std::set<G4int>;
0155 fPassingTracksVec->push_back(aSet);
0156 auto pdgVec = new std::vector<G4int>;
0157 fPDGMtrx->push_back(pdgVec);
0158 auto posVec = new std::vector<G4ThreeVector>;
0159 fPosMtrx->push_back(posVec);
0160 auto momVec = new std::vector<G4ThreeVector>;
0161 fMomMtrx->push_back(momVec);
0162 auto eneVec = new std::vector<G4double>;
0163 fEneMtrx->push_back(eneVec);
0164 auto wtVec = new std::vector<G4double>;
0165 fWtMtrx->push_back(wtVec);
0166 auto nstatVec = new std::vector<G4int>;
0167 fNstatMtrx->push_back(nstatVec);
0168 }
0169 G4cout << "G4IAEAphspWriterStack::PrepareRun() done!" << G4endl;
0170 }
0171
0172
0173
0174
0175 void G4IAEAphspWriterStack::PrepareNextEvent()
0176 {
0177
0178 for ( auto& ii : (*fIncrNumberVec) )
0179 ii++;
0180
0181
0182 for ( auto& trackIDs : (*fPassingTracksVec) )
0183 trackIDs->clear();
0184
0185
0186
0187 }
0188
0189
0190
0191
0192
0193 void G4IAEAphspWriterStack::StoreParticleIfEligible(const G4Step* aStep)
0194 {
0195 const G4ThreeVector postR = aStep->GetPostStepPoint()->GetPosition();
0196 const G4ThreeVector preR = aStep->GetPreStepPoint()->GetPosition();
0197 const G4double postZ = postR.z();
0198 const G4double preZ = preR.z();
0199
0200
0201 size_t phspIdx = 0;
0202 for (const auto& phspZ : (*fZphspVec) ) {
0203 if ( (postZ-phspZ)*(preZ-phspZ) < 0 ) {
0204
0205 const G4int trackID = aStep->GetTrack()->GetTrackID();
0206 std::set<G4int>::iterator is;
0207 is = (*fPassingTracksVec)[phspIdx]->find(trackID);
0208
0209 if ( is == (*fPassingTracksVec)[phspIdx]->end() ) {
0210
0211
0212
0213 const G4int pdgCode =
0214 aStep->GetTrack()->GetDefinition()->GetPDGEncoding();
0215 if (pdgCode == 22 || pdgCode == 11 || pdgCode == -11 ||
0216 pdgCode == 2112 || pdgCode == 2212)
0217 StoreIAEAParticle(aStep, phspIdx, pdgCode);
0218 }
0219 }
0220 phspIdx++;
0221 }
0222 }
0223
0224
0225
0226
0227 void G4IAEAphspWriterStack::StoreIAEAParticle(const G4Step* aStep,
0228 const G4int phspIndex,
0229 const G4int pdgCode)
0230 {
0231 const G4double zStop = (*fZphspVec)[phspIndex];
0232 const G4Track* aTrack = aStep->GetTrack();
0233
0234
0235
0236 const G4ThreeVector postR = aStep->GetPostStepPoint()->GetPosition();
0237 const G4ThreeVector preR = aStep->GetPreStepPoint()->GetPosition();
0238 const G4double postZ = postR.z();
0239 const G4double preZ = preR.z();
0240
0241
0242 G4double kinEnergy;
0243 if (pdgCode == 22 || pdgCode == 2112) {
0244 kinEnergy = aStep->GetPreStepPoint()->GetKineticEnergy();
0245 }
0246 else if (pdgCode == 11 || pdgCode == -11 ||
0247 pdgCode == 2212 ) {
0248 const G4double postE = aStep->GetPostStepPoint()->GetKineticEnergy();
0249 const G4double preE = aStep->GetPreStepPoint()->GetKineticEnergy();
0250 kinEnergy = preE + (postE-preE)*(zStop-preZ)/(postZ-preZ);
0251 }
0252 else {
0253 G4ExceptionDescription ED;
0254 ED << "\"" << aTrack->GetDefinition()->GetParticleName()
0255 << "\" is not supported by the IAEAphsp format; not recorded.";
0256 G4Exception("G4IAEAphspWriterStack::StoreIAEAParticle()",
0257 "IAEAphspWriterStack003", JustWarning, ED );
0258 return;
0259 }
0260
0261
0262 const G4ThreeVector phspPos = preR + (postR-preR)*(zStop-preZ)/(postZ-preZ);
0263
0264
0265 const G4ThreeVector phspMomDir =
0266 aStep->GetPreStepPoint()->GetMomentumDirection();
0267
0268
0269 const G4double wt = aTrack->GetWeight();
0270
0271
0272 const G4int nStat = (*fIncrNumberVec)[phspIndex];
0273
0274
0275
0276 ((*fPDGMtrx)[phspIndex])->push_back(pdgCode);
0277 ((*fNstatMtrx)[phspIndex])->push_back(nStat);
0278 ((*fPosMtrx)[phspIndex])->push_back(phspPos);
0279 ((*fMomMtrx)[phspIndex])->push_back(phspMomDir);
0280 ((*fEneMtrx)[phspIndex])->push_back(kinEnergy);
0281 ((*fWtMtrx)[phspIndex])->push_back(wt);
0282
0283
0284 (*fIncrNumberVec)[phspIndex] = 0;
0285
0286
0287 (*fPassingTracksVec)[phspIndex]->insert( aTrack->GetTrackID() );
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298 }
0299
0300
0301
0302
0303 void G4IAEAphspWriterStack::ClearRunVectors()
0304 {
0305
0306
0307 fIncrNumberVec->clear();
0308
0309 for (auto& trackIDs : (*fPassingTracksVec) ) delete trackIDs;
0310 fPassingTracksVec->clear();
0311
0312 for (auto& pdgVec : (*fPDGMtrx) ) delete pdgVec;
0313 fPDGMtrx->clear();
0314
0315 for (auto& posVec : (*fPosMtrx) ) delete posVec;
0316 fPosMtrx->clear();
0317
0318 for (auto& momVec : (*fMomMtrx) ) delete momVec;
0319 fMomMtrx->clear();
0320
0321 for (auto& eneVec : (*fEneMtrx) ) delete eneVec;
0322 fEneMtrx->clear();
0323
0324 for (auto& wtVec : (*fWtMtrx) ) delete wtVec;
0325 fWtMtrx->clear();
0326
0327 for (auto& nStatVec : (*fNstatMtrx) ) delete nStatVec;
0328 fNstatMtrx->clear();
0329
0330 G4cout << "G4IAEAphspWriterStack run vectors cleaned!" << G4endl;
0331 }