Skip to content

Instantly share code, notes, and snippets.

@eguiraud
Last active January 25, 2018 17:33
Show Gist options
  • Save eguiraud/54b9173a4d6996a8c6d0b8898b67e723 to your computer and use it in GitHub Desktop.
Save eguiraud/54b9173a4d6996a8c6d0b8898b67e723 to your computer and use it in GitHub Desktop.
proof of concept of TDF usage with pipe operators
#include <vector>
#include <ROOT/TDataFrame.hxx>
#include <TCanvas.h>
using namespace ROOT::Experimental;
template <typename TDF, typename OP>
auto operator|(TDF &d, OP &&op)
{
return op(d);
}
template <typename TDF, typename OP>
auto operator|(TDF &&d, OP &&op)
{
return op(d);
}
struct DefineColumns {
template <typename TDF>
auto operator()(TDF &d)
{
return d.Define("x", [] { return 42; }).Define("y", [] { return 4.2; });
}
};
struct ApplyCuts {
template <typename TDF>
auto operator()(TDF &d)
{
return d.Filter([](int x) { return x == 42; }, {"x"}, "filter");
}
};
struct GetHistos {
template <typename TDF>
auto operator()(TDF &d)
{
using HistProxy = ROOT::Detail::TDF::TResultProxy<TH1D>;
std::vector<HistProxy> hists;
hists.emplace_back(d.template Histo1D<int>("x"));
hists.emplace_back(d.template Histo1D<double>("y"));
return hists;
}
};
int main()
{
TDataFrame d(10);
auto histos = d | DefineColumns() | ApplyCuts() | GetHistos();
d.Report();
return 0;
}
#include <vector>
#include <ROOT/TDataFrame.hxx>
#include <TCanvas.h>
using namespace ROOT::Experimental;
template <typename TDF, typename OP>
auto operator|(TDF &d, OP &&op) -> decltype(op(d))
{
return op(d);
}
template <typename TDF, typename OP>
auto operator|(TDF &&d, OP &&op) -> decltype(op(d))
{
return op(d);
}
struct DefineColumns {
template <typename TDF>
auto operator()(TDF &d) -> decltype(d.Define(std::string(), std::declval<double (*)(void)>()))
{
return d.Define("x", [] { return 42; }).Define("y", [] { return 4.2; });
}
};
struct ApplyCuts {
static bool myCut(int x) { return x == 42; }
template <typename TDF>
auto operator()(TDF &d) -> decltype(d.Filter(myCut))
{
return d.Filter(myCut, {"x"}, "filter");
}
};
struct GetHistos {
using Histos = std::vector<ROOT::Detail::TDF::TResultProxy<TH1D>>;
template <typename TDF>
auto operator()(TDF &d) -> Histos
{
Histos hs;
hs.emplace_back(d.template Histo1D<int>("x"));
hs.emplace_back(d.template Histo1D<double>("y"));
return hs;
}
};
int main()
{
TDataFrame d(10);
auto histos = d | DefineColumns() | ApplyCuts() | GetHistos();
d.Report();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment