9#include <framework/gearbox/FileHandler.h>
10#include <framework/gearbox/Gearbox.h>
11#include <framework/logging/Logger.h>
12#include <framework/utilities/FileSystem.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/dataobjects/EventMetaData.h>
19#include <boost/iostreams/device/file.hpp>
20#include <boost/iostreams/filter/gzip.hpp>
23namespace io = boost::iostreams;
24namespace fs = std::filesystem;
26namespace Belle2::gearbox {
30 B2DEBUG(100,
"Opening " << filename << (compressed ?
" (gzip)" :
""));
31 if (compressed)
m_stream.push(io::gzip_decompressor());
32 m_stream.push(io::file_source(filename));
42 std::regex exp(R
"(\{EXP(?::(\d+))?\})");
43 std::regex run(R"(\{RUN(?::(\d+))?\})");
54 std::string tmp = std::regex_replace(
m_path, exp, std::string(
"%1$$0$1d"));
55 tmp = std::regex_replace(tmp, run, std::string(
"%2$$0$1d"));
56 B2DEBUG(300,
"Found run-dependence in file path, resulting in " << tmp);
58 m_pathformat.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
61 B2DEBUG(300,
"Created FileHandler for directory " <<
m_path);
68 if (!eventMetaDataPtr.
isValid()) {
69 B2DEBUG(300,
"No run info, probably initialize? Skipping backend");
72 int exp = eventMetaDataPtr->getExperiment();
73 int run = eventMetaDataPtr->getRun();
77 std::string filename = (basedir / path).
string();
81 B2DEBUG(350,
"Trying to find " << filename);
84 if (!fullpath.empty()) {
85 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"'");
89 if (!fullpath.empty()) {
90 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"' (gzip)");
98 size_t last_slash = filename.find_last_of(
'/');
107 repeat = (last_slash >=
m_path.size()) && (last_slash != std::string::npos);
109 filename[last_slash] =
'-';
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Type-safe access to single objects in the data store.
bool isValid() const
Check whether the object was created.
InputContext which serves the content of a plain file.
boost::iostreams::filtering_istream m_stream
stream to read data from
FileContext(const std::string &filename, bool compressed)
Open the given filename.
InputHandler which will read XML from plain files, optionally gzip compressed.
std::string m_path
Search path to look for files.
virtual InputContext * open(const std::string &path) override
create a new FileContext by searching the file system for a file named like path.
FileHandler(const std::string &uri)
Instantiate a new file handler, using the uri as base search path.
boost::format m_pathformat
format object in case of run-dependent data path
bool m_rundependence
bool indicating whether the data path is run-dependent
Class representing a resource context for gearbox.
#define B2_GEARBOX_REGISTER_INPUTHANDLER(classname, prefix)
Helper macro to easily register new input handlers.