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