How to copy a branch into another root file

Hi there!

Let’s imagine that I have a root file, “input.root”, which contains a tree called “arb” with three branches, “a”, “b” and “c”. As this is such a chaotic structure, and considering that I only need to use the branches “a” and “b”, I want to create a new file which contains a tree called “particles”, and to copy the branches “a” and “b” as “energy” and “momentum”, respectively.

So far, I have just started reading the input file:

TFile* fInp = new TFile(Form("input.root"));
    TFile* fOut = new TFile(Form("output.root"));
    TTree* Tree = (TTree*)fInp->Get("arb");

But I don’t know how to continue. Any suggestions?

Please read tips for efficient and successful posting and posting code

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

Hi @Miguel ,
with a recent enough ROOT version, RDataFrame is the tool for this job.
This is the complete program:

ROOT::RDataFrame df("arb", "input.root");
df.Alias("energy", "a")
  .Alias("momentum", "b")
  .Snapshot("particles", "output.root", {"energy", "momentum"});


1 Like