From 9a8205583edf36f3e8fb3f6cdea8ebdcb6b835ba Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Tue, 13 Aug 2019 11:11:19 +0800
Subject: [PATCH] WIP: construct logical volume from DD4hep and place the
 logical volume in mother volume.

---
 Simulation/DetSimGeom/CMakeLists.txt          |  5 ++--
 .../DetSimGeom/src/AnExampleDetElemTool.cpp   | 29 +++++++++++++++++++
 .../DetSimGeom/src/AnExampleDetElemTool.h     |  2 ++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/Simulation/DetSimGeom/CMakeLists.txt b/Simulation/DetSimGeom/CMakeLists.txt
index f35a12b7..f546652d 100644
--- a/Simulation/DetSimGeom/CMakeLists.txt
+++ b/Simulation/DetSimGeom/CMakeLists.txt
@@ -7,6 +7,7 @@ gaudi_depends_on_subdirs(
 
 find_package(Geant4 REQUIRED ui_all vis_all)
 include(${Geant4_USE_FILE})
+find_package(DD4hep COMPONENTS DDG4 REQUIRED)
 
 set(DetSimGeom_srcs
     src/WorldDetElemTool.cpp
@@ -14,6 +15,6 @@ set(DetSimGeom_srcs
 )
 
 gaudi_add_module(DetSimGeom ${DetSimGeom_srcs}
-    INCLUDE_DIRS DetSimInterface GaudiKernel Geant4
-    LINK_LIBRARIES DetSimInterface GaudiKernel Geant4
+    INCLUDE_DIRS DetSimInterface DD4hep GaudiKernel Geant4
+    LINK_LIBRARIES DetSimInterface DD4hep ${DD4hep_COMPONENT_LIBRARIES} GaudiKernel Geant4
 )
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
index e2b297fc..54dd53e9 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
@@ -19,6 +19,9 @@
 #include "G4PhysicalVolumeStore.hh"
 #include "G4OpticalSurface.hh"
 
+#include "DD4hep/Detector.h"
+#include "DDG4/Geant4Converter.h"
+#include "DDG4/Geant4Mapping.h"
 
 DECLARE_COMPONENT(AnExampleDetElemTool)
 
@@ -30,6 +33,32 @@ AnExampleDetElemTool::getLV() {
     G4VSolid* solidAnExample= new G4Box("sAnExample", m_x.value(), m_y.value(), m_z.value());
     G4LogicalVolume* logicAnExample= new G4LogicalVolume( solidAnExample, Galactic, "lAnExample", 0, 0, 0);
 
+    // Following is an example to get the DD4hep volume
+    dd4hep::Detector* dd4hep_geo = &(dd4hep::Detector::getInstance());
+    dd4hep_geo->fromCompact(m_dd4hep_xmls.value());
+    dd4hep::DetElement world = dd4hep_geo->world();
+    dd4hep::sim::Geant4Converter conv((*dd4hep_geo), dd4hep::DEBUG);
+
+    dd4hep::sim::Geant4GeometryInfo* geo_info = conv.create(world).detach();
+    dd4hep::sim::Geant4Mapping&  g4map = dd4hep::sim::Geant4Mapping::instance();
+    g4map.attach(geo_info);
+    // All volumes are deleted in ~G4PhysicalVolumeStore()
+    G4VPhysicalVolume* m_world = geo_info->world();
+    G4LogicalVolume* logicDD4hepExample = m_world->GetLogicalVolume();
+
+    if (logicDD4hepExample) {
+        new G4PVPlacement(0,                   // no rotation
+                          G4ThreeVector(),     // at (0,0,0)
+                          logicDD4hepExample,  // logical volume
+                          "lDD4hepExampleDetElem", // name
+                          logicAnExample,      // mother volume
+                          false,               // no boolean operations
+                          0);                  // no field
+    } else {
+        warning() << "Can't Find the logical volume lDD4hepExampleDetElem " << std::endl;
+    }
+
+
     return logicAnExample;
 }
 
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
index dcf4cae0..ed8089b4 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
@@ -22,6 +22,8 @@ private:
     Gaudi::Property<double> m_x{this, "X", 30.*m};
     Gaudi::Property<double> m_y{this, "Y", 30.*m};
     Gaudi::Property<double> m_z{this, "Z", 30.*m};
+    // DD4hep XML compact file path
+    Gaudi::Property<std::string> m_dd4hep_xmls{this, "detxml"};
 };
 
 #endif
-- 
GitLab