From e7ad7a672ea50f8646a5568ab07c68a99de8cec5 Mon Sep 17 00:00:00 2001
From: Fabian Klimpel <fklimpel@cern.ch>
Date: Thu, 11 Apr 2019 10:04:01 +0200
Subject: [PATCH] Rebase

---
 .../Acts/Geometry/CuboidVolumeBuilder.hpp     |  2 +-
 Core/include/Acts/Geometry/TrackingVolume.hpp | 12 ++++++-
 .../Acts/Geometry/detail/TrackingVolume.ipp   |  7 ++--
 Core/src/Geometry/CuboidVolumeBuilder.cpp     | 27 ++++++---------
 Core/src/Geometry/CylinderVolumeBuilder.cpp   | 10 ++----
 Core/src/Geometry/TrackingVolume.cpp          |  8 ++++-
 .../CommonHelpers/CubicTrackingGeometry.hpp   |  9 ++---
 .../CylindricalTrackingGeometry.hpp           |  2 +-
 .../Geometry/CuboidVolumeBuilderTests.cpp     | 34 +++++++++++++------
 .../Core/Geometry/TrackingVolumeCreation.hpp  |  2 +-
 Tests/Core/Propagator/StepperTests.cpp        | 11 +++---
 11 files changed, 73 insertions(+), 51 deletions(-)

diff --git a/Core/include/Acts/Geometry/CuboidVolumeBuilder.hpp b/Core/include/Acts/Geometry/CuboidVolumeBuilder.hpp
index 453e5e794..56be8f4a5 100644
--- a/Core/include/Acts/Geometry/CuboidVolumeBuilder.hpp
+++ b/Core/include/Acts/Geometry/CuboidVolumeBuilder.hpp
@@ -14,6 +14,7 @@
 #include "Acts/Geometry/GeometryContext.hpp"
 #include "Acts/Geometry/ITrackingVolumeBuilder.hpp"
 #include "Acts/Utilities/Definitions.hpp"
