RDataframe, it is possible to pass non-column argument to lambda function? example: c++ container

ROOT Version: 6.18/04
Platform: Ubuntu 18.04.4
Compiler: gcc 7.3.0


Hello,

I am wondering it is possible to pass vector or std::map container to a lambda function on RDataframe object?

for example:
// lambda function
auto isPassJSON = [](std::map<int, std::vector<std::pair<int, int> > >& m_json , unsigned& run , unsigned& luminosityBlock)
{
return Helper::isRunLumiInJSON( m_json , run, luminosityBlock );
};


// main function
std::map<int, std::vector<std::pair<int, int> > > json = Helper::parseJSONAsMap(cfg.jsonFile);
return df
.Define(“json”,“json”)
.Define(“passJSON”,isPassJSON, { “json”, “run” , “luminosityBlock” } )

I had an error:
input_line_41:1:46: error: use of undeclared identifier ‘json’

is there a better way to do it ?

Thanks,
Siewyan

Hi @SiewYan,
welcome back to the ROOT forum :smiley: Please check this post about code formatting on the forum.

Lambda functions can capture variables:

// create map object
std::map<int, std::vector<std::pair<int, int>>> json = Helper::parseJSONAsMap(cfg.jsonFile);

// capture it and use it in the lambda function
auto isPassJSON = [&json](unsigned& run , unsigned& luminosityBlock)
{
    return Helper::isRunLumiInJSON( m_json , run, luminosityBlock );
};

// use `isPassJSON` as usual, only listing the columns it acts on
df.Define("passJSON", isPassJSON, {“run” , “luminosityBlock” } );

Hope this helps!
Enrico

Hello Enrico,

Thanks! I manage to pass the json file (declared and encoded in std::map) into the lambda function. RDataframe is awesome!

Anyhow, I got a runtime error:

terminate called after throwing an instance of ‘std::bad_alloc’
what(): std::bad_alloc

I suspect the std::map is overloading the swap memory? or could this stem from the limitation of lambda function?

i am working on lxplus.
Thanks!
Cheers,
Siewyan

std::bad_alloc usually means that you ran out of RAM. Why exactly that is, I can’t say, but e.g. valgrind --tool massif might help find the culprit.

Cheers,
Enrico

Hello Enrico,

Thanks for the tips, it turn out I the parsing json happen to be looped together with the events, a static declaration on parsed json fix the problem.

Thanks!
Siewyan

1 Like

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