From febdd5975d675daac999760ccc0e27fbaf9bc6ea Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Thu, 4 Jul 2024 10:27:05 +0200
Subject: [PATCH] Have real straws (thin tubes filled with gas) for BoxOfStraws
 examples

---
 DDCore/python/dd4hep_base.py                 | 18 ++++++++++++++++++
 examples/ClientTests/compact/BoxOfStraws.xml |  4 ++--
 examples/ClientTests/scripts/BoxOfStraws.py  | 12 +++++-------
 examples/ClientTests/src/BoxOfStraws_geo.cpp | 12 ++++++++----
 4 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/DDCore/python/dd4hep_base.py b/DDCore/python/dd4hep_base.py
index 46edb15eb..64328ef28 100644
--- a/DDCore/python/dd4hep_base.py
+++ b/DDCore/python/dd4hep_base.py
@@ -336,6 +336,21 @@ class Logger:
 
   def setPrintLevel(self, level):
     "Adjust printout level of dd4hep"
+    if isinstance(level, str):
+      if level == 'VERBOSE':
+        level = OutputLevel.VERBOSE
+      elif level == 'DEBUG':
+        level = OutputLevel.DEBUG
+      elif level == 'INFO':
+        level = OutputLevel.INFO
+      elif level == 'WARNING':
+        level = OutputLevel.WARNING
+      elif level == 'ERROR':
+        level = OutputLevel.ERROR
+      elif level == 'FATAL':
+        level = OutputLevel.FATAL
+      else:
+        level = int(level)
     dd4hep.setPrintLevel(level)
 
   def always(self, msg):
@@ -407,6 +422,9 @@ class CommandLine:
          have_help = True
     if have_help and help_call:
       help_call()
+    if self.data.get('print_level'):
+      log = Logger('CommandLine')
+      log.setPrintLevel(self.data.get('print_level'))
 
   def __getattr__(self, attr):
     if self.data.get(attr):
diff --git a/examples/ClientTests/compact/BoxOfStraws.xml b/examples/ClientTests/compact/BoxOfStraws.xml
index 82a9f6766..752b9381c 100644
--- a/examples/ClientTests/compact/BoxOfStraws.xml
+++ b/examples/ClientTests/compact/BoxOfStraws.xml
@@ -57,8 +57,8 @@
   <detectors>
     <detector id="1" name="BoxOfStrawsDet" type="DD4hep_BoxOfStraws" readout="BoxOfStrawsHits" vis="VisibleGreen" region="StrawRegion" limits="BoxOfStrawsLimitSet">
       <box      x="1*m"    y="1*m" z="1000*mm"  limits="BoxOfStrawsLimitSet" vis="VisibleRed"/>
-      <straw rmax="1*mm"   y="1*m" vis="VisibleBlue">
-        <material name="Iron"/>
+      <straw rmax="2*mm"   y="1*m" thickness="0.5*mm" vis="VisibleBlue">
+        <material name="Argon"/>
         <non_sensitive/>
       </straw>
       <position x="0*m"   y="0*m"   z="0*m"/>
diff --git a/examples/ClientTests/scripts/BoxOfStraws.py b/examples/ClientTests/scripts/BoxOfStraws.py
index 09e63c545..9eabae0e5 100644
--- a/examples/ClientTests/scripts/BoxOfStraws.py
+++ b/examples/ClientTests/scripts/BoxOfStraws.py
@@ -32,11 +32,10 @@ def run():
   args = DDG4.CommandLine()
   kernel = DDG4.Kernel()
   logger = DDG4.Logger('BoxOfStraws')
-  install_dir = os.environ['DD4hepExamplesINSTALL']
-  kernel.loadGeometry(str("file:" + install_dir + "/examples/ClientTests/compact/BoxOfStraws.xml"))
+  kernel.loadGeometry(str("file:" + os.environ['DD4hepExamplesINSTALL'] + "/examples/ClientTests/compact/BoxOfStraws.xml"))
 
   DDG4.importConstants(kernel.detectorDescription(), debug=False)
