30#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Woverloaded-virtual"
32#pragma GCC diagnostic ignored "-Wshadow"
36#pragma GCC diagnostic pop
41#include <unordered_set>
68struct ParsedExpression {
79static std::pair<ColumnNames_t, ColumnNames_t>
80FindUsedColsAndAliases(
const std::string &expr,
const ColumnNames_t &treeBranchNames,
82 const ColumnNames_t &dataSourceColNames)
84 lexertk::generator tokens;
85 const auto tokensOk = tokens.process(expr);
87 const auto msg =
"Failed to tokenize expression:\n" + expr +
"\n\nMake sure it is valid C++.";
88 throw std::runtime_error(msg);
91 std::unordered_set<std::string> usedCols;
92 std::unordered_set<std::string> usedAliases;
95 const auto nTokens = tokens.size();
96 const auto kSymbol = lexertk::token::e_symbol;
97 for (
auto i = 0u; i < nTokens; ++i) {
98 const auto &tok = tokens[i];
100 if (tok.type != kSymbol || tok.value ==
"&" || tok.value ==
"|") {
108 auto dotChainKeepsGoing = [&](
unsigned int _i) {
109 return _i + 2 <= nTokens && tokens[_i + 1].value ==
"." && tokens[_i + 2].type == kSymbol;
111 while (dotChainKeepsGoing(i)) {
112 potentialColNames.emplace_back(potentialColNames.back() +
"." + tokens[i + 2].value);
118 const auto maybeAnAlias = potentialColNames[0];
119 const auto &resolvedAlias = customColumns.
ResolveAlias(maybeAnAlias);
120 if (resolvedAlias != maybeAnAlias) {
121 usedAliases.insert(maybeAnAlias);
122 for (
auto &s : potentialColNames)
123 s.replace(0, maybeAnAlias.
size(), resolvedAlias);
128 auto isRDFColumn = [&](
const std::string &col) {
133 const auto longestRDFColMatch = std::find_if(potentialColNames.crbegin(), potentialColNames.crend(), isRDFColumn);
134 if (longestRDFColMatch != potentialColNames.crend())
135 usedCols.insert(*longestRDFColMatch);
138 return {{usedCols.begin(), usedCols.end()}, {usedAliases.begin(), usedAliases.end()}};
142static std::string EscapeDots(
const std::string &s)
146 dot.Substitute(out,
"\\.",
"g");
147 return std::string(std::move(out));
150static TString ResolveAliases(
const TString &expr,
const ColumnNames_t &usedAliases,
155 for (
const auto &alias : usedAliases) {
157 TPRegexp replacer(
"\\b" + EscapeDots(alias) +
"\\b");
158 replacer.Substitute(out, col,
"g");
164static ParsedExpression ParseRDFExpression(std::string_view expr,
const ColumnNames_t &treeBranchNames,
166 const ColumnNames_t &dataSourceColNames)
169 TString preProcessedExpr(expr);
172 "(^|\\W)#(?!(ifdef|ifndef|if|else|elif|endif|pragma|define|undef|include|line))([a-zA-Z_][a-zA-Z0-9_]*)");
173 colSizeReplacer.Substitute(preProcessedExpr,
"$1R_rdf_sizeof_$3",
"g");
177 std::tie(usedCols, usedAliases) =
178 FindUsedColsAndAliases(std::string(preProcessedExpr), treeBranchNames, colRegister, dataSourceColNames);
180 const auto exprNoAliases = ResolveAliases(preProcessedExpr, usedAliases, colRegister);
184 TString exprWithVars(exprNoAliases);
187 for (
auto i = 0u; i < varNames.size(); ++i)
188 varNames[i] =
"var" + std::to_string(i);
195 std::sort(usedCols.begin(), usedCols.end(),
196 [](
const std::string &
a,
const std::string &
b) { return a.size() > b.size(); });
197 for (
const auto &col : usedCols) {
198 const auto varIdx = std::distance(usedCols.begin(), std::find(usedCols.begin(), usedCols.end(), col));
199 TPRegexp replacer(
"\\b" + EscapeDots(col) +
"\\b");
200 replacer.Substitute(exprWithVars, varNames[varIdx],
"g");
203 return ParsedExpression{std::string(std::move(exprWithVars)), std::move(usedCols), std::move(varNames)};
213static std::unordered_map<std::string, std::string> &GetJittedExprs() {
214 static std::unordered_map<std::string, std::string> jittedExpressions;
215 return jittedExpressions;
219BuildLambdaString(
const std::string &expr,
const ColumnNames_t &vars,
const ColumnNames_t &varTypes)
221 assert(vars.size() == varTypes.size());
224 const bool hasReturnStmt = re.MatchB(expr);
226 static const std::vector<std::string> fundamentalTypes = {
259 std::stringstream ss;
261 for (
auto i = 0u; i < vars.size(); ++i) {
262 std::string fullType;
263 const auto &
type = varTypes[i];
264 if (std::find(fundamentalTypes.begin(), fundamentalTypes.end(),
type) != fundamentalTypes.end()) {
266 fullType =
"const " +
type +
" ";
270 fullType =
type +
"& ";
272 ss << fullType << vars[i] <<
", ";
275 ss.seekp(-2, ss.cur);
281 ss << expr <<
"\n;}";
288static std::string DeclareLambda(
const std::string &expr,
const ColumnNames_t &vars,
const ColumnNames_t &varTypes)
292 const auto lambdaExpr = BuildLambdaString(expr, vars, varTypes);
293 auto &exprMap = GetJittedExprs();
294 const auto exprIt = exprMap.find(lambdaExpr);
295 if (exprIt != exprMap.end()) {
297 const auto lambdaName = exprIt->second;
302 const auto lambdaBaseName =
"lambda" + std::to_string(exprMap.size());
303 const auto lambdaFullName =
"R_rdf::" + lambdaBaseName;
305 const auto toDeclare =
"namespace R_rdf {\nauto " + lambdaBaseName +
" = " + lambdaExpr +
";\nusing " +
306 lambdaBaseName +
"_ret_t = typename ROOT::TypeTraits::CallableTraits<decltype(" +
307 lambdaBaseName +
")>::ret_type;\n}";
311 exprMap.insert({lambdaExpr, lambdaFullName});
313 return lambdaFullName;
318static std::string RetTypeOfLambda(
const std::string &lambdaName)
320 const auto dt =
gROOT->GetType((lambdaName +
"_ret_t").c_str());
322 const auto type = dt->GetFullTypeName();
326static void GetTopLevelBranchNamesImpl(
TTree &t, std::set<std::string> &bNamesReg, ColumnNames_t &bNames,
327 std::set<TTree *> &analysedTrees,
const std::string friendName =
"")
329 if (!analysedTrees.insert(&t).second) {
335 for (
auto branchObj : *branches) {
337 if (bNamesReg.insert(
name).second) {
338 bNames.emplace_back(
name);
339 }
else if (!friendName.empty()) {
343 const auto longName = friendName +
"." +
name;
344 if (bNamesReg.insert(longName).second)
345 bNames.emplace_back(longName);
355 for (
auto friendTreeObj : *friendTrees) {
356 auto friendElement =
static_cast<TFriendElement *
>(friendTreeObj);
357 auto friendTree = friendElement->
GetTree();
358 const std::string frName(friendElement->GetName());
359 GetTopLevelBranchNamesImpl(*friendTree, bNamesReg, bNames, analysedTrees, frName);
375 std::copy_if(columnNames.begin(), columnNames.end(), std::back_inserter(columnListWithoutSizeColumns),
376 [&](
const std::string &
name) {
377 if (name[0] ==
'#') {
378 filteredColumns.emplace_back(name);
385 if (!filteredColumns.empty()) {
386 std::string msg =
"Column name(s) {";
387 for (
auto &
c : filteredColumns)
389 msg[msg.size() - 2] =
'}';
390 msg +=
"will be ignored. Please go through a valid Alias to " + action +
" an array size column";
391 throw std::runtime_error(msg);
394 return columnListWithoutSizeColumns;
397std::string
ResolveAlias(
const std::string &col,
const std::map<std::string, std::string> &aliasMap)
399 const auto it = aliasMap.find(col);
400 if (it != aliasMap.end())
404 if (col.size() > 1 && col[0] ==
'#')
405 return "R_rdf_sizeof_" + col.substr(1);
416 const char firstChar = var[0];
419 auto isALetter = [](
char c) {
return (
c >=
'A' &&
c <=
'Z') || (
c >=
'a' &&
c <=
'z'); };
420 const bool isValidFirstChar = firstChar ==
'_' || isALetter(firstChar);
421 if (!isValidFirstChar)
425 auto isANumber = [](
char c) {
return c >=
'0' &&
c <=
'9'; };
426 auto isValidTok = [&isALetter, &isANumber](
char c) {
return c ==
'_' || isALetter(
c) || isANumber(
c); };
427 for (
const char c : var)
433 "RDataFrame::" + where +
": cannot define column \"" + std::string(var) +
"\". Not a valid C++ variable name.";
434 throw std::runtime_error(error);
442 std::set<std::string> bNamesSet;
444 std::set<TTree *> analysedTrees;
445 GetTopLevelBranchNamesImpl(t, bNamesSet, bNames, analysedTrees);
449std::string DemangleTypeIdName(
const std::type_info &typeInfo)
453 std::string tname(tn);
461 const auto theRegexSize = columnNameRegexp.size();
462 std::string theRegex(columnNameRegexp);
464 const auto isEmptyRegex = 0 == theRegexSize;
466 if (theRegexSize > 0 && theRegex[0] !=
'^')
467 theRegex =
"^" + theRegex;
468 if (theRegexSize > 0 && theRegex[theRegexSize - 1] !=
'$')
469 theRegex = theRegex +
"$";
476 for (
auto &&colName : colNames) {
478 selectedColumns.emplace_back(colName);
482 if (selectedColumns.empty()) {
483 std::string
text(callerName);
484 if (columnNameRegexp.empty()) {
485 text =
": there is no column available to match.";
487 text =
": regex \"" + std::string(columnNameRegexp) +
"\" did not match any column.";
489 throw std::runtime_error(
text);
491 return selectedColumns;
498 const std::string definedCol(definedColView);
501 if (customCols.
IsAlias(definedCol))
502 error =
"An alias with that name, pointing to column \"" + customCols.
ResolveAlias(definedCol) +
503 "\", already exists in this branch of the computation graph.";
504 else if (customCols.
HasName(definedCol))
505 error =
"A column with that name has already been Define'd. Use Redefine to force redefinition.";
509 else if (std::find(treeColumns.begin(), treeColumns.end(), definedCol) != treeColumns.end())
511 "A branch with that name is already present in the input TTree/TChain. Use Redefine to force redefinition.";
512 else if (std::find(dataSourceColumns.begin(), dataSourceColumns.end(), definedCol) != dataSourceColumns.end())
514 "A column with that name is already present in the input data source. Use Redefine to force redefinition.";
516 if (!error.empty()) {
517 error =
"RDataFrame::" + where +
": cannot define column \"" + definedCol +
"\". " + error;
518 throw std::runtime_error(error);
526 const std::string definedCol(definedColView);
529 if (customCols.
IsAlias(definedCol)) {
530 error =
"An alias with that name, pointing to column \"" + customCols.
ResolveAlias(definedCol) +
531 "\", already exists. Aliases cannot be Redefined or Varied.";
535 const bool isAlreadyDefined = customCols.
HasName(definedCol);
539 const bool isABranch = std::find(treeColumns.begin(), treeColumns.end(), definedCol) != treeColumns.end();
540 const bool isADSColumn =
541 std::find(dataSourceColumns.begin(), dataSourceColumns.end(), definedCol) != dataSourceColumns.end();
543 if (!isAlreadyDefined && !isABranch && !isADSColumn)
544 error =
"No column with that name was found in the dataset. Use Define to create a new column.";
547 if (!error.empty()) {
548 error =
"RDataFrame::" + where +
": cannot redefine or vary column \"" + definedCol +
"\". " + error;
549 throw std::runtime_error(error);
556 const std::string definedCol(definedColView);
558 if (!variationDeps.empty()) {
559 const std::string error =
560 "RDataFrame::" + where +
": cannot redefine column \"" + definedCol +
561 "\". The column depends on one or more systematic variations and re-defining varied columns is not supported.";
562 throw std::runtime_error(error);
568 if (nTemplateParams != nColumnNames) {
569 std::string err_msg =
"The number of template parameters specified is ";
570 err_msg += std::to_string(nTemplateParams);
571 err_msg +=
" while ";
572 err_msg += std::to_string(nColumnNames);
573 err_msg +=
" columns have been specified.";
574 throw std::runtime_error(err_msg);
584 if (defaultNames.size() < nRequiredNames)
585 throw std::runtime_error(
586 std::to_string(nRequiredNames) +
" column name" + (nRequiredNames == 1 ?
" is" :
"s are") +
587 " required but none were provided and the default list has size " + std::to_string(defaultNames.size()));
589 return ColumnNames_t(defaultNames.begin(), defaultNames.begin() + nRequiredNames);
592 if (names.size() != nRequiredNames) {
593 auto msg = std::to_string(nRequiredNames) +
" column name" + (nRequiredNames == 1 ?
" is" :
"s are") +
594 " required but " + std::to_string(names.size()) + (names.size() == 1 ?
" was" :
" were") +
596 for (
const auto &
name : names)
597 msg +=
" \"" +
name +
"\",";
599 throw std::runtime_error(msg);
609 for (
auto &column : requiredCols) {
610 const auto isBranch = std::find(datasetColumns.begin(), datasetColumns.end(), column) != datasetColumns.end();
613 if (definedCols.
HasName(column))
615 const auto isDataSourceColumn =
616 std::find(dataSourceColumns.begin(), dataSourceColumns.end(), column) != dataSourceColumns.end();
617 if (isDataSourceColumn)
619 unknownColumns.emplace_back(column);
621 return unknownColumns;
624std::vector<std::string>
GetFilterNames(
const std::shared_ptr<RLoopManager> &loopManager)
626 return loopManager->GetFiltersNames();
632 std::string_view dirName =
"";
633 std::string_view treeName = fullTreeName;
634 const auto lastSlash = fullTreeName.rfind(
'/');
635 if (std::string_view::npos != lastSlash) {
636 dirName = treeName.substr(0, lastSlash);
637 treeName = treeName.substr(lastSlash + 1, treeName.size());
639 return {std::string(treeName), std::string(dirName)};
646 s << std::hex << std::showbase << reinterpret_cast<size_t>(addr);
651std::shared_ptr<RDFDetail::RJittedFilter>
652BookFilterJit(std::shared_ptr<RDFDetail::RNodeBase> *prevNodeOnHeap, std::string_view
name, std::string_view expression,
655 const auto &dsColumns = ds ? ds->GetColumnNames() :
ColumnNames_t{};
657 const auto parsedExpr = ParseRDFExpression(expression, branches, customCols, dsColumns);
658 const auto exprVarTypes =
660 const auto lambdaName = DeclareLambda(parsedExpr.fExpr, parsedExpr.fVarNames, exprVarTypes);
661 const auto type = RetTypeOfLambda(lambdaName);
663 std::runtime_error(
"Filter: the following expression does not evaluate to bool:\n" + std::string(expression));
670 const auto jittedFilter = std::make_shared<RDFDetail::RJittedFilter>(
671 (*prevNodeOnHeap)->GetLoopManagerUnchecked(),
name,
676 std::stringstream filterInvocation;
677 filterInvocation <<
"ROOT::Internal::RDF::JitFilterHelper(" << lambdaName <<
", new const char*["
678 << parsedExpr.fUsedCols.size() <<
"]{";
679 for (
const auto &col : parsedExpr.fUsedCols)
680 filterInvocation <<
"\"" << col <<
"\", ";
681 if (!parsedExpr.fUsedCols.empty())
682 filterInvocation.seekp(-2, filterInvocation.cur);
687 filterInvocation <<
"}, " << parsedExpr.fUsedCols.size() <<
", \"" <<
name <<
"\", "
688 <<
"reinterpret_cast<std::weak_ptr<ROOT::Detail::RDF::RJittedFilter>*>("
690 <<
"reinterpret_cast<std::shared_ptr<ROOT::Detail::RDF::RNodeBase>*>(" << prevNodeAddr <<
"),"
691 <<
"reinterpret_cast<ROOT::Internal::RDF::RColumnRegister*>(" << definesOnHeapAddr <<
")"
694 auto lm = jittedFilter->GetLoopManagerUnchecked();
695 lm->ToJitExec(filterInvocation.str());
704 std::shared_ptr<RNodeBase> *upcastNodeOnHeap)
707 const auto &dsColumns = ds ? ds->GetColumnNames() :
ColumnNames_t{};
709 const auto parsedExpr = ParseRDFExpression(expression, branches, customCols, dsColumns);
710 const auto exprVarTypes =
712 const auto lambdaName = DeclareLambda(parsedExpr.fExpr, parsedExpr.fVarNames, exprVarTypes);
713 const auto type = RetTypeOfLambda(lambdaName);
717 auto jittedDefine = std::make_shared<RDFDetail::RJittedDefine>(
name,
type, lm, customCols, parsedExpr.fUsedCols);
719 std::stringstream defineInvocation;
720 defineInvocation <<
"ROOT::Internal::RDF::JitDefineHelper<ROOT::Internal::RDF::DefineTypes::RDefineTag>("
721 << lambdaName <<
", new const char*[" << parsedExpr.fUsedCols.size() <<
"]{";
722 for (
const auto &col : parsedExpr.fUsedCols) {
723 defineInvocation <<
"\"" << col <<
"\", ";
725 if (!parsedExpr.fUsedCols.empty())
726 defineInvocation.seekp(-2, defineInvocation.cur);
731 defineInvocation <<
"}, " << parsedExpr.fUsedCols.size() <<
", \"" <<
name
732 <<
"\", reinterpret_cast<ROOT::Detail::RDF::RLoopManager*>(" <<
PrettyPrintAddr(&lm)
733 <<
"), reinterpret_cast<std::weak_ptr<ROOT::Detail::RDF::RJittedDefine>*>("
735 <<
"), reinterpret_cast<ROOT::Internal::RDF::RColumnRegister*>(" << definesAddr
736 <<
"), reinterpret_cast<std::shared_ptr<ROOT::Detail::RDF::RNodeBase>*>("
746 std::shared_ptr<RNodeBase> *upcastNodeOnHeap)
748 const auto lambdaName = DeclareLambda(std::string(expression), {
"rdfslot_",
"rdfsampleinfo_"},
749 {
"unsigned int",
"const ROOT::RDF::RSampleInfo"});
750 const auto retType = RetTypeOfLambda(lambdaName);
754 auto jittedDefine = std::make_shared<RDFDetail::RJittedDefine>(
name, retType, lm, customCols,
ColumnNames_t{});
756 std::stringstream defineInvocation;
757 defineInvocation <<
"ROOT::Internal::RDF::JitDefineHelper<ROOT::Internal::RDF::DefineTypes::RDefinePerSampleTag>("
758 << lambdaName <<
", nullptr, 0, ";
763 defineInvocation <<
"\"" <<
name <<
"\", reinterpret_cast<ROOT::Detail::RDF::RLoopManager*>(" <<
PrettyPrintAddr(&lm)
764 <<
"), reinterpret_cast<std::weak_ptr<ROOT::Detail::RDF::RJittedDefine>*>("
766 <<
"), reinterpret_cast<ROOT::Internal::RDF::RColumnRegister*>(" << definesAddr
767 <<
"), reinterpret_cast<std::shared_ptr<ROOT::Detail::RDF::RNodeBase>*>("
775std::shared_ptr<RJittedVariation>
777 const std::vector<std::string> &variationTags, std::string_view expression,
RLoopManager &lm,
779 std::shared_ptr<RNodeBase> *upcastNodeOnHeap)
782 const auto &dsColumns = ds ? ds->GetColumnNames() :
ColumnNames_t{};
784 const auto parsedExpr = ParseRDFExpression(expression, branches, colRegister, dsColumns);
785 const auto exprVarTypes =
787 const auto lambdaName = DeclareLambda(parsedExpr.fExpr, parsedExpr.fVarNames, exprVarTypes);
788 const auto type = RetTypeOfLambda(lambdaName);
790 if (
type.rfind(
"ROOT::VecOps::RVec", 0) != 0)
791 throw std::runtime_error(
792 "Jitted Vary expressions must return an RVec object. The following expression returns a " +
type +
793 " instead:\n" + parsedExpr.fExpr);
797 auto jittedVariation = std::make_shared<RJittedVariation>(colNames, variationName, variationTags,
type, colRegister,
798 lm, parsedExpr.fUsedCols);
806 std::stringstream varyInvocation;
807 varyInvocation <<
"ROOT::Internal::RDF::JitVariationHelper(" << lambdaName <<
", new const char*["
808 << parsedExpr.fUsedCols.size() <<
"]{";
809 for (
const auto &col : parsedExpr.fUsedCols) {
810 varyInvocation <<
"\"" << col <<
"\", ";
812 if (!parsedExpr.fUsedCols.empty())
813 varyInvocation.seekp(-2, varyInvocation.cur);
814 varyInvocation <<
"}, " << parsedExpr.fUsedCols.size();
815 varyInvocation <<
", new const char*[" << colNames.size() <<
"]{";
816 for (
const auto &col : colNames) {
817 varyInvocation <<
"\"" << col <<
"\", ";
819 varyInvocation.seekp(-2, varyInvocation.cur);
820 varyInvocation <<
"}, " << colNames.size() <<
", new const char*[" << variationTags.size() <<
"]{";
821 for (
const auto &tag : variationTags) {
822 varyInvocation <<
"\"" << tag <<
"\", ";
824 varyInvocation.seekp(-2, varyInvocation.cur);
825 varyInvocation <<
"}, " << variationTags.size() <<
", \"" << variationName
826 <<
"\", reinterpret_cast<ROOT::Detail::RDF::RLoopManager*>(" <<
PrettyPrintAddr(&lm)
827 <<
"), reinterpret_cast<std::weak_ptr<ROOT::Internal::RDF::RJittedVariation>*>("
829 <<
"), reinterpret_cast<ROOT::Internal::RDF::RColumnRegister*>(" << colRegisterAddr
830 <<
"), reinterpret_cast<std::shared_ptr<ROOT::Detail::RDF::RNodeBase>*>("
834 return jittedVariation;
840 const std::type_info &helperArgType,
const std::type_info &at,
void *helperArgOnHeap,
842 std::weak_ptr<RJittedAction> *jittedActionOnHeap)
846 if (!helperArgClass) {
847 std::string exceptionText =
"An error occurred while inferring the result type of an operation.";
848 throw std::runtime_error(exceptionText.c_str());
850 const auto helperArgClassName = helperArgClass->GetName();
854 if (!actionTypeClass) {
855 std::string exceptionText =
"An error occurred while inferring the action type of the operation.";
856 throw std::runtime_error(exceptionText.c_str());
858 const std::string actionTypeName = actionTypeClass->GetName();
859 const std::string actionTypeNameBase = actionTypeName.substr(actionTypeName.rfind(
':') + 1);
866 std::stringstream createAction_str;
867 createAction_str <<
"ROOT::Internal::RDF::CallBuildAction<" << actionTypeName;
868 const auto columnTypeNames =
870 for (
auto &colType : columnTypeNames)
871 createAction_str <<
", " << colType;
874 createAction_str <<
">(reinterpret_cast<std::shared_ptr<ROOT::Detail::RDF::RNodeBase>*>("
875 <<
PrettyPrintAddr(prevNode) <<
"), new const char*[" << cols.size() <<
"]{";
876 for (
auto i = 0u; i < cols.size(); ++i) {
878 createAction_str <<
", ";
879 createAction_str <<
'"' << cols[i] <<
'"';
881 createAction_str <<
"}, " << cols.size() <<
", " << nSlots <<
", reinterpret_cast<" << helperArgClassName <<
"*>("
883 <<
"), reinterpret_cast<std::weak_ptr<ROOT::Internal::RDF::RJittedAction>*>("
885 <<
"), reinterpret_cast<ROOT::Internal::RDF::RColumnRegister*>(" << definesAddr <<
"));";
886 return createAction_str.str();
891 for (
const auto &s : strings) {
898std::shared_ptr<RNodeBase>
UpcastNode(std::shared_ptr<RNodeBase> ptr)
913 for (
auto &col : selectedColumns) {
921 if (!unknownColumns.empty()) {
922 std::stringstream unknowns;
923 std::string delim = unknownColumns.size() > 1 ?
"s: " :
": ";
924 for (
auto &unknownColumn : unknownColumns) {
925 unknowns << delim << unknownColumn;
928 throw std::runtime_error(
"Unknown column" + unknowns.str());
931 return selectedColumns;
938 auto toCheckedArgType = [&](
const std::string &
c) {
941 if (colType.rfind(
"CLING_UNKNOWN_TYPE", 0) == 0) {
943 "The type of custom column \"" +
c +
"\" (" + colType.substr(19) +
944 ") is not known to the interpreter, but a just-in-time-compiled " + context +
945 " call requires this column. Make sure to create and load ROOT dictionaries for this column's class.";
946 throw std::runtime_error(msg);
950 std::vector<std::string> colTypes;
951 colTypes.reserve(colNames.size());
952 std::transform(colNames.begin(), colNames.end(), std::back_inserter(colTypes), toCheckedArgType);
961 const auto nColumns = requestedCols.size();
962 std::vector<bool> mustBeDefined(nColumns,
false);
963 for (
auto i = 0u; i < nColumns; ++i)
964 mustBeDefined[i] = std::find(definedCols.begin(), definedCols.end(), requestedCols[i]) == definedCols.end();
965 return mustBeDefined;
970 std::unordered_set<std::string> uniqueCols;
971 for (
auto &col : cols) {
972 if (!uniqueCols.insert(col).second) {
973 const auto msg =
"Error: column \"" + col +
974 "\" was passed to Snapshot twice. This is not supported: only one of the columns would be "
975 "readable with RDataFrame.";
976 throw std::logic_error(msg);
993std::pair<std::vector<std::string>, std::vector<std::string>>
995 std::vector<std::string> &&colsWithAliases)
998 return {std::move(colsWithoutAliases), std::move(colsWithAliases)};
1000 assert(colsWithoutAliases.size() == colsWithAliases.size());
1002 auto nCols = colsWithoutAliases.size();
1004 for (std::size_t i = 0u; i < nCols; ++i) {
1005 const auto &colName = colsWithoutAliases[i];
1009 auto *
b =
tree->GetBranch(colName.c_str());
1011 b =
tree->FindBranch(colName.c_str());
1012 assert(
b !=
nullptr);
1013 auto *leaves =
b->GetListOfLeaves();
1014 if (
b->IsA() != TBranch::Class() || leaves->GetEntries() != 1)
1017 TLeaf *countLeaf =
static_cast<TLeaf *
>(leaves->At(0))->GetLeafCount();
1022 colsWithoutAliases.insert(colsWithoutAliases.begin() + i, countLeaf->
GetName());
1023 colsWithAliases.insert(colsWithAliases.begin() + i, countLeaf->
GetName());
1028 return {std::move(colsWithoutAliases), std::move(colsWithAliases)};
1033 std::set<std::string> uniqueCols;
1035 std::remove_if(columnNames.begin(), columnNames.end(),
1036 [&uniqueCols](
const std::string &colName) { return !uniqueCols.insert(colName).second; }),
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
R__EXTERN TVirtualMutex * gROOTMutex
#define R__LOCKGUARD(mutex)
The head node of a RDF computation graph.
const ColumnNames_t & GetBranchNames()
Return all valid TTree::Branch names (caching results for subsequent calls).
void ToJitExec(const std::string &) const
void Run()
Start the event loop with a different mechanism depending on IMT/no IMT, data source/no data source.
const ColumnNames_t & GetDefaultColumnNames() const
Return the list of default columns – empty if none was provided when constructing the RDataFrame.
A binder for user-defined columns and aliases.
bool IsAlias(const std::string &name) const
Return true if the given column name is an existing alias.
const DefinesMap_t & GetColumns() const
Returns a map of pointers to the defined columns.
bool HasName(std::string_view name) const
Check if the provided name is tracked in the names list.
std::string ResolveAlias(std::string_view alias) const
Return the actual column name that the alias resolves to.
std::vector< std::string > GetVariationDeps(const std::string &column) const
Get the names of all variations that directly or indirectly affect a given column.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
The public interface to the RDataFrame federation of classes.
RLoopManager * fLoopManager
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
virtual const char * GetName() const
Return name of this collection.
A TFriendElement TF describes a TTree object TF in a file.
virtual TTree * GetTree()
Return pointer to friend TTree.
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
virtual const char * GetName() const
Returns name of object.
Bool_t MatchB(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
A TTree represents a columnar dataset.
virtual TObjArray * GetListOfBranches()
virtual TList * GetListOfFriends() const
const ColumnNames_t SelectColumns(unsigned int nRequiredNames, const ColumnNames_t &names, const ColumnNames_t &defaultNames)
Choose between local column names or default column names, throw in case of errors.
ParsedTreePath ParseTreePath(std::string_view fullTreeName)
std::shared_ptr< RJittedVariation > BookVariationJit(const std::vector< std::string > &colNames, std::string_view variationName, const std::vector< std::string > &variationTags, std::string_view expression, RLoopManager &lm, RDataSource *ds, const RColumnRegister &colRegister, const ColumnNames_t &branches, std::shared_ptr< RNodeBase > *upcastNodeOnHeap)
Book the jitting of a Vary call.
void CheckValidCppVarName(std::string_view var, const std::string &where)
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *, RDataSource *, RDefineBase *, bool vector2rvec=true)
Return a string containing the type of the given branch.
void RemoveDuplicates(ColumnNames_t &columnNames)
std::shared_ptr< RNodeBase > UpcastNode(std::shared_ptr< RNodeBase > ptr)
ColumnNames_t GetValidatedColumnNames(RLoopManager &lm, const unsigned int nColumns, const ColumnNames_t &columns, const RColumnRegister &customColumns, RDataSource *ds)
Given the desired number of columns and the user-provided list of columns:
bool IsStrInVec(const std::string &str, const std::vector< std::string > &vec)
std::string ResolveAlias(const std::string &col, const std::map< std::string, std::string > &aliasMap)
std::vector< std::string > GetFilterNames(const std::shared_ptr< RLoopManager > &loopManager)
std::string PrettyPrintAddr(const void *const addr)
std::string JitBuildAction(const ColumnNames_t &cols, std::shared_ptr< RDFDetail::RNodeBase > *prevNode, const std::type_info &helperArgType, const std::type_info &at, void *helperArgOnHeap, TTree *tree, const unsigned int nSlots, const RColumnRegister &customCols, RDataSource *ds, std::weak_ptr< RJittedAction > *jittedActionOnHeap)
ColumnNames_t GetTopLevelBranchNames(TTree &t)
Get all the top-level branches names, including the ones of the friend trees.
void CheckTypesAndPars(unsigned int nTemplateParams, unsigned int nColumnNames)
bool AtLeastOneEmptyString(const std::vector< std::string_view > strings)
std::vector< T > Union(const std::vector< T > &v1, const std::vector< T > &v2)
Return a vector with all elements of v1 and v2 and duplicates removed.
void CheckForDefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &customCols, const ColumnNames_t &treeColumns, const ColumnNames_t &dataSourceColumns)
Throw if column definedColView is not already there.
bool IsInternalColumn(std::string_view colName)
Whether custom column with name colName is an "internal" column such as rdfentry_ or rdfslot_.
ColumnNames_t FilterArraySizeColNames(const ColumnNames_t &columnNames, const std::string &action)
Take a list of column names, return that list with entries starting by '#' filtered out.
void InterpreterDeclare(const std::string &code)
Declare code in the interpreter via the TInterpreter::Declare method, throw in case of errors.
std::shared_ptr< RJittedDefine > BookDefinePerSampleJit(std::string_view name, std::string_view expression, RLoopManager &lm, const RColumnRegister &customCols, std::shared_ptr< RNodeBase > *upcastNodeOnHeap)
Book the jitting of a DefinePerSample call.
void CheckForRedefinition(const std::string &where, std::string_view definedColView, const RColumnRegister &customCols, const ColumnNames_t &treeColumns, const ColumnNames_t &dataSourceColumns)
Throw if column definedColView is already there.
void CheckForDuplicateSnapshotColumns(const ColumnNames_t &cols)
ColumnNames_t ConvertRegexToColumns(const ColumnNames_t &colNames, std::string_view columnNameRegexp, std::string_view callerName)
std::pair< std::vector< std::string >, std::vector< std::string > > AddSizeBranches(const std::vector< std::string > &branches, TTree *tree, std::vector< std::string > &&colsWithoutAliases, std::vector< std::string > &&colsWithAliases)
Return copies of colsWithoutAliases and colsWithAliases with size branches for variable-sized array b...
std::vector< bool > FindUndefinedDSColumns(const ColumnNames_t &requestedCols, const ColumnNames_t &definedCols)
Return a bitset each element of which indicates whether the corresponding element in selectedColumns ...
std::shared_ptr< RJittedDefine > BookDefineJit(std::string_view name, std::string_view expression, RLoopManager &lm, RDataSource *ds, const RColumnRegister &customCols, const ColumnNames_t &branches, std::shared_ptr< RNodeBase > *upcastNodeOnHeap)
Book the jitting of a Define call.
std::vector< std::string > GetValidatedArgTypes(const ColumnNames_t &colNames, const RColumnRegister &colRegister, TTree *tree, RDataSource *ds, const std::string &context, bool vector2rvec)
void CheckForNoVariations(const std::string &where, std::string_view definedColView, const RColumnRegister &customCols)
Throw if the column has systematic variations attached.
ColumnNames_t FindUnknownColumns(const ColumnNames_t &requiredCols, const ColumnNames_t &datasetColumns, const RColumnRegister &definedCols, const ColumnNames_t &dataSourceColumns)
void TriggerRun(ROOT::RDF::RNode &node)
Trigger the execution of an RDataFrame computation graph.
std::shared_ptr< RDFDetail::RJittedFilter > BookFilterJit(std::shared_ptr< RDFDetail::RNodeBase > *prevNodeOnHeap, std::string_view name, std::string_view expression, const ColumnNames_t &branches, const RColumnRegister &customCols, TTree *tree, RDataSource *ds)
Book the jitting of a Filter call.
std::vector< std::string > ColumnNames_t
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
char * DemangleTypeIdName(const std::type_info &ti, int &errorCode)
Demangle in a portable way the type id name.