From 423f97683105a1c87d263b1fb274d08b8380089e Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Wed, 8 Aug 2018 17:38:28 +0200
Subject: [PATCH] Fix HEPMC reader for unknown generator status codes

---
 DDG4/examples/readHEPMC.py     |  6 +++---
 DDG4/src/Geant4Action.cpp      |  2 +-
 DDG4/src/Geant4InputAction.cpp |  1 +
 examples/DDG4/CMakeLists.txt   | 12 ++++++++++--
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/DDG4/examples/readHEPMC.py b/DDG4/examples/readHEPMC.py
index 41b43ec3a..98728e9f0 100644
--- a/DDG4/examples/readHEPMC.py
+++ b/DDG4/examples/readHEPMC.py
@@ -16,17 +16,17 @@ def run(input_file):
   description = kernel.detectorDescription()
   gen = DDG4.GeneratorAction(kernel,"Geant4InputAction/Input")
   kernel.generatorAction().adopt(gen)
-  gen.Input = "Geant4EventReaderHepMC|/home/frankm/SW/data/hepmc_geant4.dat"
-  gen.Input = "Geant4EventReaderHepMC|/home/frankm/SW/data/"
   gen.Input = "Geant4EventReaderHepMC|"+input_file
   gen.OutputLevel = Output.DEBUG
   gen.HaveAbort = False
   prim_vtx = DDG4.std_vector('dd4hep::sim::Geant4Vertex*')()
   parts = gen.new_particles()
   ret = 1
+  evtid=0
   while ret:
     try:
-      ret = gen.readParticles(0,prim_vtx,parts)
+      ret = gen.readParticles(evtid,prim_vtx,parts)
+      evtid = evtid + 1
     except Exception,X:
       logging.info( '\nException: readParticles: %s',str(X))
       ret = None
diff --git a/DDG4/src/Geant4Action.cpp b/DDG4/src/Geant4Action.cpp
index 090435b7f..5f0195c43 100644
--- a/DDG4/src/Geant4Action.cpp
+++ b/DDG4/src/Geant4Action.cpp
@@ -257,8 +257,8 @@ void Geant4Action::fatal(const char* fmt, ...) const {
 void Geant4Action::except(const char* fmt, ...) const {
   va_list args;
   va_start(args, fmt);
-  dd4hep::printout(dd4hep::FATAL, m_name, fmt, args);
   string err = dd4hep::format(m_name, fmt, args);
+  dd4hep::printout(dd4hep::FATAL, m_name, err.c_str());
   va_end(args);
   throw runtime_error(err);
 }
diff --git a/DDG4/src/Geant4InputAction.cpp b/DDG4/src/Geant4InputAction.cpp
index c607d8d81..ae451357f 100644
--- a/DDG4/src/Geant4InputAction.cpp
+++ b/DDG4/src/Geant4InputAction.cpp
@@ -63,6 +63,7 @@ Geant4EventReader::EventReaderStatus Geant4EventReader::skipEvent()  {
   ++m_currEvent;
   EventReaderStatus sc = readParticles(m_currEvent,vertices,particles);
   for_each(particles.begin(),particles.end(),detail::deleteObject<Particle>);
+  for_each(vertices.begin(),vertices.end(),detail::deleteObject<Vertex>);
   return sc;
 }
 
diff --git a/examples/DDG4/CMakeLists.txt b/examples/DDG4/CMakeLists.txt
index ed1ceb715..1768f62c5 100644
--- a/examples/DDG4/CMakeLists.txt
+++ b/examples/DDG4/CMakeLists.txt
@@ -16,6 +16,7 @@ dd4hep_configure_output ()
 dd4hep_package ( DDG4 MAJOR 0 MINOR 0 PATCH 1
   USES  [DD4hep REQUIRED COMPONENTS DDCore]
 )
+set(DDG4examples_INSTALL  ${CMAKE_INSTALL_PREFIX}/examples/DDG4)
 #
 #---Geant4 Testsing-----------------------------------------------------------------
 #
@@ -47,7 +48,14 @@ if (DD4HEP_USE_GEANT4)
   dd4hep_add_test_reg( test_DDG4_HepMC_reader
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_DDG4.sh"
     EXEC_ARGS  python ${DD4hep_DIR}/examples/DDG4/examples/readHEPMC.py
-                      ${CMAKE_INSTALL_PREFIX}/examples/DDG4/data/hepmc_geant4.dat
+                      ${DDG4examples_INSTALL}/data/hepmc_geant4.dat
     REQUIRES   DDG4 Geant4
-    REGEX_PASS "EventReaderHepMC::moveToEvent INFO  Current event number: 9")
+    REGEX_PASS "Geant4InputAction\\[Input\\]: Event 10 Error when moving to event -  EOF")
+  # Test HepMC input reader
+  dd4hep_add_test_reg( test_DDG4_HepMC_reader_minbias
+    COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_DDG4.sh"
+    EXEC_ARGS  python ${DD4hep_DIR}/examples/DDG4/examples/readHEPMC.py
+                      ${DDG4examples_INSTALL}/data/LHCb_MinBias_HepMC.txt
+    REQUIRES   DDG4 Geant4
+    REGEX_PASS "Geant4InputAction\\[Input\\]: Event 27 Error when moving to event -  EOF")
 endif()
-- 
GitLab