From 6eefdad109a1aa176cd3caf677e6649624f305d8 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Tue, 28 Feb 2023 17:02:31 +0100
Subject: [PATCH] Test inclusion of files for plugins

---
 UtilityApps/src/run_plugin.h                  | 29 +++++++-----
 .../ClientTests/compact/ExamplePlugins.xml    | 23 ++++++++++
 .../ClientTests/compact/IncludePlugins.xml    | 46 +++++++++++++++++++
 .../compact/NamespaceConstants.xml            |  1 +
 .../src/TestConstantMultiplier.cpp            | 22 +++++----
 5 files changed, 102 insertions(+), 19 deletions(-)
 create mode 100644 examples/ClientTests/compact/ExamplePlugins.xml
 create mode 100644 examples/ClientTests/compact/IncludePlugins.xml

diff --git a/UtilityApps/src/run_plugin.h b/UtilityApps/src/run_plugin.h
index edbe4411e..894a044fe 100644
--- a/UtilityApps/src/run_plugin.h
+++ b/UtilityApps/src/run_plugin.h
@@ -206,7 +206,7 @@ namespace {
         if ( result == EINVAL )   {
           std::cout << "FAILED to execute dd4hep plugin: '" << plug[0] 
                     << "' with args (" << num_args << ") :[ ";
-          for(size_t j = 1; j < plug.size(); ++j)   {
+          for( std::size_t j = 1; j < plug.size(); ++j )   {
             if ( plug[j] ) std::cout << plug[j] << " ";
           }
           std::cout << "]" << std::endl << std::flush;
@@ -214,21 +214,26 @@ namespace {
         }
         std::cout << "run_plugin: " << text << " Executed dd4hep plugin: '" << plug[0]
                   << "' with args (" << num_args << ") :[ ";
-        for(size_t j=1; j<plug.size(); ++j)   {
+        for( std::size_t j=1; j<plug.size(); ++j )   {
           if ( plug[j] ) std::cout << plug[j] << " ";
         }
         std::cout << "]" << std::endl << std::flush;
       }
-      result = run_plugin(description,name,a.first,a.second);
-      return result;
+      if ( name && name[0] )   {
+	result = run_plugin(description, name, a.first, a.second);
+	return result;
+      }
+      std::cout << "WARNING: No plugin name supplied. "
+		<< "Implicitly assuming execution steered by XML." << std::endl;
+      return ENOENT;
     }
   };
 
   //______________________________________________________________________________
   void load_compact(dd4hep::Detector& description, Args& args)   {
     // Load all compact files
-    for(size_t i=0; i<args.geo_files.size(); ++i)  {
-      const char* argv[] = {args.geo_files[i], args.build_types[i], 0};
+    for( std::size_t i=0; i<args.geo_files.size(); ++i )  {
+      const char* argv[] = { args.geo_files[i], args.build_types[i], 0 };
       run_plugin(description,"DD4hep_CompactLoader",2,(char**)argv);
     }
   }
@@ -241,9 +246,9 @@ namespace dd4hep  {
     //______________________________________________________________________________
     int main_wrapper(const char* name, int argc, char** argv)  {
       Args args;
-      for(int i=1; i<argc;++i) {
+      for( int i=1; i<argc;++i ) {
         if ( argv[i][0]=='-' ) {
-          if ( args.handle(i,argc,argv) )
+          if ( args.handle(i, argc, argv) )
             continue;
           else
             usage_default(name);
@@ -272,8 +277,9 @@ namespace dd4hep  {
           result = args.run(description, name);
           if ( result != EINVAL ) app.Run();
         }
-        else
+        else   {
           result = args.run(description, name);
+	}
         if ( result == EINVAL ) usage_default(name);
       }
       else {
@@ -322,7 +328,8 @@ namespace dd4hep  {
       if ( !arguments.dry_run &&
            !arguments.ui      &&
            !arguments.interpreter &&
-           arguments.plugins.empty() )
+           arguments.plugins.empty() &&
+	   arguments.geo_files.empty() )
       {
         usage_plugin_runner();
       }
@@ -354,7 +361,7 @@ namespace dd4hep  {
         int num_args = int(plug.size())-2;
         TTimeStamp start;
         char text[32];
-        long result = run_plugin(description,plug[0], num_args,(char**)&plug[1]);
+        long result = run_plugin(description, plug[0], num_args, (char**)&plug[1]);
         TTimeStamp stop;
         ::snprintf(text,sizeof(text),"[%8.3f sec]",stop.AsDouble()-start.AsDouble());
         if ( result == EINVAL )   {
diff --git a/examples/ClientTests/compact/ExamplePlugins.xml b/examples/ClientTests/compact/ExamplePlugins.xml
new file mode 100644
index 000000000..994dacecc
--- /dev/null
+++ b/examples/ClientTests/compact/ExamplePlugins.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<lccdd>
+<!-- #==========================================================================
+     #  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.
+     #
+     #==========================================================================
+-->
+  <plugins>
+    <plugin name="DD4hep_TestPrintLine"/>
+    <plugin name="DD4hep_VolumeDump"/>
+    <plugin name="DD4hep_TestConstantsMultiplier">
+      <argument value="-name"/>
+      <argument value="have_fun"/>
+    </plugin>
+    <plugin name="DD4hep_TestPrintLine"/>
+  </plugins>
+</lccdd>
diff --git a/examples/ClientTests/compact/IncludePlugins.xml b/examples/ClientTests/compact/IncludePlugins.xml
new file mode 100644
index 000000000..408e04588
--- /dev/null
+++ b/examples/ClientTests/compact/IncludePlugins.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<lccdd>
+<!-- #==========================================================================
+     #  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.
+     #
+     #==========================================================================
+     #  Check issue: https://github.com/AIDASoft/DD4hep/issues/1062
+     #==========================================================================
+-->
+
+  <define>
+    <constant name="world_constant" value="5*m"/>
+  </define>
+
+  <!-- Include some geometry file
+       The syntax is not obvious without knowing the code.
+       However this cryptification is necessary to steer the order of inclusion.
+   -->
+  <debug>
+    <type name="incguard" value="0"/>
+    <type name="includes" value="1"/>
+  </debug>
+
+  <!-- This here get parsed late, after the sub-detectors are constructed, but before closing -->
+  <includes>
+    <file ref="${DD4hepExamplesINSTALL}/examples/ClientTests/compact/BoxTrafos.xml"/>
+    <file ref="${DD4hepExamplesINSTALL}/examples/ClientTests/compact/ExamplePlugins.xml"/>
+  </includes>
+
+  <!-- This here get parsed late, after the sub-detectors are constructed, but before closing -->
+  <includes>
+     <xml ref="${DD4hepExamplesINSTALL}/examples/ClientTests/compact/ExamplePlugins.xml"/>
+  </includes>
+
+  <!-- This here is parsed at the very end    -->
+  <plugins>
+     <include ref="${DD4hepExamplesINSTALL}/examples/ClientTests/compact/ExamplePlugins.xml"/>
+  </plugins>
+
+</lccdd>
diff --git a/examples/ClientTests/compact/NamespaceConstants.xml b/examples/ClientTests/compact/NamespaceConstants.xml
index e85eab529..c27f56d99 100644
--- a/examples/ClientTests/compact/NamespaceConstants.xml
+++ b/examples/ClientTests/compact/NamespaceConstants.xml
@@ -29,6 +29,7 @@
     <gdmlFile  ref="${DD4hepINSTALL}/DDDetectors/compact/elements.xml"/>
     <gdmlFile  ref="${DD4hepINSTALL}/DDDetectors/compact/materials.xml"/>
   </includes>
+
   <!--
   <plugins>
     <plugin name="DD4hep_TestConstantsMultiplier"/>
diff --git a/examples/ClientTests/src/TestConstantMultiplier.cpp b/examples/ClientTests/src/TestConstantMultiplier.cpp
index fe71d39c3..1e301a60d 100644
--- a/examples/ClientTests/src/TestConstantMultiplier.cpp
+++ b/examples/ClientTests/src/TestConstantMultiplier.cpp
@@ -27,7 +27,6 @@
 #include <fstream>
 #include <cerrno>
 
-using namespace std;
 using namespace dd4hep;
 
 /// Plugin function: Test in memory XML parsing of a simple sub detector
@@ -43,18 +42,21 @@ using namespace dd4hep;
  */
 static int multiply_constants (Detector& detector, int argc, char** argv)  {
   bool help = false;
+  std::string name = "TestConstantsMultiplier";
   for(int i=0; i<argc && argv[i]; ++i)  {
-    if ( 0 == ::strncmp("-help",argv[i],4) )
+    if ( 0 == ::strncmp("-name",argv[i],4) )
+      name = argv[++i];
+    else if ( 0 == ::strncmp("-help",argv[i],4) )
       help = true;
     else
       help = true;
   }
   if ( help )   {
     /// Help printout describing the basic command line interface
-    cout <<
+    std::cout <<
       "Usage: -plugin <name> -arg [-arg]                              \n"
       "     name:   factory name     DD4hep_TestConstantsMultiplier   \n"
-      "\tArguments given: " << arguments(argc,argv) << endl << flush;
+      "\tArguments given: " << arguments(argc,argv) << std::endl << std::flush;
     ::exit(EINVAL);
   }
   int num_test = 0;
@@ -64,7 +66,7 @@ static int multiply_constants (Detector& detector, int argc, char** argv)  {
     if ( c.dataType() == "number" )   {
       try {
         double res = _multiply(c.name(),1.0);
-        printout(INFO,"TestConstantsMultiplier","+++ Constant: %-16s = %-16s [%s]  -> %-16s = %9.3g",
+        printout(INFO,name,"+++ Constant: %-16s = %-16s [%s]  -> %-16s = %9.3g",
                  c.name(), c->GetTitle(), c.dataType().c_str(), c.name(), res);
         ++num_test;
       }
@@ -72,9 +74,13 @@ static int multiply_constants (Detector& detector, int argc, char** argv)  {
       }
     }
   }
-  printout(ALWAYS,"TestConstantsMultiplier",
-           "+++ Tested %d numeric constants for expression evaluation.",num_test);
+  printout(ALWAYS,name,"+++ Tested %d numeric constants for expression evaluation.",num_test);
   return 1;
 }
-
 DECLARE_APPLY(DD4hep_TestConstantsMultiplier,multiply_constants)
+
+static int  print_line(Detector& , int, char** )  {
+  printout(ALWAYS,"Line","+++ ------------------------------------------------------------------");
+  return 1;
+}
+DECLARE_APPLY(DD4hep_TestPrintLine,print_line)
-- 
GitLab