From 5dbf9b8ee199a80a1cf55776468e73b9ef2e4258 Mon Sep 17 00:00:00 2001
From: Wouter Deconinck <wdconinc@gmail.com>
Date: Wed, 4 Aug 2021 13:32:34 -0500
Subject: [PATCH] [gdmlFile] optionally import physvol below top level

This allows syntax like
```xml
  <detectors>
    <detector id="1" name="FT_MT" type="DD4hep_GdmlDetector" vis="InvisibleWithChildren">
      <gdmlFile ref="GdmlDetector.gdml" physvol="lvUpstreamRegion_0"/>
      <parent name="/world"/>
    </detector>
  </detectors>
```
to only load the specific `physvol` in `examples/ClientTests/compact/GdmlDetector.gdml`.
---
 DDCore/src/gdml/GdmlPlugins.cpp | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/DDCore/src/gdml/GdmlPlugins.cpp b/DDCore/src/gdml/GdmlPlugins.cpp
index e17aaa631..67292e959 100644
--- a/DDCore/src/gdml/GdmlPlugins.cpp
+++ b/DDCore/src/gdml/GdmlPlugins.cpp
@@ -269,6 +269,7 @@ static Ref_t create_detector(Detector& description, xml_h e, Ref_t /* sens_det *
   string      name    = x_det.nameStr();
   string      par_nam = x_par.nameStr();
   string      gdml   = x_gdml.attr<string>(_U(ref));
+  string      gdml_physvol = dd4hep::getAttrOrDefault<string>(x_gdml, _Unicode(physvol), "");
   DetElement  det_parent = description.detector(par_nam);
   TGDMLParse parser;
   if ( !gdml.empty() && gdml[0] == '/' )  {
@@ -293,6 +294,18 @@ static Ref_t create_detector(Detector& description, xml_h e, Ref_t /* sens_det *
   Volume mother = det_parent.volume();
   PlacedVolume pv;
 
+  if ( !gdml_physvol.empty() ) {
+    PlacedVolume node = volume->FindNode(gdml_physvol.c_str());
+    if ( !node.isValid() ) {
+      printout(ERROR,"ROOTGDMLParse","+++ Invalid gdml placed volume %s", gdml_physvol.c_str());
+      printout(ERROR,"ROOTGDMLParse","+++ Valid top-level nodes are:");
+      volume->PrintNodes();
+      except("ROOTGDMLParse","+++ Failed to parse GDML file:%s for node:%s",
+        gdml.c_str(), gdml_physvol.c_str());
+    }
+    volume = node.volume();
+  }
+
   if ( x_pos && x_rot )   {
     Rotation3D rot(RotationZYX(x_rot.z(),x_rot.y(),x_rot.x()));
     Transform3D transform(rot,Position(x_pos.x(),x_pos.y(),x_pos.z()));
-- 
GitLab