From d2c3478a5acb362a29de971a34a4df925dbe47ec Mon Sep 17 00:00:00 2001
From: Andreas Salzburger <Andreas.Salzburger@cern.ch>
Date: Thu, 4 Jul 2019 14:43:13 +0200
Subject: [PATCH] synchronise root/json writer

---
 .../src/detail/GeometryExampleBase.hpp        | 17 ++--
 Plugins/Json/src/JsonGeometryConverter.cpp    |  2 +-
 .../ACTFW/Plugins/Root/RootMaterialWriter.hpp | 41 ++++++++++
 Plugins/Root/src/RootMaterialWriter.cpp       | 82 ++++++++++++++++++-
 external/acts-core                            |  2 +-
 5 files changed, 132 insertions(+), 12 deletions(-)

diff --git a/Examples/Geometry/src/detail/GeometryExampleBase.hpp b/Examples/Geometry/src/detail/GeometryExampleBase.hpp
index b8b9aadd..6afaba2b 100644
--- a/Examples/Geometry/src/detail/GeometryExampleBase.hpp
+++ b/Examples/Geometry/src/detail/GeometryExampleBase.hpp
@@ -183,15 +183,14 @@ processGeometry(int               argc,
     // Get the file name from the options
     std::string materialFileName = vm["mat-output-file"].as<std::string>();
 
-    // if (!materialFileName.empty() and vm["output-root"].template as<bool>())
-    // {
-    //
-    //  // The writer of the indexed material
-    //  FW::Root::RootMaterialWriter::Config rmwConfig("MaterialWriter");
-    //  rmwConfig.fileName = materialFileName + ".root";
-    //  FW::Root::RootMaterialWriter rmwImpl(rmwConfig);
-    //
-    //}
+    if (!materialFileName.empty() and vm["output-root"].template as<bool>()) {
+
+      // The writer of the indexed material
+      FW::Root::RootMaterialWriter::Config rmwConfig("MaterialWriter");
+      rmwConfig.fileName = materialFileName + ".root";
+      FW::Root::RootMaterialWriter rmwImpl(rmwConfig);
+      rmwImpl.write(*tGeometry);
+    }
 
     if (!materialFileName.empty() and vm["output-json"].template as<bool>()) {
       /// The name of the output file
diff --git a/Plugins/Json/src/JsonGeometryConverter.cpp b/Plugins/Json/src/JsonGeometryConverter.cpp
index 4972cfcb..ea8108aa 100644
--- a/Plugins/Json/src/JsonGeometryConverter.cpp
+++ b/Plugins/Json/src/JsonGeometryConverter.cpp
@@ -321,7 +321,7 @@ FW::Json::JsonGeometryConverter::convertToRep(
   }
   // write the material if there's one
   if (tVolume.volumeMaterial() != nullptr) {
-    volRep.material = tVolume.volumeMaterial().get();
+    volRep.material = tVolume.volumeMaterial();
   }
   // there are confied layers
   if (tVolume.confinedLayers()) {
diff --git a/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialWriter.hpp b/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialWriter.hpp
index c8140c5e..88bd721b 100644
--- a/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialWriter.hpp
+++ b/Plugins/Root/include/ACTFW/Plugins/Root/RootMaterialWriter.hpp
@@ -15,9 +15,12 @@
 #include <map>
 #include <mutex>
 #include "ACTFW/Framework/ProcessCode.hpp"
+#include "Acts/Detector/TrackingGeometry.hpp"
+#include "Acts/Detector/TrackingVolume.hpp"
 #include "Acts/Material/IMaterialDecorator.hpp"
 #include "Acts/Material/ISurfaceMaterial.hpp"
 #include "Acts/Material/IVolumeMaterial.hpp"
+#include "Acts/Surfaces/Surface.hpp"
 #include "Acts/Utilities/Definitions.hpp"
 #include "Acts/Utilities/GeometryID.hpp"
 #include "Acts/Utilities/Logger.hpp"
@@ -51,6 +54,22 @@ namespace Root {
     /// Configuration of the Writer
     struct Config
     {
+
+      /// Steering to handle sensitive data
+      bool processSensitives = true;
+
+      /// Steering to handle approach data
+      bool processApproaches = true;
+
+      /// Steering to handle representing data
+      bool processRepresenting = true;
+
+      /// Steering to handle boundary data
+      bool processBoundaries = true;
+
+      /// Steering to handle volume data
+      bool processVolumes = true;
+
       /// The name of the output tree
       std::string folderNameBase = "Material";
       /// The volume identification string
@@ -115,7 +134,29 @@ namespace Root {
     void
     write(const Acts::DetectorMaterialMaps& detMaterial);
 
+    /// Write out the material map from Geometry
+    ///
+    /// @param tGeometry is the TrackingGeometry
+    void
+    write(const Acts::TrackingGeometry& tGeometry);
+
   private:
+    /// Collect the material from the tracking geometry
+    ///
+    /// @param tVolume The TrackingVolume for the material to be collected
+    /// @param [in,out] detMatMap the map to be filled
+    void
+    collectMaterial(const Acts::TrackingVolume& tVolume,
+                    Acts::DetectorMaterialMaps& detMatMap);
+
+    /// Collect the material from the tracking geometry
+    ///
+    /// @param tLayer The TrackingVolume for the material to be collected
+    /// @param [in,out] detMatMap the map to be filled
+    void
+    collectMaterial(const Acts::Layer&          tLayer,
+                    Acts::DetectorMaterialMaps& detMatMap);
+
     /// The config class
     Config m_cfg;
 
diff --git a/Plugins/Root/src/RootMaterialWriter.cpp b/Plugins/Root/src/RootMaterialWriter.cpp
index c1db267b..a00523d4 100644
--- a/Plugins/Root/src/RootMaterialWriter.cpp
+++ b/Plugins/Root/src/RootMaterialWriter.cpp
@@ -73,7 +73,7 @@ FW::Root::RootMaterialWriter::write(
     m_outputFile->mkdir(tdName.c_str());
     m_outputFile->cd(tdName.c_str());
 
-    ACTS_INFO("Writing out map at " << tdName);
+    ACTS_VERBOSE("Writing out map at " << tdName);
 
     size_t bins0 = 1, bins1 = 1;
     // understand what sort of material you have in mind
@@ -214,3 +214,83 @@ FW::Root::RootMaterialWriter::write(
     rho->Write();
   }
 }
+
+void
+FW::Root::RootMaterialWriter::write(const Acts::TrackingGeometry& tGeometry)
+{
+  // Create a detector material map and loop recursively through it
+  Acts::DetectorMaterialMaps detMatMap;
+  auto                       hVolume = tGeometry.highestTrackingVolume();
+  if (hVolume != nullptr) {
+    collectMaterial(*hVolume, detMatMap);
+  }
+  // Write the resulting map to the file
+  write(detMatMap);
+}
+
+void
+FW::Root::RootMaterialWriter::collectMaterial(
+    const Acts::TrackingVolume& tVolume,
+    Acts::DetectorMaterialMaps& detMatMap)
+{
+
+  // If the volume has volume material, write that
+  if (tVolume.volumeMaterialPtr() != nullptr and m_cfg.processVolumes) {
+    detMatMap.second[tVolume.geoID()] = tVolume.volumeMaterialPtr();
+  }
+
+  // If confined layers exist, loop over them and collect the layer material
+  if (tVolume.confinedLayers() != nullptr) {
+    for (auto& lay : tVolume.confinedLayers()->arrayObjects()) {
+      collectMaterial(*lay, detMatMap);
+    }
+  }
+
+  // If any of the boundary surfaces has material collect that
+  if (m_cfg.processBoundaries) {
+    for (auto& bou : tVolume.boundarySurfaces()) {
+      const auto& bSurface = bou->surfaceRepresentation();
+      if (bSurface.surfaceMaterialPtr() != nullptr) {
+        detMatMap.first[bSurface.geoID()] = bSurface.surfaceMaterialPtr();
+      }
+    }
+  }
+
+  // If the volume has sub volumes, step down
+  if (tVolume.confinedVolumes() != nullptr) {
+    for (auto& tvol : tVolume.confinedVolumes()->arrayObjects()) {
+      collectMaterial(*tvol, detMatMap);
+    }
+  }
+}
+
+void
+FW::Root::RootMaterialWriter::collectMaterial(
+    const Acts::Layer&          tLayer,
+    Acts::DetectorMaterialMaps& detMatMap)
+{
+  // If the representing surface has material, collect it
+  const auto& rSurface = tLayer.surfaceRepresentation();
+  if (rSurface.surfaceMaterialPtr() != nullptr and m_cfg.processRepresenting) {
+    detMatMap.first[rSurface.geoID()] = rSurface.surfaceMaterialPtr();
+  }
+
+  // Check the approach surfaces
+  if (tLayer.approachDescriptor() != nullptr and m_cfg.processApproaches) {
+    for (auto& aSurface : tLayer.approachDescriptor()->containedSurfaces()) {
+      if (aSurface->surfaceMaterialPtr() != nullptr) {
+        detMatMap.first[aSurface->geoID()] = aSurface->surfaceMaterialPtr();
+      }
+    }
+  }
+
+  // Check the sensitive surfaces
+  if (tLayer.surfaceArray() != nullptr and m_cfg.processSensitives) {
+    // sensitive surface loop
+    for (auto& sSurface : tLayer.surfaceArray()->surfaces()) {
+      if (sSurface->surfaceMaterialPtr() != nullptr) {
+        detMatMap.first[sSurface->geoID()] = sSurface->surfaceMaterialPtr();
+      }
+    }
+  }
+}
diff --git a/external/acts-core b/external/acts-core
index a8f31303..19d57d96 160000
--- a/external/acts-core
+++ b/external/acts-core
@@ -1 +1 @@
-Subproject commit a8f31303ee8720ed2946bfe2d59e81d0f70e307e
+Subproject commit 19d57d9646a3c5ad89a55ed7f6a8fe355e77d63a
-- 
GitLab