HepMC3 event record library
ReaderFactory.h
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @file ReaderFactory.h
8 * @brief Declaration of \b deduce_reader and related functions
9 *
10 */
11#ifndef HEPMC3_READERFACTORY_H
12#define HEPMC3_READERFACTORY_H
13
14
15#include "HepMC3/ReaderFactory_fwd.h"
16#include "HepMC3/CompressedIO.h"
17
18namespace HepMC3 {
19
20/**
21 * @brief This function deduces the type of input file based on the name/URL
22 * and its content, and will return an appropriate Reader object.
23 *
24 */
25
26inline std::shared_ptr<Reader> deduce_reader(const std::string &filename)
27{
28 InputInfo input(filename);
29 if (input.m_init && !input.m_error && input.m_reader) return input.m_reader;
30 if (input.m_root || input.m_remote) {
31 HEPMC3_DEBUG(10, "deduce_reader: Attempt ReaderRootTree for " << filename);
32 return std::make_shared<ReaderPlugin>(filename, libHepMC3rootIO, std::string("newReaderRootTreefile"));
33 }
34 if (input.m_protobuf) {
35 HEPMC3_DEBUG(10, "deduce_reader: Attempt ProtobufIO for " << filename);
36 return std::make_shared<ReaderPlugin>(filename, libHepMC3protobufIO, std::string("newReaderprotobuffile"));
37 }
38#if HEPMC3_USE_COMPRESSION
39 HEPMC3_DEBUG(10, "Attempt ReaderGZ for " << filename);
40 char buf[6];
41 snprintf(buf, 6, "%s", input.m_head.at(0).c_str());
42 Compression det = detect_compression_type(buf, buf + 6);
43 if ( det != Compression::plaintext ) {
44 HEPMC3_DEBUG(10, "Detected supported compression: " << det);
45 return deduce_reader(std::shared_ptr< std::istream >(new ifstream(filename.c_str())));
46 }
47#endif
48 std::string f = filename;
49 return input.native_reader(f);
50}
51}
52#endif
HepMC3 interface to bxzstr library and some routines.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
Definition Errors.h:41
HepMC3 main namespace.
bxz::Compression Compression
Compression types from bxzstr.
std::shared_ptr< Reader > deduce_reader(const std::string &filename)
This function deduces the type of input file based on the name/URL and its content,...
Compression detect_compression_type(const char *in_buff_start, const char *in_buff_end)
Function to detect compression type.
bxz::ifstream ifstream
ifstream