Only perform Define on events where branch exists

Dear Experts,

I have an RDataFrame which consists of several thousand files. A very few of these files are missing some branches (specifically the BornLeptonsAuxDyn.<>) and thus the following operation fails:

df = df.Define("bornMass","ComputeBornMass(BornLeptonsAuxDyn.px,,BornLeptonsAuxDyn.pz,BornLeptonsAuxDyn.e,RunNumber)")

(see further down for the defintion of ComputeBornMass())

Is there a way to still be able to perform this operation on my RDataFrame although a small fraction of the files do not contain these variables. I.e.: can I somehow say that I only want to process this command if the branches actually exists for the event being processed?

I’m using ROOT version 6.28/04.

Thank you for any help.


float ComputeBornMass(VecF_t& px, VecF_t& py, VecF_t& pz, VecF_t& e, Long64_t dsid) {
  TLorentzVector p1;
  TLorentzVector p2;
  if(!((dsid >= 700320 && dsid <= 700328) || (dsid >= 700615 && dsid <= 700623)))return 0;
  const auto ninpt = int(px.size());
  if(ninpt > 2){
    std::cout<<"ERROR \t Number of born leptons is "<<ninpt<<std::endl;
    for (int j=0; j < ninpt; ++j) {
      std::cout<<"(px,py,pz,e) = ("<<px[j]<<","<<py[j]<<","<<pz[j]<<","<<e[j]<<")"<<std::endl;
  p1.SetPxPyPzE(px[0], py[0], pz[0], e[0]);
  p2.SetPxPyPzE(px[1], py[1], pz[1], e[1]);
  return (p1 + p2).M();

I’m sure @vpadulan knows how to proceed

Dear @Eirik_Gramstad ,

Thanks for reaching out to the forum!

You are encountering [DF] Add support for 'missing' columns · Issue #8704 · root-project/root · GitHub and unfortunately I cannot say that we made progress in that direction. But you can refer to the GH issue for further details in the future, I will add this post to bump the priority.

On another note, I see your code uses TLorentzVector which is a legacy, unsupported class… I would suggest you move to ROOT: ROOT::Math::LorentzVector< CoordSystem > Class Template Reference as indicated in the documentation of TLorentzVector.



Hi @vpadulan

Thanks for the pointer. I’ll follow any progress there.

And thanks for the tip about TLorentzVector. I wasn’t aware of this. I’ll update my code.


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