From f56be6665f2715a185350ed19d40c596755b1d54 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Fri, 12 Jun 2020 20:42:07 +0800
Subject: [PATCH] WIP: create ISensDetTool.h

---
 Detector/DetEcalMatrix/compact/det.xml        | 10 ++--
 .../src/calorimeter/EcalMatrix.cpp            | 46 +++++++++++--------
 Simulation/DetSimAna/CMakeLists.txt           |  3 ++
 .../DetSimGeom/src/AnExampleDetElemTool.cpp   |  2 +-
 .../DetSimInterface/ISensDetTool.h            | 28 +++++++++++
 5 files changed, 66 insertions(+), 23 deletions(-)
 create mode 100644 Simulation/DetSimInterface/DetSimInterface/ISensDetTool.h

diff --git a/Detector/DetEcalMatrix/compact/det.xml b/Detector/DetEcalMatrix/compact/det.xml
index 883478a8..462dbf7a 100644
--- a/Detector/DetEcalMatrix/compact/det.xml
+++ b/Detector/DetEcalMatrix/compact/det.xml
@@ -39,11 +39,13 @@
   
   <readouts>
     <readout name="CaloHitsCollection">
-      <segmentation type="CartesianGridXYZ" 
-                    grid_size_x="1*cm" 
-                    grid_size_y="1*cm" 
+      <!-- <segmentation type="NoSegmentation"/> -->
+
+      <segmentation type="CartesianGridXYZ"
+                    grid_size_x="1*cm"
+                    grid_size_y="1*cm"
                     grid_size_z="1*cm"/>
-      <id>system:8,x:-16,y:-16,z:-16</id>
+      <id>system:8,x:32:-6,y:-6,z:-6</id>
     </readout>
   </readouts>
 
diff --git a/Detector/DetEcalMatrix/src/calorimeter/EcalMatrix.cpp b/Detector/DetEcalMatrix/src/calorimeter/EcalMatrix.cpp
index a1ccded4..b835f023 100644
--- a/Detector/DetEcalMatrix/src/calorimeter/EcalMatrix.cpp
+++ b/Detector/DetEcalMatrix/src/calorimeter/EcalMatrix.cpp
@@ -59,23 +59,33 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
     std::cout<<"rmin="<<caloData->extent[0]<<",rmax="<<caloData->extent[1]<<",zmin="<<caloData->extent[2]<<",zmax="<<caloData->extent[3]<<std::endl;
     dd4hep::Readout readout = sens.readout();
     dd4hep::Segmentation seg = readout.segmentation();
