-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Add a c++ implementation for podio-dump
#620
Open
tmadlener
wants to merge
8
commits into
AIDASoft:master
Choose a base branch
from
tmadlener:podio-dump-cpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
29c6712
Move sortAlphabetically to public header
tmadlener 46527bc
First version of podio-dump in c++
tmadlener 4bf6f9b
[wip] Nicer printing
tmadlener 29bd1a4
[wip] Depend on fmt
tmadlener 0dda628
Use fmt lib to for formatting
tmadlener b0d4257
Remove unnecessary code
tmadlener 9aba876
Better error message
tmadlener 04b0bd2
Add detailed printing
tmadlener File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef PODIO_UTILITIES_MISCHELPERS_H | ||
#define PODIO_UTILITIES_MISCHELPERS_H | ||
|
||
#include <algorithm> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace podio::utils { | ||
|
||
/** | ||
* Sort the input vector of strings alphabetically, case insensitive. | ||
*/ | ||
inline std::vector<std::string> sortAlphabeticaly(std::vector<std::string> strings) { | ||
// Obviously there is no tolower(std::string) in c++, so this is slightly more | ||
// involved and we make use of the fact that lexicographical_compare works on | ||
// ranges and the fact that we can feed it a dedicated comparison function, | ||
// where we convert the strings to lower case char-by-char. The alternative is | ||
// to make string copies inside the first lambda, transform them to lowercase | ||
// and then use operator< of std::string, which would be effectively | ||
// hand-writing what is happening below. | ||
std::sort(strings.begin(), strings.end(), [](const auto& lhs, const auto& rhs) { | ||
return std::lexicographical_compare( | ||
lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), | ||
[](const auto& cl, const auto& cr) { return std::tolower(cl) < std::tolower(cr); }); | ||
}); | ||
return strings; | ||
} | ||
} // namespace podio::utils | ||
|
||
#endif // PODIO_UTILITIES_MISCHELPERS_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,54 @@ | ||
#ifndef PODIO_TOOLS_ARGPARSEUTILS_H | ||
#define PODIO_TOOLS_ARGPARSEUTILS_H | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <vector> | ||
|
||
template <typename... Flags> | ||
auto findFlags(const std::vector<std::string>& argv, Flags... flags) { | ||
return std::find_if(argv.begin(), argv.end(), [&](const auto& elem) { return ((elem == flags) || ...); }); | ||
} | ||
|
||
inline std::vector<std::string> splitString(const std::string& str, const char delimiter) { | ||
std::vector<std::string> tokens; | ||
std::string token; | ||
for (char ch : str) { | ||
if (ch == delimiter) { | ||
if (!token.empty()) { | ||
tokens.push_back(token); | ||
token.clear(); | ||
} | ||
} else { | ||
token += ch; | ||
} | ||
} | ||
if (!token.empty()) { | ||
tokens.push_back(token); | ||
} | ||
return tokens; | ||
} | ||
|
||
inline size_t parseSizeOrExit(const std::string& str) { | ||
const auto parseError = [&str]() { | ||
std::cerr << "'" << str << "' cannot be parsed into an integer number" << std::endl; | ||
std::exit(1); | ||
}; | ||
|
||
try { | ||
std::size_t pos{}; | ||
auto number = std::stoll(str, &pos); | ||
if (pos != str.size()) { | ||
throw std::invalid_argument(""); | ||
} | ||
return number; | ||
} catch (const std::invalid_argument& err) { | ||
parseError(); | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
#endif // PODIO_TOOLS_ARGPARSEUTILS_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
#include "argparseUtils.h" | ||
#include "tabulate.h" | ||
|
||
#include "podio/Frame.h" | ||
#include "podio/Reader.h" | ||
#include "podio/podioVersion.h" | ||
#include "podio/utilities/MiscHelpers.h" | ||
|
||
#include <fmt/core.h> | ||
#include <fmt/ostream.h> | ||
#include <fmt/ranges.h> | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <iterator> | ||
#include <numeric> | ||
#include <string> | ||
#include <tuple> | ||
|
||
template <> | ||
struct fmt::formatter<podio::version::Version> : ostream_formatter {}; | ||
|
||
struct ParsedArgs { | ||
std::string inputFile{}; | ||
std::string category{"events"}; | ||
std::vector<size_t> events = std::vector<size_t>(1, 0); | ||
bool detailed{false}; | ||
}; | ||
|
||
constexpr auto usageMsg = R"(usage: podio-dump [-h] [-c CATEGORY] [-e ENTRIES] [-d] [--version] inputfile)"; | ||
|
||
constexpr auto helpMsg = R"( | ||
Dump contents of a podio file to stdout | ||
|
||
positional arguments: | ||
inputfile Name of the file to dump content from | ||
|
||
options: | ||
-h, --help show this help message and exit | ||
-c CATEGORY, --category CATEGORY | ||
Which Frame category to dump | ||
-e ENTRIES, --entries ENTRIES | ||
Which entries to print. A single number, comma separated list of numbers or "first:last" for an inclusive range of entries. Defaults to the first entry. | ||
-d, --detailed Dump the full contents not just the collection info | ||
--version show program's version number and exit | ||
)"; | ||
|
||
void printUsageAndExit() { | ||
fmt::print(stderr, "{}\n", usageMsg); | ||
std::exit(1); | ||
} | ||
|
||
auto getArgumentValueOrExit(const std::vector<std::string>& argv, std::vector<std::string>::const_iterator it) { | ||
const int argc = argv.size(); | ||
const auto index = std::distance(argv.begin(), it); | ||
if (index > argc - 2) { | ||
printUsageAndExit(); | ||
} | ||
return argv[index + 1]; | ||
} | ||
|
||
std::vector<size_t> parseEventRange(const std::string& evtRange) { | ||
const auto splitRange = splitString(evtRange, ','); | ||
const auto parseError = [&evtRange]() { | ||
fmt::print(stderr, "'{}' canot be parsed into a list of entries\n", evtRange); | ||
std::exit(1); | ||
}; | ||
|
||
if (splitRange.size() == 1) { | ||
const auto colonSplitRange = splitString(evtRange, ':'); | ||
if (colonSplitRange.size() == 1) { | ||
return {parseSizeOrExit(splitRange[0])}; | ||
} else if (colonSplitRange.size() == 2) { | ||
// we have two numbers signifying an inclusive range | ||
const auto start = parseSizeOrExit(colonSplitRange[0]); | ||
const auto end = parseSizeOrExit(colonSplitRange[1]); | ||
std::vector<size_t> events(end - start + 1); | ||
std::iota(events.begin(), events.end(), start); | ||
return events; | ||
} else { | ||
parseError(); | ||
} | ||
} else { | ||
std::vector<size_t> events; | ||
events.reserve(splitRange.size()); | ||
std::transform(splitRange.begin(), splitRange.end(), std::back_inserter(events), | ||
[](const auto& elem) { return parseSizeOrExit(elem); }); | ||
|
||
return events; | ||
} | ||
|
||
parseError(); | ||
return {}; | ||
} | ||
|
||
ParsedArgs parseArgs(std::vector<std::string> argv) { | ||
// find help or version | ||
if (const auto it = findFlags(argv, "-h", "--help", "--version"); it != argv.end()) { | ||
if (*it == "--version") { | ||
fmt::print("podio {}\n", podio::version::build_version); | ||
} else { | ||
fmt::print("{}\n{}", usageMsg, helpMsg); | ||
} | ||
std::exit(0); | ||
} | ||
|
||
ParsedArgs args; | ||
// detailed flag | ||
if (const auto it = findFlags(argv, "-d", "--detailed"); it != argv.end()) { | ||
args.detailed = true; | ||
argv.erase(it); | ||
} | ||
// category | ||
if (const auto it = findFlags(argv, "-c", "--category"); it != argv.end()) { | ||
args.category = getArgumentValueOrExit(argv, it); | ||
argv.erase(it, it + 2); | ||
} | ||
// event range | ||
if (const auto it = findFlags(argv, "-e", "--events"); it != argv.end()) { | ||
args.events = parseEventRange(*(it + 1)); | ||
argv.erase(it, it + 2); | ||
} | ||
|
||
if (argv.size() != 1) { | ||
printUsageAndExit(); | ||
} | ||
args.inputFile = argv[0]; | ||
|
||
return args; | ||
} | ||
|
||
template <typename T> | ||
std::string getTypeString() { | ||
if constexpr (std::is_same_v<T, int>) { | ||
return "int"; | ||
} else if constexpr (std::is_same_v<T, float>) { | ||
return "float"; | ||
} else if constexpr (std::is_same_v<T, double>) { | ||
return "double"; | ||
} else if constexpr (std::is_same_v<T, std::string>) { | ||
return "std::string"; | ||
} | ||
|
||
return "unknown"; | ||
} | ||
|
||
template <typename T> | ||
void getParameterOverview(const podio::Frame& frame, std::vector<std::tuple<std::string, std::string, size_t>>& rows) { | ||
const auto typeString = getTypeString<T>(); | ||
for (const auto& parKey : podio::utils::sortAlphabeticaly(frame.getParameterKeys<T>())) { | ||
rows.emplace_back(parKey, typeString, frame.getParameter<std::vector<T>>(parKey)->size()); | ||
} | ||
} | ||
|
||
void printFrameOverview(const podio::Frame& frame) { | ||
fmt::print("Collections:\n"); | ||
const auto collNames = frame.getAvailableCollections(); | ||
|
||
std::vector<std::tuple<std::string, std::string_view, size_t, std::string>> rows; | ||
rows.reserve(collNames.size()); | ||
|
||
for (const auto& name : podio::utils::sortAlphabeticaly(collNames)) { | ||
const auto coll = frame.get(name); | ||
rows.emplace_back(name, coll->getValueTypeName(), coll->size(), fmt::format("{:0>8x}", coll->getID())); | ||
} | ||
printTable(rows, {"Name", "ValueType", "Size", "ID"}); | ||
|
||
fmt::print("\nParameters:\n"); | ||
std::vector<std::tuple<std::string, std::string, size_t>> paramRows{}; | ||
getParameterOverview<int>(frame, paramRows); | ||
getParameterOverview<float>(frame, paramRows); | ||
getParameterOverview<double>(frame, paramRows); | ||
getParameterOverview<std::string>(frame, paramRows); | ||
|
||
printTable(paramRows, {"Name", "Type", "Elements"}); | ||
} | ||
|
||
template <typename... Args> | ||
void print_flush(fmt::format_string<Args...> fmtstr, Args&&... args) { | ||
fmt::print(fmtstr, std::forward<Args>(args)...); | ||
std::fflush(stdout); | ||
} | ||
|
||
void printFrameDetailed(const podio::Frame& frame) { | ||
fmt::print("Collections:\n"); | ||
const auto collNames = frame.getAvailableCollections(); | ||
for (const auto& name : podio::utils::sortAlphabeticaly(collNames)) { | ||
const auto coll = frame.get(name); | ||
print_flush("{}\n", name); | ||
coll->print(); | ||
print_flush("\n"); | ||
} | ||
|
||
print_flush("\nParameters\n:"); | ||
frame.getParameters().print(); | ||
print_flush("\n"); | ||
} | ||
|
||
void printGeneralInfo(const podio::Reader& reader, const std::string& filename) { | ||
fmt::print("input file: {}\n", filename); | ||
fmt::print("datamodel model definitions stored in this file: {}\n\n", reader.getAvailableDatamodels()); | ||
|
||
std::vector<std::tuple<std::string, size_t>> rows{}; | ||
for (const auto& cat : reader.getAvailableCategories()) { | ||
rows.emplace_back(cat, reader.getEntries(std::string(cat))); | ||
} | ||
fmt::print("Frame categories in this file:\nName\tEntries\n"); | ||
printTable(rows, {"Name", "Entries"}); | ||
} | ||
|
||
void printFrame(const podio::Frame& frame, const std::string& category, size_t iEntry, bool detailed) { | ||
fmt::print("{:#^82}\n", fmt::format(" {}: {} ", category, iEntry)); | ||
if (detailed) { | ||
printFrameDetailed(frame); | ||
} else { | ||
printFrameOverview(frame); | ||
} | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
// We strip the executable name off directly for parsing | ||
const auto args = parseArgs({argv + 1, argv + argc}); | ||
|
||
auto reader = podio::makeReader(args.inputFile); | ||
|
||
printGeneralInfo(reader, args.inputFile); | ||
|
||
for (const auto event : args.events) { | ||
try { | ||
const auto& frame = reader.readFrame(args.category, event); | ||
printFrame(frame, args.category, event, args.detailed); | ||
} catch (std::runtime_error& err) { | ||
fmt::print(stderr, "{}\n", err.what()); | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Executable needs to be installed.