Geant4在RunAction/EventAction中调用DetectorConstruction和PrimaryGeneratorAction

1、使用dynamic_cast/static_cast

RunAction.cc

    const auto* detectorConstruction = dynamic_cast< const DetectorConstruction* >
            (G4RunManager::GetRunManager()->GetUserDetectorConstruction() );
    G4Material* targetMaterial = detectorConstruction->GetTargetMaterial();

    // There is no primary generator action object for "master"
    const auto* generatorAction = dynamic_cast< const PrimaryGeneratorAction* >
            (G4RunManager::GetRunManager()->GetUserPrimaryGeneratorAction() );
    G4double  particleEnergy;
    if(generatorAction) {
        const G4ParticleGun *particleGun = generatorAction->GetParticleGun();
        particleEnergy = particleGun->GetParticleEnergy();//粒子能量
//      particleGun->GetParticleMomentum();               //粒子动量
//      particleGun->GetParticlePolarization();           //粒子位置
//      particleGun->GetNumberOfParticles();              //粒子数量
//      particleGun->GetParticleDefinition()->GetParticleName(); //粒子名称
//      particleGun->GetParticleDefinition()->GetParticleTable(); //粒子种类
    }

 

 2、传参

在主程序中链接DetectorConstruction

主程序

int main(int argc,char** argv)
{
……
    DetectorConstruction* det= new DetectorConstruction;
    runManager->SetUserInitialization(det);
    runManager->SetUserInitialization(new ActionInitialization(det));
……
}

ActionInitialization中链接PrimaryGeneratorAction

ActionInitialization.hh

class DetectorConstruction;
class ActionInitialization : public G4VUserActionInitialization
{
    public:
        ActionInitialization(DetectorConstruction* detector);
……
    private:
        DetectorConstruction* fDetector;
};

 

ActionInitialization.cc

ActionInitialization::ActionInitialization(DetectorConstruction* detector)
 : G4VUserActionInitialization(),
   fDetector(detector)
{}

void ActionInitialization::BuildForMaster() const
{
  SetUserAction(new RunAction(fDetector,0));
}


void ActionInitialization::Build() const
{
    auto primary = new PrimaryGeneratorAction();
    SetUserAction(primary);

    SetUserAction(new RunAction(fDetector,primary));

    auto eventAction = new EventAction();
    SetUserAction(eventAction);

    SetUserAction(new SteppingAction(eventAction));
}

 

RunAction.hh

  class PrimaryGeneratorAction;
  class DetctorConstruction;
  class RunAction : public G4UserRunAction
  {
    public:
    RunAction(PrimaryGeneratorAction*,DetectorConstruction*);
    ……
    private:
    PrimaryGeneratorAction* fPrimary;
    DetetorConstruction* fDetConstruction;
    }

 

RunAction.cc

RunAction::RunAction(PrimaryGeneratorAction* kin,DetectorConstruction* det)
        :G4UserRunAction(), fPrimary(kin),fDetConstruction(det)
{
……
}
void RunAction::BeginOfRunAction(const G4Run*)
{
    
……
    G4Material* mat;
    if(fDetConstruction){
        mat = fDetConstruction->GetMaterial();
    }
    G4double energy;
    if (fPrimary) {
         energy = fPrimary->GetParticleGun()->GetParticleEnergy();
    }
}

 

posted @ 2023-08-10 11:05  endcase  阅读(46)  评论(0编辑  收藏  举报