From 2d4afeab1be97bf419cd101ca68c30728bc15a9a Mon Sep 17 00:00:00 2001
From: Andrea Valassi <andrea.valassi@cern.ch>
Date: Thu, 12 Jan 2023 16:16:52 +0000
Subject: [PATCH 1/2] Update G4LogicalBorderSurface hh/icc/cc from
 geant4-10.6-release to geant4-10.7-release

---
 .../volumes/include/G4LogicalBorderSurface.hh | 29 +++++++++--------
 .../include/G4LogicalBorderSurface.icc        |  8 ++++-
 .../volumes/src/G4LogicalBorderSurface.cc     | 31 +++++++++----------
 3 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/source/geometry/volumes/include/G4LogicalBorderSurface.hh b/source/geometry/volumes/include/G4LogicalBorderSurface.hh
index 95ef77356d..4cea8c1077 100644
--- a/source/geometry/volumes/include/G4LogicalBorderSurface.hh
+++ b/source/geometry/volumes/include/G4LogicalBorderSurface.hh
@@ -31,12 +31,11 @@
 // of two physical volumes.
 
 // Author: John Apostolakis (John.Apostolakis@cern.ch), 17-06-1997
-//
 // --------------------------------------------------------------------
-#ifndef G4LogicalBorderSurface_h
-#define G4LogicalBorderSurface_h 1
+#ifndef G4LogicalBorderSurface_hh
+#define G4LogicalBorderSurface_hh 1
 
-#include <vector>
+#include <map>
 
 #include "G4LogicalSurface.hh"
 #include "G4VPhysicalVolume.hh"
@@ -44,12 +43,14 @@
 class G4VPhysicalVolume;
 class G4LogicalBorderSurface;
 
-using G4LogicalBorderSurfaceTable = std::vector<G4LogicalBorderSurface*>;
+using G4LogicalBorderSurfaceTable
+      = std::map<std::pair<const G4VPhysicalVolume*,
+                           const G4VPhysicalVolume*>, G4LogicalBorderSurface*>;
 
 class G4LogicalBorderSurface : public G4LogicalSurface
 {
 
-  public:  // with description
+  public:
 
     G4LogicalBorderSurface( const G4String& name,
                                   G4VPhysicalVolume* vol1, 
@@ -64,7 +65,7 @@ class G4LogicalBorderSurface : public G4LogicalSurface
 
     G4bool operator==( const G4LogicalBorderSurface& right ) const;
     G4bool operator!=( const G4LogicalBorderSurface& right ) const;
-      // Operators.
+      // Operators
 
     static G4LogicalBorderSurface* GetSurface( const G4VPhysicalVolume* vol1,
                                                const G4VPhysicalVolume* vol2 );
@@ -72,7 +73,8 @@ class G4LogicalBorderSurface : public G4LogicalSurface
                                     G4VPhysicalVolume* vol2 );
     inline const G4VPhysicalVolume* GetVolume1() const;
     inline const G4VPhysicalVolume* GetVolume2() const;
-      // Generic accessors.
+    inline std::size_t GetIndex() const;
+      // Generic accessors
 
     inline void SetVolume1( G4VPhysicalVolume* vol1 );
     inline void SetVolume2( G4VPhysicalVolume* vol2 );
@@ -80,17 +82,19 @@ class G4LogicalBorderSurface : public G4LogicalSurface
 
     static void CleanSurfaceTable();
     static const G4LogicalBorderSurfaceTable* GetSurfaceTable();
-    static size_t GetNumberOfBorderSurfaces();
+    static std::size_t GetNumberOfBorderSurfaces();
     static void DumpInfo(); 
-      // To handle the table of surfaces.
+      // To handle the table of surfaces
 
   private:
 
     G4VPhysicalVolume* Volume1;  // Physical Volume pointer on side 1
     G4VPhysicalVolume* Volume2;  // Physical Volume pointer on side 2
 
+    std::size_t Index;           // Creation order index
+
     static G4LogicalBorderSurfaceTable *theBorderSurfaceTable;
-      // The static Table of BorderSurfaces.
+      // The static Table of BorderSurfaces
 };
 
 // ********************************************************************
