From fbbc1dbed654e34865ee196341d3c6ec9deca25a Mon Sep 17 00:00:00 2001
From: Markus Frank <markus.frank@cern.ch>
Date: Fri, 17 Oct 2014 14:27:26 +0000
Subject: [PATCH] Fix bug in event reading

---
 DDG4/examples/CLICSidSimu.py       |  5 +++--
 DDG4/include/DDG4/Geant4Particle.h |  2 ++
 DDG4/lcio/LCIOEventReader.cpp      |  3 ++-
 DDG4/src/Geant4InputAction.cpp     |  9 +++++---
 DDG4/src/Geant4Particle.cpp        | 34 ++++++++++++++++++++++++++++++
 5 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/DDG4/examples/CLICSidSimu.py b/DDG4/examples/CLICSidSimu.py
index 3e8ef6111..425fffb96 100644
--- a/DDG4/examples/CLICSidSimu.py
+++ b/DDG4/examples/CLICSidSimu.py
@@ -44,10 +44,10 @@ def run():
   kernel.eventAction().adopt(prt)
 
   # Configure I/O
-  evt_root = simple.setupROOTOutput('RootOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
   evt_lcio = simple.setupLCIOOutput('LcioOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
   evt_lcio.OutputLevel = Output.ERROR
 
+  evt_root = simple.setupROOTOutput('RootOutput','CLICSiD_'+time.strftime('%Y-%m-%d_%H-%M'))
 
   generator_output_level = Output.INFO
 
@@ -110,7 +110,8 @@ def run():
   #gen.Input = "LCIOStdHepReader|/home/frankm/SW/data/smuonLR_PointK_3TeV_BS_noBkg_run0001.stdhep"
   #gen.Input = "LCIOStdHepReader|/home/frankm/SW/data/bbbb_3TeV.stdhep"
   #gen.Input = "LCIOFileReader|/home/frankm/SW/data/mcparticles_pi-_5GeV.slcio"
-  gen.Input = "LCIOFileReader|/home/frankm/SW/data/bbbb_3TeV.slcio"
+  gen.Input = "LCIOFileReader|/home/frankm/SW/data/mcparticles_mu-_5GeV.slcio"
+  #gen.Input = "LCIOFileReader|/home/frankm/SW/data/bbbb_3TeV.slcio"
   #gen.Input = "LCIOStdHepReader|/home/frankm/SW/data/FCC-eh.stdhep"
   #gen.Input = "Geant4EventReaderHepMC|/home/frankm/SW/data/data.hepmc.txt"
   gen.OutputLevel = 4 # generator_output_level
diff --git a/DDG4/include/DDG4/Geant4Particle.h b/DDG4/include/DDG4/Geant4Particle.h
index 35bbb66b5..6e31d970a 100644
--- a/DDG4/include/DDG4/Geant4Particle.h
+++ b/DDG4/include/DDG4/Geant4Particle.h
@@ -298,6 +298,8 @@ namespace DD4hep {
       Geant4ParticleMap() {}
       /// Default destructor
       virtual ~Geant4ParticleMap();
+      /// Dump content
+      void dump()  const;
       /// Clear particle maps
       void clear();
       /// Adopt particle maps
diff --git a/DDG4/lcio/LCIOEventReader.cpp b/DDG4/lcio/LCIOEventReader.cpp
index f51da1a04..952b0593c 100644
--- a/DDG4/lcio/LCIOEventReader.cpp
+++ b/DDG4/lcio/LCIOEventReader.cpp
@@ -64,7 +64,8 @@ LCIOEventReader::readParticles(int event_number, vector<Particle*>& particles)
   else if ( m_numEvent == event_number )
     ret = readParticles(event_number,&primaries);
   else
-    ret = EVENT_READER_NO_DIRECT;
+    ret = readParticles(event_number,&primaries);
+    //ret = EVENT_READER_NO_DIRECT;
 
   ++m_numEvent;
   if ( ret != EVENT_READER_OK ) return ret;
diff --git a/DDG4/src/Geant4InputAction.cpp b/DDG4/src/Geant4InputAction.cpp
index 161a4e9de..e5c095576 100644
--- a/DDG4/src/Geant4InputAction.cpp
+++ b/DDG4/src/Geant4InputAction.cpp
@@ -46,11 +46,14 @@ Geant4EventReader::EventReaderStatus Geant4EventReader::skipEvent()  {
 /// Move to the indicated event number.
 Geant4EventReader::EventReaderStatus 
 Geant4EventReader::moveToEvent(int event_number)   {
-  if ( m_currEvent == event_number-1 )  {
+  if ( event_number >= INT_MIN )   {
+    return EVENT_READER_OK;  // Logic below does not work as expected.
+  }                          // This shortcuts it!
+  if ( m_currEvent == event_number )  {
     return EVENT_READER_OK;
   }
   else if ( hasDirectAccess() )   {
-    m_currEvent = event_number-1;
+    m_currEvent = event_number;
     return EVENT_READER_OK;
   }
   else if ( event_number<m_currEvent )   {
@@ -59,7 +62,7 @@ Geant4EventReader::moveToEvent(int event_number)   {
   else  {
     for(int i=m_currEvent; i<event_number;++i)
       skipEvent();
-    m_currEvent = event_number-1;
+    m_currEvent = event_number;
     return EVENT_READER_OK;
   }
   return EVENT_READER_ERROR;
diff --git a/DDG4/src/Geant4Particle.cpp b/DDG4/src/Geant4Particle.cpp
index a529c210a..fd1feb806 100644
--- a/DDG4/src/Geant4Particle.cpp
+++ b/DDG4/src/Geant4Particle.cpp
@@ -17,6 +17,7 @@
 #include "G4ParticleTable.hh"
 #include "G4ParticleDefinition.hh"
 #include "G4VProcess.hh"
+#include <iostream>
 
 using namespace DD4hep;
 using namespace DD4hep::Simulation;
@@ -328,6 +329,38 @@ void Geant4ParticleMap::clear()    {
   equivalentTracks.clear();
 }
 
+/// Dump content
+void Geant4ParticleMap::dump()  const  {
+  int cnt;
+  char text[64];
+  using namespace std;
+  const Geant4ParticleMap* m = this;
+
+  cnt = 0;
+  cout << "Particle map:" << endl;
+  for(Geant4ParticleMap::ParticleMap::const_iterator i=m->particleMap.begin(); i!=m->particleMap.end();++i)  {
+    ::snprintf(text,sizeof(text)," [%-4d:%p]",(*i).second->id,(void*)(*i).second);
+    cout << text;
+    if ( ++cnt == 8 ) {
+      cout << endl;
+      cnt = 0;
+    }
+  }
+  cout << endl;
+
+  cnt = 0;
+  cout << "Equivalents:" << endl;
+  for(Geant4ParticleMap::TrackEquivalents::const_iterator i=m->equivalentTracks.begin(); i!=m->equivalentTracks.end();++i)  {
+    ::snprintf(text,sizeof(text)," [%-5d : %-5d]",(*i).first,(*i).second);
+    cout << text;
+    if ( ++cnt == 8 ) {
+      cout << endl;
+      cnt = 0;
+    }
+  }
+  cout << endl;
+}
+
 /// Adopt particle maps
 void Geant4ParticleMap::adopt(ParticleMap& pm, TrackEquivalents& equiv)    {
   clear();
@@ -335,6 +368,7 @@ void Geant4ParticleMap::adopt(ParticleMap& pm, TrackEquivalents& equiv)    {
   equivalentTracks = equiv;
   pm.clear();
   equiv.clear();
+  //dumpMap(this);
 }
 
 /// Access the equivalent track id (shortcut to the usage of TrackEquivalents)
-- 
GitLab