From a50564c97d1734f47fe319a7a099d3f2ede80927 Mon Sep 17 00:00:00 2001
From: lintao <lintao51@gmail.com>
Date: Fri, 21 Aug 2020 23:13:20 +0800
Subject: [PATCH] WIP: add an interface to get the decoder according to the
 readout name.

---
 Detector/DetInterface/DetInterface/IGeoSvc.h | 13 ++++++--
 Detector/GeoSvc/src/GeoSvc.cpp               | 31 ++++++++++++++++++++
 Detector/GeoSvc/src/GeoSvc.h                 |  4 ++-
 Examples/src/DumpIDAlg/DumpIDAlg.cpp         | 14 +--------
 4 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/Detector/DetInterface/DetInterface/IGeoSvc.h b/Detector/DetInterface/DetInterface/IGeoSvc.h
index 58df046d..2925d7a2 100644
--- a/Detector/DetInterface/DetInterface/IGeoSvc.h
+++ b/Detector/DetInterface/DetInterface/IGeoSvc.h
@@ -13,14 +13,18 @@
 #include "GaudiKernel/IService.h"
 
 namespace dd4hep {
-class Detector;
-class DetElement;
+    class Detector;
+    class DetElement;
+    namespace DDSegmentation {
+        class BitFieldCoder;
+    }
 }
 
 // class G4VUserDetectorConstruction;
 
 class GAUDI_API IGeoSvc : virtual public IService {
-
+public:
+  typedef dd4hep::DDSegmentation::BitFieldCoder Decoder;
 public:
   /// InterfaceID
   DeclareInterfaceID(IGeoSvc, 1, 0);
@@ -30,6 +34,9 @@ public:
   // receive Geant4 Geometry
   // virtual G4VUserDetectorConstruction* getGeant4Geo() = 0;
 
+  // short cut to retrieve the Decoder according to the Readout name
+  virtual Decoder* getDecoder(const std::string& readout_name) = 0;
+
   virtual ~IGeoSvc() {}
 };
 
diff --git a/Detector/GeoSvc/src/GeoSvc.cpp b/Detector/GeoSvc/src/GeoSvc.cpp
index 305896c4..1739aa8a 100644
--- a/Detector/GeoSvc/src/GeoSvc.cpp
+++ b/Detector/GeoSvc/src/GeoSvc.cpp
@@ -46,3 +46,34 @@ dd4hep::Detector*
 GeoSvc::lcdd() {
     return m_dd4hep_geo;
 }
+
+IGeoSvc::Decoder*
+GeoSvc::getDecoder(const std::string& readout_name) {
+
+    IGeoSvc::Decoder* decoder = nullptr;
+
+    if (!lcdd()) {
+        error() << "Failed to get lcdd()" << endmsg;
+        return decoder;
+    }
+
+    auto readouts = m_dd4hep_geo->readouts();
+    if (readouts.find(readout_name) == readouts.end()) {
+        error() << "Failed to find readout name '" << readout_name << "'"
+                << " in DD4hep::readouts. "
+                << endmsg;
+        return decoder;
+    }
+    
+    dd4hep::Readout readout = lcdd()->readout(readout_name);
+    auto m_idspec = readout.idSpec(); 
+
+    decoder = m_idspec.decoder();
+
+    if (!decoder) {
+        error() << "Failed to get the decoder with readout '"
+                << readout_name << "'" << endmsg;
+    }
+
+    return decoder;
+}
diff --git a/Detector/GeoSvc/src/GeoSvc.h b/Detector/GeoSvc/src/GeoSvc.h
index 9c98daa9..1be2b4ff 100644
--- a/Detector/GeoSvc/src/GeoSvc.h
+++ b/Detector/GeoSvc/src/GeoSvc.h
@@ -28,6 +28,8 @@ public:
     dd4hep::DetElement getDD4HepGeo() override;
     dd4hep::Detector* lcdd() override;
 
+    Decoder* getDecoder(const std::string& readout_name) override;
+
 private:
 
     // DD4hep XML compact file path
@@ -38,4 +40,4 @@ private:
 };
 
 
-#endif GeoSvc_h
+#endif // GeoSvc_h
diff --git a/Examples/src/DumpIDAlg/DumpIDAlg.cpp b/Examples/src/DumpIDAlg/DumpIDAlg.cpp
index 627227d6..3d99477f 100644
--- a/Examples/src/DumpIDAlg/DumpIDAlg.cpp
+++ b/Examples/src/DumpIDAlg/DumpIDAlg.cpp
@@ -30,20 +30,8 @@ StatusCode DumpIDAlg::initialize()
     }
 
     // get the DD4hep readout
-    auto readouts = m_dd4hep_geo->readouts();
     const std::string name_readout = "EcalBarrelCollection";
-    if (readouts.find(name_readout) != readouts.end()) {
-        dd4hep::Readout readout = m_dd4hep_geo->readout(name_readout);
-
-        auto m_idspec = readout.idSpec();
-
-        info() << "The idspec is " << m_idspec.fieldDescription() << " for " << name_readout << endmsg;
-
-        dd4hep::DDSegmentation::BitFieldCoder* decoder = m_idspec.decoder();
-        
-        m_decoder = decoder;
-    }
-
+    m_decoder = m_geosvc->getDecoder(name_readout);
     if (!m_decoder) {
         error() << "Failed to get the decoder. " << endmsg;
         return StatusCode::FAILURE;
-- 
GitLab