6 #ifndef HEPMC3_READERFACTORY_H
7 #define HEPMC3_READERFACTORY_H
25 std::string libHepMC3rootIO=
"libHepMC3rootIO.so.3";
27 libHepMC3rootIO=
"libHepMC3rootIO.dydl";
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;
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);
55 if(!file->is_open()) {
56 printf(
"Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
58 return shared_ptr<Reader>(
nullptr);
61 pipe=S_ISFIFO(buffer.st_mode);
62 if (pipe) { printf(
"Info in deduce_reader: the file %s is a pipe\n",filename.c_str());
return deduce_reader(*file); }
66 while (std::getline(*file, line)&&nonempty<3) {
67 if (line.empty())
continue;
72 if (file)
delete file;
77 printf(
"Info in deduce_reader: Attempt ReaderRootTree for: %s\n",filename.c_str());
78 if( strncmp(head.at(0).c_str(),
"root",4) == 0||remote)
79 return std::make_shared<ReaderPlugin>(filename,libHepMC3rootIO,std::string(
"newReaderRootTreefile"));
82 printf(
"Info in deduce_reader: Attempt ReaderAscii for: %s\n",filename.c_str());
83 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
85 printf(
"Info in deduce_reader: Attempt ReaderAsciiHepMC2 for: %s\n",filename.c_str());
86 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
88 printf(
"Info in deduce_reader: Attempt ReaderLHEF for: %s\n",filename.c_str());
89 if( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
91 printf(
"Info in deduce_reader: Attempt ReaderHEPEVT for: %s\n",filename.c_str());
92 std::stringstream st_e(head.at(0).c_str());
102 if (attr==
' ')
continue;
107 HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
112 printf(
"Info in deduce_reader: All attempts failed for: %s\n",filename.c_str());
113 return shared_ptr<Reader>(
nullptr);
120 std::vector<std::string> head;
123 size_t backnonempty=0;
124 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++;} }
127 printf(
"Info in deduce_reader: input stream is too short or invalid.\n");
128 return shared_ptr<Reader>(
nullptr);
131 for (
size_t i=0; i<back; i++) stream.unget();
133 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3",14)==0 )
135 printf(
"Info in deduce_reader: Attempt ReaderAscii\n");
139 if( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent",18)==0 )
141 printf(
"Info in deduce_reader: Attempt ReaderAsciiHepMC2\n");
145 if( strncmp(head.at(0).c_str(),
"<LesHouchesEvents",17) == 0)
147 printf(
"Info in deduce_reader: Attempt ReaderLHEF\n");
150 printf(
"Info in deduce_reader: Attempt ReaderHEPEVT\n");
151 std::stringstream st_e(head.at(0).c_str());
161 if (attr==
' ')
continue;
166 HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
170 printf(
"Info in deduce_reader: All attempts failed\n");
171 return shared_ptr<Reader>(
nullptr);