How to Extract Variable from Branch in ROOT

Is there a way to extract the variables inside a Branch in ROOT.
If I write:

 ROOT::RDataFrame d("Delphes", "tag_1_delphes_events.root");
 auto df_mass = d.Define("Dimuon_mass", InvariantMass<float>, {"Muon.PT", "Muon.Eta", "Muon.Phi", "mu_mass"});

I can calculate the invariant mass by using the values inside the Muon.PT branch, etc.

Is there a way to just save the values inside the Muon.PT branch, etc to use in inside RVec.

I want to do something like this

 auto d1 = d.Define("pt", pt<float>, "Muon.PT");  RVec<RVec<size_t>>
 reco_zz_to_4l(rvec_f pt, rvec_f eta, rvec_f phi, rvec_f mu_mass,
 rvec_i charge);

but have it work.


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

thank you for creating a post that’s smaller in scope than this, I’m not sure how to help there.

Unfortunately you can’t mix RDataFrame column definitions and standard C++, it just can’t work within the C++ (or python) programming language.

So if you want to use a quantity as an argument to a C++ function, it needs to be a C++ variable. If you want to use a quantity within RDataFrame, you can Define it.

You can use RDataFrame’s Take method to extract all values of a given RDF column into a regular std::vector. But it’s usually more efficient to do all operations you need within RDataFrame, and extract just the histograms or the final quantities you need.

Hope this clarifies things a bit.
The ROOT tutorials have a few examples of full analyses done with RDF that you can check out.


@eguiraud This does help as I am working through it more; I believe I understand your reply.
What I have a question about is the Define in RDataFrame. If I am eventually calculating the invariant mass (which I am using the tutorial’s code to do so) by using:

auto df_mass = df_muon_mass.Define("Dimuon_mass", InvariantMass<float>, {"Muon.PT", "Muon.Eta", "Muon.Phi", "muon_mass"});

how can I define muon_mass if I do not have that branch in my tree. I tried many different was to define it. After writing,

auto df_muon_mass = df.Define("muon_mass", ROOT::RVec<float>, {"49254", "0.1"});

where 49254 is the collection size and 0.1 is the value I want to give the muon mass. I am also defining it to be an RVec so that I can calculate the invariant mass. My other branch values (Muon.PT,etc which are already present in my tree) are floats.

I keep getting this error:

Processing invariantmass.C...
In file included from input_line_8:1:
/mnt/c/1/MG5_aMC_v2_6_6/triplet2s/Events/run_01/invariantmass.C:109:64: error: expected '(' for function-style cast or type construction
   auto df_muon_mass = df.Define("muon_mass", ROOT::RVec<float>, {"49254", "0.1"});

How can I write my Define correctly so that the Invariant mass will be calculated?

The problem with that code is two-fold: firstly, C++ constructors are a bit special and you can’t use them as factory functions: ROOT::RVec<float> is not a function that returns a ROOT::RVec<float>, hence the compilation error. Secondly, Define requires valid column names as last argument, you cannot just pass any argument to the function as in df.Define("muon_mass", ROOT::RVec<float>, {"49254", "0.1"});.

This should work (it uses a C++11 lambda expression):

// a function that returns the RVec you want
auto make_rvec = [] { return ROOT::RVec<float>(49254, 0.1); };
df.Define("muon_mass", make_rvec);

or all togheter:

df.Define("muon_mass", [] { return ROOT::RVec<float>(49254, 0.1); });


@eguiraud Thank you for your response. I did try that however I get this error:

terminate called after throwing an instance of ‘std::runtime_error’
what(): Cannot call operator + on vectors of different sizes.

The collection size was obtained from looking at the collection size of the other muon branches, so what would be the problem here?

Maybe the collection has different sizes for different entries in the dataset?

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