From e673ead96cbefc365fbb838eb36558057170d77c Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Tue, 15 Oct 2019 14:58:52 +0800
Subject: [PATCH] WIP: to make SD work properly, we construct the world from
 dd4hep directly.

---
 .../DetSimAna/src/ExampleAnaElemTool.cpp      |  9 ++++++-
 .../DetSimGeom/src/AnExampleDetElemTool.cpp   | 26 ++++++++++---------
 .../DetSimGeom/src/WorldDetElemTool.cpp       | 23 ++++++++--------
 3 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
index f9f54489..d0611ef2 100644
--- a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
+++ b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
@@ -51,7 +51,14 @@ ExampleAnaElemTool::EndOfEventAction(const G4Event* anEvent) {
             warning() << "Collection iCol " << icol << " is missing" << endmsg;
             continue;
         }
-        info() << "Collection " << collect->GetName() << endmsg;
+        size_t nhits = collect->GetSize();
+        info() << "Collection " << collect->GetName()
+               << " #" << icol
+               << " has " << nhits << " hits."
+               << endmsg;
+
+
+        
     }
 }
 
diff --git a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
index ef1ce8d1..0f0a064d 100644
--- a/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/AnExampleDetElemTool.cpp
@@ -29,10 +29,10 @@ DECLARE_COMPONENT(AnExampleDetElemTool)
 G4LogicalVolume*
 AnExampleDetElemTool::getLV() {
 
-    G4Material* Galactic = G4Material::GetMaterial("Galactic");
+    // G4Material* Galactic = G4Material::GetMaterial("Galactic");
 
-    G4VSolid* solidAnExample= new G4Box("sAnExample", m_x.value(), m_y.value(), m_z.value());
-    G4LogicalVolume* logicAnExample= new G4LogicalVolume( solidAnExample, Galactic, "lAnExample", 0, 0, 0);
+    // 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());
@@ -48,19 +48,20 @@ AnExampleDetElemTool::getLV() {
     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
+        // 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;
+    // return logicAnExample;
+    return logicDD4hepExample;
 }
 
 void
@@ -89,7 +90,7 @@ AnExampleDetElemTool::ConstructSDandField() {
         G4VSensitiveDetector* g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
         if (g4sd == nullptr) {
             std::string tmp = typ;
-            // tmp[0] = ::toupper(tmp[0]);
+            tmp[0] = ::toupper(tmp[0]);
             typ = "Geant4" + tmp;
             g4sd = dd4hep::PluginService::Create<G4VSensitiveDetector*>(typ, nam, lcdd);
             if (g4sd == nullptr) {
@@ -112,6 +113,7 @@ AnExampleDetElemTool::ConstructSDandField() {
                 throw std::runtime_error("ConstructSDandField: Failed to access G4LogicalVolume for SD " + nam + " of type " +
                                          typ + ".");
             }
+            info() << " -> Adding " << g4v->GetName() << endmsg;
             G4SDManager::GetSDMpointer()->AddNewDetector(g4sd);
             g4v->SetSensitiveDetector(g4sd);
         }
diff --git a/Simulation/DetSimGeom/src/WorldDetElemTool.cpp b/Simulation/DetSimGeom/src/WorldDetElemTool.cpp
index 289157a8..ccef28d7 100644
--- a/Simulation/DetSimGeom/src/WorldDetElemTool.cpp
+++ b/Simulation/DetSimGeom/src/WorldDetElemTool.cpp
@@ -27,27 +27,28 @@ DECLARE_COMPONENT(WorldDetElemTool)
 G4LogicalVolume*
 WorldDetElemTool::getLV() {
 
-    G4Material* Galactic = G4Material::GetMaterial("Galactic");
+    // G4Material* Galactic = G4Material::GetMaterial("Galactic");
 
-    G4VSolid* solidWorld= new G4Box("sWorld", 60*m, 60*m, 60*m);
-    G4LogicalVolume* logicWorld= new G4LogicalVolume( solidWorld, Galactic, "lWorld", 0, 0, 0);
+    // G4VSolid* solidWorld= new G4Box("sWorld", 60*m, 60*m, 60*m);
+    // G4LogicalVolume* logicWorld= new G4LogicalVolume( solidWorld, Galactic, "lWorld", 0, 0, 0);
 
     // An example, get a detelem first, then place the detector components inside world.
     ToolHandle<IDetElemTool> inner_detelem_tool("AnExampleDetElemTool");
     G4LogicalVolume* inner_lv = inner_detelem_tool->getLV();
 
     if (inner_lv) {
-        new G4PVPlacement(0,                   // no rotation
-                          G4ThreeVector(),     // at (0,0,0)
-                          inner_lv,            // logical volume
-                          "pAnExampleDetElem", // name
-                          logicWorld,          // mother volume
-                          false,               // no boolean operations
-                          0);                  // no field
+        // new G4PVPlacement(0,                   // no rotation
+        //                   G4ThreeVector(),     // at (0,0,0)
+        //                   inner_lv,            // logical volume
+        //                   "pAnExampleDetElem", // name
+        //                   logicWorld,          // mother volume
+        //                   false,               // no boolean operations
+        //                   0);                  // no field
     } else {
         warning() << "Can't Find the logical volume ExampleDetElem " << std::endl;
     }
-    return logicWorld;
+    // return logicWorld;
+    return inner_lv;
 }
 
 void
-- 
GitLab