Skip to content
Snippets Groups Projects
Commit 19650e25 authored by Vangelis Kourlitis's avatar Vangelis Kourlitis
Browse files

add functionality to set particle gun position, e.g. /mygun/position

parent 1ab309e8
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ public:
void SetPrimaryEnergy(G4double ekin);
void SetPrimaryName(const G4String& pname);
void SetPrimaryDirection(const G4ThreeVector &pdir);
void SetPrimaryPosition(const G4ThreeVector &ppos);
static void Print();
static G4int GetNumberOfPrimaryTypes() { return gNumberCandidateParticles; }
......@@ -49,11 +50,13 @@ private:
static G4double gPrimaryEnergy;
static std::string gPrimaryType;
static G4ThreeVector gPrimaryDir;
static G4ThreeVector gPrimaryPos;
G4bool fIsUserNumPrimaryPerEvt;
G4bool fIsUserPrimaryType;
G4bool fIsUserPrimaryDir;
G4bool fIsUserPrimaryPos;
G4bool fIsUserPrimaryEnergy;
G4int fNumPrimaryPerEvt;
......
......@@ -27,6 +27,7 @@ private:
G4UIcmdWithAString* fPrimaryTypeCmd;
G4UIcmdWithADoubleAndUnit* fPrimaryEnergyCmd;
G4UIcmdWith3Vector* fPrimaryDirCmd;
G4UIcmdWith3Vector* fPrimaryPosCmd;
};
......
......@@ -25,6 +25,7 @@ G4int MyPrimaryGeneratorAction::gNumPrimaryPerEvt(-1);
G4double MyPrimaryGeneratorAction::gPrimaryEnergy(-1.);
std::string MyPrimaryGeneratorAction::gPrimaryType("");
G4ThreeVector MyPrimaryGeneratorAction::gPrimaryDir(0.,0.,0.);
G4ThreeVector MyPrimaryGeneratorAction::gPrimaryPos(0.,0.,0.);
// These are the particle types that can be used as primary beam particle, on a event-by-event based.
......@@ -88,6 +89,7 @@ void MyPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent) {
fPrimaryParticleDirection = G4RandomDirection();
}
// Beam position: always the origin i.e. [0,0,0].
// unless specified by user via SetPrimaryPosition
//
// Set the particle gun
G4ParticleDefinition* pDef = fParticleTable->FindParticle(fPrimaryParticleName);
......@@ -134,6 +136,12 @@ void MyPrimaryGeneratorAction::SetPrimaryDirection(const G4ThreeVector &pdir) {
fIsUserPrimaryDir = true;
}
void MyPrimaryGeneratorAction::SetPrimaryPosition(const G4ThreeVector &ppos) {
fPrimaryParticlePosition = ppos;
gPrimaryPos = fPrimaryParticlePosition;
fIsUserPrimaryPos = true;
}
G4int MyPrimaryGeneratorAction::GetPrimaryTypeIndex(const G4String& pname) {
G4int indx = gPrimaryNameToIndexMap.find(pname)->second;
return indx;
......@@ -161,6 +169,11 @@ void MyPrimaryGeneratorAction::Print() {
} else {
str += " Primary energy : " + std::to_string(gPrimaryEnergy/GeV) + " [GeV] \n";
}
G4String spos= "[";
spos += std::to_string(gPrimaryPos.x()) + ", "
+ std::to_string(gPrimaryPos.y()) + ", "
+ std::to_string(gPrimaryPos.x()) + "]\n";
str += " Primary position : " + spos;
if (gPrimaryDir.mag()==0.) {
str += " Primary direction : isotropic for each primary \n";
} else {
......
......@@ -31,6 +31,10 @@ MyPrimaryGeneratorMessenger::MyPrimaryGeneratorMessenger(MyPrimaryGeneratorActio
fPrimaryDirCmd = new G4UIcmdWith3Vector("/mygun/direction",this);
fPrimaryDirCmd->SetGuidance("set primary particle direction");
fPrimaryDirCmd->AvailableForStates(G4State_PreInit,G4State_Idle);
fPrimaryPosCmd = new G4UIcmdWith3Vector("/mygun/position",this);
fPrimaryPosCmd->SetGuidance("set primary particle position");
fPrimaryPosCmd->AvailableForStates(G4State_PreInit,G4State_Idle);
}
......@@ -40,6 +44,7 @@ MyPrimaryGeneratorMessenger::~MyPrimaryGeneratorMessenger() {
delete fPrimaryTypeCmd;
delete fPrimaryEnergyCmd;
delete fPrimaryDirCmd;
delete fPrimaryPosCmd;
}
void MyPrimaryGeneratorMessenger::SetNewValue(G4UIcommand* command, G4String newValue) {
......@@ -55,4 +60,7 @@ void MyPrimaryGeneratorMessenger::SetNewValue(G4UIcommand* command, G4String new
if (command==fPrimaryDirCmd) {
fTheGun->SetPrimaryDirection(fPrimaryDirCmd->GetNew3VectorValue(newValue));
}
if (command==fPrimaryPosCmd) {
fTheGun->SetPrimaryPosition(fPrimaryPosCmd->GetNew3VectorValue(newValue));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment