HepMC3 event record library
ReaderFactory.h
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2020 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC3_READERFACTORY_H
7 #define HEPMC3_READERFACTORY_H
8 
9 #include "HepMC3/ReaderAscii.h"
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 
20 namespace HepMC3 {
21 std::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*/
23 std::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  if (!file)
55  {
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);
58  }
59  if(!file->is_open()) {
60  printf("Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
61  file->close();
62  return std::shared_ptr<Reader>(nullptr);
63  }
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); }
67 #endif
68  std::string line;
69  size_t nonempty=0;
70  while (std::getline(*file, line)&&nonempty<3) {
71  if (line.empty()) continue;
72  nonempty++;
73  head.push_back(line);
74  }
75  file->close();
76  delete file;
77  }
78  /* To assure there are at least two elements in the vector*/
79  head.push_back("");
80  head.push_back("");
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"));
84  if (!remote)
85  {
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 )
88  return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(filename)));
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 )
91  return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(filename)));
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)
94  return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(filename)));
95  printf("Info in deduce_reader: Attempt ReaderHEPEVT for: %s\n",filename.c_str());
96  std::stringstream st_e(head.at(0).c_str());
97  char attr=' ';
98  bool HEPEVT=true;
99  int m_i,m_p;
100  while (true)
101  {
102  if (!(st_e>>attr)) {
103  HEPEVT=false;
104  break;
105  }
106  if (attr==' ') continue;
107  if (attr!='E') {
108  HEPEVT=false;
109  break;
110  }
111  HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
112  break;
113  }
114  if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(filename)));
115  }
116  printf("Info in deduce_reader: All attempts failed for: %s\n",filename.c_str());
117  return std::shared_ptr<Reader>(nullptr);
118 }
119 
120 
121 /** @brief This function will deduce the type of input stream based on its content and will return appropriate Reader*/
122 std::shared_ptr<Reader> deduce_reader(std::istream &stream)
123 {
124  std::vector<std::string> head;
125  head.push_back("");
126  size_t back=0;
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++;} }
129  if (!stream)
130  {
131  printf("Info in deduce_reader: input stream is too short or invalid.\n");
132  return std::shared_ptr<Reader>(nullptr);
133  }
134 
135  for (size_t i=0; i<back; i++) stream.unget();
136 
137  if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::Asciiv3",14)==0 )
138  {
139  printf("Info in deduce_reader: Attempt ReaderAscii\n");
140  return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(stream)));
141  }
142 
143  if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::IO_GenEvent",18)==0 )
144  {
145  printf("Info in deduce_reader: Attempt ReaderAsciiHepMC2\n");
146  return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(stream)));
147  }
148 
149  if( strncmp(head.at(0).c_str(),"<LesHouchesEvents",17) == 0)
150  {
151  printf("Info in deduce_reader: Attempt ReaderLHEF\n");
152  return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(stream)));
153  }
154  printf("Info in deduce_reader: Attempt ReaderHEPEVT\n");
155  std::stringstream st_e(head.at(0).c_str());
156  char attr=' ';
157  bool HEPEVT=true;
158  int m_i,m_p;
159  while (true)
160  {
161  if (!(st_e>>attr)) {
162  HEPEVT=false;
163  break;
164  }
165  if (attr==' ') continue;
166  if (attr!='E') {
167  HEPEVT=false;
168  break;
169  }
170  HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
171  break;
172  }
173  if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(stream)));
174  printf("Info in deduce_reader: All attempts failed\n");
175  return std::shared_ptr<Reader>(nullptr);
176 }
177 }
178 #endif
GenEvent I/O parsing and serialization for LHEF files.
Definition: ReaderLHEF.h:35
Definition of class ReaderHEPEVT.
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
GenEvent I/O parsing and serialization for HEPEVT files.
Definition: ReaderHEPEVT.h:32
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: Reader.h:25
Definition of class ReaderLHEF.