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 
9 #include "HepMC3/ReaderAscii.h"
11 #include "HepMC3/ReaderHEPEVT.h"
12 #include "HepMC3/ReaderLHEF.h"
13 
14 #include <memory>
15 #include <string>
16 #include <sys/stat.h>
17 #include <string.h>
18 
19 namespace HepMC3 {
20 
21 
22 std::shared_ptr<Reader> deduce_reader(const std::string &filename)
23 {
24  bool remote=false;
25  if (filename.find("http://")!=std::string::npos) remote=true;
26  if (filename.find("https://")!=std::string::npos) remote=true;
27  if (filename.find("root://")!=std::string::npos) remote=true;
28  if (filename.find("gsidcap://")!=std::string::npos) remote=true;
29 
30  std::vector<std::string> head;
31  if (!remote)
32  {
33  struct stat buffer;
34  if (stat (filename.c_str(), &buffer)!=0)
35  {
36  printf("Error in deduce_reader: file does not exist: %s\n",filename.c_str());
37  return std::shared_ptr<Reader> (nullptr);
38  }
39 
40  std::ifstream file(filename);
41  if(!file.is_open()) {
42  printf("Error in deduce_reader: could not open file for testing HepMC version: %s\n",filename.c_str());
43  return shared_ptr<Reader>(nullptr);
44  }
45 
46  std::string line;
47  size_t nonempty=0;
48  while (std::getline(file, line)&&nonempty<3) {
49  if (line.empty()) continue;
50  nonempty++;
51  head.push_back(line);
52  }
53  file.close();
54  }
55 #ifdef HEPMC3_READERROOTTREE_H
56  printf("Info in deduce_reader: Attempt ReaderRootTree for: %s\n",filename.c_str());
57  if( strncmp(head.at(0).c_str(),"root",4) == 0||remote)
58  return std::shared_ptr<Reader>((Reader*) ( new ReaderRootTree(filename)));
59 #else
60  printf("Info in deduce_reader: Will not attempt ReaderRootTree. include ReaderRootTree.h to enable ROOT support");
61  if (remote)
62  {
63  printf("Info in deduce_reader: file is on remote filesystem, but no root support is enabled: %s\n",filename.c_str());
64  return shared_ptr<Reader>(nullptr);
65  }
66 #endif
67  printf("Info in deduce_reader: Attempt ReaderAscii for: %s\n",filename.c_str());
68  if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::Asciiv3",14)==0 )
69  return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(filename)));
70  printf("Info in deduce_reader: Attempt ReaderAsciiHepMC2 for: %s\n",filename.c_str());
71  if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::IO_GenEvent",18)==0 )
72  return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(filename)));
73  printf("Info in deduce_reader: Attempt ReaderLHEF for: %s\n",filename.c_str());
74  if( strncmp(head.at(0).c_str(),"<LesHouchesEvents",17) == 0)
75  return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(filename)));
76  printf("Info in deduce_reader: Attempt ReaderHEPEVT for: %s\n",filename.c_str());
77  std::stringstream st_e(head.at(0).c_str());
78  char attr=' ';
79  bool HEPEVT=true;
80  int m_i,m_p;
81  while (true)
82  {
83  if (!(st_e>>attr)) {
84  HEPEVT=false;
85  break;
86  }
87  if (attr==' ') continue;
88  if (attr!='E') {
89  HEPEVT=false;
90  break;
91  }
92  HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
93  break;
94  }
95  if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(filename)));
96  printf("Info in deduce_reader: All attempts failed for: %s\n",filename.c_str());
97  return shared_ptr<Reader>(nullptr);
98 }
99 
100 }
101 #endif
HepMC3 main namespace.
Definition: WriterDOT.h:19
Definition of class ReaderHEPEVT.
Definition of class ReaderAsciiHepMC2.
Definition of class ReaderAscii.
Fortran common block HEPEVT.
Definition of class ReaderLHEF.