From 4c5b3177e2f0c6d994cb9b4022e922adadcb686c Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Tue, 5 Mar 2019 13:57:44 +0100
Subject: [PATCH] Add basic test to read GDML property tables

---
 DDCore/src/plugins/Compact2Objects.cpp        |  26 +++-
 DDCore/src/plugins/StandardPlugins.cpp        |  32 ++++
 DDG4/src/Geant4Converter.cpp                  |   2 +-
 examples/CMakeLists.txt                       |   1 +
 examples/ClientTests/compact/MiniTel.xml      |   5 +-
 examples/OpticalSurfaces/CMakeLists.txt       |  38 +++++
 .../compact/ReadGDMLMatrices.xml              | 142 ++++++++++++++++++
 7 files changed, 238 insertions(+), 8 deletions(-)
 create mode 100644 examples/OpticalSurfaces/CMakeLists.txt
 create mode 100644 examples/OpticalSurfaces/compact/ReadGDMLMatrices.xml

diff --git a/DDCore/src/plugins/Compact2Objects.cpp b/DDCore/src/plugins/Compact2Objects.cpp
index e9f505733..7cbb18b4f 100644
--- a/DDCore/src/plugins/Compact2Objects.cpp
+++ b/DDCore/src/plugins/Compact2Objects.cpp
@@ -105,6 +105,7 @@ namespace {
   bool s_debug_segmentation = false;
   bool s_debug_constants    = false;
   bool s_debug_include      = false;
+  bool s_debug_matrix       = false;
 }
 
 static Ref_t create_ConstantField(Detector& /* description */, xml_h e) {
@@ -262,6 +263,7 @@ template <> void Converter<Debug>::operator()(xml_h e) const {
     else if ( nam.substr(0,6) == "consta" ) s_debug_constants    = (0 != val);
     else if ( nam.substr(0,6) == "define" ) s_debug_constants    = (0 != val);
     else if ( nam.substr(0,6) == "includ" ) s_debug_include      = (0 != val);
+    else if ( nam.substr(0,6) == "matrix" ) s_debug_matrix       = (0 != val);
   }
 }
   
