Create a new branch with input tree branches

Dear all:

I’d like to manipulate the tree branches like the tree->Draw() option:

tree->Draw(“jet_pt>>hist”,“(weight>0)*2*(event==1)”)

Is there a function or method of that: I’d like to create a new branch (not histogram) with the same number of events, based on the old tree branches. For example:

my_new_branch->FUNCTION(“jet_pt”,“(weight>0)*2*(event==1)”)

or

tree->FUNCTION(“jet_pt>>my_new_branch”,“(weight>0)*2*(event==1)”)

where jet_pt, weight and event are branches from the old tree(or input tree). and my_new_branch is the new branch (not histogram) I’d like to create with the same number of events as the old tree.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22.06
Platform: centos 7.9
Compiler: gcc 8.3.0


One way to do it is RDataFrame using the Define and Snapshot functions. Define adds a new (virtual) column to the data frame, and snapshot writes the result (old and new columns) to a new ROOT file. Roughly like this

ROOT::RDataFrame df("myTree", "myFile");
df_enhanced = df.Define("myBranch", "(weight > 0) * 2 * (event == 1)");
// Or possibly:
// df_enhanced = df.Define("myBranch", "jet_pt * (weight > 0) * 2 * (event == 1)");
df_enhanced.Snapshot("outputTree", "outputFile.root", "");
1 Like

Hi Jblomer:

Thanks a lot! That’s great!

Cheers,
Yingjie

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