Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:29:53

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 //
0028 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0029 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0030 
0031 #include "Run.hh"
0032 
0033 #include "PrimaryGeneratorAction.hh"
0034 
0035 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0049 
0050 Run::~Run() = default;
0051 
0052 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0074 
0075 void Run::EndOfRun()
0076 {
0077   // Write X-ray data or proton data into files
0078   //    G4int runID = GetRunID();
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0101 
0102 G4bool Run::GetIsPIXE()
0103 {
0104   return isPIXE;
0105 }
0106 
0107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
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);  // appendix
0143   }
0144   else {
0145     // if runId ==0, delete the existing file and create a new file
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 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......