Message: Trouble with particle gun/general particle source Not Logged In (login)
 Next-in-Thread Next-in-Thread
 Next-in-Forum Next-in-Forum

Question Trouble with particle gun/general particle source 

Forum: Particles
Date: 13 Oct, 2014
From: Chris Ferraioli <Chris Ferraioli>

Hello,

I'm running geant4.9.4.p02 on a Scientific Linux 5 machine and I'm having some trouble setting the position, energy, etc. of my particle gun. I have attached my PrimaryGeneratorAction files below.

The problem I'm running into is that I cannot set my particle gun attributes within the PrimaryGeneratorAction file, though I can do so interactively using /gun/position commands. If I try to set the position in the PrimaryGeneratorEvent, I simply get a particle produced at the default 0,0,0 position of my detector. The reason this is an issue is that I'd like to set two (or more) particle guns in different positions to test detector response to multiple incident particles, but without the ability to control in my PrimaryGenerator, I am stuck. No other attributes can be set in PrimaryGeneratorEvent either - event energy, particle type, etc., all must be set interactively with /gun/ commands.

I've also tried using general particle source (currently commented out in the code below), but when I try to run a /gps command I am met with a "command not found" error.

Any help would be appreciated!

Cheers, Chris Ferraioli

------------ ExNO2PrimaryGeneratorAction.hh ------------

#ifndef ExN02PrimaryGeneratorAction_h
#define ExN02PrimaryGeneratorAction_h 1

#include "G4VUserPrimaryGeneratorAction.hh"
#include "globals.hh"
#include "G4ParticleGun.hh"
//#include "G4GeneralParticleSource.hh" #include "G4ThreeVector.hh"

class ExN02DetectorConstruction; class G4ParticleGun; //class G4GeneralParticleSource; class G4Event; class ExN02PrimaryGeneratorActionMessenger;

class ExN02PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
{
  public:
    ExN02PrimaryGeneratorAction(ExN02DetectorConstruction*);    
   ~ExN02PrimaryGeneratorAction();

  public:
    void GeneratePrimaries(G4Event*);
  G4ParticleGun* GetParticleGun() {return particleGun;};
//  G4GeneralParticleSource* GetParticleGun() {return particleGun;};

  void SetVxSmearing(G4ThreeVector);
  void SetVxPosition(G4ThreeVector);

  private:
    G4ParticleGun* particleGun;
//    G4GeneralParticleSource* particleGun;    
    ExN02DetectorConstruction* myDetector;
  ExN02PrimaryGeneratorActionMessenger* actMessenger;
  ExN02DetectorConstruction*         Detector;         

  G4int                         smearFlag, smearErea;
  G4double                      xVertex, yVertex, zVertex;
  G4double                      dRwidth;
  G4bool                        vertexDefined; 
};
#endif

------------ ExNO2PrimaryGeneratorAction.cc ------------

#include "ExN02PrimaryGeneratorAction.hh"
#include "ExN02DetectorConstruction.hh"
#include "G4Event.hh"
//#include "G4GeneralParticleSource.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "globals.hh"

ExN02PrimaryGeneratorAction::ExN02PrimaryGeneratorAction(
                                               ExN02DetectorConstruction* myDC)
:myDetector(myDC)
{
  G4int n_particle = 1;
  particleGun = new G4ParticleGun(n_particle);
//  particleGun = new G4GeneralParticleSource();

  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
  G4ParticleDefinition* particle = particleTable->FindParticle("neutron");

  particleGun->SetParticleDefinition(particle);
  particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
  particleGun->SetParticleEnergy(3.0*GeV);

  G4double position = -0.5*(myDetector->GetWorldFullLength());
  particleGun->SetParticlePosition(G4ThreeVector(-3.75*cm,-4.6875*cm,position));
}

ExN02PrimaryGeneratorAction::~ExN02PrimaryGeneratorAction()
{
  delete particleGun;
}

void ExN02PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{ 
  particleGun->GeneratePrimaryVertex(anEvent);
}

 Add Message Add Message
to: "Trouble with particle gun/general particle source"

 Subscribe Subscribe

This site runs SLAC HyperNews version 1.11-slac-98, derived from the original HyperNews


[ Geant 4 Home | Geant 4 HyperNews | Search | Request New Forum | Feedback ]