From 6dcc87100e366aa157090480675b46d32c028a45 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Tue, 11 Oct 2016 12:03:30 +0200
Subject: [PATCH] Fix example to read HepMC files.

---
 DDG4/examples/readHEPMC.py            |  7 ++++++-
 DDG4/include/DDG4/Geant4InputAction.h |  2 ++
 DDG4/src/Geant4InputAction.cpp        | 18 ++++++++++++++----
 examples/DDG4/CMakeLists.txt          |  4 ++--
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/DDG4/examples/readHEPMC.py b/DDG4/examples/readHEPMC.py
index 9834519b5..ee48bf212 100644
--- a/DDG4/examples/readHEPMC.py
+++ b/DDG4/examples/readHEPMC.py
@@ -17,6 +17,7 @@ def run(input_file):
   gen.Input = "Geant4EventReaderHepMC|/home/frankm/SW/data/"
   gen.Input = "Geant4EventReaderHepMC|"+input_file
   gen.OutputLevel = Output.DEBUG
+  gen.HaveAbort = False
   prim_vtx = DDG4.Geant4Vertex()
   prim_vtx.x = 0.0
   prim_vtx.y = 0.0
@@ -24,7 +25,11 @@ def run(input_file):
   parts = gen.new_particles()
   ret = 1
   while ret:
-    ret = gen.readParticles(0,prim_vtx,parts)
+    try:
+      ret = gen.readParticles(0,prim_vtx,parts)
+    except Exception,X:
+      print '\nException: readParticles:',str(X)
+      ret = None
     if ret:
       for p in parts:
         print 'ID:%5d PDG-id:%8d Charge:%1d Mass:%8.3g Momentum:(%8.2g,%8.2g,%8.2g) '\
diff --git a/DDG4/include/DDG4/Geant4InputAction.h b/DDG4/include/DDG4/Geant4InputAction.h
index b71b03715..4333e996d 100644
--- a/DDG4/include/DDG4/Geant4InputAction.h
+++ b/DDG4/include/DDG4/Geant4InputAction.h
@@ -123,6 +123,8 @@ namespace DD4hep  {
       Geant4EventReader*  m_reader;
       /// current event number without initially skipped events
       int m_currentEventNumber;
+      /// Flag to call abortEvent in case of failure (default: true)
+      bool m_abort;
 
     public:
       /// Read an event and return a LCCollectionVec of MCParticles.
diff --git a/DDG4/src/Geant4InputAction.cpp b/DDG4/src/Geant4InputAction.cpp
index 9f3112502..559ea45f3 100644
--- a/DDG4/src/Geant4InputAction.cpp
+++ b/DDG4/src/Geant4InputAction.cpp
@@ -84,6 +84,7 @@ Geant4InputAction::Geant4InputAction(Geant4Context* ctxt, const string& nam)
   declareProperty("Sync",           m_firstEvent=0);
   declareProperty("Mask",           m_mask = 0);
   declareProperty("MomentumScale",  m_momScale = 1.0);
+  declareProperty("HaveAbort",      m_abort = true);
   m_needsControl = true;
 }
 
@@ -131,13 +132,22 @@ int Geant4InputAction::readParticles(int evt_number,
   }
   int status = m_reader->moveToEvent(evid);
   if ( Geant4EventReader::EVENT_READER_OK != status )  {
-    abortRun(issue(evid)+"Error when moving to event - may be end of file.",
-             "Error when reading file %s",m_input.c_str());
+    string msg = issue(evid)+"Error when moving to event - may be end of file.";
+    if ( m_abort )  {
+      abortRun(msg,"Error when reading file %s",m_input.c_str());
+      return status;
+    }
+    except("%s Error when reading file %s.", msg.c_str(), m_input.c_str());
+    return status;
   }
   status = m_reader->readParticles(evid, prim_vertex, particles);
   if ( Geant4EventReader::EVENT_READER_OK != status )  {
-    abortRun(issue(evid)+"Error when reading file - may be end of file.",
-             "Error when reading file %s",m_input.c_str());
+    string msg = issue(evid)+"Error when moving to event - may be end of file.";
+    if ( m_abort )  {
+      abortRun(msg,"Error when reading file %s",m_input.c_str());
+      return status;
+    }
+    except("%s Error when reading file %s.", msg.c_str(), m_input.c_str());
   }
   return status;
 }
diff --git a/examples/DDG4/CMakeLists.txt b/examples/DDG4/CMakeLists.txt
index e662c4f98..c7edeba44 100644
--- a/examples/DDG4/CMakeLists.txt
+++ b/examples/DDG4/CMakeLists.txt
@@ -28,8 +28,8 @@ if (DD4HEP_USE_GEANT4)
   # Test HepMC input reader
   dd4hep_add_test_reg( test_DDG4_HepMC_reader
     COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_DDG4.sh"
-    EXEC_ARGS  python ${CMAKE_INSTALL_PREFIX}/python/examples/DDG4/examples/readHEPMC.py
+    EXEC_ARGS  python ${DD4hep_DIR}/examples/DDG4/examples/readHEPMC.py
             ${CMAKE_INSTALL_PREFIX}/examples/DDG4/data/hepmc_geant4.dat
     REQUIRES   DDG4 Geant4
-    REGEX_PASS "EventReaderHepMC::moveToEvent INFO  Current event number: 10")
+    REGEX_PASS "EventReaderHepMC::moveToEvent INFO  Current event number: 9")
 endif()
-- 
GitLab