From 51bd6c030879984b74c8c44b2e0c7cf9871898eb Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Tue, 15 Oct 2019 14:07:35 +0800
Subject: [PATCH] Accessing hit collections.

---
 Simulation/DetSimAna/CMakeLists.txt           |  5 +--
 .../DetSimAna/src/ExampleAnaElemTool.cpp      | 35 +++++++++++++++++--
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/Simulation/DetSimAna/CMakeLists.txt b/Simulation/DetSimAna/CMakeLists.txt
index ba4ee82a..7bf795e6 100644
--- a/Simulation/DetSimAna/CMakeLists.txt
+++ b/Simulation/DetSimAna/CMakeLists.txt
@@ -7,12 +7,13 @@ gaudi_depends_on_subdirs(
 
 find_package(Geant4 REQUIRED ui_all vis_all)
 include(${Geant4_USE_FILE})
+find_package(DD4hep COMPONENTS DDG4 REQUIRED)
 
 set(DetSimAna_srcs
     src/ExampleAnaElemTool.cpp
 )
 
 gaudi_add_module(DetSimAna ${DetSimAna_srcs}
-    INCLUDE_DIRS DetSimInterface GaudiKernel Geant4
-    LINK_LIBRARIES DetSimInterface GaudiKernel Geant4
+    INCLUDE_DIRS DetSimInterface DD4hep GaudiKernel Geant4
+    LINK_LIBRARIES DetSimInterface DD4hep ${DD4hep_COMPONENT_LIBRARIES} GaudiKernel Geant4
 )
diff --git a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
index 569c968c..f9f54489 100644
--- a/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
+++ b/Simulation/DetSimAna/src/ExampleAnaElemTool.cpp
@@ -2,6 +2,12 @@
 
 #include "G4Event.hh"
 
+#include "DD4hep/Detector.h"
+#include "DD4hep/Plugins.h"
+#include "DDG4/Geant4Converter.h"
+#include "DDG4/Geant4Mapping.h"
+
+
 DECLARE_COMPONENT(ExampleAnaElemTool)
 
 void
@@ -20,8 +26,33 @@ ExampleAnaElemTool::BeginOfEventAction(const G4Event* anEvent) {
 }
 
 void
-ExampleAnaElemTool::EndOfEventAction(const G4Event*) {
-
+ExampleAnaElemTool::EndOfEventAction(const G4Event* anEvent) {
+
+    // save all data
+
+    // readout defined in DD4hep
+    auto lcdd = &(dd4hep::Detector::getInstance());
+    auto allReadouts = lcdd->readouts();
+
+    for (auto& readout : allReadouts) {
+        info() << "Readout " << readout.first << endmsg;
+    }
+
+    // retrieve the hit collections
+    G4HCofThisEvent* collections = anEvent->GetHCofThisEvent();
+    if (!collections) {
+        warning() << "No collections found. " << endmsg;
+        return;
+    }
+    int Ncol = collections->GetNumberOfCollections();
+    for (int icol = 0; icol < Ncol; ++icol) {
+        G4VHitsCollection* collect = collections->GetHC(icol);
+        if (!collect) {
+            warning() << "Collection iCol " << icol << " is missing" << endmsg;
+            continue;
+        }
+        info() << "Collection " << collect->GetName() << endmsg;
+    }
 }
 
 void
-- 
GitLab