diff --git a/DDG4/src/Geant4InputHandling.cpp b/DDG4/src/Geant4InputHandling.cpp index 86152beba674862100c0f2c238e5810450501e25..53a94b1ce03fff952a9413731314e042450af89a 100644 --- a/DDG4/src/Geant4InputHandling.cpp +++ b/DDG4/src/Geant4InputHandling.cpp @@ -375,6 +375,9 @@ static G4PrimaryParticle* createG4Primary(const Geant4ParticleHandle p) { // The particle is fully defined with the 4-vector set above, setting the mass isn't necessary, not // using the 4-vector, means the PDG mass is used, and the momentum is scaled if the mass is set here // g4->SetMass(p->mass); + if ( p->spin[0] != 0.0 || p->spin[1] != 0.0 || p->spin[2] != 0.0 ) { + g4->SetPolarization(p->spin[0], p->spin[1], p->spin[2]); + } return g4; } diff --git a/DDG4/src/Geant4ParticleGenerator.cpp b/DDG4/src/Geant4ParticleGenerator.cpp index 159f79fb0746e6f4baf46a48f901bae564bbab61..17aa4b8ec464519d1229ce037198dfb5efb2c902 100644 --- a/DDG4/src/Geant4ParticleGenerator.cpp +++ b/DDG4/src/Geant4ParticleGenerator.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include // Geant4 include files @@ -158,9 +159,32 @@ void Geant4ParticleGenerator::operator()(G4Event*) { p->psz = unit_direction.Z()*momentum; p->mass = m_particle->GetPDGMass(); p->charge = 3 * m_particle->GetPDGCharge(); - p->spin[0] = 0; - p->spin[1] = 0; - p->spin[2] = 0; + // Optical photons must have a defined polarization; assign a random one + // perpendicular to the momentum direction (physical requirement for Geant4 optics). + if ( m_particle->GetParticleName() == "opticalphoton" ) { + if ( momentum == 0.0 ) { + except("Geant4ParticleGenerator: Cannot assign polarization to optical photon with zero momentum!"); + } + // Build two unit vectors orthogonal to the photon direction using Gram-Schmidt. + ROOT::Math::XYZVector e1, e2; + if ( std::fabs(unit_direction.X()) < 0.9 ) + e1 = ROOT::Math::XYZVector(1, 0, 0); + else + e1 = ROOT::Math::XYZVector(0, 1, 0); + e2 = unit_direction.Cross(e1).unit(); + e1 = e2.Cross(unit_direction).unit(); // normalize defensively + Geant4Random& rnd = evt.random(); + double angle = CLHEP::twopi * rnd.rndm(); + ROOT::Math::XYZVector pol = (std::cos(angle)*e1 + std::sin(angle)*e2).unit(); + p->spin[0] = pol.X(); + p->spin[1] = pol.Y(); + p->spin[2] = pol.Z(); + } + else { + p->spin[0] = 0; + p->spin[1] = 0; + p->spin[2] = 0; + } p->colorFlow[0] = 0; p->colorFlow[1] = 0; p->vsx = vtx->x;