From f876c6bd8f6f9a8a40e3bcc81f46dad6b226e1d0 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Sat, 9 Nov 2019 22:11:59 +0800
Subject: [PATCH] WIP: accessing dd4hep lcdd uses the geosvc.

---
 Detector/DetCEPCv4/compact/vxd07.xml          |  2 +-
 Examples/options/tut_detsim.py                | 29 ++++++++++++++-----
 Simulation/DetSimGeom/CMakeLists.txt          |  3 +-
 .../DetSimGeom/src/AnExampleDetElemTool.cpp   | 20 ++++++++++---
 .../DetSimGeom/src/AnExampleDetElemTool.h     |  5 ++++
 5 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/Detector/DetCEPCv4/compact/vxd07.xml b/Detector/DetCEPCv4/compact/vxd07.xml
index 48490eec..d37a2731 100644
--- a/Detector/DetCEPCv4/compact/vxd07.xml
+++ b/Detector/DetCEPCv4/compact/vxd07.xml
@@ -93,7 +93,7 @@
   <readouts>
     <readout name="VXDCollection">
       <!-- fixme: for now DD4hep cannot handle signed values - side should actually be "-2" -->
-      <id>system:5,side:2,layer:9,module:8,sensor:8</id>
+      <id>system:5,side:-2,layer:9,module:8,sensor:8</id>
     </readout>
   </readouts>
 
diff --git a/Examples/options/tut_detsim.py b/Examples/options/tut_detsim.py
index 89a2618d..516a0075 100644
--- a/Examples/options/tut_detsim.py
+++ b/Examples/options/tut_detsim.py
@@ -13,6 +13,27 @@ from Gaudi.Configuration import *
 from Configurables import CEPCDataSvc
 dsvc = CEPCDataSvc("EventDataSvc")
 
+
+##############################################################################
+# Geometry Svc
+##############################################################################
+
+# geometry_option = "CepC_v4-onlyTracker.xml"
+geometry_option = "CepC_v4-onlyVXD.xml"
+
+if not os.getenv("DETCEPCV4ROOT"):
+    print("Can't find the geometry. Please setup envvar DETCEPCV4ROOT." )
+    sys.exit(-1)
+
+geometry_path = os.path.join(os.getenv("DETCEPCV4ROOT"), "compact", geometry_option)
+if not os.path.exists(geometry_path):
+    print("Can't find the compact geometry file: %s"%geometry_path)
+    sys.exit(-1)
+
+from Configurables import GeoSvc
+geosvc = GeoSvc("GeoSvc")
+geosvc.compact = geometry_path
+
 ##############################################################################
 # Physics Generator
 ##############################################################################
@@ -51,14 +72,6 @@ detsimalg.RootDetElem = "WorldDetElemTool"
 from Configurables import AnExampleDetElemTool
 example_dettool = AnExampleDetElemTool("AnExampleDetElemTool")
 
-# geometry_option = "CepC_v4-onlyTracker.xml"
-geometry_option = "CepC_v4-onlyVXD.xml"
-
-if os.getenv("DETCEPCV4ROOT"):
-    example_dettool.detxml = os.path.join(os.getenv("DETCEPCV4ROOT"), "compact", geometry_option)
-else:
-    print("Can't find the geometry. Please setup envvar DETCEPCV4ROOT." )
-    sys.exit(-1)
 
 ##############################################################################
 # POD I/O
diff --git a/Simulation/DetSimGeom/CMakeLists.txt b/Simulation/DetSimGeom/CMakeLists.txt
index f546652d..fa2c4401 100644
--- a/Simulation/DetSimGeom/CMakeLists.txt
+++ b/Simulation/DetSimGeom/CMakeLists.txt
@@ -3,6 +3,7 @@ gaudi_subdir(DetSimGeom v0r0)
 
 gaudi_depends_on_subdirs(
     Simulation/DetSimInterface
+    Detector/DetInterface
 )
 
 find_package(Geant4 REQUIRED ui_all vis_all)
@@ -15,6 +16,6 @@ set(DetSimGeom_srcs
 )
 
 gaudi_add_module(DetSimGeom ${DetSimGeom_srcs}
-    INCLUDE_DIRS DetSimInterface DD4hep GaudiKernel Geant4
+    INCLUDE_DIRS DetSimInterface DetInterface 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 0f0a064d..8ba60b44 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
@@ -24,6 +24,7 @@
 #include "DDG4/Geant4Converter.h"
 #include "DDG4/Geant4Mapping.h"
 
+
 DECLARE_COMPONENT(AnExampleDetElemTool)
 
 G4LogicalVolume*
@@ -35,9 +36,13 @@ AnExampleDetElemTool::getLV() {
     // 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::Detector* dd4hep_geo = &(dd4hep::Detector::getInstance());
+    // dd4hep_geo->fromCompact(m_dd4hep_xmls.value());
+    // dd4hep::DetElement world = dd4hep_geo->world();
+
+    dd4hep::Detector* dd4hep_geo = m_geosvc->lcdd();
+    dd4hep::DetElement world = m_geosvc->getDD4HepGeo();
+
     dd4hep::sim::Geant4Converter conv((*dd4hep_geo), dd4hep::DEBUG);
 
     dd4hep::sim::Geant4GeometryInfo* geo_info = conv.create(world).detach();
@@ -76,7 +81,7 @@ AnExampleDetElemTool::ConstructSDandField() {
     dd4hep::sim::Geant4GeometryInfo* p = dd4hep::sim::Geant4Mapping::instance().ptr();
     _SV& vols = p->sensitives;
 
-    auto lcdd = &(dd4hep::Detector::getInstance());
+    auto lcdd = m_geosvc->lcdd();
 
     for (_SV::const_iterator iv = vols.begin(); iv != vols.end(); ++iv) {
         dd4hep::SensitiveDetector sd = (*iv).first;
@@ -125,6 +130,13 @@ AnExampleDetElemTool::ConstructSDandField() {
 StatusCode
 AnExampleDetElemTool::initialize() {
     StatusCode sc;
+
+    m_geosvc = service<IGeoSvc>("GeoSvc");
+    if (!m_geosvc) {
+        error() << "Failed to find GeoSvc." << endmsg;
+        return StatusCode::FAILURE;
+    }
+
     return sc;
 }
 
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
index f742c486..7c922dad 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
@@ -7,6 +7,8 @@
 #include "G4SystemOfUnits.hh"
 #include "G4PhysicalConstants.hh"
 
+#include "DetInterface/IGeoSvc.h"
+
 
 class AnExampleDetElemTool: public extends<AlgTool, IDetElemTool> {
 
@@ -25,6 +27,9 @@ private:
     Gaudi::Property<double> m_z{this, "Z", 30.*m};
     // DD4hep XML compact file path
     Gaudi::Property<std::string> m_dd4hep_xmls{this, "detxml"};
+
+    SmartIF<IGeoSvc> m_geosvc;
+
 };
 
 #endif
-- 
GitLab