Custom vector column contains all zeros (?!?)

ROOT Version: 6.24/08
Platform: lxplus

Dear all,

I am selecting dimuon events using rDataFrame. Such muons have to fulfil some quality requirements; thus, in order to study their pts, I wrote a function to return a RVec containig the pts of the two custom muons.
Pseudo-code:

using rvec_f = ROOT::RVec<float>;
rvec_f tightMuonPt (arg1, arg2, ..., argN) {

  TLorentzVector mu1;
  TLorentzVector mu2;
  std::vector<float> pts{};
  //here perform muon selection based on args
  if (pass selection) {

    pts.push_back(mu1.Pt());
    pts.push_back(mu2.Pt());

  }

  rvec_f out(pts.data(), pts.size());
  return out;
}

I have explicitly checked (by printing) that out is correctly filled, i.e., it is filled with non-zero values.

But, if I try to define a custom column in my dataframe storing such pts, and I try to plot the values, I get all zeros.
Pseudo-code:

f = f.Define("tightMuon_pt", tightMuonPt, {"arg1", "arg2", ..., "argN"});
f = f.Define("tightMuon1_pt", "tightMuon_pt.at(0)");
auto h_tightMuon1_pt = f.Histo1D({"tightMuon1_pt", "tightMuon1_pt", 200u, 0., 500.}, "tightMuon1_pt");

I checked the histogram h_tightMuon1_pt and it filled by all-zeros.
Do you have any idea what I am doing wrong?
I am available to provide a working piece of code, if needed.

Thanks a lot for your time, my best regards,

Fabio.

Hi Fabio,

this is a special RVec constructor that makes out a view over the data owned by pts, see ROOT: ROOT::VecOps::RVec< T > Class Template Reference .

However, pts is destroyed at the end of tightMuonPt and the returned RVec is left pointing to garbage.

Try this instead:

rvec_f tightMuonPt (arg1, arg2, ..., argN) {
  TLorentzVector mu1;
  TLorentzVector mu2;
  rvec_f pts{};
  //here perform muon selection based on args
  if (pass selection) {
    pts.push_back(mu1.Pt());
    pts.push_back(mu2.Pt());
  }

  return pts;
}

Unrelated to your problem, but also note that TLorentzVectors are extremely slow and it’s not recommended to use them as part of hot event loops. ROOT::Math::LorentzVector is the superior alternative, see the note at ROOT: TLorentzVector Class Reference .

Cheers,
Enrico

Dear Enrico,

indeed your proposed changes work. On a side note yes, thanks, I am aware of TLorentzVector being not recommended anymore, and I already planned to switch to something like ROOT::Math::PtEtaPhiMVector.

Thanks a lot for your insights!
Best,
F.

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