Rdata frame, problems returning a RDataFrame from a function

Hi guys, nice evening

I am trying to fill a RDataframe with some vectors. To improve the code organization, I have defined a function that takes as input an RDataFrame, and fill it:

void FillTry(ROOT::RDataFrame &d1, TString datafileName){
	int j(0); 	// Variable for loop
	d1.Define("id", [&j](){		// Id of the events
		return j;

Something like that. I tried to run the function like this:

	ROOT::RDataFrame d(100); // PDF2

	d1.Snapshot("myTree", "a.root");

But I receive the following errors:

terminate called after throwing an instance of 'std::runtime_error'
  what():  : there is no column available to match.

Please read tips for efficient and successful posting and posting code

Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.

ROOT Version: ROOT 6.26/10
Platform: Ubuntu

Hi Adriano,

Thanks for this interesting question.

The issue is that Define returns a node of the processing graph enriched with the new column, it does not add the column to the node it is called from (see here)

Here I tried to propose a slightly modified version of your code that works:

auto FillTry(ROOT::RDataFrame &d1){
	int j = 0; 	// Variable for loop
	return d1.Define("id", [&j](){return j++;});	// Id of the events

void test() {

    ROOT::RDataFrame d(100); // PDF2
	auto filledFrame = FillTry(d);
	filledFrame.Snapshot("myTree", "a.root");

    // Print the content of the file, for the sake of the example
    TFile f("a.root");

A final small remark about performance: you pass by value a TString in your original FillTry function. This triggers a copy and should be perhaps replaced by a const reference for performance reasons, i.e. void FillTry(ROOT::RDataFrame &d1, const TString &datafileName)

Don’t hesitate to write back in case of further questions!


1 Like

Thank you for the advice and of course for your solution! It’s working know and the code is simple and asy to read


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