How to initialize RVec


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18.00
Platform: lxplus


Hi, I am trying to initialize the RVec in C++, but I can’t.

I would like to make a new RVec which is filled with “1.0” to multiply to Data in ROOT::RDataFrame.

#include "ROOT/RDataFrame.hxx"
#include "ROOT/RVec.hxx"
#include "ROOT/RDF/RInterface.hxx"
#include "Math/Vector4D.h"
#include "Math/Vector4Dfwd.h"
#include <vector>

using namespace ROOT::VecOps;
using rvec_f = const RVec<float> &;

rvec_f data_scale( rvec_f pt ){
  vector<float> tmp;
  for( int i = 0; i < pt.size() ; i++){
    tmp.push_back(1.0);
  }
  rvec_f out(tmp.data(), tmp.size());
  return out;
}

I just found that “Owning and adopting memory part in the template references (https://root.cern.ch/doc/v614/classROOT_1_1VecOps_1_1RVec.html)”, but it does not work.

The error messages start with:

/cms/ldap_home/sarakm0704/WORK/ttbb/ana_rdf.C:22:10: error: reference cannot be initialized with multiple values
  rvec_f out(tmp.data(), tmp.size());
         ^   ~~~~~~~~~~~~~~~~~~~~~~

Thanks!
Jieun

Hi Jieun,
welcome to the ROOT forum!

The problem with your code is that rvec_f is a reference to RVec<float>, and you can’t initialize references like that in C++. I would suggest the following changes:

using namespace ROOT::VecOps;
using rvec_f = RVec<float>; // no const/ref qualifiers here

// return a value, take a const reference
rvec_f data_scale(const rvec_f& pt) {
  return rvec_f(pt.size(), 1.0f); // RVec has the same constructors and methods as std::vector
}

Cheers,
Enrico

Thanks a lot! :smile:

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