-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add globbing support in `Reader` and `DataSource` * add docstrings, error handling, throwing on unsupported platforms * exclude test from sanitizers since otherwise its dependencies are not met * remove obsolete include * add reading multiple files and glob patterns with `get_reader` * fix broken syntax in branch for no-glob platforms * move glob to utils * Revert "add reading multiple files and glob patterns with `get_reader`" This reverts commit 60c9b02. * test standalone glob utility * add reading multiple files in `get_reader`, glob utilities accessible in python * import whole root in utils * fix typos, comments, loading glob header in python * fix glob namespace in selection.xml * define PODIO_HAS_GLOB_SUPPORT
- Loading branch information
Showing
16 changed files
with
242 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef PODIO_UTILITIES_GLOB_H | ||
#define PODIO_UTILITIES_GLOB_H | ||
#include <string> | ||
#include <vector> | ||
|
||
// Support for glob expansion. | ||
#if __has_include(<glob.h>) | ||
#define PODIO_HAS_GLOB_SUPPORT 1 | ||
#else | ||
#define PODIO_HAS_GLOB_SUPPORT 0 | ||
#endif | ||
|
||
namespace podio::utils { | ||
/// @brief Expands a given glob pattern into a list of matching file paths. | ||
/// | ||
/// This function takes a glob pattern as input and returns a vector of strings | ||
/// containing the paths that match the pattern. It supports standard glob rules | ||
/// extended with tilde expansion and brace expansion. If the pattern doesn't | ||
/// contain any wildcards then it is placed in the returned vector as is. Paths | ||
/// that cannot be accessed are displayed on std::cerr, but the expansion process | ||
/// is not aborted. On platforms without <glob.h> no expansion is done and vector | ||
/// containing the original pattern is returned | ||
/// | ||
/// @param pattern The glob pattern to expand. | ||
/// @return A vector of strings containing the matching file paths. | ||
/// | ||
/// @throws std::runtime_error If no matches are found or if there is an error | ||
/// during glob expansion. | ||
std::vector<std::string> expand_glob(const std::string& pattern); | ||
|
||
/// @brief Checks if a given pattern is a glob pattern. | ||
/// | ||
/// This function determines whether the provided pattern contains any standard | ||
/// glob or brace expansion wildcards. | ||
/// | ||
/// @param pattern The pattern to check. | ||
/// @return true if the pattern is a glob pattern, false otherwise. | ||
bool is_glob_pattern(const std::string& pattern); | ||
|
||
} // namespace podio::utils | ||
|
||
#endif // PODIO_UTILITIES_GLOB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "podio/utilities/Glob.h" | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
#if __has_include(<glob.h>) | ||
#include <glob.h> | ||
#else | ||
#include <system_error> | ||
#endif | ||
|
||
namespace podio::utils { | ||
|
||
bool is_glob_pattern(const std::string& pattern) { | ||
bool escape = false; | ||
for (auto c : pattern) { | ||
if (escape) { | ||
escape = false; | ||
} else if (c == '\\') { | ||
escape = true; | ||
} else if (c == '*' || c == '?' || c == '[' || c == '{') { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
#if __has_include(<glob.h>) | ||
|
||
int glob_err_handler(const char* epath, int eerrno) { | ||
std::cerr << "Glob expansion error accessing path: " << epath << " (error code: " << eerrno << ")\n"; | ||
return 0; | ||
} | ||
|
||
std::vector<std::string> expand_glob(const std::string& pattern) { | ||
glob_t glob_result; | ||
auto retv = glob(pattern.c_str(), GLOB_TILDE | GLOB_BRACE | GLOB_NOMAGIC, glob_err_handler, &glob_result); | ||
if (retv == GLOB_NOMATCH) { | ||
throw std::runtime_error("Glob expansion found no matches for pattern: " + pattern); | ||
} else if (retv != 0) { | ||
globfree(&glob_result); | ||
throw std::runtime_error("Glob expansion error"); | ||
} | ||
std::vector<std::string> results; | ||
results.reserve(glob_result.gl_pathc); | ||
for (size_t i = 0; i < glob_result.gl_pathc; ++i) { | ||
results.emplace_back(glob_result.gl_pathv[i]); | ||
} | ||
globfree(&glob_result); | ||
return results; | ||
} | ||
|
||
#else | ||
|
||
std::vector<std::string> expand_glob(const std::string& pattern) { | ||
if (is_glob_pattern(pattern)) { | ||
throw std::system_error("Glob expansion is not supported on this platform") | ||
} | ||
return {pattern}; | ||
} | ||
|
||
#endif // __has_include(<glob.h>) | ||
|
||
} // namespace podio::utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "podio/Reader.h" | ||
#include "podio/utilities/Glob.h" | ||
#if PODIO_ENABLE_DATASOURCE | ||
#include "podio/DataSource.h" | ||
#endif | ||
|
||
#define ASSERT(condition, msg) \ | ||
if (!(condition)) { \ | ||
throw std::runtime_error(msg); \ | ||
} | ||
|
||
int main() { | ||
const auto pattern = "example_frame_?.root"; | ||
const auto expected_events = 20; | ||
// standalone globbing | ||
|
||
ASSERT(podio::utils::is_glob_pattern(pattern), "Failed to recognize glob pattern"); | ||
auto files = podio::utils::expand_glob(pattern); | ||
ASSERT(files.size() == 2, "Glob expanded to a wrong number of files"); | ||
ASSERT(files[0] == "example_frame_0.root", "Glob expanded to an unexpected file"); | ||
ASSERT(files[1] == "example_frame_1.root", "Glob expanded to an unexpected file"); | ||
{ | ||
// externally resolved glob | ||
const auto reader = podio::makeReader(files); | ||
ASSERT((reader.getEvents() == expected_events), "Reader read invalid number of events"); | ||
#if PODIO_ENABLE_DATASOURCE | ||
auto rdf = podio::CreateDataFrame(files); | ||
ASSERT(rdf.Count().GetValue() == expected_events, "DataSource read invalid number of events"); | ||
#endif // PODIO_ENABLE_DATASOURCE | ||
} | ||
{ | ||
// implicit globbing | ||
const auto reader = podio::makeReader(pattern); | ||
ASSERT((reader.getEvents() == expected_events), "Reader read invalid number of events"); | ||
#if PODIO_ENABLE_DATASOURCE | ||
auto rdf = podio::CreateDataFrame(pattern); | ||
ASSERT(rdf.Count().GetValue() == expected_events, "DataSource read invalid number of events"); | ||
#endif // PODIO_ENABLE_DATASOURCE | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Small test case for checking get_reader working with | ||
a single file, list of files, and a glob pattern""" | ||
|
||
import podio | ||
|
||
assert podio.utils.is_glob_pattern("example_frame_?.root") | ||
files = podio.utils.expand_glob("example_frame_?.root") | ||
assert files == ["example_frame_0.root", "example_frame_1.root"] | ||
|
||
reader = podio.reading.get_reader("example_frame.root") | ||
assert len(reader.get("events")) == 10 | ||
reader = podio.reading.get_reader(files) | ||
assert len(reader.get("events")) == 20 |
Oops, something went wrong.