From b9ccd1a2fa315131135ee653abef8aebe7e9ba6d Mon Sep 17 00:00:00 2001
From: Michal Kreps <michal.kreps@cern.ch>
Date: Wed, 12 Apr 2023 10:53:28 +0100
Subject: [PATCH] Backport fix from Geant4 11.0 of uninitialised variable in
 G4OpBoundaryProcess::DielectricDielectric() which caused some reflections to
 be wrong.

---
 .../processes/optical/src/G4OpBoundaryProcess.cc  | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/source/processes/optical/src/G4OpBoundaryProcess.cc b/source/processes/optical/src/G4OpBoundaryProcess.cc
index e97c564e57..f03ca5060b 100644
--- a/source/processes/optical/src/G4OpBoundaryProcess.cc
+++ b/source/processes/optical/src/G4OpBoundaryProcess.cc
@@ -960,6 +960,19 @@ void G4OpBoundaryProcess::DielectricDielectric()
   G4bool Inside = false;
   G4bool Swap   = false;
 
+  if (theFinish == polished) {
+    theFacetNormal = theGlobalNormal;
+  }
+  else {
+    theFacetNormal = GetFacetNormal(OldMomentum, theGlobalNormal);
+  }
+
+  // These variables deliberately shadow at this moment, this is backport
+  // from Geant4 11.0
+  G4double cost1 = -OldMomentum * theFacetNormal;
+  G4double cost2 = 0.;
+  G4double sint2 = 0.;         
+
   G4bool SurfaceRoughnessCriterionPass = true;
   if (theSurfaceRoughness != 0. && Rindex1 > Rindex2) {
     G4double wavelength = h_Planck*c_light/thePhotonMomentum;
@@ -1303,7 +1316,7 @@ void G4OpBoundaryProcess::CalculateReflectivity()
   }
 
   G4double PdotN = OldMomentum * theFacetNormal;
-  cost1 = -PdotN;
+  G4double cost1 = -PdotN;
 
   if (std::abs(cost1) < 1.0 - kCarTolerance) {
     sint1 = std::sqrt(1. - cost1*cost1);
-- 
GitLab