From 2be7b002305d722b8b400ee5ed62bc016cf0000e Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Fri, 2 Mar 2018 14:09:58 +0100
Subject: [PATCH] Fixes.

---
 DDG4/examples/CLICSidSimuLCIO.py   |  2 +-
 DDG4/include/DDG4/Geant4Particle.h |  5 ++++
 DDG4/src/Geant4Particle.cpp        | 39 ++++++++++++++++++++++++++++++
 DDG4/src/Geant4PhysicsList.cpp     | 32 ++----------------------
 4 files changed, 47 insertions(+), 31 deletions(-)

diff --git a/DDG4/examples/CLICSidSimuLCIO.py b/DDG4/examples/CLICSidSimuLCIO.py
index 8e79b4518..80e060dc5 100644
--- a/DDG4/examples/CLICSidSimuLCIO.py
+++ b/DDG4/examples/CLICSidSimuLCIO.py
@@ -18,7 +18,6 @@ def run():
   install_dir = os.environ['DD4hepINSTALL']
   example_dir = install_dir+'/examples/DDG4/examples';
   kernel.loadGeometry("file:"+install_dir+"/examples/CLICSiD/compact/compact.xml")
-  kernel.loadXML("file:"+example_dir+"/DDG4_field.xml")
 
   simple = DDG4.Simple(kernel,tracker='LcioTestTrackerAction')
   simple.printDetectors()
@@ -125,6 +124,7 @@ def run():
   ph.addParticleConstructor('G4BosonConstructor')
   ph.addParticleConstructor('G4LeptonConstructor')
   ph.addParticleProcess('e[+-]','G4eMultipleScattering',-1,1,1)
+  ph.addPhysicsConstructor('G4StepLimiterPhysics')
   ph.addPhysicsConstructor('G4OpticalPhysics')
   ph.enableUI()
   phys.add(ph)
diff --git a/DDG4/include/DDG4/Geant4Particle.h b/DDG4/include/DDG4/Geant4Particle.h
index 10ec3dd3c..b1dc6a077 100644
--- a/DDG4/include/DDG4/Geant4Particle.h
+++ b/DDG4/include/DDG4/Geant4Particle.h
@@ -27,6 +27,7 @@ class G4VProcess;
 // C/C++ include files
 #include <set>
 #include <map>
