From 42cb2db983552162f390aaece689bea2e4913468 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Fri, 3 Mar 2023 14:31:19 +0100
Subject: [PATCH] Add examples for volume and sensitive configuration from
 plugins/compact

---
 examples/ClientTests/CMakeLists.txt           | 72 +++++++++++++++----
 examples/ClientTests/scripts/DDG4TestSetup.py |  5 +-
 examples/ClientTests/scripts/MiniTel.py       | 21 +++++-
 examples/ClientTests/scripts/MiniTelSetup.py  |  4 +-
 4 files changed, 83 insertions(+), 19 deletions(-)

diff --git a/examples/ClientTests/CMakeLists.txt b/examples/ClientTests/CMakeLists.txt
index 925b5065c..d161623f5 100644
--- a/examples/ClientTests/CMakeLists.txt
+++ b/examples/ClientTests/CMakeLists.txt
@@ -322,7 +322,8 @@ endforeach()
 # IronCylinder has no segmentation!
 # MaterialTester no geometry
 # SectorBarrelCalorimeter is bad
-foreach (test Assemblies BoxTrafos CaloEndcapReflection LheD_tracker MagnetFields MiniTel SiliconBlock 
+foreach (test Assemblies BoxTrafos CaloEndcapReflection
+	 LheD_tracker MagnetFields MiniTel SiliconBlock 
 	 ParamVolume1D ParamVolume2D ParamVolume3D
          NestedSimple NestedDetectors MultiCollections )
   #
@@ -336,8 +337,9 @@ foreach (test Assemblies BoxTrafos CaloEndcapReflection LheD_tracker MagnetField
   if (DD4HEP_USE_GEANT4)
     dd4hep_add_test_reg( ClientTests_g4material_scan_${test}_LONGTEST
       COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
-      EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4MaterialScan --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
-                        "--position=0,0,0" "--direction=0,1,0"
+      EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4MaterialScan
+      		  --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
+                 "--position=0,0,0" "--direction=0,1,0"
       REGEX_PASS " Terminate Geant4 and delete associated actions." )
   endif(DD4HEP_USE_GEANT4)
 endforeach()
@@ -357,14 +359,16 @@ foreach (test BoxTrafos CaloEndcapReflection IronCylinder MiniTel SiliconBlock N
   # ROOT Geometry checks
   dd4hep_add_test_reg( ClientTests_check_geometry_${test}_LONGTEST
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
-    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/checkGeometry --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
-                      --full=true --ntracks=10
+    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/checkGeometry
+    	        --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
+                --full=true --ntracks=10
     REGEX_PASS " Execution finished..." )
   #
   # ROOT Geometry overlap checks
   dd4hep_add_test_reg( ClientTests_check_overlaps_${test}_LONGTEST
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
-    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/checkOverlaps --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
+    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/checkOverlaps
+    	        --compact=file:${ClientTestsEx_INSTALL}/compact/${test}.xml
                       --tolerance=0.1
     REGEX_PASS " Execution finished..." )
 endforeach()
@@ -373,7 +377,8 @@ endforeach()
 # Checksum test of the Minitel3 sub-detector
 dd4hep_add_test_reg( MiniTel_check_checksum_Minitel3
   COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_CLICSiD.sh"
-  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/MiniTelGenerate.xml -plugin DD4hepDetectorChecksum -readout -detector Minitel3
+  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/MiniTelGenerate.xml
+  	      -plugin DD4hepDetectorChecksum -readout -detector Minitel3
   REGEX_PASS "Combined hash code                      36773df7c4d6cf2b  \\(52 sub-codes\\)"
   REGEX_FAIL "Exception;EXCEPTION;ERROR"
 )
@@ -381,28 +386,45 @@ dd4hep_add_test_reg( MiniTel_check_checksum_Minitel3
 # Checksum test of the full detector
 dd4hep_add_test_reg( MiniTel_check_checksum_full
   COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_CLICSiD.sh"
-  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/MiniTelGenerate.xml -plugin DD4hepDetectorChecksum -readout
+  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/MiniTelGenerate.xml
+  	      -plugin DD4hepDetectorChecksum -readout
   REGEX_PASS "Combined hash code                      99b55eb7a563d587  \\(201 sub-codes\\)"
   REGEX_FAIL "Exception;EXCEPTION;ERROR"
 )
 #
 # Test the sequential processing of two xml files
-dd4hep_add_test_reg( include_plugins_command_line
+dd4hep_add_test_reg( minitel_config_plugins_include_command_line
   COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
   EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/MiniTel.xml 
        	     		  -input ${ClientTestsEx_INSTALL}/compact/ExamplePlugins.xml
-  REGEX_PASS "Tested 5 numeric constants for expression evaluation"
+  REGEX_PASS "\\+\\+\\+ Tested 5 numeric constants for expression evaluation"
   REGEX_FAIL "Exception;EXCEPTION;ERROR"
 )
 #
 # Test the sequential processing of two xml files
-dd4hep_add_test_reg( include_plugins_command_xml
+dd4hep_add_test_reg( minitel_config_plugins_include_command_xml
   COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
   EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/IncludePlugins.xml 
   REGEX_PASS "Tested 5 numeric constants for expression evaluation"
   REGEX_FAIL "Exception;EXCEPTION;ERROR"
 )
 #
+# Test setting properties to the world volume and a single sub-detector
+dd4hep_add_test_reg( minitel_config_subdet
+  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
+  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/WorldSettings.xml 
+  REGEX_PASS "\\+\\+ Applied 5 settings to MyLHCBdetector5"
+  REGEX_FAIL "Exception;EXCEPTION;ERROR"
+)
+#
+# Test setting properties to the world volume and a single sub-detector
+dd4hep_add_test_reg( minitel_config_world
+  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
+  EXEC_ARGS  geoPluginRun -input ${ClientTestsEx_INSTALL}/compact/WorldSettings.xml 
+  REGEX_PASS "\\+\\+ Applied 3 settings to /world"
+  REGEX_FAIL "Exception;EXCEPTION;ERROR"
+)
+#
 #      EXEC_ARGS  test_with_root.sh ${script}
 #
 if (DD4HEP_USE_GEANT4)
@@ -411,14 +433,15 @@ if (DD4HEP_USE_GEANT4)
   dd4hep_add_test_reg( ClientTests_sim_Check_Temp_Pressure_Air
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
     EXEC_ARGS  ${Python_EXECUTABLE} ${ClientTestsEx_INSTALL}/scripts/Check_Air.py
-    -geometry  ${ClientTestsEx_INSTALL}/compact/Check_Air.xml batch
+    	              -geometry  ${ClientTestsEx_INSTALL}/compact/Check_Air.xml batch
     REGEX_PASS "Imean:  85.538 eV   temperature: 333.33 K  pressure:   2.22 atm"
     REGEX_FAIL "Exception;EXCEPTION;ERROR;Error;FATAL" )
   #
   # Geant4 test with gdml input file (LHCb:FT)
   dd4hep_add_test_reg( ClientTests_g4_gdml_detector
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
-    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4GeometryScan --compact=${ClientTestsEx_INSTALL}/compact/GdmlDetector.xml
+    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4GeometryScan
+    	              --compact=${ClientTestsEx_INSTALL}/compact/GdmlDetector.xml
                       --position=200,200,-2000 --direction=0,0,1
     REGEX_PASS "   122   2777.0000  5200.000  "
     REGEX_FAIL "Exception;EXCEPTION;ERROR;Error;FATAL" )
@@ -426,7 +449,8 @@ if (DD4HEP_USE_GEANT4)
   # Geant4 test with gdml input file (LHCb:MT)
   dd4hep_add_test_reg( ClientTests_g4_gdml_MT
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
-    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4GeometryScan --compact=${ClientTestsEx_INSTALL}/compact/MT.xml
+    EXEC_ARGS  ${Python_EXECUTABLE} ${DD4hep_ROOT}/bin/g4GeometryScan
+    	              --compact=${ClientTestsEx_INSTALL}/compact/MT.xml
                       --position=200,200,7900 --direction=0,0,1
     REGEX_PASS "    15   2777.0000  4210.000   "
     REGEX_FAIL "Exception;EXCEPTION;ERROR;Error;FATAL" )
@@ -473,11 +497,29 @@ if (DD4HEP_USE_GEANT4)
     dd4hep_add_test_reg( ClientTests_sim_${script}
       COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
       EXEC_ARGS  ${Python_EXECUTABLE} ${ClientTestsEx_INSTALL}/scripts/MultiCollections.py 
-                        -compact ${ClientTestsEx_INSTALL}/compact/${script}.xml -batch
+                 -compact ${ClientTestsEx_INSTALL}/compact/${script}.xml -batch
       REGEX_PASS NONE
       REGEX_FAIL "Exception;EXCEPTION;ERROR;Error" )
   endforeach(script)
   #
+  # Test setting properties to a single sub-detector
+  dd4hep_add_test_reg( minitel_config_region_subdet_geant4
+    COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
+    EXEC_ARGS  ${Python_EXECUTABLE} ${ClientTestsEx_INSTALL}/scripts/MiniTel.py
+    	       -batch -events 1 -geometry /examples/ClientTests/compact/WorldSettings.xml -debug
+    REGEX_PASS "\\+ Apply REGION settings: minitel_region_5         to volume MyLHCBdetector5."
+    REGEX_FAIL "Exception;EXCEPTION;ERROR"
+  )
+  #
+  # Test setting properties to the world volume
+  dd4hep_add_test_reg( minitel_config_region_world_geant4
+    COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_ClientTests.sh"
+    EXEC_ARGS  ${Python_EXECUTABLE} ${ClientTestsEx_INSTALL}/scripts/MiniTel.py
+    	       -batch -events 1 -geometry /examples/ClientTests/compact/WorldSettings.xml -debug
+    REGEX_PASS "Volume world_volume Region: DefaultRegionForTheWorld. Apply user limits from world_region"
+    REGEX_FAIL "Exception;EXCEPTION;ERROR"
+  )
+  #
   if(Geant4_VERSION VERSION_LESS 10.7)
     dd4hep_print("|++> Geant4 fast simulation not supported for Geant4 ${Geant4_VERSION}")
   else()
diff --git a/examples/ClientTests/scripts/DDG4TestSetup.py b/examples/ClientTests/scripts/DDG4TestSetup.py
index 4f1664777..514286422 100644
--- a/examples/ClientTests/scripts/DDG4TestSetup.py
+++ b/examples/ClientTests/scripts/DDG4TestSetup.py
@@ -75,11 +75,14 @@ class Setup:
     part.enableUI()
     return part
 
-  def setupPhysics(self, model='QGSP_BERT'):
+  def setupPhysics(self, model='QGSP_BERT', dump=False):
     # Now build the physics list:
     self.phys = self.kernel.physicsList()
     self.phys.extends = model
+    self.phys.decays = True
     self.phys.enableUI()
+    if dump:
+      self.phys.dump()
     return self
 
   def run(self, num_events=None):
diff --git a/examples/ClientTests/scripts/MiniTel.py b/examples/ClientTests/scripts/MiniTel.py
index b9c17f11b..664cf3d17 100644
--- a/examples/ClientTests/scripts/MiniTel.py
+++ b/examples/ClientTests/scripts/MiniTel.py
@@ -24,10 +24,27 @@ import DDG4
 
 def run():
   from MiniTelSetup import Setup
-  m = Setup()
-  if len(sys.argv) >= 2 and sys.argv[1] == "batch":
+  args = DDG4.CommandLine()
+
+  m = Setup(geometry=args.geometry)
+  cmds = []
+  if args.events:
+    cmds = ['/run/beamOn ', str(args.events)]
+
+  if args.batch:
     DDG4.setPrintLevel(DDG4.OutputLevel.WARNING)
+    cmds.append('/ddg4/UI/terminate')
     m.kernel.UI = ''
+
+  if args.debug:
+    # Configure G4 geometry setup
+    seq, act = m.geant4.addDetectorConstruction("Geant4DetectorGeometryConstruction/ConstructGeo")
+    act.DebugVolumes = True
+    act.DebugRegions = True
+    act.DebugLimits  = True
+    seq, act = m.geant4.addDetectorConstruction("Geant4DetectorSensitivesConstruction/ConstructSD")
+
+  m.ui.Commands = cmds
   m.configure()
   m.defineOutput()
   m.setupGun()
diff --git a/examples/ClientTests/scripts/MiniTelSetup.py b/examples/ClientTests/scripts/MiniTelSetup.py
index a445b939c..6f0a01904 100644
--- a/examples/ClientTests/scripts/MiniTelSetup.py
+++ b/examples/ClientTests/scripts/MiniTelSetup.py
@@ -23,8 +23,10 @@ import DDG4TestSetup
 
 
 class Setup(DDG4TestSetup.Setup):
-  def __init__(self, geometry="/examples/ClientTests/compact/MiniTel.xml", macro=None, vis=None):
+  def __init__(self, geometry=None, macro=None, vis=None):
     install_dir = os.environ['DD4hepExamplesINSTALL']
+    if geometry is None:
+      geometry = "/examples/ClientTests/compact/MiniTel.xml"
     DDG4TestSetup.Setup.__init__(self, "file:" + install_dir + geometry, macro=macro, vis=vis)
 
   def configure(self, output_level=None):
-- 
GitLab