HepMC3 event record library
ReaderGZ.h
Go to the documentation of this file.
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_READERGZ_H
7#define HEPMC3_READERGZ_H
8///
9/// @file ReaderGZ.h
10/// @brief Definition of class \b ReaderGZ
11///
12/// @class HepMC3::ReaderGZ
13/// @brief GenEvent I/O parsing for structured text files compressed with gzip
14///
15/// @ingroup IO
16///
17#include <string>
18#include <fstream>
19#include <istream>
20#include <string.h>
21#include "HepMC3/Reader.h"
22#include "HepMC3/ReaderAscii.h"
24#include "HepMC3/ReaderHEPEVT.h"
25#include "HepMC3/ReaderLHEF.h"
26#include "HepMC3/GenEvent.h"
27#include "gzstream.h"
28namespace HepMC3 {
29/** @brief Union to hold first 4 byts of file, i.e. magic bytes */
30union magic_t {
31 uint8_t bytes[4]; ///< bytes
32 uint32_t number; ///< int
33};
34class ReaderGZ : public Reader {
35public:
36 /** @brief Construcor*/
37 ReaderGZ(const std::string& filename) : m_gzstream(filename.c_str()), m_gzstream_test(filename.c_str())
38 {
39 std::ifstream file(filename);
40 if(!file.is_open()) {
41 printf("Error in ReaderGZ: could not open file%s\n",filename.c_str());
42 return;
43 }
44 magic_t my_magic = {0x1f, 0x8b, 0x08, 0x08};
45 magic_t file_magic;
46 file.read((char *) file_magic.bytes, sizeof(file_magic));
47 if ( file_magic.number == my_magic.number )
48 {
50 }
51 else
52 {
53 printf("Error in ReaderGZ: make sure %s is a gziped file!\n",filename.c_str());
54 return;
55 }
56 };
57 ~ReaderGZ() {};
58 /** @brief Read event */
59 bool read_event(GenEvent& evt) {
60 return m_reader->read_event(evt);
61 };
62 /** @brief State */
63 bool failed() {
64 return m_gzstream.rdstate();
65 }
66 /** @brief Close */
67 void close() {
68 if (m_reader) m_reader->close();
69 };
70private:
71 igzstream m_gzstream; ///< Stream to read
72 igzstream m_gzstream_test; ///< Stream to test
73 std::shared_ptr<Reader> m_reader; ///< Actual reader
74 /** @brief THis function will deduce the type of input file based on the name/URL and it's content and will return appropriate Reader*/
75 std::shared_ptr<Reader> deduce_reader(std::istream & stream_test,std::istream & stream) {
76 std::vector<std::string> head;
77 head.push_back(std::string(""));
78 head.push_back(std::string(""));
79 int goodcount=0;
80 int count=0;
81 while (getline(stream_test,head[goodcount])&&goodcount<1&&count<10) {
82 if (head[goodcount].length()) goodcount++;
83 count++;
84 }
85 if (count>10&&goodcount<2)
86 {
87 printf("Info in ReaderGZ::deduce_reader: The first %i lines in stream do not contain readable information\n",count);
88 return shared_ptr<Reader>(nullptr);
89 }
90 printf("Info in ReaderGZ::deduce_reader: Attempt ReaderAscii for stream\n");
91 if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::Asciiv3",14)==0 )
92 return std::shared_ptr<Reader>((Reader*) ( new ReaderAscii(stream)));
93 printf("Info in ReaderGZ::deduce_reader: Attempt ReaderAsciiHepMC2 for stream\n");
94 if( strncmp(head.at(0).c_str(),"HepMC::Version",14) == 0 && strncmp(head.at(1).c_str(),"HepMC::IO_GenEvent",18)==0 )
95 return std::shared_ptr<Reader>((Reader*) ( new ReaderAsciiHepMC2(stream)));
96 printf("Info in deduce_reader: Attempt ReaderLHEF for stream\n");
97 if( strncmp(head.at(0).c_str(),"<LesHouchesEvents",17) == 0)
98 return std::shared_ptr<Reader>((Reader*) ( new ReaderLHEF(stream)));
99 printf("Info in deduce_reader: Attempt ReaderHEPEVT for stream\n");
100 std::stringstream st_e(head.at(0).c_str());
101 char attr=' ';
102 bool HEPEVT=true;
103 int m_i,m_p;
104 while (true)
105 {
106 if (!(st_e>>attr)) {
107 HEPEVT=false;
108 break;
109 }
110 if (attr==' ') continue;
111 if (attr!='E') {
112 HEPEVT=false;
113 break;
114 }
115 HEPEVT=static_cast<bool>(st_e>>m_i>>m_p);
116 break;
117 }
118 if (HEPEVT) return std::shared_ptr<Reader>((Reader*) ( new ReaderHEPEVT(stream)));
119 printf("Info in deduce_reader: All attempts failed for stream\n");
120 return shared_ptr<Reader>(nullptr);
121 }
122};
123}
124#endif
Definition of class GenEvent.
Definition of class ReaderAsciiHepMC2.
Definition of class ReaderAscii.
Definition of class ReaderHEPEVT.
Definition of class ReaderLHEF.
Definition of interface Reader.
Stores event-related information.
Definition GenEvent.h:42
Parser for HepMC2 I/O files.
GenEvent I/O parsing for structured text files.
Definition ReaderAscii.h:29
GenEvent I/O parsing for structured text files compressed with gzip.
Definition ReaderGZ.h:34
bool read_event(GenEvent &evt)
Read event.
Definition ReaderGZ.h:59
std::shared_ptr< Reader > deduce_reader(std::istream &stream_test, std::istream &stream)
THis function will deduce the type of input file based on the name/URL and it's content and will retu...
Definition ReaderGZ.h:75
void close()
Close
Definition ReaderGZ.h:67
std::shared_ptr< Reader > m_reader
Actual reader.
Definition ReaderGZ.h:73
igzstream m_gzstream
Stream to read.
Definition ReaderGZ.h:71
ReaderGZ(const std::string &filename)
Construcor.
Definition ReaderGZ.h:37
igzstream m_gzstream_test
Stream to test.
Definition ReaderGZ.h:72
bool failed()
State.
Definition ReaderGZ.h:63
GenEvent I/O parsing and serialization for HEPEVT files.
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.
Definition ReaderGZ.h:28
Fortran common block HEPEVT.
Union to hold first 4 byts of file, i.e. magic bytes.
Definition ReaderGZ.h:30
uint8_t bytes[4]
bytes
Definition ReaderGZ.h:31
uint32_t number
int
Definition ReaderGZ.h:32