6#ifndef HEPMC3_READERFACTORY_H
7#define HEPMC3_READERFACTORY_H
19#include "HepMC3/CompressedIO.h"
26std::shared_ptr<Reader>
deduce_reader(std::shared_ptr<std::istream> stream);
37 std::string libHepMC3rootIO =
"libHepMC3rootIO.so.3";
38#if defined(__darwin__) || defined(__APPLE__)
39 libHepMC3rootIO =
"libHepMC3rootIO.dylib";
41#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
42 libHepMC3rootIO =
"HepMC3rootIO.dll";
46 if (filename.find(
"http://") != std::string::npos) remote =
true;
47 if (filename.find(
"https://") != std::string::npos) remote =
true;
48 if (filename.find(
"root://") != std::string::npos) remote =
true;
49 if (filename.find(
"gsidcap://") != std::string::npos) remote =
true;
51 std::vector<std::string> head;
55#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
56 if (!(stat (filename.c_str(), &buffer) == 0))
58 if (!(stat (filename.c_str(), &buffer) == 0 && (S_ISFIFO(buffer.st_mode) || S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode))))
61 HEPMC3_ERROR(
"deduce_reader: file " << filename <<
" does not exist or is not a regular file/FIFO/link");
62 return std::shared_ptr<Reader> (
nullptr);
65 std::ifstream* file=
new std::ifstream(filename);
68 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
69 return std::shared_ptr<Reader>(
nullptr);
71 if (!file->is_open()) {
72 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
74 return std::shared_ptr<Reader>(
nullptr);
77#if defined(__linux__) || defined(__darwin__)|| defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
78 pipe = S_ISFIFO(buffer.st_mode);
80 HEPMC3_DEBUG(0,
"deduce_reader: the file " << filename <<
" is a pipe");
87 while (std::getline(*file, line) && nonempty < 3) {
88 if (line.empty())
continue;
98 HEPMC3_DEBUG(0,
"deduce_reader: Attempt ReaderRootTree for " << filename);
99 if ( strncmp(head.at(0).c_str(),
"root", 4) == 0 || remote)
100 return std::make_shared<ReaderPlugin>(filename,libHepMC3rootIO,std::string(
"newReaderRootTreefile"));
103#if HEPMC3_USE_COMPRESSION
106 snprintf(buf,6,
"%s",head.at(0).c_str());
107 Compression det = detect_compression_type(buf, buf + 6);
108 if ( det != Compression::plaintext ) {
109 HEPMC3_DEBUG(0,
"Detected supported compression " << std::to_string(det));
110 return deduce_reader(std::shared_ptr< std::istream >(
new ifstream(filename.c_str())));
113 HEPMC3_DEBUG(0,
"Attempt ReaderAscii for " << filename);
114 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
116 HEPMC3_DEBUG(0,
"Attempt ReaderAsciiHepMC2 for " << filename);
117 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
120 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
122 HEPMC3_DEBUG(0,
"Attempt ReaderHEPEVT for " << filename);
123 std::stringstream st_e(head.at(0).c_str());
129 if (!(st_e >> attr)) {
133 if (attr ==
' ')
continue;
138 HEPEVT=
static_cast<bool>(st_e >> m_i >> m_p);
143 HEPMC3_DEBUG(0,
"deduce_reader: all attempts failed for " << filename);
144 return std::shared_ptr<Reader>(
nullptr);
151 std::vector<std::string> head;
154 size_t backnonempty = 0;
155 while ( (back < 200 && backnonempty < 100) && stream) {
156 char c = stream.get();
159 if (head.back().length() != 0) head.push_back(
"");
168 return std::shared_ptr<Reader>(
nullptr);
171 for (
size_t i = 0; i < back; i++) stream.unget();
173 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
179 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
185 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
191 std::stringstream st_e(head.at(0).c_str());
197 if (!(st_e >> attr)) {
201 if (attr ==
' ')
continue;
206 HEPEVT =
static_cast<bool>(st_e >> m_i >> m_p);
211 return std::shared_ptr<Reader>(
nullptr);
216 std::vector<std::string> head;
219 size_t backnonempty = 0;
220 while ( (back < 200 && backnonempty < 100) && stream) {
221 char c = stream->get();
224 if (head.back().length() != 0) head.push_back(
"");
233 return std::shared_ptr<Reader>(
nullptr);
236 for (
size_t i = 0; i < back; i++) stream->unget();
238 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
244 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
250 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
256 std::stringstream st_e(head.at(0).c_str());
262 if (!(st_e >> attr)) {
266 if (attr ==
' ')
continue;
271 HEPEVT =
static_cast<bool>(st_e >> m_i >> m_p);
276 return std::shared_ptr<Reader>(
nullptr);
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of class ReaderAsciiHepMC2.
Definition of class ReaderAscii.
Definition of class ReaderHEPEVT.
Definition of class ReaderLHEF.
Definition of class ReaderPlugin.
Parser for HepMC2 I/O files.
GenEvent I/O parsing for structured text files.
GenEvent I/O parsing and serialization for HEPEVT files.
GenEvent I/O parsing and serialization for LHEF files.
Base class for all I/O readers.
std::shared_ptr< Reader > deduce_reader(std::istream &stream)
This function will deduce the type of input stream based on its content and will return appropriate R...
Fortran common block HEPEVT.