HepMC3 event record library
ReaderFactory.h
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5//
6#ifndef HEPMC3_READERFACTORY_H
7#define HEPMC3_READERFACTORY_H
8
11#include "HepMC3/ReaderHEPEVT.h"
12#include "HepMC3/ReaderLHEF.h"
13#include "HepMC3/ReaderPlugin.h"
14
15#include <memory>
16#include <string>
17#include <sys/stat.h>
18#include <string.h>
19
20namespace HepMC3 {
21std::shared_ptr<Reader> deduce_reader(std::istream &stream);
22/** @brief THis function will deduce the type of input file based on the name/URL and it's content and will return appropriate Reader*/
23std::shared_ptr<Reader> deduce_reader(const std::string &filename)
24{
25 std::string libHepMC3rootIO="libHepMC3rootIO.so.3";
26#if defined(__darwin__) || defined(__APPLE__)
27 libHepMC3rootIO="libHepMC3rootIO.dydl";
28#endif
29#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
30 libHepMC3rootIO="HepMC3rootIO.dll";
31#endif
32 bool remote=false;
33 bool pipe=false;
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;
38
39 std::vector<std::string> head;
40 if (!remote)
41 {
42 struct stat buffer;
43#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
44 if (!(stat (filename.c_str(), &buffer)==0))
45#else
46 if (!(stat (filename.c_str(), &buffer)==0 && (S_ISFIFO(buffer.st_mode)|| S_ISREG(buffer.st_mode) || S_ISLNK(buffer.st_mode))))
47#endif
48 {
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);
51 }
52
53 std::ifstream* file= new std::ifstream(filename);
54
55 if(!file->is_open()) {
56 printf("Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
57 file->close();
58 return std::shared_ptr<Reader>(nullptr);
59 }
60#if defined(__linux__) || defined(__darwin__)|| defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)
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); }
63#endif
64 std::string line;
65 size_t nonempty=0;
66 while (std::getline(*file, line)&&nonempty<3) {
67 if (line.empty()) continue;
68 nonempty++;
69 head.push_back(line);
70 }
71 file->close();
72 if (file) delete file;
73 }
74 /* To assure there are at least two elements in the vector*/
75 head.push_back("");
76 head.push_back("");
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"));
80 if (!remote)
81 {
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 )
84 return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(filename)));
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 )
87 return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(filename)));
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)
90 return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(filename)));
91 printf("Info in deduce_reader: Attempt ReaderHEPEVT for: %s\n",filename.c_str());
92 std::stringstream st_e(head.at(0).c_str());
93 char attr=' ';
94 bool HEPEVT=true;
95 int m_i,m_p;
96 while (true)
97 {
98 if (!(st_e>>attr)) {
99 HEPEVT=false;
100 break;
101 }
102 if (attr==' ') continue;
103 if (attr!='E') {
104 HEPEVT=false;
105 break;
106 }
107 HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
108 break;
109 }
110 if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(filename)));
111 }
112 printf("Info in deduce_reader: All attempts failed for: %s\n",filename.c_str());
113 return std::shared_ptr<Reader>(nullptr);
114}
115
116
117/** @brief THis function will deduce the type of input stream based on its content and will return appropriate Reader*/
118std::shared_ptr<Reader> deduce_reader(std::istream &stream)
119{
120 std::vector<std::string> head;
121 head.push_back("");
122 size_t back=0;
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++;} }
125 if (!stream)
126 {
127 printf("Info in deduce_reader: input stream is too short or invalid.\n");
128 return std::shared_ptr<Reader>(nullptr);
129 }
130
131 for (size_t i=0; i<back; i++) stream.unget();
132
133 if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::Asciiv3",14)==0 )
134 {
135 printf("Info in deduce_reader: Attempt ReaderAscii\n");
136 return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(stream)));
137 }
138
139 if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::IO_GenEvent",18)==0 )
140 {
141 printf("Info in deduce_reader: Attempt ReaderAsciiHepMC2\n");
142 return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(stream)));
143 }
144
145 if( strncmp(head.at(0).c_str(),"<LesHouchesEvents",17) == 0)
146 {
147 printf("Info in deduce_reader: Attempt ReaderLHEF\n");
148 return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(stream)));
149 }
150 printf("Info in deduce_reader: Attempt ReaderHEPEVT\n");
151 std::stringstream st_e(head.at(0).c_str());
152 char attr=' ';
153 bool HEPEVT=true;
154 int m_i,m_p;
155 while (true)
156 {
157 if (!(st_e>>attr)) {
158 HEPEVT=false;
159 break;
160 }
161 if (attr==' ') continue;
162 if (attr!='E') {
163 HEPEVT=false;
164 break;
165 }
166 HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
167 break;
168 }
169 if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(stream)));
170 printf("Info in deduce_reader: All attempts failed\n");
171 return std::shared_ptr<Reader>(nullptr);
172}
173}
174#endif
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.
Definition: ReaderAscii.h:29
GenEvent I/O parsing and serialization for HEPEVT files.
Definition: ReaderHEPEVT.h:33
GenEvent I/O parsing and serialization for LHEF files.
Definition: ReaderLHEF.h:35
Base class for all I/O readers.
Definition: Reader.h:25
HepMC3 main namespace.
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.