Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 07:51:37

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 // Author: M.A. Cortes-Giraldo, Universidad de Sevilla
0027 //
0028 // History changelog prior creation of this example:
0029 // - 13/04/2009: Messenger class added.
0030 // - 17/10/2009: version 1.0
0031 // - 20/11/2009: version 1.1 before publishing:
0032 //   - Changed some names by more suitable ones
0033 // - 02/08/2010: version 1.2-dev:
0034 //   - Added possbility of applying axial symmetries
0035 // - 14/09/2023: version 2.0
0036 //   - Following Geant4 coding guidelines
0037 // - 18/10/2025: version 3.0
0038 //   - Creation of IAEASourceIdRegistry for thread-safe source_id assignation
0039 //
0040 
0041 #ifndef G4IAEAphspReader_h
0042 #define G4IAEAphspReader_h 1
0043 
0044 #include "G4VPrimaryGenerator.hh"
0045 
0046 #include <vector>
0047 
0048 #include "globals.hh"
0049 #include "G4ThreeVector.hh"
0050 
0051 
0052 class G4Event;
0053 class G4IAEAphspReaderMessenger;
0054 
0055 
0056 class G4IAEAphspReader :public G4VPrimaryGenerator
0057 {
0058 
0059 public:
0060 
0061   G4IAEAphspReader(const char* filename, const G4int threads = 1);
0062   G4IAEAphspReader(const G4String filename, const G4int threads = 1);
0063   // 'filename' must include the path if needed, but NOT the extension
0064   ~G4IAEAphspReader() override;
0065   
0066   void GeneratePrimaryVertex(G4Event* evt) override;   // Mandatory
0067 
0068   inline void SetVerbose(const G4int verb)
0069   {
0070     fVerbose = verb;
0071     if (fVerbose > 0)
0072       G4cout << "G4IAEAphspReader::fVerbose = " << fVerbose << G4endl;
0073   }
0074 
0075   inline void SetTotalParallelRuns(const G4int nParallelRuns)
0076   {
0077     fTotalParallelRuns = nParallelRuns;
0078     if (fVerbose > 0)
0079       G4cout << "G4IAEAphspReader::fTotalParallelRuns = " << fTotalParallelRuns
0080          << G4endl;
0081   }
0082 
0083   void SetParallelRun(const G4int parallelRun);
0084   inline void SetTotalThreads(const G4int threads) {fTotalThreads = threads;}
0085   inline void SetTimesRecycled(const G4int ntimes) {fTimesRecycled = ntimes;}
0086 
0087   inline void SetGlobalPhspTranslation(const G4ThreeVector & pos)
0088   {fGlobalPhspTranslation = pos;}
0089   inline void SetRotationOrder(const G4int ord)  { fRotationOrder = ord; }
0090   inline void SetRotationX(const G4double alpha) { fAlpha = alpha; }
0091   inline void SetRotationY(const G4double beta)  { fBeta = beta; }
0092   inline void SetRotationZ(const G4double gamma) { fGamma = gamma; }
0093   inline void SetIsocenterPosition(const G4ThreeVector & pos)
0094   {fIsocenterPosition = pos;}
0095   void SetCollimatorRotationAxis(const G4ThreeVector & axis);
0096   void SetGantryRotationAxis(const G4ThreeVector & axis);
0097   inline void SetCollimatorAngle(const G4double ang) {fCollimatorAngle = ang;}
0098   inline void SetGantryAngle(const G4double ang) {fGantryAngle = ang;}
0099 
0100   inline void SetAxialSymmetryX(const G4bool value) 
0101   {
0102     fAxialSymmetryX = value;
0103     if (value) {
0104       fAxialSymmetryY = false;
0105       fAxialSymmetryZ = false;
0106     }
0107   }
0108   inline void SetAxialSymmetryY(const G4bool value)
0109   {
0110     fAxialSymmetryY = value;
0111     if (value) {
0112       fAxialSymmetryZ = false;
0113       fAxialSymmetryX = false;
0114     }
0115   }
0116   inline void SetAxialSymmetryZ(const G4bool value)
0117   {
0118     fAxialSymmetryZ = value;
0119     if (value) {
0120       fAxialSymmetryX = false;
0121       fAxialSymmetryY = false;
0122     }
0123   }
0124 
0125   inline G4String GetFileName() const         {return fFileName;}
0126   inline G4int GetSourceReadId() const        {return fSourceReadId;}
0127   inline G4long GetOrigHistories() const      {return fOrigHistories;}
0128   inline G4long GetUsedOrigHistories() const  {return fUsedOrigHistories;}
0129   inline G4long GetTotalParticles() const     {return fTotalParticles;}
0130   inline G4int GetNumberOfExtraFloats() const {return fNumberOfExtraFloats;}
0131   inline G4int GetNumberOfExtraInts() const   {return fNumberOfExtraInts;}
0132   inline std::vector<G4int>* GetExtraFloatTypes() const
0133   {return fExtraFloatTypes;}
0134   inline std::vector<G4int>* GetExtraIntTypes() const
0135   {return fExtraIntTypes;}
0136   G4long GetTotalParticlesOfType(const G4String type) const;
0137   G4double GetConstantVariable(const G4int index) const;
0138 
0139   inline std::vector<G4int>* GetParticleTypeVec() const
0140   {return fParticleTypeVec;}
0141   inline std::vector<G4double>* GetKinEVec() const
0142   {return fKinEVec;}
0143   inline std::vector<G4ThreeVector>* GetPosVec() const
0144   {return fPosVec;}
0145   inline std::vector<G4ThreeVector>* GetMomDirVec() const
0146   {return fMomDirVec;}
0147   inline std::vector<G4double>* GetWeightVec() const
0148   {return fWeightVec;}
0149   inline std::vector< std::vector<G4double> >* GetExtraFloatVec() const
0150   {return fExtraFloatVec;}
0151   inline std::vector< std::vector<G4long> >* GetExtraIntVec() const
0152   {return fExtraIntVec;}
0153 
0154   inline G4int GetTotalParallelRuns() const {return fTotalParallelRuns;}
0155   inline G4int GetParallelRun() const       {return fParallelRun;}
0156   inline G4int GetTotalThreads() const      {return fTotalThreads;}
0157   inline G4long GetFirstParticle() const    {return fFirstParticle;}
0158   inline G4long GetLastParticle() const     {return fLastParticle;}
0159   inline G4int GetTimesRecycled() const     {return fTimesRecycled;}
0160 
0161   inline G4ThreeVector GetGlobalPhspTranslation() const
0162   {return fGlobalPhspTranslation;}
0163   inline G4int GetRotationOrder() const {return fRotationOrder;}
0164   inline G4double GetRotationX() const {return fAlpha;}
0165   inline G4double GetRotationY() const {return fBeta;}
0166   inline G4double GetRotationZ() const {return fGamma;}
0167   inline G4ThreeVector GetIsocenterPosition() const
0168   {return fIsocenterPosition;}
0169   inline G4double GetCollimatorAngle() const {return fCollimatorAngle;}
0170   inline G4double GetGantryAngle() const {return fGantryAngle;}
0171   inline G4ThreeVector GetCollimatorRotationAxis() const
0172   {return fCollimatorRotAxis;}
0173   inline G4ThreeVector GetGantryRotationAxis() const {return fGantryRotAxis;}
0174 
0175   inline G4bool GetAxialSymmetryX() const {return fAxialSymmetryX;}
0176   inline G4bool GetAxialSymmetryY() const {return fAxialSymmetryY;}
0177   inline G4bool GetAxialSymmetryZ() const {return fAxialSymmetryZ;}
0178 
0179 
0180 private:
0181 
0182   G4IAEAphspReader() = default;
0183 
0184   void InitializeMembers();
0185   void InitializeSource(const G4String filename);
0186   void ComputeFirstLastParticle();
0187   void ReadAndStoreFirstParticle();
0188   void PrepareThisEvent();
0189   void ReadThisEvent();
0190   void GeneratePrimaryParticles(G4Event* evt);
0191   void PerformRotations(G4ThreeVector& mom);
0192   void PerformGlobalRotations(G4ThreeVector& mom);
0193   void PerformHeadRotations(G4ThreeVector& mom);
0194   void RestartSourceFile();
0195 
0196 
0197   // ========== Data members ==========
0198 
0199 private:
0200 
0201   // ----------------------
0202   // FILE GLOBAL PROPERTIES
0203   // ----------------------
0204 
0205   G4String fFileName;
0206   // Must include the path, but NOT the IAEA extension
0207 
0208   G4int fSourceReadId;
0209   // The Id the file source has for the IAEA routines.
0210   // This value is set by IAEA routines, but should correspond to thread Id.
0211 
0212   // static const G4int fAccessRead = 1;
0213   // A value needed to open the file in the IAEA codes
0214 
0215   G4long fOrigHistories;
0216   // Number of original histories which generated the phase space file
0217 
0218   G4long fTotalParticles;
0219   // Number of particles stored in the phase space file
0220 
0221   G4int fNumberOfExtraFloats, fNumberOfExtraInts;
0222   // Number of extra variables stored for each particle
0223 
0224   std::vector<G4int>* fExtraFloatTypes; 
0225   std::vector<G4int>* fExtraIntTypes;
0226   // Identification to classify the different extra variables
0227 
0228   // ---------------------
0229   // PARTICLE PROPERTIES
0230   // ---------------------
0231 
0232   std::vector<G4int>* fParticleTypeVec;
0233   std::vector<G4double>* fKinEVec;
0234   std::vector<G4ThreeVector>* fPosVec;
0235   std::vector<G4ThreeVector>* fMomDirVec;
0236   std::vector<G4double>* fWeightVec;
0237   std::vector< std::vector<G4double> >* fExtraFloatVec;
0238   std::vector< std::vector<G4long> >* fExtraIntVec;
0239 
0240   // -------------------
0241   // COUNTERS AND FLAGS
0242   // -------------------
0243 
0244   G4int fTotalParallelRuns;
0245   // For independent parallel runs, number of fragments in which the
0246   // PSF is divided.
0247 
0248   G4int fParallelRun;
0249   // Sets the fragment of PSF from which the particles must be read.
0250 
0251   G4int fTotalThreads;
0252   // Stores the total number of threads being used. Set via G4RunManager.
0253 
0254   G4long fFirstParticle;
0255   // First particle to read.
0256   // Value given by the number of independent parallel runs and threads.
0257 
0258   G4long fLastParticle;
0259   // Last particle to read.
0260   // Value given by the number of independent parallel runs and threads.
0261 
0262   G4int fTimesRecycled;
0263   // Set the number of times that each particle is recycled (not repeated)
0264 
0265   G4int fNStat;
0266   // Decides how many events should pass before throwing a new particle
0267 
0268   G4long fUsedOrigHistories;
0269   // Variable that stores the number of original histories read so far
0270 
0271   G4long fCurrentParticle;
0272   // Number to store the current particle position in PSF
0273 
0274   G4bool fEndOfFile;
0275   // Flag active when the file has reached the end
0276 
0277   G4bool fLastGenerated;
0278   // Flag active only when the last particle has been simulated
0279 
0280   // ------------------------
0281   // SPATIAL TRANSFORMATIONS
0282   // ------------------------
0283 
0284   G4ThreeVector fGlobalPhspTranslation;
0285   // Global translation performed to particles
0286 
0287   G4int fRotationOrder;
0288   // Variable to decide first, second and third rotations
0289   // For example, 132 means rotations using X, Z and Y global axis
0290 
0291   G4double fAlpha, fBeta, fGamma;
0292   // Angles of rotations around global axis
0293 
0294   G4ThreeVector fIsocenterPosition;
0295   // Position of the isocenter if needed
0296 
0297   G4double fCollimatorAngle, fGantryAngle;
0298   G4ThreeVector fCollimatorRotAxis, fGantryRotAxis;
0299   // Angles and axis of isocentric rotations in the machine
0300   // The collimator ALWAYS rotates first.
0301 
0302   // --------------------
0303   // ROTATIONAL SYMMETRY
0304   // --------------------
0305 
0306   // Boolean data members to apply rotational symmetry around XYZ axis
0307   // Only one can be set to true.
0308   G4bool fAxialSymmetryX;
0309   G4bool fAxialSymmetryY;
0310   G4bool fAxialSymmetryZ;
0311 
0312   // ----------------
0313   // MESSENGER CLASS
0314   // ----------------
0315 
0316   G4IAEAphspReaderMessenger* fMessenger;
0317 
0318   // ----------
0319   // VERBOSITY
0320   // ----------
0321   G4int fVerbose;
0322 
0323 };
0324 
0325 #endif