37 std::string libHepMC3rootIO =
"libHepMC3rootIO.so.3";
38 std::string libHepMC3protobufIO =
"libHepMC3protobufIO.so.3";
39#if defined(__darwin__) || defined(__APPLE__)
40 libHepMC3rootIO =
"libHepMC3rootIO.dylib";
41 libHepMC3protobufIO =
"libHepMC3protobufIO.dylib";
43#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
44 libHepMC3protobufIO =
"HepMC3protobufIO.dll";
45 libHepMC3rootIO =
"HepMC3rootIO.dll";
50 if (filename.find(
"http://") != std::string::npos) remote =
true;
51 if (filename.find(
"https://") != std::string::npos) remote =
true;
52 if (filename.find(
"root://") != std::string::npos) remote =
true;
53 if (filename.find(
"gsidcap://") != std::string::npos) remote =
true;
55 std::vector<std::string> head;
59#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
60 if (!(stat (filename.c_str(), &buffer) == 0))
62 if (!(stat (filename.c_str(), &buffer) == 0 && (S_ISFIFO(buffer.st_mode) || S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode))))
65 HEPMC3_ERROR(
"deduce_reader: file " << filename <<
" does not exist or is not a regular file/FIFO/link");
66 return std::shared_ptr<Reader> (
nullptr);
69 std::shared_ptr< std::ifstream > file = std::make_shared< std::ifstream >(filename);
72 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
73 return std::shared_ptr<Reader>(
nullptr);
75 if (!file->is_open()) {
76 HEPMC3_ERROR(
"deduce_reader could not open file for testing HepMC version: " << filename);
78 return std::shared_ptr<Reader>(
nullptr);
81#if defined(__linux__) || defined(__darwin__)|| defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
82 pipe = S_ISFIFO(buffer.st_mode);
84 HEPMC3_DEBUG(10,
"deduce_reader: the file " << filename <<
" is a pipe");
91 while (std::getline( *(file.get()), line) && nonempty < 3) {
92 if (line.empty())
continue;
101 HEPMC3_DEBUG(10,
"deduce_reader: Attempt ReaderRootTree for " << filename);
102 if ( strncmp(head.at(0).c_str(),
"root", 4) == 0 || remote)
103 return std::make_shared<ReaderPlugin>(filename,libHepMC3rootIO,std::string(
"newReaderRootTreefile"));
106 HEPMC3_DEBUG(10,
"deduce_reader: Attempt ProtobufIO for " << filename);
107 if ( strncmp(head.at(0).c_str(),
"hmpb",4) == 0 )
108 return std::make_shared<ReaderPlugin>(filename,libHepMC3protobufIO,std::string(
"newReaderprotobuffile"));
109#if HEPMC3_USE_COMPRESSION
112 snprintf(buf,6,
"%s",head.at(0).c_str());
113 Compression det = detect_compression_type(buf, buf + 6);
114 if ( det != Compression::plaintext ) {
115 HEPMC3_DEBUG(10,
"Detected supported compression " << std::to_string(det));
116 return deduce_reader(std::shared_ptr< std::istream >(
new ifstream(filename.c_str())));
119 HEPMC3_DEBUG(10,
"Attempt ReaderAscii for " << filename);
120 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
122 HEPMC3_DEBUG(10,
"Attempt ReaderAsciiHepMC2 for " << filename);
123 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
125 HEPMC3_DEBUG(10,
"Attempt ReaderLHEF for " << filename);
126 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
128 HEPMC3_DEBUG(10,
"Attempt ReaderHEPEVT for " << filename);
129 std::stringstream st_e(head.at(0).c_str());
135 if (!(st_e >> attr)) {
139 if (attr ==
' ')
continue;
144 HEPEVT=
static_cast<bool>(st_e >> m_i >> m_p);
149 HEPMC3_DEBUG(10,
"deduce_reader: all attempts failed for " << filename);
150 return std::shared_ptr<Reader>(
nullptr);
157 const size_t raw_header_size = 100;
158 std::string raw_header(raw_header_size + 1,
'\0');
159 auto fstream =
dynamic_cast<std::ifstream*
>(&stream);
161 fstream->read(&(raw_header[0]), raw_header_size);
163 stream.read(&(raw_header[0]), raw_header_size);
165 std::vector<std::string> head;
167 for (
size_t i = 0; i < raw_header_size; ++i) {
168 const char c = raw_header[i];
169 if (c ==
'\0')
break;
171 if (head.back().length() != 0) {
180 for (
size_t i = 0; i < raw_header_size; ++i) {
static_cast<std::filebuf*
>(fstream->rdbuf())->sungetc(); }
181 HEPMC3_DEBUG(10,
"After sungetc() fstream->good()=" + std::to_string(fstream->good()));
183 for (
size_t i = 0; i < raw_header_size; ++i) { stream.rdbuf()->sungetc(); }
184 HEPMC3_DEBUG(10,
"After sungetc() stream.good()=" + std::to_string(stream.good()));
189 return std::shared_ptr<Reader>(
nullptr);
192 if ( strncmp(head.at(0).c_str(),
"hmpb",4) == 0 )
194 std::string libHepMC3protobufIO =
"libHepMC3protobufIO.so.3";
195#if defined(__darwin__) || defined(__APPLE__)
196 libHepMC3protobufIO =
"libHepMC3protobufIO.dylib";
198#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
199 libHepMC3protobufIO =
"HepMC3protobufIO.dll";
202 return std::make_shared<ReaderPlugin>(stream,libHepMC3protobufIO,std::string(
"newReaderprotobufstream"));
205 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
211 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
217 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
223 std::stringstream st_e(head.at(0).c_str());
229 if (!(st_e >> attr)) {
233 if (attr ==
' ')
continue;
238 HEPEVT =
static_cast<bool>(st_e >> m_i >> m_p);
243 return std::shared_ptr<Reader>(
nullptr);
251 return std::shared_ptr<Reader>(
nullptr);
253 const size_t raw_header_size = 100;
254 std::string raw_header(raw_header_size + 1,
'\0');
255 auto fstream = std::dynamic_pointer_cast<std::ifstream>(stream);
257 fstream->read(&(raw_header[0]), raw_header_size);
259 stream->read(&(raw_header[0]), raw_header_size);
261 std::vector<std::string> head;
263 for (
size_t i = 0; i < raw_header_size; ++i) {
264 const char c = raw_header[i];
265 if (c ==
'\0')
break;
267 if (head.back().length() != 0) {
276 for (
size_t i = 0; i < raw_header_size; ++i) {
static_cast<std::filebuf*
>(fstream->rdbuf())->sungetc(); }
277 HEPMC3_DEBUG(10,
"After sungetc() fstream->good()="+std::to_string(fstream->good()));
279 for (
size_t i = 0; i < raw_header_size; ++i) { stream->rdbuf()->sungetc(); }
280 HEPMC3_DEBUG(10,
"After sungetc() stream->good()="+std::to_string(stream->good()));
286 return std::shared_ptr<Reader>(
nullptr);
289 if ( strncmp(head.at(0).c_str(),
"hmpb",4) == 0 )
291 std::string libHepMC3protobufIO =
"libHepMC3protobufIO.so.3";
292#if defined(__darwin__) || defined(__APPLE__)
293 libHepMC3protobufIO =
"libHepMC3protobufIO.dylib";
295#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
296 libHepMC3protobufIO =
"HepMC3protobufIO.dll";
299 return std::make_shared<ReaderPlugin>(*stream,libHepMC3protobufIO,std::string(
"newReaderprotobufstream"));
302 if ( strncmp(head.at(0).c_str(),
"HepMC::Version", 14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::Asciiv3", 14) == 0 )
308 if ( strncmp(head.at(0).c_str(),
"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),
"HepMC::IO_GenEvent", 18) == 0 )
314 if ( strncmp(head.at(0).c_str(),
"<LesHouchesEvents", 17) == 0)
320 std::stringstream st_e(head.at(0).c_str());
326 if (!(st_e >> attr)) {
330 if (attr ==
' ')
continue;
335 HEPEVT =
static_cast<bool>(st_e >> m_i >> m_p);
340 return std::shared_ptr<Reader>(
nullptr);