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 
0027 #include "ActionInitialization.hh"
0028 #include "ActionInitializationMessenger.hh"
0029 
0030 #include "globals.hh"
0031 #include "PrimaryGeneratorAction.hh"
0032 #include "RunAction.hh"
0033 #include "SteppingAction.hh"
0034 #include "G4RunManager.hh"
0035 #include "G4Threading.hh"
0036 
0037 #include "G4IAEAphspReader.hh"
0038 #include "G4IAEAphspWriterStack.hh"
0039 
0040 
0041 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0042 
0043 ActionInitialization::ActionInitialization()
0044   : G4VUserActionInitialization()
0045 {
0046   // Messenger
0047   fMessenger = new ActionInitializationMessenger(this);
0048 
0049   // IAEAphsp source file name (including path) for primary generator
0050   fIAEAphspReaderName = "";
0051   fNumberOfThreads = G4RunManager::GetRunManager()->GetNumberOfThreads();
0052 
0053   // Name prefix, including path, of IAEAphsp output files (default, nothing).
0054   fIAEAphspWriterNamePrefix = "";
0055 
0056   // Vector to register phsp planes (Z=const)
0057   fZphspVec = new std::vector<G4double>;
0058 }
0059 
0060 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0061 
0062 ActionInitialization::~ActionInitialization()
0063 {
0064   if (fZphspVec) {
0065     fZphspVec->clear();
0066     delete fZphspVec;
0067   }
0068   if (fMessenger)           delete fMessenger;
0069 }
0070 
0071 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0072 
0073 void ActionInitialization::BuildForMaster() const
0074 {
0075   // G4cout << "ActionInitialization::BuildForMaster() started" << G4endl;
0076   SetUserAction(new RunAction());
0077 }
0078 
0079 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0080 
0081 void ActionInitialization::Build() const
0082 {
0083   // G4cout << "ActionInitialization::Build() started" << G4endl;
0084   G4cout << "IAEAphsp file to read is \"" << fIAEAphspReaderName << "\"."
0085      << G4endl;
0086 
0087   PrimaryGeneratorAction* prim = new PrimaryGeneratorAction(fNumberOfThreads);
0088   if ( !fIAEAphspReaderName.empty() )   // never true in sequential mode
0089     prim->SetIAEAphspReader(fIAEAphspReaderName);
0090   SetUserAction(prim);
0091 
0092   RunAction* runAct = new RunAction();
0093   if (fIAEAphspWriterNamePrefix != "") {  // never true in sequential mode
0094     // Set G4IAEAphspWriterStack object for the local thread
0095     // and register zphsp values to it
0096     runAct->SetIAEAphspWriterStack(fIAEAphspWriterNamePrefix);
0097 
0098     if (fZphspVec->size() > 0) {
0099       for (const auto& zphsp : (*fZphspVec))
0100     runAct->AddZphsp(zphsp);
0101     }
0102     else {
0103       G4ExceptionDescription msg;
0104       msg << "IAEAphsp output file name provided, but no zphsp values "
0105       << "have been registered!" << G4endl;
0106       G4Exception("ActionInitialization::Build()",
0107           "ActionInit001", FatalException, msg);
0108     }
0109   }
0110   SetUserAction(runAct);
0111 
0112   SteppingAction *steppingAction = new SteppingAction();
0113   SetUserAction(steppingAction);
0114 }
0115 
0116 
0117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0118 
0119 void ActionInitialization::SetIAEAphspReader(const G4String& name)
0120 {
0121   fIAEAphspReaderName = name;
0122 
0123   if ( !(G4Threading::IsMultithreadedApplication()) ) {
0124     // In sequential mode, when this command is issued, Build() has been
0125     // called already. Thus, we must set G4IAEAphspReader object here
0126 
0127     const G4VUserPrimaryGeneratorAction* basePrim =
0128       G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction();
0129     if (!basePrim) return; // No PGA yet (very unlikely in sequential)
0130 
0131     // 1) cast while preserving constness
0132     const auto* myConstPrim =
0133       dynamic_cast<const PrimaryGeneratorAction*>(basePrim);
0134     if (!myConstPrim) return;
0135 
0136     // 2) Drop constness to set G4IAEAphspReader object
0137     auto* myPrim = const_cast<PrimaryGeneratorAction*>(myConstPrim);
0138     myPrim->SetIAEAphspReader(name);
0139   }
0140 }
0141 
0142 
0143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0144 
0145 void ActionInitialization::SetIAEAphspWriterPrefix(const G4String& prefix)
0146 {
0147   fIAEAphspWriterNamePrefix = prefix;
0148 
0149   if ( !(G4Threading::IsMultithreadedApplication()) ) {
0150     // In sequential mode, when this command is issued, Build() has been
0151     // called already. Thus, we must set G4IAEAphspWriterStack object here
0152     const G4UserRunAction* baseRA =
0153       G4RunManager::GetRunManager()->GetUserRunAction();
0154     if (!baseRA) return; // No run action defined
0155 
0156     // 1) cast while preserving constness
0157     const auto* myConstRA = dynamic_cast<const RunAction*>(baseRA);
0158     if (!myConstRA) return;
0159 
0160     // 2) Drop constness to modify RunAction object status
0161     auto* myRA = const_cast<RunAction*>(myConstRA);
0162     myRA->SetIAEAphspWriterStack(prefix);
0163   }
0164 }
0165 
0166 
0167 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
0168 
0169 void ActionInitialization::AddZphsp(const G4double zphsp)
0170 {
0171   fZphspVec->push_back(zphsp);
0172 
0173   if ( !(G4Threading::IsMultithreadedApplication()) ) {
0174     // In sequential mode, when this command is issued, Build() has been
0175     // called already. Thus, we must set G4IAEAphspWriterStack object here
0176     const G4UserRunAction* baseRA =
0177       G4RunManager::GetRunManager()->GetUserRunAction();
0178     if (!baseRA) return; // No run action defined
0179 
0180     // 1) cast while preserving constness
0181     const auto* myConstRA = dynamic_cast<const RunAction*>(baseRA);
0182     if (!myConstRA) return;
0183 
0184     // 2) Drop constness to modify RunAction object status
0185     auto* myRA = const_cast<RunAction*>(myConstRA);
0186     myRA->AddZphsp(zphsp);
0187   }
0188 }