Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Sort the branches alphabetically when writing Frames #421

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ROOTFrameWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void ROOTFrameWriter::writeFrame(const podio::Frame& frame, const std::string& c
// been initialized
if (catInfo.tree == nullptr) {
catInfo.idTable = frame.getCollectionIDTableForWrite();
catInfo.collsToWrite = collsToWrite;
catInfo.collsToWrite = root_utils::sortAlphabeticaly(collsToWrite);
catInfo.tree = new TTree(category.c_str(), (category + " data tree").c_str());
catInfo.tree->SetDirectory(m_file.get());
}
Expand Down
21 changes: 21 additions & 0 deletions src/rootUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "TClass.h"
#include "TTree.h"

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -182,6 +184,25 @@ inline auto reconstructCollectionInfo(TTree* eventTree, podio::CollectionIDTable
return collInfo;
}

/**
* 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::root_utils

#endif