-  geant4 = DDG4.Geant4(kernel, tracker='Geant4TrackerCombineAction')
+  geant4 = DDG4.Geant4(kernel)
   geant4.printDetectors()
   # Configure UI
   if args.macro:
@@ -45,8 +44,6 @@ def run():
     ui = geant4.setupCshUI()
   if args.batch:
     ui.Commands = ['/run/beamOn ' + str(args.events), '/ddg4/UI/terminate']
-  if args.print:
-    logger.setPrintLevel(int(args.print))
   #
   # Configure field
   geant4.setupTrackingField(prt=True)
@@ -62,13 +59,14 @@ def run():
   seq, act = geant4.addDetectorConstruction("Geant4RegexSensitivesConstruction/ConstructSDRegEx")
   act.Detector = 'BoxOfStrawsDet'
   act.OutputLevel = Output.ALWAYS
-  act.Regex = '/world_volume_(.*)/BoxOfStrawsDet_(.*)/row_(.*)/straw_(.*)'
+  act.Regex = '/world_volume_(.*)/BoxOfStrawsDet_(.*)/layer_(.*)/straw_(.*)/gas_(.*)'
+  act.Regex = '/world_volume_(.*)/BoxOfStrawsDet_(.*)/layer_(.*)/straw_(.*)'
   #
   # Configure I/O
   geant4.setupROOTOutput('RootOutput', 'BoxOfStraws_' + time.strftime('%Y-%m-%d_%H-%M'))
   #
   # Setup particle gun
-  gun = geant4.setupGun("Gun", particle='e+', energy=10 * GeV, multiplicity=1)
+  gun = geant4.setupGun("Gun", particle='pi+', energy=100 * GeV, multiplicity=1)
   gun.enableUI()
   #
   # And handle the simulation particles.
diff --git a/examples/ClientTests/src/BoxOfStraws_geo.cpp b/examples/ClientTests/src/BoxOfStraws_geo.cpp
index afd12b1c3..058414b06 100644
--- a/examples/ClientTests/src/BoxOfStraws_geo.cpp
+++ b/examples/ClientTests/src/BoxOfStraws_geo.cpp
@@ -30,22 +30,26 @@ static Ref_t create_detector(Detector& description, xml_h e, SensitiveDetector s
   xml_dim_t    x_pos   = x_det.child(_U(position));
   xml_det_t    x_straw = x_det.child(_Unicode(straw));
   std::string  nam     = x_det.nameStr();
+  const double thick   = x_straw.thickness();
   const double delta   = 2e0*x_straw.rmax();
   const int    num_x   = int(2e0*x_box.x() / delta);
   const int    num_z   = int(2e0*x_box.z() / delta);
 
-  // Have box like straws: voxelization should be more efficient and for our test it does not matter.
   Tube   straw(0., x_straw.rmax()-tol, x_straw.y()-tol);
-  //Box    straw(x_straw.rmax()-tol, x_straw.rmax()-tol, x_straw.y()-tol);
-  Volume straw_vol("straw", straw, description.material(x_straw.materialStr()));
+  Volume straw_vol("straw", straw, description.material("Iron"));
+  
+  Tube   straw_gas(0., x_straw.rmax()-tol-thick, x_straw.y()-tol-thick);
+  Volume straw_gas_vol("gas", straw_gas, description.material(x_straw.materialStr()));
 
   straw_vol.setAttributes(description, x_straw.regionStr(), x_straw.limitsStr(), x_straw.visStr());
+  straw_vol.placeVolume(straw_gas_vol);
+
   printout(INFO, "BoxOfStraws", "%s: Straw: rmax: %7.3f y: %7.3f mat: %s vis: %s solid: %s",
            nam.c_str(), x_straw.rmax(), x_straw.y(), x_straw.materialStr().c_str(),
            x_straw.visStr().c_str(), straw.type());
   if ( x_straw.hasChild(_U(sensitive)) )  {
     sens.setType("tracker");
-    straw_vol.setSensitiveDetector(sens);
+    straw_gas_vol.setSensitiveDetector(sens);
   }
 
   Box    box(x_box.x(), x_box.y(), x_box.z());
-- 
GitLab