+#include <vector>
 
 /// Namespace for the AIDA detector description toolkit
 namespace dd4hep {
@@ -234,6 +235,10 @@ namespace dd4hep {
       /// Offset all particle identifiers (id, parents, daughters) by a constant number
       void offset(int off)  const;
 
+      /// Access Geant4 particle definitions by regular expression
+      static std::vector<G4ParticleDefinition*> g4DefinitionsRegEx(const std::string& expression);
+      /// Access Geant4 particle definitions by exact match
+      static G4ParticleDefinition* g4DefinitionsExact(const std::string& expression);
     };
 
     inline Geant4ParticleHandle::Geant4ParticleHandle(const Geant4ParticleHandle& c)
diff --git a/DDG4/src/Geant4Particle.cpp b/DDG4/src/Geant4Particle.cpp
index 61aa060e3..ae2e83d64 100644
--- a/DDG4/src/Geant4Particle.cpp
+++ b/DDG4/src/Geant4Particle.cpp
@@ -26,6 +26,7 @@
 
 #include <sstream>
 #include <iostream>
+#include <regex.h>
 
 using namespace dd4hep;
 using namespace dd4hep::sim;
@@ -155,6 +156,44 @@ std::string Geant4ParticleHandle::particleType() const   {
   return text;
 }
 
+/// Access Geant4 particle definitions by regular expression
+std::vector<G4ParticleDefinition*> Geant4ParticleHandle::g4DefinitionsRegEx(const std::string& expression)   {
+  std::vector<G4ParticleDefinition*> results;
+  std::string exp = expression;   //'^'+expression+"$";
+  G4ParticleTable* pt = G4ParticleTable::GetParticleTable();
+  G4ParticleTable::G4PTblDicIterator* iter = pt->GetIterator();
+  char msgbuf[128];
+  regex_t reg;
+  int ret = ::regcomp(&reg, exp.c_str(), 0);
+  if (ret) {
+    throw std::runtime_error(format("Geant4ParticleHandle", "REGEX: Failed to compile particle name %s", exp.c_str()));
+  }
+  results.clear();
+  iter->reset();
+  while ((*iter)()) {
+    G4ParticleDefinition* p = iter->value();
+    ret = ::regexec(&reg, p->GetParticleName().c_str(), 0, NULL, 0);
+    if (!ret)
+      results.push_back(p);
+    else if (ret == REG_NOMATCH)
+      continue;
+    else {
+      ::regerror(ret, &reg, msgbuf, sizeof(msgbuf));
+      ::regfree(&reg);
+      throw std::runtime_error(format("Geant4ParticleHandle", "REGEX: Failed to match particle name %s err=%s", exp.c_str(), msgbuf));
+    }
+  }
+  ::regfree(&reg);
+  return results;
+}
+
+/// Access Geant4 particle definitions by exact match
+G4ParticleDefinition* Geant4ParticleHandle::g4DefinitionsExact(const std::string& expression)   {
+  G4ParticleTable*      tab = G4ParticleTable::GetParticleTable();
+  G4ParticleDefinition* def = tab->FindParticle(expression);
+  return def;
+}
+
 /// Access to the creator process name
 std::string Geant4ParticleHandle::processName() const   {
   if ( particle->process ) return particle->process->GetProcessName();
diff --git a/DDG4/src/Geant4PhysicsList.cpp b/DDG4/src/Geant4PhysicsList.cpp
index a876ae7b6..47bd6ccfd 100644
--- a/DDG4/src/Geant4PhysicsList.cpp
+++ b/DDG4/src/Geant4PhysicsList.cpp
@@ -14,6 +14,7 @@
 // Framework include files
 #include "DDG4/Geant4PhysicsList.h"
 #include "DDG4/Geant4UIMessenger.h"
+#include "DDG4/Geant4Particle.h"
 #include "DDG4/Geant4Kernel.h"
 #include "DD4hep/InstanceCount.h"
 #include "DD4hep/Printout.h"
@@ -63,34 +64,6 @@ namespace {
     }
     virtual void ConstructParticle()     {  seq->constructParticles(phys);     }
   };
-
-  void _findDef(const string& expression, vector<G4ParticleDefinition*>& results) {
-    string exp = expression;   //'^'+expression+"$";
-    G4ParticleTable* pt = G4ParticleTable::GetParticleTable();
-    G4ParticleTable::G4PTblDicIterator* iter = pt->GetIterator();
-    char msgbuf[128];
-    regex_t reg;
-    int ret = ::regcomp(&reg, exp.c_str(), 0);
-    if (ret) {
-      throw runtime_error(format("Geant4PhysicsList", "REGEX: Failed to compile particle name %s", exp.c_str()));
-    }
-    results.clear();
-    iter->reset();
-    while ((*iter)()) {
-      G4ParticleDefinition* p = iter->value();
-      ret = ::regexec(&reg, p->GetParticleName().c_str(), 0, NULL, 0);
-      if (!ret)
-        results.push_back(p);
-      else if (ret == REG_NOMATCH)
-        continue;
-      else {
-        ::regerror(ret, &reg, msgbuf, sizeof(msgbuf));
-        ::regfree(&reg);
-        throw runtime_error(format("Geant4PhysicsList", "REGEX: Failed to match particle name %s err=%s", exp.c_str(), msgbuf));
-      }
-    }
-    ::regfree(&reg);
-  }
 }
 
 /// Default constructor
@@ -253,8 +226,7 @@ void Geant4PhysicsList::constructProcesses(G4VUserPhysicsList* physics_pointer)
   for (PhysicsProcesses::const_iterator i = m_processes.begin(); i != m_processes.end(); ++i) {
     const string& part_name = (*i).first;
     const ParticleProcesses& procs = (*i).second;
-    vector<G4ParticleDefinition*> defs;
-    _findDef(part_name, defs);
+    vector<G4ParticleDefinition*> defs(Geant4ParticleHandle::g4DefinitionsRegEx(part_name));
     if (defs.empty()) {
       except("Particle:%s Cannot find the corresponding entry in the particle table.", part_name.c_str());
     }
-- 
GitLab