6 #ifndef HEPMC3_READERFACTORY_H
7 #define HEPMC3_READERFACTORY_H
25 std::string libHepMC3rootIO=
"libHepMC3rootIO.so.3";
26 #if defined(__darwin__) || defined(__APPLE__)
27 libHepMC3rootIO=
"libHepMC3rootIO.dydl";
29 #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
30 libHepMC3rootIO=
"HepMC3rootIO.dll";
34 if (filename.find(
"http://")!=std::string::npos) remote=
true;
35 if (filename.find(
"https://")!=std::string::npos) remote=
true;
36 if (filename.find(
"root://")!=std::string::npos) remote=
true;
37 if (filename.find(
"gsidcap://")!=std::string::npos) remote=
true;
39 std::vector<std::string> head;
43 #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
44 if (!(stat (filename.c_str(), &buffer)==0))
46 if (!(stat (filename.c_str(), &buffer)==0 && (S_ISFIFO(buffer.st_mode)|| S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode))))
49 printf(
"Error in deduce_reader: file %s does not exist or is not a regular file/FIFO/link\n",filename.c_str());
50 return std::shared_ptr<Reader> (
nullptr);
53 std::ifstream* file=
new std::ifstream(filename);
56 printf(
"Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
57 return std::shared_ptr<Reader>(
nullptr);
59 if(!file->is_open()) {
60 printf(
"Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
62 return std::shared_ptr<Reader>(
nullptr);
64 #if defined(__linux__) || defined(__darwin__)|| defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
65 pipe=S_ISFIFO(buffer.st_mode);
66 if (pipe) { printf(
"Info in deduce_reader: the file %s is a pipe\n",filename.c_str());
return deduce_reader(*file); }
70 while (std::getline(*file, line)&&nonempty<3) {
71 if (line.empty())
continue;
81 printf(
"Info in deduce_reader: Attempt ReaderRootTree for: %s\n",filename.c_str());
82 if( strncmp(head.at(0).c_str(),
"root",4) == 0||remote)
83 return std::make_shared<ReaderPlugin>(filename,libHepMC3rootIO,std::string(
"newReaderRootTreefile"));
86 printf(
"Info in deduce_reader: Attempt ReaderAscii for: %s\n",filename.c_str());
87 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
89 printf(
"Info in deduce_reader: Attempt ReaderAsciiHepMC2 for: %s\n",filename.c_str());
90 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
92 printf(
"Info in deduce_reader: Attempt ReaderLHEF for: %s\n",filename.c_str());
93 if( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
95 printf(
"Info in deduce_reader: Attempt ReaderHEPEVT for: %s\n",filename.c_str());
96 std::stringstream st_e(head.at(0).c_str());
106 if (attr==
' ')
continue;
111 HEPEVT=
static_cast<bool>(st_e>>m_i>>m_p);
116 printf(
"Info in deduce_reader: All attempts failed for: %s\n",filename.c_str());
117 return std::shared_ptr<Reader>(
nullptr);
124 std::vector<std::string> head;
127 size_t backnonempty=0;
128 while ( (back<200&&backnonempty<100)&&stream) {
char c=stream.get(); back++;
if (c==
'\n') {
if (head.back().length()!=0) head.push_back(
"");}
else { head.back()+=c; backnonempty++;} }
131 printf(
"Info in deduce_reader: input stream is too short or invalid.\n");
132 return std::shared_ptr<Reader>(
nullptr);
135 for (
size_t i=0; i<back; i++) stream.unget();
137 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
139 printf(
"Info in deduce_reader: Attempt ReaderAscii\n");
143 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
145 printf(
"Info in deduce_reader: Attempt ReaderAsciiHepMC2\n");
149 if( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
151 printf(
"Info in deduce_reader: Attempt ReaderLHEF\n");
154 printf(
"Info in deduce_reader: Attempt ReaderHEPEVT\n");
155 std::stringstream st_e(head.at(0).c_str());
165 if (attr==
' ')
continue;
170 HEPEVT=
static_cast<bool>(st_e>>m_i>>m_p);
174 printf(
"Info in deduce_reader: All attempts failed\n");
175 return std::shared_ptr<Reader>(
nullptr);
GenEvent I/O parsing and serialization for LHEF files.
Definition of class ReaderHEPEVT.
GenEvent I/O parsing for structured text files.
GenEvent I/O parsing and serialization for HEPEVT files.
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...
Parser for HepMC2 I/O files.
Definition of class ReaderAsciiHepMC2.
Definition of class ReaderAscii.
Fortran common block HEPEVT.
Definition of class ReaderPlugin.
Base class for all I/O readers.
Definition of class ReaderLHEF.