11 #include <framework/gearbox/FileHandler.h>
12 #include <framework/gearbox/Gearbox.h>
13 #include <framework/logging/Logger.h>
14 #include <framework/utilities/FileSystem.h>
15 #include <framework/datastore/StoreObjPtr.h>
16 #include <framework/dataobjects/EventMetaData.h>
19 #include <boost/filesystem.hpp>
20 #include <boost/iostreams/device/file.hpp>
21 #include <boost/iostreams/filter/gzip.hpp>
24 namespace io = boost::iostreams;
25 namespace fs = boost::filesystem;
27 namespace Belle2::gearbox {
29 FileContext::FileContext(
const string& filename,
bool compressed)
31 B2DEBUG(100,
"Opening " << filename << (compressed ?
" (gzip)" :
""));
32 if (compressed) m_stream.push(io::gzip_decompressor());
33 m_stream.push(io::file_source(filename));
36 FileHandler::FileHandler(
const std::string& uri):
InputHandler(uri), m_path(uri)
43 std::regex exp(R
"(\{EXP(?::(\d+))?\})");
44 std::regex run(R"(\{RUN(?::(\d+))?\})");
54 eventMetaDataPtr.isRequired();
55 std::string tmp = std::regex_replace(
m_path, exp, std::string(
"%1$$0$1d"));
56 tmp = std::regex_replace(tmp, run, std::string(
"%2$$0$1d"));
57 B2DEBUG(300,
"Found run-dependence in file path, resulting in " << tmp);
59 m_pathformat.exceptions(boost::io::all_error_bits ^ boost::io::too_many_args_bit);
62 B2DEBUG(300,
"Created FileHandler for directory " <<
m_path);
69 if (!eventMetaDataPtr.
isValid()) {
70 B2DEBUG(300,
"No run info, probably initialize? Skipping backend");
73 int exp = eventMetaDataPtr->getExperiment();
74 int run = eventMetaDataPtr->getRun();
78 std::string filename = (basedir / path).
string();
82 B2DEBUG(350,
"Trying to find " << filename);
85 if (!fullpath.empty()) {
86 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"'");
90 if (!fullpath.empty()) {
91 if (
m_rundependence) B2INFO(
"gearbox::FileHandler: Reading '" << fullpath <<
"' (gzip)");
99 size_t last_slash = filename.find_last_of(
'/');
108 repeat = (last_slash >=
m_path.size()) && (last_slash != std::string::npos);
110 filename[last_slash] =
'-';