@@ -634,19 +636,33 @@ template <> void Converter<OpticalSurface>::operator()(xml_h element) const {
  */
 template <> void Converter<PropertyTable>::operator()(xml_h e) const {
   string val;
-  vector<float> values;
+  vector<double> values;
   size_t cols = e.attr<long>(_U(coldim));
   stringstream str(e.attr<string>(_U(values)));
 
+  if ( s_debug_matrix )    {
+    printout(ALWAYS,"Compact","+++ Reading proeprty table %s with %d columns.",
+             e.attr<string>(_U(name)).c_str(), cols);
+  }
   values.reserve(1024);
   while ( !str.eof() )   {
     str >> val;
+    if ( !str.good() ) break;
+    if ( s_debug_matrix )    {
+      cout << " state:" << (str.good() ? "OK " : "BAD") << " '" << val << "'";
+    }
     values.push_back(_toDouble(val));
+    if ( 0 == (values.size()%cols) ) cout << endl;
+  }
+  if ( s_debug_matrix )    {
+    cout << endl;
   }
   /// Create table and register table
   PropertyTable table(description, e.attr<string>(_U(name)), "", values.size()/cols, cols);
   for (size_t i=0, n=values.size(); i<n; ++i)
     table->Set(i/cols, i%cols, values[i]);
+  if ( s_debug_matrix )
+    table->Print();
 }
 #endif
 
@@ -1138,20 +1154,22 @@ template <> void Converter<DetElement>::operator()(xml_h element) const {
              (det.isValid() ? "++ Converted" : "FAILED    "), name.c_str(), type.c_str(),
              (sd.isValid() ? ("[" + sd.type() + "]").c_str() : ""));
 
-    if (!det.isValid()) {
+    if (!det.isValid())  {
       PluginDebug dbg;
       PluginService::Create<NamedObject*>(type, &description, &element, &sens);
       throw runtime_error("Failed to execute subdetector creation plugin. " + dbg.missingFactory(type));
     }
     description.addDetector(det);
+#if ROOT_VERSION_CODE > ROOT_VERSION(6,16,0)
     description.surfaceManager().registerSurfaces(det);
+#endif
     return;
   }
-  catch (const exception& e) {
+  catch (const exception& e)  {
     printout(ERROR, "Compact", "++ FAILED    to convert subdetector: %s: %s", name.c_str(), e.what());
     terminate();
   }
-  catch (...) {
+  catch (...)  {
     printout(ERROR, "Compact", "++ FAILED    to convert subdetector: %s: %s", name.c_str(), "UNKNONW Exception");
     terminate();
   }
diff --git a/DDCore/src/plugins/StandardPlugins.cpp b/DDCore/src/plugins/StandardPlugins.cpp
index bb2371d69..1610293e4 100644
--- a/DDCore/src/plugins/StandardPlugins.cpp
+++ b/DDCore/src/plugins/StandardPlugins.cpp
@@ -36,6 +36,9 @@
 #include "TSystem.h"
 #include "TClass.h"
 #include "TRint.h"
+#if ROOT_VERSION_CODE > ROOT_VERSION(6,16,0)
+#include "TGDMLMatrix.h"
+#endif
 
 // C/C++ include files
 #include <cerrno>
@@ -260,6 +263,35 @@ static long root_ui(Detector& description, int /* argc */, char** /* argv */) {
 }
 DECLARE_APPLY(DD4hep_InteractiveUI,root_ui)
 
+/// Basic entry point to dump all known GDML tables
+/**
+ *
+ *  Factory: DD4hep_Dump_GDMLTables
+ *
+ *  \author  M.Frank
+ *  \version 1.0
+ *  \date    01/04/2014
+ */
+static long root_gdml_tables(Detector& description, int /* argc */, char** /* argv */) {
+  size_t num_tables = 0;
+  size_t num_elements = 0;
+#if ROOT_VERSION_CODE > ROOT_VERSION(6,16,0)
+  const TObjArray* c = description.manager().GetListOfGDMLMatrices();
+  TObjArrayIter arr(c);
+  for(TObject* i = arr.Next(); i; i=arr.Next())   {
+    TGDMLMatrix* m = (TGDMLMatrix*)i;
+    num_elements += (m->GetRows()*m->GetCols());
+    ++num_tables;
+    m->Print();
+  }
+#endif
+  printout(INFO,"Dump_GDMLTables",
+           "+++ Successfully dumped %ld GDML tables with %ld elements.",
+           num_tables, num_elements);
+  return 1;
+}
+DECLARE_APPLY(DD4hep_Dump_GDMLTables,root_gdml_tables)
+
 /// Basic entry point to dump the ROOT TGeoElementTable object
 /**
  *  Dump the elment table to stdout or file.
diff --git a/DDG4/src/Geant4Converter.cpp b/DDG4/src/Geant4Converter.cpp
index aefa74942..3e4428f80 100644
--- a/DDG4/src/Geant4Converter.cpp
+++ b/DDG4/src/Geant4Converter.cpp
@@ -1319,7 +1319,7 @@ template <typename O, typename C, typename F> void handle(const O* o, const C& c
 
 template <typename O, typename F> void handleArray(const O* o, const TObjArray* c, F pmf) {
   TObjArrayIter arr(c);
-  for(TObject* i = *arr; i; i=arr.Next())
+  for(TObject* i = arr.Next(); i; i=arr.Next())
     (o->*pmf)(i);
 }
 
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 44d8dbe16..1eb931f25 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -51,6 +51,7 @@ dd4hep_enable_tests (
   ClientTests
   Conditions
   DDG4
+  #OpticalSurfaces
   Persistency
   SimpleDetector
   DDG4_MySensDet
diff --git a/examples/ClientTests/compact/MiniTel.xml b/examples/ClientTests/compact/MiniTel.xml
index 889945c64..8e0b0c1b4 100644
--- a/examples/ClientTests/compact/MiniTel.xml
+++ b/examples/ClientTests/compact/MiniTel.xml
@@ -30,9 +30,8 @@
 
 
   <display>
-    <vis name="DetVis"  alpha="1.0"    r="0"   g="1.0" b="0.0"  showDaughters="true"   visible="true" />
-    <vis name="ModVis"  alpha="1.0"   r="1"   g="0.0" b="0.0"  showDaughters="true"  visible="false" />
-
+    <vis name="DetVis"  alpha="1.0"   r="0"   g="1.0" b="0.0"  showDaughters="true"  visible="true"/>
+    <vis name="ModVis"  alpha="1.0"   r="1"   g="0.0" b="0.0"  showDaughters="true"  visible="false"/>
   </display>
 
 
diff --git a/examples/OpticalSurfaces/CMakeLists.txt b/examples/OpticalSurfaces/CMakeLists.txt
new file mode 100644
index 000000000..9b83fb740
--- /dev/null
+++ b/examples/OpticalSurfaces/CMakeLists.txt
@@ -0,0 +1,38 @@
+#==========================================================================
+#  AIDA Detector description implementation 
+#--------------------------------------------------------------------------
+# Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
+# All rights reserved.
+#
+# For the licensing terms see $DD4hepINSTALL/LICENSE.
+# For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
+#
+#==========================================================================
+cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
+include ( ${DD4hep_DIR}/cmake/DD4hep.cmake )
+
+#--------------------------------------------------------------------------
+dd4hep_configure_output()
+dd4hep_package ( OpticalSurfaces MAJOR 0 MINOR 0 PATCH 1
+  USES           [ROOT   REQUIRED COMPONENTS Geom GenVector MathCore] 
+                 [DD4hep REQUIRED COMPONENTS DDCore DDCond]
+  OPTIONAL       XERCESC
+  INCLUDE_DIRS   include
+  )
+set(OpticalSurfaces_INSTALL ${CMAKE_INSTALL_PREFIX}/examples/OpticalSurfaces)
+# dd4hep_add_plugin(   OpticalSurfacesExample SOURCES src/*.cpp  )
+dd4hep_install_dir(  compact DESTINATION ${OpticalSurfaces_INSTALL} )
+dd4hep_configure_scripts( OpticalSurfaces DEFAULT_SETUP WITH_TESTS)
+#
+message (STATUS "OpticalSurfaces: ROOT version: ${ROOT_VERSION}")
+#
+#---Testing: Load Telescope geometry and read conditions ------------------
+dd4hep_add_test_reg( Surfaces_read_GDMLMatrix
+  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_OpticalSurfaces.sh"
+  EXEC_ARGS  geoPluginRun -volmgr -destroy 
+  -compact file:${OpticalSurfaces_INSTALL}/compact/ReadGDMLMatrix.xml
+  -plugin DD4hep_DetectorDump
+  REGEX_PASS "Successfully dumped 2 GDML tables with 42 elements."
+  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
+  )
+#
diff --git a/examples/OpticalSurfaces/compact/ReadGDMLMatrices.xml b/examples/OpticalSurfaces/compact/ReadGDMLMatrices.xml
new file mode 100644
index 000000000..15e667486
--- /dev/null
+++ b/examples/OpticalSurfaces/compact/ReadGDMLMatrices.xml
@@ -0,0 +1,142 @@
+<lccdd xmlns:compact="http://www.lcsim.org/schemas/compact/1.0"
+  xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  xs:noNamespaceSchemaLocation="http://www.lcsim.org/schemas/compact/1.0/compact.xsd">
+
+  <info name="TelescopeSensor"
+    title="Simple emulation of the EU Telescope"
+    author="Christoph Hombach"
+    url="https://github.com/AIDASoft/DD4hep/blob/master/examples/AlignDet/compact/Telescope.xml"
+    status="development"
+    version="1.0">
+    <comment>Simple emulation of the EU Telescope to test alignment procedures</comment>
+  </info>
+
+  <includes>
+    <gdmlFile  ref="${DD4hepINSTALL}/DDDetectors/compact/elements.xml"/>
+    <gdmlFile  ref="${DD4hepINSTALL}/DDDetectors/compact/materials.xml"/>
+  </includes>
+
+  <define>
+    <constant name="world_side" value="1*m"/>
+    <constant name="world_x" value="world_side/2"/>
+    <constant name="world_y" value="world_side/2"/>
+    <constant name="world_z" value="world_side/2"/>
+    <constant name="CrossingAngle" value="0.020"/>
+    <constant name="PhotMomWaveConv" value="1243.125*eV"/>
+  </define>
+  <debug>
+    <type name="matrix"  value="1"/>
+    <type name="surface" value="1"/>
+  </debug>
+
+  <materials>
+  </materials>
+
+  <limits>
+  </limits>
+
+  <display>
+    <vis name="DetVis" alpha="1.0" r="0" g="0.0" b="1.0" showDaughters="true" visible="false"/>
+    <vis name="ModVis" alpha="0.3" r="0.8" g="0.8" b="1.0" showDaughters="true" visible="true"/>
+
+    <vis name="SensorVis" alpha="1.0" r="1.0"    g="0.2"    b="0.2"    showDaughters="false" visible="true"/>
+    <vis name="ChipVis"   alpha="1.0" r="0.7451" g="0.7451" b="0.7451" showDaughters="false" visible="true"/>
+    <vis name="PCBVis"    alpha="1.0" r="0.0000" g="0.3910" b="0.0000" showDaughters="false" visible="true"/>
+  </display>
+
+
+  <properties>
+  <matrix name= "Someother_property"
+	  coldim  = "6" values="  
+    PhotMomWaveConv/100.0  0.0 1 2 3 4
+    PhotMomWaveConv/190.0  0.0 1 2 3 4
+    PhotMomWaveConv/200.0  0.0 1 2 3 4
+    PhotMomWaveConv/220.0  0.0 1 2 3 4
+    PhotMomWaveConv/240.0  0.0 1 2 3 4
+  "/>
+  <matrix name= "Rich1Mirror2SurfaceEfficiencyPT"
+	  coldim  = "2" values="  
+    PhotMomWaveConv/100.0  0.0
+    PhotMomWaveConv/190.0  0.0
+    PhotMomWaveConv/200.0  0.0
+    PhotMomWaveConv/220.0  0.0
+    PhotMomWaveConv/240.0  0.0
+    PhotMomWaveConv/260.0  0.0
+  "/>
+
+  </properties>
+
+  <detectors>
+    <comment>No comment so far</comment>
+
+    <detector name="Telescope" type="DD4hep_Example_Telescope"  vis="DetVis" id ="3" readout="TelescopeHits">
+      <!-- Front arm -->
+      <module z="0.0*cm" id="1" vis="ModVis">
+        <sensor thickness="3*mm" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="7*mm" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="2.5*cm" id="2" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="5.0*cm" id="3" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="7.5*cm" id="4" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <!-- DuT -->
+      <module z="25.0*cm" id="5" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="768" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <!-- Back arm -->
+      <module z="42.5*cm" id="6" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="45.0*cm" id="7" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="47.5*cm" id="8" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+
+      <module z="50.0*cm" id="9" vis="ModVis">
+        <sensor thickness="300*micron" pitch="55*micron" NoOfPixY="256" NoOfPixX="256" vis="SensorVis"/>
+        <chip thickness="700*micron" vis="ChipVis"/>
+        <PCB thickness="1.4*mm" vis="PCBVis"/>
+      </module>
+    </detector>
+  </detectors>
+
+  <readouts>
+    <readout name="TelescopeHits">
+      <segmentation type="CartesianGridXY" grid_size_x="55*micron" grid_size_y="55*micron" />
+      <id>system:8,module:10,sensor:10,x:16,y:16</id>
+    </readout>
+  </readouts>
+
+  <fields>
+  </fields>
+
+</lccdd>
-- 
GitLab