+#include "Acts/Utilities/BinningType.hpp"
 
 namespace Acts {
 
@@ -159,7 +160,6 @@ class CuboidVolumeBuilder : public ITrackingVolumeBuilder {
   void
   sortVolumes(std::vector<std::pair<TrackingVolumePtr, Vector3D>>& tapVec,
               BinningValue bValue) const;
-};
 
   /// @brief This function builds a world TrackingVolume based on a given
   /// configuration
diff --git a/Core/include/Acts/Geometry/TrackingVolume.hpp b/Core/include/Acts/Geometry/TrackingVolume.hpp
index 9e7e55cae..b61651228 100644
--- a/Core/include/Acts/Geometry/TrackingVolume.hpp
+++ b/Core/include/Acts/Geometry/TrackingVolume.hpp
@@ -283,6 +283,10 @@ class TrackingVolume : public Volume {
 
   /// Return the confined volumes of this container array - if it exists
   std::shared_ptr<const TrackingVolumeArray> confinedVolumes() const;
+  
+  /// Return the confined dense volumes
+  const MutableTrackingVolumeVector
+	denseVolumes() const;
 
   /// @brief Visit all sensitive surfaces
   ///
@@ -504,7 +508,7 @@ private:
   std::shared_ptr<const TrackingVolumeArray> m_confinedVolumes = nullptr;
 
   /// confined dense
-  TrackingVolumeVector m_confinedDenseVolumes;
+  MutableTrackingVolumeVector m_confinedDenseVolumes;
 
   /// Volumes to glue Volumes from the outside
   GlueVolumesDescriptor* m_glueVolumeDescriptor{nullptr};
@@ -549,6 +553,12 @@ inline const LayerArray* TrackingVolume::confinedLayers() const {
   return m_confinedLayers.get();
 }
 
+inline const MutableTrackingVolumeVector
+TrackingVolume::denseVolumes() const
+{
+	return m_confinedDenseVolumes;
+}
+
 inline std::shared_ptr<const TrackingVolumeArray>
 TrackingVolume::confinedVolumes() const {
   return m_confinedVolumes;
diff --git a/Core/include/Acts/Geometry/detail/TrackingVolume.ipp b/Core/include/Acts/Geometry/detail/TrackingVolume.ipp
index 59a3f3905..a94d23b84 100644
--- a/Core/include/Acts/Geometry/detail/TrackingVolume.ipp
+++ b/Core/include/Acts/Geometry/detail/TrackingVolume.ipp
@@ -102,10 +102,9 @@ std::vector<BoundaryIntersection> TrackingVolume::compatibleBoundaries(
     }
     nonExcludedBoundaries.push_back(bSurface);
   }
-  
-  const std::vector<std::shared_ptr<const TrackingVolume>> denseVolumes
-      = confinedDenseVolumes();
-  for (const auto& dv : denseVolumes) {
+
+  const std::vector<std::shared_ptr<TrackingVolume>> confinedDenseVolumes = denseVolumes();
+  for (const auto& dv : confinedDenseVolumes) {
     auto& bSurfacesConfined = dv->boundarySurfaces();
     for (auto& bsIter : bSurfacesConfined) {
       // get the boundary surface pointer
diff --git a/Core/src/Geometry/CuboidVolumeBuilder.cpp b/Core/src/Geometry/CuboidVolumeBuilder.cpp
index 9f570d4f0..0bf48ac5f 100644
--- a/Core/src/Geometry/CuboidVolumeBuilder.cpp
+++ b/Core/src/Geometry/CuboidVolumeBuilder.cpp
@@ -146,7 +146,7 @@ std::shared_ptr<Acts::TrackingVolume> Acts::CuboidVolumeBuilder::buildVolume(
 	// Build confined volumes
 	if(cfg.trackingVolumes.empty())
 		for(VolumeConfig vc : cfg.volumeCfg)
-			cfg.trackingVolumes.push_back(buildVolume(vc));
+			cfg.trackingVolumes.push_back(buildVolume(gctx, vc));
 	
 	
 	std::shared_ptr<TrackingVolume> trackVolume;
@@ -158,28 +158,23 @@ std::shared_ptr<Acts::TrackingVolume> Acts::CuboidVolumeBuilder::buildVolume(
 								   bounds,
 								   cfg.material,
 								   nullptr,
-								   {},
+								   nullptr,
 								   cfg.trackingVolumes,
-								   {},
 								   cfg.name);
 	}
 	else
 	{
-		  // Build layer array
-	  std::pair<double, double> minMax = binningRange(cfg);
-	  LayerArrayCreator layArrCreator(
-		  getDefaultLogger("LayerArrayCreator", Logging::INFO));
-	  std::unique_ptr<const LayerArray>  layArr( 
-		  layArrCreator.layerArray(layVec,
-								   minMax.first,
-								   minMax.second,
-								   BinningType::arbitrary,
-								   BinningValue::binX));
 	
   // Build TrackingVolume
-  auto trackVolume = TrackingVolume::create(
-      std::make_shared<const Transform3D>(trafo), bounds, cfg.volumeMaterial,
-      std::move(layArr), nullptr, cfg.trackingVolumes, cfg.name);
+  trackVolume
+      = TrackingVolume::create(std::make_shared<const Transform3D>(trafo),
+                               bounds,
+                               cfg.volumeMaterial,
+                               std::move(layArr),
+                               nullptr,
+                               cfg.trackingVolumes,
+                               cfg.name);
+   }
   trackVolume->sign(GeometrySignature::Global);
 
   return trackVolume;
diff --git a/Core/src/Geometry/CylinderVolumeBuilder.cpp b/Core/src/Geometry/CylinderVolumeBuilder.cpp
index 639cafa34..6b8b0bcb4 100644
--- a/Core/src/Geometry/CylinderVolumeBuilder.cpp
+++ b/Core/src/Geometry/CylinderVolumeBuilder.cpp
@@ -134,13 +134,9 @@ Acts::CylinderVolumeBuilder::trackingVolume(
 
   // Find out with Layer analysis
   // analyze the layers
-  wConfig.nVolumeConfig = analyzeLayers(gctx, negativeLayers);
-  wConfig.cVolumeConfig = analyzeLayers(gctx, centralLayers);
-  wConfig.pVolumeConfig = analyzeLayers(gctx, positiveLayers);
-
-  wConfig.nVolumeConfig = analyzeContent(negativeLayers, {});  // TODO
-  wConfig.cVolumeConfig = analyzeContent(centralLayers, centralVolumes);
-  wConfig.pVolumeConfig = analyzeContent(positiveLayers, {});  // TODO
+  wConfig.nVolumeConfig = analyzeContent(gctx, negativeLayers, {});  // TODO
+  wConfig.cVolumeConfig = analyzeContent(gctx, centralLayers, centralVolumes);
+  wConfig.pVolumeConfig = analyzeContent(gctx, positiveLayers, {});  // TODO
 
   std::string layerConfiguration = "|";
   if (wConfig.nVolumeConfig) {
diff --git a/Core/src/Geometry/TrackingVolume.cpp b/Core/src/Geometry/TrackingVolume.cpp
index 9cb607dd0..7bb4464ad 100644
--- a/Core/src/Geometry/TrackingVolume.cpp
+++ b/Core/src/Geometry/TrackingVolume.cpp
@@ -93,6 +93,12 @@ const Acts::TrackingVolume* Acts::TrackingVolume::lowestTrackingVolume(
     return (m_confinedVolumes->object(gp).get());
   }
 
+  // search for dense volumes
+  if (!m_confinedDenseVolumes.empty())
+    for (auto& denseVolume : m_confinedDenseVolumes)
+      if (denseVolume->inside(gp, 0.001)) return denseVolume.get();
+
+
   // there is no lower sub structure
   return this;
 }
@@ -462,7 +468,7 @@ void Acts::TrackingVolume::closeGeometry(
     for (auto& volumesIter : m_confinedDenseVolumes) {
       auto mutableVolumesIter
           = std::const_pointer_cast<TrackingVolume>(volumesIter);
-      mutableVolumesIter->closeGeometry(volumeMap, vol);
+      mutableVolumesIter->closeGeometry(surfaceMaterialMap, volumeMap, vol);
     }
   }
 }
diff --git a/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CubicTrackingGeometry.hpp b/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CubicTrackingGeometry.hpp
index 66a37d89d..122194175 100644
--- a/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CubicTrackingGeometry.hpp
+++ b/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CubicTrackingGeometry.hpp
@@ -55,6 +55,7 @@ struct CubicTrackingGeometry {
     surfaceMaterial = std::shared_ptr<const ISurfaceMaterial>(
         new HomogeneousSurfaceMaterial(matProp));
   }
+<<<<<<< HEAD
 
   /// Call operator to build the standard cubic tracking geometry
   std::shared_ptr<const TrackingGeometry> operator()() {
@@ -121,7 +122,7 @@ struct CubicTrackingGeometry {
 
     auto trackVolume1 = TrackingVolume::create(
         std::make_shared<const Transform3D>(trafoVol1), boundsVol, nullptr,
-        std::move(layArr1), nullptr, "Volume 1");
+        std::move(layArr1), nullptr, {}, "Volume 1");
     trackVolume1->sign(GeometrySignature::Global);
 
     // Build volume for surfaces with positive x-values
@@ -137,17 +138,17 @@ struct CubicTrackingGeometry {
 
     auto trackVolume2 = TrackingVolume::create(
         std::make_shared<const Transform3D>(trafoVol2), boundsVol, nullptr,
-        std::move(layArr2), nullptr, "Volume 2");
+        std::move(layArr2), nullptr, {}, "Volume 2");
 
     trackVolume2->sign(GeometrySignature::Global);
 
     // Glue volumes
     trackVolume2->glueTrackingVolume(
-        geoContext, BoundarySurfaceFace::negativeFaceYZ, trackVolume1,
+        geoContext, BoundarySurfaceFace::negativeFaceYZ, trackVolume1.get(),
         BoundarySurfaceFace::positiveFaceYZ);
 
     trackVolume1->glueTrackingVolume(
-        geoContext, BoundarySurfaceFace::positiveFaceYZ, trackVolume2,
+        geoContext, BoundarySurfaceFace::positiveFaceYZ, trackVolume2.get(),
         BoundarySurfaceFace::negativeFaceYZ);
 
     // Build world volume
diff --git a/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CylindricalTrackingGeometry.hpp b/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CylindricalTrackingGeometry.hpp
index 065b046e7..16c7d2359 100644
--- a/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CylindricalTrackingGeometry.hpp
+++ b/Tests/Core/CommonHelpers/include/Acts/Tests/CommonHelpers/CylindricalTrackingGeometry.hpp
@@ -231,7 +231,7 @@ struct CylindricalTrackingGeometry {
     // create the Tracking volume
     auto pVolume = TrackingVolume::create(nullptr, pVolumeBounds, nullptr,
                                           std::move(pLayerArray), nullptr,
-                                          "Pixel::Barrel");
+                                          {}, "Pixel::Barrel");
 
     // The combined volume
     auto detectorVolume = cylinderVolumeHelper->createContainerTrackingVolume(
diff --git a/Tests/Core/Geometry/CuboidVolumeBuilderTests.cpp b/Tests/Core/Geometry/CuboidVolumeBuilderTests.cpp
index 81970d55a..0e5fe97e2 100644
--- a/Tests/Core/Geometry/CuboidVolumeBuilderTests.cpp
+++ b/Tests/Core/Geometry/CuboidVolumeBuilderTests.cpp
@@ -268,6 +268,10 @@ namespace Test {
     // Production factory
     CuboidVolumeBuilder cvb;
 
+    // Create a test context
+    GeometryContext tgContext = GeometryContext();
+    MagneticFieldContext mfContext = MagneticFieldContext();
+    
     // Build a volume that confines another volume
     CuboidVolumeBuilder::VolumeConfig vCfg;
     vCfg.position = {1. * units::_m, 0., 0.};
@@ -295,23 +299,25 @@ namespace Test {
     cvb.setConfig(config);
     TrackingGeometryBuilder::Config tgbCfg;
     tgbCfg.trackingVolumeBuilders.push_back(
-        std::make_shared<const CuboidVolumeBuilder>(cvb));
+        [=](const auto& context, const auto& inner, const auto& vb) {
+          return cvb.trackingVolume(context, inner, vb);
+        });
     TrackingGeometryBuilder                 tgb(tgbCfg);
-    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry();
+    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry(tgContext);
 
     // Test that the right volume is selected
     BOOST_TEST(
-        detector->lowestTrackingVolume({1. * units::_m, 0., 0.})->volumeName()
+        detector->lowestTrackingVolume(tgContext, {1. * units::_m, 0., 0.})->volumeName()
         == vCfg.name);
     BOOST_TEST(
-        detector->lowestTrackingVolume({1.1 * units::_m, 0., 0.})->volumeName()
+        detector->lowestTrackingVolume(tgContext, {1.1 * units::_m, 0., 0.})->volumeName()
         == cvCfg1.name);
     BOOST_TEST(
-        detector->lowestTrackingVolume({0.9 * units::_m, 0., 0.})->volumeName()
+        detector->lowestTrackingVolume(tgContext, {0.9 * units::_m, 0., 0.})->volumeName()
         == cvCfg2.name);
 
     // Set propagator and navigator
-    PropagatorOptions<ActionList<StepVolumeCollector>> propOpts;
+    PropagatorOptions<ActionList<StepVolumeCollector>> propOpts(tgContext, mfContext);
     propOpts.maxStepSize = 10. * units::_mm;
     StraightLineStepper sls;
     Navigator           navi(detector);
@@ -327,7 +333,7 @@ namespace Test {
         nullptr, startParams, startMom, 1.);
 
     // Launch and collect results
-    const auto& result = prop.propagate(sbtp, propOpts);
+    const auto& result = prop.propagate(sbtp, propOpts).value();
     const StepVolumeCollector::this_result& stepResult
         = result.get<typename StepVolumeCollector::result_type>();
 
@@ -360,6 +366,10 @@ namespace Test {
     // Production factory
     CuboidVolumeBuilder cvb;
 
+    // Create a test context
+    GeometryContext tgContext = GeometryContext();
+    MagneticFieldContext mfContext = MagneticFieldContext();
+      
     // Build a volume that confines another volume
     CuboidVolumeBuilder::VolumeConfig vCfg1;
     vCfg1.position = {1. * units::_m, 0., 0.};
@@ -408,12 +418,14 @@ namespace Test {
     cvb.setConfig(config);
     TrackingGeometryBuilder::Config tgbCfg;
     tgbCfg.trackingVolumeBuilders.push_back(
-        std::make_shared<const CuboidVolumeBuilder>(cvb));
+        [=](const auto& context, const auto& inner, const auto& vb) {
+          return cvb.trackingVolume(context, inner, vb);
+        });
     TrackingGeometryBuilder                 tgb(tgbCfg);
-    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry();
+    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry(tgContext);
 
     // Set propagator and navigator
-    PropagatorOptions<ActionList<StepVolumeCollector>> propOpts;
+    PropagatorOptions<ActionList<StepVolumeCollector>> propOpts(tgContext, mfContext);
     propOpts.maxStepSize = 10. * units::_mm;
     StraightLineStepper sls;
     Navigator           navi(detector);
@@ -429,7 +441,7 @@ namespace Test {
         nullptr, startParams, startMom, 1.);
 
     // Launch and collect results
-    const auto& result = prop.propagate(sbtp, propOpts);
+    const auto& result = prop.propagate(sbtp, propOpts).value();
     const StepVolumeCollector::this_result& stepResult
         = result.get<typename StepVolumeCollector::result_type>();
 
diff --git a/Tests/Core/Geometry/TrackingVolumeCreation.hpp b/Tests/Core/Geometry/TrackingVolumeCreation.hpp
index c38327ebe..06a76eff5 100644
--- a/Tests/Core/Geometry/TrackingVolumeCreation.hpp
+++ b/Tests/Core/Geometry/TrackingVolumeCreation.hpp
@@ -78,7 +78,7 @@ TrackingVolumePtr constructCylinderVolume(
       innerVolumeR, outerVolumeR, bUmax + volumeEnvelope);
 
   TrackingVolumePtr volume = TrackingVolume::create(
-      nullptr, volumeBounds, nullptr, std::move(layerArray), nullptr, name);
+      nullptr, volumeBounds, nullptr, std::move(layerArray), nullptr, {}, name);
   ///  return the volume
   return volume;
 }
diff --git a/Tests/Core/Propagator/StepperTests.cpp b/Tests/Core/Propagator/StepperTests.cpp
index 1ec3d942e..0484db821 100644
--- a/Tests/Core/Propagator/StepperTests.cpp
+++ b/Tests/Core/Propagator/StepperTests.cpp
@@ -30,6 +30,7 @@
 #include "Acts/Utilities/Definitions.hpp"
 #include "Acts/Geometry/GeometryContext.hpp"
 #include "Acts/MagneticField/MagneticFieldContext.hpp"
+#include "Acts/Surfaces/RectangleBounds.hpp"
 
 namespace tt = boost::test_tools;
 using namespace Acts::UnitLiterals;
@@ -689,9 +690,11 @@ BOOST_AUTO_TEST_CASE(step_extension_vacmatvac_test) {
     cvb.setConfig(conf);
     TrackingGeometryBuilder::Config tgbCfg;
     tgbCfg.trackingVolumeBuilders.push_back(
-        std::make_shared<const CuboidVolumeBuilder>(cvb));
+        [=](const auto& context, const auto& inner, const auto& vb) {
+          return cvb.trackingVolume(context, inner, vb);
+        });
     TrackingGeometryBuilder                 tgb(tgbCfg);
-    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry();
+    std::shared_ptr<const TrackingGeometry> detector = tgb.trackingGeometry(tgContext);
 
     // Build navigator
     Navigator naviVac(detector);
@@ -709,7 +712,7 @@ BOOST_AUTO_TEST_CASE(step_extension_vacmatvac_test) {
     // Set options for propagator
     DenseStepperPropagatorOptions<ActionList<StepCollector, MaterialInteractor>,
                                   AbortList<EndOfWorld>>
-        propOpts;
+        propOpts(tgContext, mfContext);
     propOpts.abortList.get<EndOfWorld>().maxX = 3. * units::_m;
 
     // Build stepper and propagator
@@ -729,7 +732,7 @@ BOOST_AUTO_TEST_CASE(step_extension_vacmatvac_test) {
         prop(es, naviVac);
 
     // Launch and collect results
-    const auto&                       result = prop.propagate(sbtp, propOpts);
+    const auto&                       result = prop.propagate(sbtp, propOpts).value();
     const StepCollector::this_result& stepResult
         = result.get<typename StepCollector::result_type>();
 
-- 
GitLab