From eb901fd75c6fcf6bdcce5872a2736dda93e8e660 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Sat, 9 Nov 2019 21:46:31 +0800
Subject: [PATCH] WIP: implement the GeoSvc based on DD4hep.

---
 Detector/DetInterface/DetInterface/IGeoSvc.h | 11 +++--
 Detector/GeoSvc/CMakeLists.txt               | 24 ++++++++++
 Detector/GeoSvc/src/GeoSvc.cpp               | 48 ++++++++++++++++++++
 Detector/GeoSvc/src/GeoSvc.h                 | 41 +++++++++++++++++
 4 files changed, 119 insertions(+), 5 deletions(-)
 create mode 100644 Detector/GeoSvc/CMakeLists.txt
 create mode 100644 Detector/GeoSvc/src/GeoSvc.cpp
 create mode 100644 Detector/GeoSvc/src/GeoSvc.h

diff --git a/Detector/DetInterface/DetInterface/IGeoSvc.h b/Detector/DetInterface/DetInterface/IGeoSvc.h
index 02db1bfa..58df046d 100644
--- a/Detector/DetInterface/DetInterface/IGeoSvc.h
+++ b/Detector/DetInterface/DetInterface/IGeoSvc.h
@@ -1,9 +1,10 @@
 //
 //  IGeoSvc.h
 //
-//  Based on FCCSW.
-//  Created by Julia Hrdinka on 30/03/15.
-//
+//  Based on FCCSW with some modification.
+//  In the design, the geometry shoud only depends on DD4hep. 
+//  
+//  -- Tao Lin, 2019/11/09
 //
 
 #ifndef IGEOSVC_H
@@ -16,7 +17,7 @@ class Detector;
 class DetElement;
 }
 
-class G4VUserDetectorConstruction;
+// class G4VUserDetectorConstruction;
 
 class GAUDI_API IGeoSvc : virtual public IService {
 
@@ -27,7 +28,7 @@ public:
   virtual dd4hep::DetElement getDD4HepGeo() = 0;
   virtual dd4hep::Detector* lcdd() = 0;
   // receive Geant4 Geometry
-  virtual G4VUserDetectorConstruction* getGeant4Geo() = 0;
+  // virtual G4VUserDetectorConstruction* getGeant4Geo() = 0;
 
   virtual ~IGeoSvc() {}
 };
diff --git a/Detector/GeoSvc/CMakeLists.txt b/Detector/GeoSvc/CMakeLists.txt
new file mode 100644
index 00000000..569c8533
--- /dev/null
+++ b/Detector/GeoSvc/CMakeLists.txt
@@ -0,0 +1,24 @@
+
+##############################################################################
+# Package: GeoSvc
+#    Desc: implement the IGeoSvc interface.
+##############################################################################
+
+gaudi_subdir(GeoSvc v0r0)
+
+gaudi_depends_on_subdirs(
+    FWCore
+    Detector/DetInterface
+)
+
+
+find_package(ROOT COMPONENTS MathCore GenVector Geom Tree)
+find_package(DD4hep COMPONENTS DDG4 DDRec REQUIRED)
+
+gaudi_add_module(GeoSvc
+                 src/GeoSvc.cpp
+                 INCLUDE_DIRS
+                   DetInterface DD4hep GaudiKernel ROOT 
+                 LINK_LIBRARIES
+                   DD4hep ${DD4hep_COMPONENT_LIBRARIES} GaudiKernel ROOT
+)
\ No newline at end of file
diff --git a/Detector/GeoSvc/src/GeoSvc.cpp b/Detector/GeoSvc/src/GeoSvc.cpp
new file mode 100644
index 00000000..305896c4
--- /dev/null
+++ b/Detector/GeoSvc/src/GeoSvc.cpp
@@ -0,0 +1,48 @@
+#include "GeoSvc.h"
+
+#include "DD4hep/Detector.h"
+#include "DD4hep/Plugins.h"
+#include "DDG4/Geant4Converter.h"
+#include "DDG4/Geant4Mapping.h"
+
+DECLARE_COMPONENT(GeoSvc)
+
+GeoSvc::GeoSvc(const std::string& name, ISvcLocator* svc)
+: base_class(name, svc), m_dd4hep_geo(nullptr) {
+
+}
+
+GeoSvc::~GeoSvc() {
+
+}
+
+StatusCode
+GeoSvc::initialize() {
+    StatusCode sc = Service::initialize();
+
+    m_dd4hep_geo = &(dd4hep::Detector::getInstance());
+    // if failed to load the compact, a runtime error will be thrown.
+    m_dd4hep_geo->fromCompact(m_dd4hep_xmls.value());
+
+    return sc;
+}
+
+StatusCode
+GeoSvc::finalize() {
+    StatusCode sc;
+
+    return sc;
+}
+
+dd4hep::DetElement
+GeoSvc::getDD4HepGeo() {
+    if (lcdd()) {
+        return lcdd()->world();
+    }
+    return dd4hep::DetElement();
+}
+
+dd4hep::Detector*
+GeoSvc::lcdd() {
+    return m_dd4hep_geo;
+}
diff --git a/Detector/GeoSvc/src/GeoSvc.h b/Detector/GeoSvc/src/GeoSvc.h
new file mode 100644
index 00000000..9c98daa9
--- /dev/null
+++ b/Detector/GeoSvc/src/GeoSvc.h
@@ -0,0 +1,41 @@
+#ifndef GeoSvc_h
+#define GeoSvc_h
+
+// Interface
+#include "DetInterface/IGeoSvc.h"
+
+// Gaudi
+#include "GaudiKernel/IIncidentListener.h"
+#include "GaudiKernel/IIncidentSvc.h"
+#include "GaudiKernel/Incident.h"
+#include "GaudiKernel/MsgStream.h"
+#include "GaudiKernel/Service.h"
+#include "GaudiKernel/ServiceHandle.h"
+
+// DD4Hep
+#include "DD4hep/Detector.h"
+
+class GeoSvc: public extends<Service, IGeoSvc> {
+public:
+    GeoSvc(const std::string& name, ISvcLocator* svc);
+    ~GeoSvc();
+
+    // Service
+    StatusCode initialize() override;
+    StatusCode finalize() override;
+
+    // IGeoSvc
+    dd4hep::DetElement getDD4HepGeo() override;
+    dd4hep::Detector* lcdd() override;
+
+private:
+
+    // DD4hep XML compact file path
+    Gaudi::Property<std::string> m_dd4hep_xmls{this, "compact"};
+
+    // 
+    dd4hep::Detector* m_dd4hep_geo;
+};
+
+
+#endif GeoSvc_h
-- 
GitLab