From 189b8a1609dc2097b6be0e3f8b9c13c311ad988c Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Fri, 12 Jun 2020 23:20:54 +0800
Subject: [PATCH] WIP: using SD tool to create a SD for calorimeter.

---
 .../DetSimGeom/src/AnExampleDetElemTool.cpp   | 21 ++++++++++++++++++-
 .../DetSimGeom/src/AnExampleDetElemTool.h     |  7 +++++--
 Simulation/DetSimSD/CMakeLists.txt            |  1 +
 .../DetSimSD/src/CalorimeterSensDetTool.cpp   | 18 +++++++++++++++-
 .../DetSimSD/src/CalorimeterSensDetTool.h     |  3 +++
 5 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
index 1df29b0c..bca46749 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
@@ -92,7 +92,26 @@ AnExampleDetElemTool::ConstructSDandField() {
                << endmsg;
         // continue;
         // Sensitive detectors are deleted in ~G4SDManager
-        G4VSensitiveDetector* g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
+        G4VSensitiveDetector* g4sd = nullptr;
+
+        // try to use SD tool to find the SD
+        if (!g4sd) {
+            if (typ=="calorimeter") {
+                m_calo_sdtool = ToolHandle<ISensDetTool>("CalorimeterSensDetTool");
+                if (m_calo_sdtool) {
+                    info() << "Find the CalorimeterSensDetTool." << endmsg;
+                    g4sd = m_calo_sdtool->createSD(nam);
+                    info() << "create g4SD: " << g4sd << endmsg;
+                }
+            } else if (typ=="tracker") {
+
+            }
+        }
+        
+        if (!g4sd) {
+            g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
+        }
+
         if (g4sd == nullptr) {
             std::string tmp = typ;
             tmp[0] = ::toupper(tmp[0]);
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
index 7c922dad..b0b22861 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.h
@@ -3,11 +3,14 @@
 
 #include "GaudiKernel/AlgTool.h"
 #include "GaudiKernel/Property.h"
-#include "DetSimInterface/IDetElemTool.h"
+#include <GaudiKernel/ToolHandle.h>
+
 #include "G4SystemOfUnits.hh"
 #include "G4PhysicalConstants.hh"
 
 #include "DetInterface/IGeoSvc.h"
+#include "DetSimInterface/IDetElemTool.h"
+#include "DetSimInterface/ISensDetTool.h"
 
 
 class AnExampleDetElemTool: public extends<AlgTool, IDetElemTool> {
@@ -29,7 +32,7 @@ private:
     Gaudi::Property<std::string> m_dd4hep_xmls{this, "detxml"};
 
     SmartIF<IGeoSvc> m_geosvc;
-
+    ToolHandle<ISensDetTool> m_calo_sdtool;
 };
 
 #endif
diff --git a/Simulation/DetSimSD/CMakeLists.txt b/Simulation/DetSimSD/CMakeLists.txt
index 12ca0282..f982aaec 100644
--- a/Simulation/DetSimSD/CMakeLists.txt
+++ b/Simulation/DetSimSD/CMakeLists.txt
@@ -3,6 +3,7 @@ gaudi_subdir(DetSimSD v0r0)
 
 gaudi_depends_on_subdirs(
     FWCore
+    Detector/DetInterface
     Simulation/DetSimInterface
 )
 
diff --git a/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp b/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp
index 35e0dcb2..55bed90d 100644
--- a/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp
+++ b/Simulation/DetSimSD/src/CalorimeterSensDetTool.cpp
@@ -2,12 +2,24 @@
 
 #include "G4VSensitiveDetector.hh"
 
+#include "DetSimSD/DDG4SensitiveDetector.h"
+
+#include "DD4hep/Detector.h"
+
 DECLARE_COMPONENT(CalorimeterSensDetTool);
 
 StatusCode
 CalorimeterSensDetTool::initialize() {
     StatusCode sc;
 
+
+    m_geosvc = service<IGeoSvc>("GeoSvc");
+    if (!m_geosvc) {
+        error() << "Failed to find GeoSvc." << endmsg;
+        return StatusCode::FAILURE;
+    }
+
+
     return sc;
 }
 
@@ -21,7 +33,11 @@ CalorimeterSensDetTool::finalize() {
 G4VSensitiveDetector*
 CalorimeterSensDetTool::createSD(const std::string& name) {
 
-    return nullptr;
+    dd4hep::Detector* dd4hep_geo = m_geosvc->lcdd();
+
+    G4VSensitiveDetector* sd = new DDG4SensitiveDetector(name, *dd4hep_geo);
+
+    return sd;
 }
 
 
diff --git a/Simulation/DetSimSD/src/CalorimeterSensDetTool.h b/Simulation/DetSimSD/src/CalorimeterSensDetTool.h
index 5ecaf8c1..775d2451 100644
--- a/Simulation/DetSimSD/src/CalorimeterSensDetTool.h
+++ b/Simulation/DetSimSD/src/CalorimeterSensDetTool.h
@@ -9,6 +9,7 @@
 
 #include "GaudiKernel/AlgTool.h"
 #include "DetSimInterface/ISensDetTool.h"
+#include "DetInterface/IGeoSvc.h"
 
 class CalorimeterSensDetTool: public extends<AlgTool, ISensDetTool> {
 
@@ -25,6 +26,8 @@ public:
 
 private:
 
+    // in order to initialize SD, we need to get the lcdd()
+    SmartIF<IGeoSvc> m_geosvc;
 
 };
 
-- 
GitLab