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())
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
58 ~ReaderGZ() {};
59 /** @brief Read event */
60 bool read_event(GenEvent& evt) {
61 return m_reader->read_event(evt);
62 };
63 /** @brief State */
64 bool failed() {
65 return m_gzstream.rdstate();
66 }
67 /** @brief Close */
68 void close() {
69 if (m_reader) m_reader->close();
70 };
71private:
72 igzstream m_gzstream; ///< Stream to read
73 std::shared_ptr<Reader> m_reader; ///< Actual reader
74};
75}
76#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:41
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:60
void close()
Close
Definition: ReaderGZ.h:68
std::shared_ptr< Reader > m_reader
Actual reader.
Definition: ReaderGZ.h:73
igzstream m_gzstream
Stream to read.
Definition: ReaderGZ.h:70
ReaderGZ(const std::string &filename)
Construcor.
Definition: ReaderGZ.h:37
bool failed()
State.
Definition: ReaderGZ.h:64
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...
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