6#ifndef HEPMC3_READERFACTORY_H
7#define HEPMC3_READERFACTORY_H
35 std::string libHepMC3rootIO=
"libHepMC3rootIO.so.3";
36 #if defined(__darwin__) || defined(__APPLE__)
37 libHepMC3rootIO=
"libHepMC3rootIO.dydl";
39 #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
40 libHepMC3rootIO=
"HepMC3rootIO.dll";
44 if (filename.find(
"http://")!=std::string::npos) remote=
true;
45 if (filename.find(
"https://")!=std::string::npos) remote=
true;
46 if (filename.find(
"root://")!=std::string::npos) remote=
true;
47 if (filename.find(
"gsidcap://")!=std::string::npos) remote=
true;
49 std::vector<std::string> head;
53 #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
54 if (!(stat (filename.c_str(), &buffer)==0))
56 if (!(stat (filename.c_str(), &buffer)==0 && (S_ISFIFO(buffer.st_mode)|| S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode))))
59 HEPMC3_ERROR(
"deduce_reader: file " << filename <<
" does not exist or is not a regular file/FIFO/link");
60 return std::shared_ptr<Reader> (
nullptr);
63 std::ifstream* file=
new std::ifstream(filename);
66 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
67 return std::shared_ptr<Reader>(
nullptr);
69 if (!file->is_open()) {
70 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
72 return std::shared_ptr<Reader>(
nullptr);
75 #if defined(__linux__) || defined(__darwin__)|| defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
76 pipe = S_ISFIFO(buffer.st_mode);
78 HEPMC3_DEBUG(0,
"deduce_reader: the file " << filename <<
" is a pipe");
85 while (std::getline(*file, line) && nonempty < 3) {
86 if (line.empty())
continue;
96 HEPMC3_DEBUG(0,
"deduce_reader: Attempt ReaderRootTree for " << filename);
97 if ( strncmp(head.at(0).c_str(),
"root",4) == 0||remote)
98 return std::make_shared<ReaderPlugin>(filename,libHepMC3rootIO,std::string(
"newReaderRootTreefile"));
101 HEPMC3_DEBUG(0,
"Attempt ReaderAscii for " << filename);
102 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
104 HEPMC3_DEBUG(0,
"Attempt ReaderAsciiHepMC2 for " << filename);
105 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
108 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
110 HEPMC3_DEBUG(0,
"Attempt ReaderHEPEVT for " << filename);
111 std::stringstream st_e(head.at(0).c_str());
121 if (attr==
' ')
continue;
126 HEPEVT=
static_cast<bool>(st_e>>m_i>>m_p);
131 HEPMC3_DEBUG(0,
"deduce_reader: all attempts failed for " << filename);
132 return std::shared_ptr<Reader>(
nullptr);
139 std::vector<std::string> head;
142 size_t backnonempty=0;
143 while ( (back < 200 && backnonempty < 100) && stream) {
144 char c = stream.get(); back++;
146 if (head.back().length()!=0) head.push_back(
"");
148 head.back()+=c; backnonempty++;
154 return std::shared_ptr<Reader>(
nullptr);
157 for (
size_t i=0; i<back; i++) stream.unget();
159 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
165 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
171 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
177 std::stringstream st_e(head.at(0).c_str());
187 if (attr==
' ')
continue;
192 HEPEVT=
static_cast<bool>(st_e>>m_i>>m_p);
197 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.