added multi particles
This commit is contained in:
+2
-6
@@ -162,16 +162,12 @@ void G4System::run_gui(){
|
||||
}
|
||||
}
|
||||
|
||||
void G4System::run_batch(int nEvents,const std::string& partSpecies, double minEnergy,
|
||||
void G4System::run_batch(int nEvents, const std::vector<std::string>& partSpecies, double minEnergy,
|
||||
double maxEnergy, std::string filename){
|
||||
check();
|
||||
|
||||
|
||||
G4String partSpec = partSpecies;
|
||||
G4cout << "running with particle species " << partSpec << G4endl;
|
||||
UImanager->ApplyCommand("/run/initialize");
|
||||
UImanager->ApplyCommand("/vis/disable");
|
||||
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpec);
|
||||
actionInitialization->setGeneratorProperties(minEnergy, maxEnergy, partSpecies);
|
||||
actionInitialization->setFileName(filename);
|
||||
runManager->BeamOn(nEvents);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ PrimaryGeneratorAction::PrimaryGeneratorAction()
|
||||
G4int nofParticles = 1;
|
||||
fParticleGun = new G4ParticleGun(nofParticles);
|
||||
|
||||
partSpecies = "e-";
|
||||
partSpecies = std::vector<std::string>{"e-"};
|
||||
minPartEnergy = 1000;
|
||||
maxPartEnergy = 1000;
|
||||
|
||||
@@ -84,62 +84,84 @@ void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
|
||||
rand = G4INCL::Random::shoot();
|
||||
partEnergy = rand*(maxPartEnergy-minPartEnergy)+minPartEnergy;
|
||||
|
||||
//G4cout << "shooting with " << partEnergy << " GeV" << G4endl;
|
||||
|
||||
auto particleDefinition
|
||||
= G4ParticleTable::GetParticleTable()->FindParticle(partSpecies);
|
||||
if(particleDefinition == nullptr){
|
||||
G4cout << "Particle " << partSpecies << " not found" << G4endl;
|
||||
throw std::runtime_error("Particle not found");
|
||||
return;
|
||||
//create fractions for each entry in the partSpecies vector such that the partEnergy can be distributed
|
||||
//according to the fractions
|
||||
std::vector<G4double> part_energies;
|
||||
G4double sum = 0;
|
||||
for(const auto& part : partSpecies){
|
||||
G4double randfrac = 0;
|
||||
while(randfrac > 1 || randfrac <= 0)
|
||||
randfrac = G4INCL::Random::shoot();
|
||||
part_energies.push_back(randfrac);
|
||||
sum += randfrac;
|
||||
}
|
||||
fParticleGun->SetParticleDefinition(particleDefinition);
|
||||
fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
|
||||
fParticleGun->SetParticleEnergy(partEnergy*GeV);
|
||||
|
||||
// In order to avoid dependence of PrimaryGeneratorAction
|
||||
// on DetectorConstruction class we get world volume
|
||||
// from G4LogicalVolumeStore
|
||||
//
|
||||
G4double worldZHalfLength = 0.;
|
||||
auto worldLV = G4LogicalVolumeStore::GetInstance()->GetVolume("World");
|
||||
|
||||
// Check that the world volume has box shape
|
||||
G4Box* worldBox = nullptr;
|
||||
if ( worldLV ) {
|
||||
worldBox = dynamic_cast<G4Box*>(worldLV->GetSolid());
|
||||
//normalise w.r.t total
|
||||
for(auto& en : part_energies){
|
||||
en /= sum;
|
||||
en *= partEnergy;
|
||||
}
|
||||
|
||||
if ( worldBox ) {
|
||||
worldZHalfLength = worldBox->GetZHalfLength();
|
||||
}
|
||||
else {
|
||||
G4ExceptionDescription msg;
|
||||
msg << "World volume of box shape not found." << G4endl;
|
||||
msg << "Perhaps you have changed geometry." << G4endl;
|
||||
msg << "The gun will be place in the center.";
|
||||
G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
|
||||
"MyCode0002", JustWarning, msg);
|
||||
}
|
||||
if(false){
|
||||
//draw particle position from a unit distribution in x-y with +-3 cm around the center
|
||||
G4double x = -1000;
|
||||
G4double y = -1000;
|
||||
G4double x_width = 6;
|
||||
G4double y_width = 6;
|
||||
while(x < -x_width/2. || x > x_width/2. || y < -y_width/2. || y > y_width/2.){
|
||||
x = G4INCL::Random::shoot()*x_width - x_width/2;
|
||||
y = G4INCL::Random::shoot()*y_width - y_width/2;
|
||||
// now create a vertex for every particle
|
||||
|
||||
for (size_t i = 0; i < partSpecies.size(); i++){
|
||||
G4String single_partType = partSpecies[i];
|
||||
G4double single_partEnergy = part_energies[i];
|
||||
//create a vertex for the particle
|
||||
auto particleDefinition
|
||||
= G4ParticleTable::GetParticleTable()->FindParticle(single_partType);
|
||||
if(particleDefinition == nullptr){
|
||||
G4cout << "Particle " << single_partType << " not found" << G4endl;
|
||||
throw std::runtime_error("Particle not found");
|
||||
return;
|
||||
}
|
||||
fParticleGun->SetParticlePosition(G4ThreeVector(x*cm, y*cm, -worldZHalfLength));
|
||||
fParticleGun->SetParticleDefinition(particleDefinition);
|
||||
fParticleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
|
||||
fParticleGun->SetParticleEnergy(single_partEnergy*GeV);
|
||||
|
||||
// In order to avoid dependence of PrimaryGeneratorAction
|
||||
// on DetectorConstruction class we get world volume
|
||||
// from G4LogicalVolumeStore
|
||||
//
|
||||
G4double worldZHalfLength = 0.;
|
||||
auto worldLV = G4LogicalVolumeStore::GetInstance()->GetVolume("World");
|
||||
|
||||
// Check that the world volume has box shape
|
||||
G4Box* worldBox = nullptr;
|
||||
if ( worldLV ) {
|
||||
worldBox = dynamic_cast<G4Box*>(worldLV->GetSolid());
|
||||
}
|
||||
|
||||
if ( worldBox ) {
|
||||
worldZHalfLength = worldBox->GetZHalfLength();
|
||||
}
|
||||
else {
|
||||
G4ExceptionDescription msg;
|
||||
msg << "World volume of box shape not found." << G4endl;
|
||||
msg << "Perhaps you have changed geometry." << G4endl;
|
||||
msg << "The gun will be place in the center.";
|
||||
G4Exception("PrimaryGeneratorAction::GeneratePrimaries()",
|
||||
"MyCode0002", JustWarning, msg);
|
||||
}
|
||||
if(partSpecies.size()){ //only change impact point if there are multiple particles
|
||||
//draw particle position from a unit distribution in x-y with +-5 cm around the center
|
||||
G4double x = -1000;
|
||||
G4double y = -1000;
|
||||
G4double x_width = 10;
|
||||
G4double y_width = 10;
|
||||
while(x < -x_width/2. || x > x_width/2. || y < -y_width/2. || y > y_width/2.){
|
||||
x = G4INCL::Random::shoot()*x_width - x_width/2;
|
||||
y = G4INCL::Random::shoot()*y_width - y_width/2;
|
||||
}
|
||||
fParticleGun->SetParticlePosition(G4ThreeVector(x*cm, y*cm, -worldZHalfLength));
|
||||
|
||||
}
|
||||
else{
|
||||
fParticleGun->SetParticlePosition(G4ThreeVector(0.*cm, 0.*cm, -worldZHalfLength));
|
||||
}
|
||||
//printout for checking
|
||||
|
||||
fParticleGun->GeneratePrimaryVertex(anEvent);
|
||||
}
|
||||
else{
|
||||
fParticleGun->SetParticlePosition(G4ThreeVector(0.*cm, 0.*cm, -worldZHalfLength));
|
||||
}
|
||||
//printout for checking
|
||||
//G4cout << "shooting at " << x << " " << y << " " << -worldZHalfLength << G4endl;
|
||||
|
||||
fParticleGun->GeneratePrimaryVertex(anEvent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user