-    double cellSize_x = ::atof( seg.segmentation()->parameter("grid_size_x")->value().c_str() ) * 10;// from cm to mm
-    double cellSize_y = ::atof( seg.segmentation()->parameter("grid_size_y")->value().c_str() ) * 10;// from cm to mm
-    double cellSize_z = ::atof( seg.segmentation()->parameter("grid_size_z")->value().c_str() ) * 10;// from cm to mm
-    int n_layer = int(2*dim.dx()*10/cellSize_x) ; // here the calorimeter is placed in barrel, so x direaction is layer direction
-    std::cout<<"cellx="<<cellSize_x<<",celly="<<cellSize_y<<",cellz="<<cellSize_z<<",dx="<<dim.dx()*10<<"mm,n_layer="<<n_layer<<std::endl;
-    for(int i=1 ; i <= n_layer; i++)
-    {
-        LayeredCalorimeterData::Layer caloLayer ;
-        caloLayer.distance = caloData->extent[0] + (i-0.5)*cellSize_x; //NEED TO START FROM ORIGIN, to mm
-        caloLayer.sensitive_thickness = cellSize_x ;
-        caloLayer.inner_thickness = cellSize_x ;
-        caloLayer.outer_thickness = cellSize_x ;
-        caloLayer.absorberThickness = cellSize_x;
-        caloLayer.cellSize0 = cellSize_y;
-        caloLayer.cellSize1 = cellSize_z;
-        caloData->layers.push_back(caloLayer); 
-    }
+
+    std::cout << "TAO: "
+              << " field description: " << seg.segmentation()->fieldDescription()
+              // << " grid size x/y/z: "
+              // << seg.segmentation()->parameter("grid_size_x")->toString()
+              // << seg.segmentation()->parameter("grid_size_x")->toString()
+              // << seg.segmentation()->parameter("grid_size_x")->toString()
+              << std::endl;
+
+
+    // double cellSize_x = ::atof( seg.segmentation()->parameter("grid_size_x")->value().c_str() ) * 10;// from cm to mm
+    // double cellSize_y = ::atof( seg.segmentation()->parameter("grid_size_y")->value().c_str() ) * 10;// from cm to mm
+    // double cellSize_z = ::atof( seg.segmentation()->parameter("grid_size_z")->value().c_str() ) * 10;// from cm to mm
+    // int n_layer = int(2*dim.dx()*10/cellSize_x) ; // here the calorimeter is placed in barrel, so x direaction is layer direction
+    // std::cout<<"cellx="<<cellSize_x<<",celly="<<cellSize_y<<",cellz="<<cellSize_z<<",dx="<<dim.dx()*10<<"mm,n_layer="<<n_layer<<std::endl;
+    // for(int i=1 ; i <= n_layer; i++)
+    // {
+    //     LayeredCalorimeterData::Layer caloLayer ;
+    //     caloLayer.distance = caloData->extent[0] + (i-0.5)*cellSize_x; //NEED TO START FROM ORIGIN, to mm
+    //     caloLayer.sensitive_thickness = cellSize_x ;
+    //     caloLayer.inner_thickness = cellSize_x ;
+    //     caloLayer.outer_thickness = cellSize_x ;
+    //     caloLayer.absorberThickness = cellSize_x;
+    //     caloLayer.cellSize0 = cellSize_y;
+    //     caloLayer.cellSize1 = cellSize_z;
+    //     caloData->layers.push_back(caloLayer); 
+    // }
 
     if ( x_det.isSensitive() )   {
         dd4hep::SensitiveDetector sd = sens;
@@ -86,7 +96,7 @@ static dd4hep::Ref_t create_detector(dd4hep::Detector& theDetector,
         phv.addPhysVolID("system",x_det.id());
     }
     sdet.setPlacement(phv);
-    sdet.addExtension< LayeredCalorimeterData >(caloData) ; 
+    // sdet.addExtension< LayeredCalorimeterData >(caloData) ; 
     MYDEBUG("create_detector DONE. ");
     return sdet;
 }
diff --git a/Simulation/DetSimAna/CMakeLists.txt b/Simulation/DetSimAna/CMakeLists.txt
index b43d75eb..e0c9aca5 100644
--- a/Simulation/DetSimAna/CMakeLists.txt
+++ b/Simulation/DetSimAna/CMakeLists.txt
@@ -36,6 +36,9 @@ gaudi_add_module(DetSimAna ${DetSimAna_srcs}
       ${DD4hep_COMPONENT_LIBRARIES} 
       GaudiKernel
       # Geant4
+
+      -Wl,--no-as-needed 
       EDM4HEP::edm4hep EDM4HEP::edm4hepDict
+      -Wl,--as-needed 
       # ${podio_LIBRARIES}
 )
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
index 8ba60b44..1df29b0c 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
@@ -96,7 +96,7 @@ AnExampleDetElemTool::ConstructSDandField() {
         if (g4sd == nullptr) {
             std::string tmp = typ;
             tmp[0] = ::toupper(tmp[0]);
-            typ = "Geant4" + tmp;
+            typ = "Geant4CEPC" + tmp;
             g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
             if (g4sd == nullptr) {
                 dd4hep::PluginDebug dbg;
diff --git a/Simulation/DetSimInterface/DetSimInterface/ISensDetTool.h b/Simulation/DetSimInterface/DetSimInterface/ISensDetTool.h
new file mode 100644
index 00000000..cb06d940
--- /dev/null
+++ b/Simulation/DetSimInterface/DetSimInterface/ISensDetTool.h
@@ -0,0 +1,28 @@
+#ifndef ISensDetTool_h
+#define ISensDetTool_h
+/*
+ * ISensDetTool is a tool to configure and create a Geant4's sensitive detector.
+ * After create the SD, the Geant4 will take ownership of the SD.
+ *
+ * This tool is used to replace the DDG4's Geant4SensitiveDetector.
+ * It will be invoked in ConstructSDandField().
+ *
+ * -- 12 June 2020, Tao Lin <lintao@ihep.ac.cn>
+ */
+
+#include "GaudiKernel/IAlgTool.h"
+
+class G4VSensitiveDetector;
+
+class ISensDetTool: virtual public IAlgTool {
+public:
+
+    DeclareInterfaceID(ISensDetTool, 0, 1);
+
+    virtual ~ISensDetTool() {};
+
+    virtual G4VSensitiveDetector* createSD(const std::string& name);
+
+};
+
+#endif
-- 
GitLab