@@ -99,5 +103,4 @@ class G4LogicalBorderSurface : public G4LogicalSurface
 
 #include "G4LogicalBorderSurface.icc"
 
-#endif /* G4LogicalBorderSurface_h */
-
+#endif
diff --git a/source/geometry/volumes/include/G4LogicalBorderSurface.icc b/source/geometry/volumes/include/G4LogicalBorderSurface.icc
index 42b378076d..279f59acf7 100644
--- a/source/geometry/volumes/include/G4LogicalBorderSurface.icc
+++ b/source/geometry/volumes/include/G4LogicalBorderSurface.icc
@@ -23,7 +23,7 @@
 // * acceptance of all terms of the Geant4 Software license.          *
 // ********************************************************************
 // 
-// class G4LogicalBorderSurface inline implementation
+// G4LogicalBorderSurface inline implementation
 //
 // ----------------------------------------------------------------------
 
@@ -47,6 +47,12 @@ const G4VPhysicalVolume* G4LogicalBorderSurface::GetVolume2() const
   return Volume2;
 }
 
+inline
+std::size_t G4LogicalBorderSurface::GetIndex() const
+{
+  return Index;
+}
+
 inline
 void G4LogicalBorderSurface::SetVolume1(G4VPhysicalVolume* vol1)
 {
diff --git a/source/geometry/volumes/src/G4LogicalBorderSurface.cc b/source/geometry/volumes/src/G4LogicalBorderSurface.cc
index 6d623dec21..63299096b0 100644
--- a/source/geometry/volumes/src/G4LogicalBorderSurface.cc
+++ b/source/geometry/volumes/src/G4LogicalBorderSurface.cc
@@ -29,7 +29,7 @@
 // of two physical volumes.
 //
 // Author: John Apostolakis (John.Apostolakis@cern.ch), 26-06-1997
-// ----------------------------------------------------------------------
+// --------------------------------------------------------------------
 
 #include "G4LogicalBorderSurface.hh"
 #include "G4VPhysicalVolume.hh"
@@ -47,16 +47,17 @@ G4LogicalBorderSurface(const G4String& name,
                              G4VPhysicalVolume* vol2,
                              G4SurfaceProperty* surfaceProperty)
   : G4LogicalSurface(name, surfaceProperty),
-    Volume1(vol1), Volume2(vol2)
+    Volume1(vol1), Volume2(vol2),
+    Index(theBorderSurfaceTable != nullptr ? theBorderSurfaceTable->size() : 0)
 {
-  if (!theBorderSurfaceTable)
+  if (theBorderSurfaceTable == nullptr)
   {
     theBorderSurfaceTable = new G4LogicalBorderSurfaceTable;
   }
 
   // Store in the table of Surfaces
   //
-  theBorderSurfaceTable->push_back(this);
+  theBorderSurfaceTable->insert(std::make_pair(std::make_pair(vol1,vol2),this));
 }
 
 G4LogicalBorderSurface::~G4LogicalBorderSurface()
@@ -92,7 +93,7 @@ const G4LogicalBorderSurfaceTable* G4LogicalBorderSurface::GetSurfaceTable()
   return theBorderSurfaceTable;
 }
 
