Merging two Branches within a Tree

Hello,

I have two TBranches within the same TTree, which are identical, except they are filled with different values. I’m wondering if I can merge these two Branches into a combined Branch within the same Tree.

Thank you.

Hi,
if you have access to ROOT 6.10, you can use TDataFrame to Define a branch that contains the values of both current branches and save it to a tree in a new file with Snapshot:

#include "ROOT/TDataFrame.hxx"

int main() {
  ROOT::Experimental::TDataFrame d("in_tree", "in_file.root");
  // a lambda that puts two double variables in a vector
  auto twoDoublesToAVector = [](double b1, double b2) { return std::vector<double>({b1, b2}); };
  d.Define("b1andb2", twoDoublesToAVector, {"b1", "b2"}) // create new branch
   .Snapshot("out_tree", "out_file", "b1andb2"); // save to a tree in a new file
  return 0;
}

If what you want to do is instead concatenating the two branches and get double the entries in a bigger tree, I think the easiest way is instead creating two files with one tree each, and have each of the two output trees have the values of one of the two original branches. You can then use both files/trees together with TChain or TDataFrame. For example:

#include "ROOT/TDataFrame.hxx"

int main() {
  ROOT::Experimental::TDataFrame d("in_tree", "in_file.root");
  d.Define("b", "b1").Snapshot("out_tree", "file1.root", "b");
  d.Define("b", "b2").Snapshot("out_tree", "file2.root", "b");

  return 0;
}

You can use the same approach also without TDataFrame, in case you don’t have access to ROOT 6.10, you will just have to use lower level interfaces like TTree::SetBranchAddress and TTree::Fill to get it done.

1 Like

Thanks for the reply.

The version of ROOT I am working with is 6.08/00.

What I want to do is just combine the two Branches into a bigger one with double the entries. I would rather keep everything in one file if possible. I just have a single file with a single Tree, and multiple Branches. Two of the Branches are essentially identical, so I would like to combine them, within the same Tree, within the same file.

As far as I know, you will have to do it in two passes:

  1. write each of the branches to two ttrees in two tfiles, as a branch with the same name.
  2. merge the files, e.g. with the hadd command line utility

Is it really difficult for you to use 6.10?
If you are at CERN and you are using a cc7 node, you can have it at your disposal with this single command

. /cvmfs/sft.cern.ch/lcg/app/releases/ROOT/6.10.00/x86_64-centos7-gcc48-opt/root/bin/thisroot.sh

Cheers,
D

Hello,

I do not work at CERN. Where I am located, the newest version of ROOT we have available is 6.08.

But I think I’ve found a workaround for my problem that will not require merging any files, Trees, or Branches.

Thanks for the responses.

Hi,

as a matter of fact, in order to mount cvmfs you do not need to work at CERN, the way in which formulated the requirement was too strict.

D

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