I’ve been working with rDataFrame to analyse data for a while now.
For some MC analyses sometimes I’d like study a filtered event sample and sometimes I’d like to study the full sample. In order not to rewrite the full macro I use a bool to enable the filtering or not.
However if I create a filtered set of the RDF within the if block I can’t use it outside the block. How can I allocate a variable before the if blocks that then can hold the filtered entities? Just doing RDataFrame d_filter; at the beginning doesn’t work.
I’ve tried to illustrated my intention with a code example.
Thank you
Francesca
int my_analysis(bool doFilter){
RDataFrame d("bla","bla.root");
if(doFilter) {
auto d_filter = d.Filter("variable < 10");
}
else {
auto d_filter = d.Filter("other_variable <10");
}
auto doStuff = d_filter.Define(...);
return 0;
}
#include <ROOT/RDataFrame.hxx>
int main() {
ROOT::RDataFrame df(10);
// with a helper lambda (could also be a helper function) that you can also call on the spot
ROOT::RDF::RNode filtered_df2 = [&](bool p) -> ROOT::RDF::RNode { return p ? df.Filter("true") : df.Filter("false"); }(true);
// with a unique_ptr<RNode>
std::unique_ptr<ROOT::RDF::RNode> filtered_df;
if (true) {
filtered_df = std::make_unique<ROOT::RDF::RNode>(df.Filter("true"));
} else {
filtered_df = std::make_unique<ROOT::RDF::RNode>(df.Filter("false"));
}
return 0;
}
Also, with your permission, can I move this thread out of the newbie section? I think it could be useful to reference it in the future and posts in this section self-destruct after some time.