-size_t G4LogicalBorderSurface::GetNumberOfBorderSurfaces()
+std::size_t G4LogicalBorderSurface::GetNumberOfBorderSurfaces()
 {
   if (theBorderSurfaceTable != nullptr)
   {
@@ -107,14 +108,10 @@ G4LogicalBorderSurface::GetSurface(const G4VPhysicalVolume* vol1,
 {
   if (theBorderSurfaceTable != nullptr)
   {
-    for(auto pos = theBorderSurfaceTable->cbegin();
-        pos != theBorderSurfaceTable->cend(); ++pos)
-    {
-      if( (*pos)->GetVolume1() == vol1 && (*pos)->GetVolume2() == vol2 )
-      { return *pos; }
-    }
+    auto pos = theBorderSurfaceTable->find(std::make_pair(vol1,vol2));
+    if(pos != theBorderSurfaceTable->cend()) return pos->second;
   }
-  return 0;
+  return nullptr;
 }
 
 // Dump info for known surfaces
@@ -129,11 +126,11 @@ void G4LogicalBorderSurface::DumpInfo()
     for(auto pos = theBorderSurfaceTable->cbegin();
         pos != theBorderSurfaceTable->cend(); ++pos)
     {
-      G4cout << (*pos)->GetName() << " : " << G4endl
+      G4LogicalBorderSurface* pSurf = pos->second;
+      G4cout << pSurf->GetName() << " : " << G4endl
              << " Border of volumes "
-             << (*pos)->GetVolume1()->GetName() << " and " 
-             << (*pos)->GetVolume2()->GetName()
-             << G4endl;
+             << pSurf->GetVolume1()->GetName() << " and " 
+             << pSurf->GetVolume2()->GetName() << G4endl;
     }
   }
   G4cout << G4endl;
@@ -146,7 +143,7 @@ void G4LogicalBorderSurface::CleanSurfaceTable()
     for(auto pos = theBorderSurfaceTable->cbegin();
         pos != theBorderSurfaceTable->cend(); ++pos)
     {
-      if (*pos)  { delete *pos; }
+      if (pos->second)  { delete pos->second; }
     }
     theBorderSurfaceTable->clear();
   }
-- 
GitLab


From e9f6673decd03283041860449266d16066443577 Mon Sep 17 00:00:00 2001
From: Andrea Valassi <andrea.valassi@cern.ch>
Date: Thu, 12 Jan 2023 16:29:02 +0000
Subject: [PATCH 2/2] Modify G4GDMLWriteStructure.cc replacing everything
 related to BorderSurface (upgrade from 10.6 to 10.7)

---
 .../gdml/src/G4GDMLWriteStructure.cc          | 97 ++++++++++---------
 1 file changed, 50 insertions(+), 47 deletions(-)

diff --git a/source/persistency/gdml/src/G4GDMLWriteStructure.cc b/source/persistency/gdml/src/G4GDMLWriteStructure.cc
index 667eb36a0c..f1829aaca2 100644
--- a/source/persistency/gdml/src/G4GDMLWriteStructure.cc
+++ b/source/persistency/gdml/src/G4GDMLWriteStructure.cc
@@ -288,48 +288,52 @@ void G4GDMLWriteStructure::AssemblyWrite(xercesc::DOMElement* volumeElement,
 }
 
 
-void G4GDMLWriteStructure::
-BorderSurfaceCache(const G4LogicalBorderSurface* const bsurf)
+void G4GDMLWriteStructure::BorderSurfaceCache(
+  const G4LogicalBorderSurface* const bsurf)
 {
-   if (!bsurf)  { return; }
-
-   const G4SurfaceProperty* psurf = bsurf->GetSurfaceProperty();
-
-   // Generate the new element for border-surface
-   //
-   const G4String& bsname = GenerateName(bsurf->GetName(), bsurf);
-   const G4String& psname = GenerateName(psurf->GetName(), psurf);
-   xercesc::DOMElement* borderElement = NewElement("bordersurface");
-   borderElement->setAttributeNode(NewAttribute("name", bsname));
-   borderElement->setAttributeNode(NewAttribute("surfaceproperty", psname));
-
-   const G4String volumeref1 = GenerateName(bsurf->GetVolume1()->GetName(),
-                                            bsurf->GetVolume1());
-   const G4String volumeref2 = GenerateName(bsurf->GetVolume2()->GetName(),
-                                            bsurf->GetVolume2());
-   xercesc::DOMElement* volumerefElement1 = NewElement("physvolref");
-   xercesc::DOMElement* volumerefElement2 = NewElement("physvolref");
-   volumerefElement1->setAttributeNode(NewAttribute("ref",volumeref1));
-   volumerefElement2->setAttributeNode(NewAttribute("ref",volumeref2));
-   borderElement->appendChild(volumerefElement1);
-   borderElement->appendChild(volumerefElement2);
+  if(bsurf == nullptr)
+  {
+    return;
+  }
 
-   if (FindOpticalSurface(psurf))
-   {
-     const G4OpticalSurface* opsurf =
-       dynamic_cast<const G4OpticalSurface*>(psurf);
-     if (!opsurf)
-     {
-       G4Exception("G4GDMLWriteStructure::BorderSurfaceCache()",
-                   "InvalidSetup", FatalException, "No optical surface found!");
-       return;
-     }
-     OpticalSurfaceWrite(solidsElement, opsurf);
-   }
+  const G4SurfaceProperty* psurf = bsurf->GetSurfaceProperty();
+
+  // Generate the new element for border-surface
+  //
+  const G4String& bsname             = GenerateName(bsurf->GetName(), bsurf);
+  const G4String& psname             = GenerateName(psurf->GetName(), psurf);
+  xercesc::DOMElement* borderElement = NewElement("bordersurface");
+  borderElement->setAttributeNode(NewAttribute("name", bsname));
+  borderElement->setAttributeNode(NewAttribute("surfaceproperty", psname));
+
+  const G4String volumeref1 =
+    GenerateName(bsurf->GetVolume1()->GetName(), bsurf->GetVolume1());
+  const G4String volumeref2 =
+    GenerateName(bsurf->GetVolume2()->GetName(), bsurf->GetVolume2());
+  xercesc::DOMElement* volumerefElement1 = NewElement("physvolref");
+  xercesc::DOMElement* volumerefElement2 = NewElement("physvolref");
+  volumerefElement1->setAttributeNode(NewAttribute("ref", volumeref1));
+  volumerefElement2->setAttributeNode(NewAttribute("ref", volumeref2));
+  borderElement->appendChild(volumerefElement1);
+  borderElement->appendChild(volumerefElement2);
+
+  if(FindOpticalSurface(psurf))
+  {
+    const G4OpticalSurface* opsurf =
+      dynamic_cast<const G4OpticalSurface*>(psurf);
+    if(opsurf == nullptr)
+    {
+      G4Exception("G4GDMLWriteStructure::BorderSurfaceCache()", "InvalidSetup",
+                  FatalException, "No optical surface found!");
+      return;
+    }
+    OpticalSurfaceWrite(solidsElement, opsurf);
+  }
 
-   borderElementVec.push_back(borderElement);
+  borderElementVec.push_back(borderElement);
 }
 
+
 void G4GDMLWriteStructure::
 SkinSurfaceCache(const G4LogicalSkinSurface* const ssurf)
 {
@@ -399,21 +403,20 @@ G4GDMLWriteStructure::GetSkinSurface(const G4LogicalVolume* const lvol)
   return surf;
 }
 
-const G4LogicalBorderSurface*
-G4GDMLWriteStructure::GetBorderSurface(const G4VPhysicalVolume* const pvol)
+const G4LogicalBorderSurface* G4GDMLWriteStructure::GetBorderSurface(
+  const G4VPhysicalVolume* const pvol)
 {
-  G4LogicalBorderSurface* surf = 0;
+  G4LogicalBorderSurface* surf = nullptr;
   G4int nsurf = G4LogicalBorderSurface::GetNumberOfBorderSurfaces();
-  if (nsurf)
+  if(nsurf)
   {
     const G4LogicalBorderSurfaceTable* btable =
-          G4LogicalBorderSurface::GetSurfaceTable();
-    std::vector<G4LogicalBorderSurface*>::const_iterator pos;
-    for (pos = btable->begin(); pos != btable->end(); pos++)
+      G4LogicalBorderSurface::GetSurfaceTable();
+    for(auto pos = btable->cbegin(); pos != btable->cend(); ++pos)
     {
-      if (pvol == (*pos)->GetVolume1())  // just the first in the couple 
-      {                                  // could be enough?
-        surf = *pos; // break;
+      if(pvol == pos->first.first)  // just the first in the couple
+      {                             // could be enough?
+        surf = pos->second;         // break;
         BorderSurfaceCache(surf);
       }
     }
-- 
GitLab