Efficient way to cast the result from RDataFrame.Take to std::vector<double>

Hi there!

I am using the following to recover back the values from a data frame into a std::vector.

auto parValues = df.Take<double>("X");

std::vector <double> vs;
for( const auto v : parValues )
     vs.push_back(v);

Sorry, I am not very familiar with auto, there is a better way to cast the output of Take() to a std::vector<double>?

Thank yoU!

Dear @Javier_Galan ,

The Take operation returns an std::vector by default, see the docs.

Just remember to actually extract the value from the RResultPtr, i.e.

auto parValues = df.Take<double>("x"); // This is an RResultPtr<std::vector<double>>

const auto &parValues_vec = *parValues; // This is an std::vector<double>

// Or you can also iterate on the RResultPtr directly
for (const auto &v: parValues){
    std::cout << v << std::endl;
}

See also the documentation of ROOT: ROOT::RDF::RResultPtr< T > Class Template Reference

Cheers,
Vincenzo

(if you want to avoid a copy, you can take a reference instead std::vector<double> &v = *parValues or move the result out of the result ptr: std::vector<double> v = std::move(*parValues);)

2 Likes

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