How to add columns to RDataFrame in a loop

Hi all,

I have been trying to add few columns to RDataFrame and store in a ntuple but I fail epically.

void AddXGBoost2 (std::string inputfile , std::string trailer ="_xgboost.root", std::string treename ="DecayTree" )

  std::vector<std::string> vars = {"logdira", "bs_eta", "bs_pt", "prodPT", "boxipchi2", "boxfdchi2", "boxchi2ndof", "logminkipchi2", "B_180_cc_deltaPhi", "B_180_cc_IT", "B_180_cc_mult", "minProbNNk", "doca12", "dchi2twotrk", "vertexz", "powipchi2"};

  ROOT::RDataFrame df(treename.c_str(), inputfile.c_str());
  std::string oname = createOutputName(inputfile,trailer);

  for (int i = 1; i < 11; ++i){
    std::string bdtfile = "trees200_" + std::to_string(i) + ".root";
    std::string mybdt = "myBDT-" + std::to_string(i);
    RBDT<> bdt(mybdt, bdtfile.c_str());
    std::string cfold = "XBDT_f" + std::to_string(i);
    df  = df.Define(cfold,Compute<16, float>(bdt),vars);


but it throws an error as error: no viable overloaded '=' df = df.Define(cfold,Compute<16, float>(bdt),vars); ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is there any workaround for this?

Hi @swanski82 ,

each RDF object has a different C++ type that represents the state of the computation graph at that point, so you can’t assign the result of a Define to df.

You need to normalize the types of the nodes to the abstract ROOT::RDF::RNode:

#include <ROOT/RDataFrame.hxx>

int main() {
  ROOT::RDataFrame df(1);
  ROOT::RDF::RNode dfHandle = df;
  for (int i = 0; i < 10; ++i)
    dfHandle = dfHandle.Define("x" + std::to_string(i), [i] { return i; });



This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.