Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-06 07:54:56

0001 #include "DD4hep/DDTest.h"
0002 
0003 #include <iostream>
0004 #include <iomanip>
0005 #include <vector>
0006 #include <algorithm>
0007 #include <exception>
0008 
0009 #include "DD4hep/Plugins.h"
0010 #include "DD4hep/Primitives.h"
0011 #include "DDG4/Geant4InputAction.h"
0012 #include "DDG4/Geant4Particle.h"
0013 #include "DDG4/Geant4Vertex.h"
0014 
0015 //using namespace dd4hep::sim;
0016 typedef dd4hep::sim::Geant4Vertex   Vertex;
0017 typedef dd4hep::sim::Geant4Particle Particle;
0018 
0019 static dd4hep::DDTest test( "EventReader" ) ;
0020 
0021 class TestTuple {
0022 public:
0023   std::string readerType;
0024   std::string inputFile;
0025   bool skipEOF; //LCIO skipNEvents does not signal end of file??
0026   TestTuple( std::string const& rT, std::string const& iF, bool sEOF=false): readerType(rT), inputFile(iF), skipEOF(sEOF) {}
0027 };
0028 
0029 
0030 int main(int argc, char** argv ){
0031 
0032   if( argc < 2 ) {
0033     std::cout << " usage:  test_EventReaders Path/To/InputFiles " << std::endl ;
0034     exit(1) ;
0035   }
0036 
0037   std::string inputFileFolder = argv[1];
0038 
0039   std::vector<TestTuple> tests;
0040   #ifdef DD4HEP_USE_LCIO
0041   tests.push_back( TestTuple( "LCIOStdHepReader", "bbudsc_3evt.stdhep" ) );
0042   tests.push_back( TestTuple( "LCIOFileReader",   "muons.slcio" , /*skipEOF= */ true ) );
0043   #endif
0044   tests.push_back( TestTuple( "Geant4EventReaderHepEvtShort", "Muons10GeV.HEPEvt" ) );
0045   #ifdef DD4HEP_USE_HEPMC3
0046   tests.push_back( TestTuple( "HEPMC3FileReader", "g4pythia.hepmc", /*skipEOF= */ true) );
0047   tests.push_back( TestTuple( "HEPMC3FileReader", "Pythia_output.hepmc", /*skipEOF= */ true) );
0048   #endif
0049   #ifdef DD4HEP_USE_EDM4HEP
0050   tests.push_back( TestTuple( "EDM4hepFileReader", "ZH250_ISR.edm4hep.root", /*skipEOF= */ true) );
0051   #endif
0052 
0053   try{
0054     for(std::vector<TestTuple>::const_iterator it = tests.begin(); it != tests.end(); ++it) {
0055       std::string readerType = (*it).readerType;
0056       std::string fileName = (*it).inputFile;
0057       bool skipEOF =  (*it).skipEOF;
0058       //InputFiles are in DDTest/inputFiles, argument is cmake_source directory
0059       std::string inputFile = argv[1]+ std::string("/inputFiles/") + fileName;
0060       dd4hep::sim::Geant4EventReader* thisReader = dd4hep::PluginService::Create<dd4hep::sim::Geant4EventReader*>(readerType, inputFile);
0061       if ( not thisReader ) {
0062         test.log( "Plugin not found" );
0063         test.log( readerType );
0064         continue;
0065       }
0066       test( thisReader->currentEventNumber() == 0 , readerType + std::string("Initial Event Number") );
0067       if (!thisReader->hasDirectAccess()) {
0068         thisReader->moveToEvent(1);
0069         test( thisReader->currentEventNumber() == 1 , readerType + std::string("Event Number after Skip") );
0070       }
0071       std::vector<Particle*> particles;
0072       std::vector<Vertex*> vertices ;
0073       dd4hep::sim::Geant4EventReader::EventReaderStatus sc = thisReader->readParticles(2,vertices,particles);
0074       std::for_each(particles.begin(),particles.end(),dd4hep::detail::deleteObject<Particle>);
0075       test( thisReader->currentEventNumber() == 2 && sc == dd4hep::sim::Geant4EventReader::EVENT_READER_OK,
0076             readerType + std::string("Event Number Read") );
0077 
0078       //Reset Reader to check what happens if moving too far in the file
0079       if (not skipEOF) {
0080         thisReader = dd4hep::PluginService::Create<dd4hep::sim::Geant4EventReader*>(readerType, std::move(inputFile));
0081         sc = thisReader->moveToEvent(1000000);
0082         test( sc != dd4hep::sim::Geant4EventReader::EVENT_READER_OK , readerType + std::string("EventReader False") );
0083       }
0084     }
0085   } catch( std::exception &e ){
0086     test.error("Exception occurred:");
0087     test.log(e.what());
0088   }
0089   return 0;
0090 }
0091