Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Last active May 19, 2023 15:53
Show Gist options
  • Save eguiraud/0706418f937e33800e6e1eac2e1766f5 to your computer and use it in GitHub Desktop.
Save eguiraud/0706418f937e33800e6e1eac2e1766f5 to your computer and use it in GitHub Desktop.
Bulk RDF with functor class
#include <ROOT/RDataFrame.hxx>
#include <ROOT/RVec.hxx>
#include <iostream>
using ROOT::RVecF;
using ROOT::RVecU;
struct ToolAdapter {
Tool tool;
ToolAdapter() : tool(...) {}
void operator()(const ROOT::RDF::Experimental::REventMask &eventMask,
ROOT::RVec<RVecF> &results, const ROOT::RVec<RVecF> &Muon_pts) {
const auto size = eventMask.Size();
for (std::size_t i = 0u; i < size; ++i) {
if (eventMask[i]) {
const auto sz = xs[i].size();
tool.setcolumn("Muon_pt", Muon_pts[i].data(), Muon_pts[i].size());
// ...
results[i].resize(sz);
tool.setoutput("...", results[i].data(), results[i].size();
too.compute();
}
}
};
int main() {
ROOT::RDataFrame df(10);
auto m = df.Define("arr", [] { return RVecF{1.f,2.f,3.f}; })
.Define("arrSquared", BulkSquare{}, {"arr"})
.Mean<RVecF>("arrSquared")
.GetValue();
std::cout << "mean entry*entry: " << m << '\n';
}
#include <ROOT/RDataFrame.hxx>
#include <ROOT/RVec.hxx>
#include <iostream>
using ROOT::RVecF;
using ROOT::RVecU;
struct BulkSquare {
void operator()(const ROOT::RDF::Experimental::REventMask &eventMask,
RVecF &results, const RVecF &xs) {
const auto size = eventMask.Size();
for (std::size_t i = 0u; i < size; ++i)
if (eventMask[i])
results[i] = xs[i] * xs[i];
}
};
int main() {
ROOT::RDataFrame df(10);
auto m = df.Define("x", [](ULong64_t entry) { return float(entry); },
{"rdfentry_"})
.Define("x2", BulkSquare{}, {"x"})
.Mean<float>("x2")
.GetValue();
std::cout << "mean entry*entry: " << m << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment