diff --git a/k4Gen/src/components/HepMC2FileReader.cpp b/k4Gen/src/components/HepMC2FileReader.cpp new file mode 100644 index 0000000..7287fbb --- /dev/null +++ b/k4Gen/src/components/HepMC2FileReader.cpp @@ -0,0 +1,39 @@ + +#include "HepMC2FileReader.h" + +#include "GaudiKernel/IEventProcessor.h" +#include "GaudiKernel/IIncidentSvc.h" +#include "GaudiKernel/Incident.h" + +DECLARE_COMPONENT(HepMC2FileReader) + +HepMC2FileReader::HepMC2FileReader(const std::string& type, const std::string& name, const IInterface* parent) + : GaudiTool(type, name, parent), m_file(nullptr) { + declareInterface(this); +} + +HepMC2FileReader::~HepMC2FileReader() { ; } + +StatusCode HepMC2FileReader::initialize() { + if (m_filename.empty()) { + error() << "Input file name is not specified!" << endmsg; + return StatusCode::FAILURE; + } + // open file using HepMC routines + m_file = std::make_unique(m_filename.value()); + StatusCode sc = GaudiTool::initialize(); + return sc; +} + +StatusCode HepMC2FileReader::getNextEvent(HepMC3::GenEvent& event) { + if (!m_file->read_event(event)) { + error() << "Premature end of file: Please set the number of events according to hepMC file." << endmsg; + return Error("Reached end of file before finished processing"); + } + return StatusCode::SUCCESS; +} + +StatusCode HepMC2FileReader::finalize() { + m_file.reset(); + return GaudiTool::finalize(); +} diff --git a/k4Gen/src/components/HepMC2FileReader.h b/k4Gen/src/components/HepMC2FileReader.h new file mode 100644 index 0000000..f8fb501 --- /dev/null +++ b/k4Gen/src/components/HepMC2FileReader.h @@ -0,0 +1,30 @@ + +#ifndef GENERATION_HEPMC2FILEREADER_H +#define GENERATION_HEPMC2FILEREADER_H + +#include "GaudiAlg/GaudiTool.h" +#include "Generation/IHepMCProviderTool.h" + +#include "HepMC3/GenEvent.h" +#include "HepMC3/ReaderAsciiHepMC2.h" + +class HepMC2FileReader : public GaudiTool, virtual public IHepMCProviderTool { +public: + HepMC2FileReader(const std::string& type, const std::string& name, const IInterface* parent); + virtual ~HepMC2FileReader(); ///< Destructor + virtual StatusCode initialize(); + virtual StatusCode finalize(); + + /// Wrapper for HepMC's fill_next_event() -- + /// as in the hepmc original, the user is responsible + /// for the deletion of the event returned from this function + virtual StatusCode getNextEvent(HepMC3::GenEvent& event); + /// Wrapper for HepMC file io. + +private: + void close(); + Gaudi::Property m_filename{this, "Filename", "", "Name of the HepMC file to read"}; + std::unique_ptr m_file; +}; + +#endif // GENERATION_HEPMC2FILEREADER_H diff --git a/k4Gen/src/components/HepMCFileReaderTool.cpp b/k4Gen/src/components/HepMCFileReader.cpp similarity index 86% rename from k4Gen/src/components/HepMCFileReaderTool.cpp rename to k4Gen/src/components/HepMCFileReader.cpp index 16a6487..7730457 100644 --- a/k4Gen/src/components/HepMCFileReaderTool.cpp +++ b/k4Gen/src/components/HepMCFileReader.cpp @@ -1,5 +1,5 @@ -#include "HepMCFileReaderTool.h" +#include "HepMCFileReader.h" #include "GaudiKernel/IEventProcessor.h" #include "GaudiKernel/IIncidentSvc.h" @@ -27,10 +27,6 @@ StatusCode HepMCFileReader::initialize() { StatusCode HepMCFileReader::getNextEvent(HepMC3::GenEvent& event) { if (!m_file->read_event(event)) { - // if (m_file->rdstate() == std::ios::eofbit) { - // error() << "Error reading HepMC file" << endmsg; - // return StatusCode::FAILURE; - // } error() << "Premature end of file: Please set the number of events according to hepMC file." << endmsg; return Error("Reached end of file before finished processing"); } diff --git a/k4Gen/src/components/HepMCFileReaderTool.h b/k4Gen/src/components/HepMCFileReader.h similarity index 100% rename from k4Gen/src/components/HepMCFileReaderTool.h rename to k4Gen/src/components/HepMCFileReader.h