Calculating the 2D flight distance dxy for secondary vertices using sv_x and sv_y

I want to calculate the 2D flight distance sv_dxy using sv_x and sv_y.

import ROOT
from ROOT import RDataFrame

#include <cmath>
float compute_svDxy(float svX, float svY) {
    return std::sqrt(svX*svX + svY*svY);

dfq = RDataFrame("demo/tree", "QCD_Output_All.root")
dfg = RDataFrame("demo/tree", "BulkGrav_Output_All.root")

dfq = dfq.Define("svDxy_q", "compute_svDxy(svX, svY)")
dfg = dfg.Define("svDxy_g", "compute_svDxy(svX, svY)")

dfq.Snapshot("demo/tree", "QCD_Output_With_svDxy.root")
dfg.Snapshot("demo/tree", "BulkGrav_Output_With_svDxy.root")

My error is,

ValueError                                Traceback (most recent call last)
Cell In[32], line 14
     11 dfq = RDataFrame("demo/tree", "QCD_Output_All.root")
     12 dfg = RDataFrame("demo/tree", "BulkGrav_Output_All.root")
---> 14 dfq = dfq.Define("svDxy_q", "compute_svDxy(svX, svY)")
     15 dfg = dfg.Define("svDxy_g", "compute_svDxy(svX, svY)")
     17 dfq.Snapshot("demo/tree", "QCD_Output_With_svDxy.root")

File ~/miniconda3/envs/root_env/lib/python3.10/site-packages/ROOT/_pythonization/, in _PyDefine(rdf, col_name, callable_or_str, cols, extra_args)
    369     raise TypeError(f"First argument of Define must be a valid string for the new column name. {type(col_name).__name__} is not a string.")
    371 if isinstance(callable_or_str, str): # If string argument is passed. Invoke the Original Define.
--> 372     return rdf._OriginalDefine(col_name, callable_or_str)
    374 if not callable(callable_or_str): # The 2st argument is either a string or a python callable.
    375     raise TypeError(f"The second argument of a Define operation should be a callable. {type(callable_or_str).__name__} object is not callable.")

ValueError: Template method resolution failed:
  ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::Define(basic_string_view<char,char_traits<char> > name, basic_string_view<char,char_traits<char> > expression) =>
    ValueError: nullptr result where temporary expected
cling JIT session error: Failed to materialize symbols: { (main, { _ZN4ROOT3RDF10RInterfaceINS_6Detail3RDF12RLoopManagerEvE6DefineESt17basic_string_viewIcSt11char_traitsIcEES9_ }) }

Hello, and welcome to the forum! Which version of ROOT are you using?

The error could indicate that there is a typo in the column names. In the first sentence you write sv_x with an underscore, but the code refers to svX. Can you double-check that they are correct? Can you share the input files?


ROOT Version: 6.30/04

the input root file size is greater than 3 mb, so I cannot upload it. However, I can show you the details using,

import ROOT

dfg = ROOT.RDataFrame("demo/tree", "BulkGrav_Output_All.root")



Dataframe from TChain demo/tree in file BulkGrav_Output_All.root

Property                Value
--------                -----
Columns in total           12
Columns from defines        0
Event loops run             0
Processing slots            1

Column                  Type                            Origin
------                  ----                            ------
npv                     UInt_t                          Dataset
nsv                     UInt_t                          Dataset
svChi2                  ROOT::VecOps::RVec<float>       Dataset
svDxyError              ROOT::VecOps::RVec<float>       Dataset
svEta                   ROOT::VecOps::RVec<float>       Dataset
svMass                  ROOT::VecOps::RVec<float>       Dataset
svNormalizedChi2        ROOT::VecOps::RVec<float>       Dataset
svNumDaughters          ROOT::VecOps::RVec<int>         Dataset
svPt                    ROOT::VecOps::RVec<float>       Dataset
svX                     ROOT::VecOps::RVec<float>       Dataset
svY                     ROOT::VecOps::RVec<float>       Dataset
svZ                     ROOT::VecOps::RVec<float>       Dataset

Hi @Swasti,

from describe your svX and svY are RVec’s and your function uses floats as inputs - that could be the problem. As an example of gInterpreter.Declare with RVecs you can take a look at: ROOT: tutorials/dataframe/ File Reference and function: GoodElectronsAndMuons.


1 Like

Thank you so much!

1 Like

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