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>
17 #include <boost/filesystem.hpp>
18 #include <boost/iostreams/device/file.hpp>
19 #include <boost/iostreams/filter/gzip.hpp>
22 namespace io = boost::iostreams;
23 namespace fs = boost::filesystem;
25 namespace Belle2::gearbox {
27 FileContext::FileContext(
const string& filename,
bool compressed)
29 B2DEBUG(100,
"Opening " << filename << (compressed ?
" (gzip)" :
""));
30 if (compressed) m_stream.push(io::gzip_decompressor());
31 m_stream.push(io::file_source(filename));
34 FileHandler::FileHandler(
const std::string& uri):
InputHandler(uri), m_path(uri)
41 std::regex exp(R
"(\{EXP(?::(\d+))?\})");
42 std::regex run(R"(\{RUN(?::(\d+))?\})");
53 std::string tmp = std::regex_replace(
m_path, exp, std::string(
"%1$$0$1d"));
54 tmp = std::regex_replace(tmp, run, std::string(
"%2$$0$1d"));
55 B2DEBUG(300,
"Found run-dependence in file path, resulting in " << tmp);
57 m_pathformat.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
60 B2DEBUG(300,
"Created FileHandler for directory " <<
m_path);
67 if (!eventMetaDataPtr.
isValid()) {
68 B2DEBUG(300,
"No run info, probably initialize? Skipping backend");
71 int exp = eventMetaDataPtr->getExperiment();
72 int run = eventMetaDataPtr->getRun();
76 std::string filename = (basedir / path).
string();
80 B2DEBUG(350,
"Trying to find " << filename);
83 if (!fullpath.empty()) {
84 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"'");
88 if (!fullpath.empty()) {
89 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"' (gzip)");
97 size_t last_slash = filename.find_last_of(
'/');
106 repeat = (last_slash >=
m_path.size()) && (last_slash != std::string::npos);
108 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.
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.
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.