Fill a Branch with existing Branch from a root file

I am trying to fill a branch (new_Branch) with existing a branch (old_file) data from a root file
and my out put(new_file) will be my new tree (new_tree) with new Branch (new_Branch) which i am trying to fill it.
so the new Branch have a new name but have the the same data of old Branch

#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <iostream>
#include <vector>
#include "Hit.h"
#include "Neutrino.h"
#include "Track.h"
#include "Vec3D.h"

void aa(){

double eventNumber_;
double event_id;

    TFile *oldfile = new TFile("mcv5.0.monopole.geamon.jte.jchain.aanet.2.root", "read");
    TFile *newfile = new TFile("aa.root", "RECREATE"); 

     TTree *oldtree = (TTree*)oldfile->Get("E");
     oldtree->SetBranchStatus("*", 0);

     int id;

        for (auto activeBranchName : {"id"})
        oldtree->SetBranchStatus(activeBranchName, 1);
        oldtree->SetBranchAddress("id", &id);     

     TTree *newtree = oldtree->CloneTree();

    TTree *tree1 = new TTree("MONTECARLO1","MONTECARLO1") ;   /*new tree*/  
    TTree *tree2 = new TTree("PHYSICS1","PHYSICS1") ;         /* new tree*/

       int entries = oldtree->GetEntries();

    TBranch *b = tree1->Branch("eventNumber_", &eventNumber_);
    tree1->Branch("neutrino_", &neutrino_);
    tree2->Branch("event_id", &event_id);   

     for(int i =0 ;i<entries ;i++){
     newtree->GetBranch("id"); /*old branch */

int main(){


My problem ,i can’t until now link between old branch and new branch to store the old branch in the new branch in the loop.
i hope the problem has been explained well.

Hi @eddymaoui,
see the copytree and copytree2 tutorials: if you just want to copy a branch from a TTree to another, you can just SetBranchStatus("branch", 1) and then call Write on the TTree returned by CloneTree.

@pcanal might help with how to also add other branches to the new TTree.


HI @eguiraud @pcanal
the old branch cited in the old file ,i don’t wan add the old branch
i wana fill the new branch with the containt of old branch ,
for example
i wana fill the branche “eventNumber_” with the containt of the old branch called “id”
help me please

With a new enough ROOT version you can use RDataFrame, which makes these kind of tasks very simple. This is all the code you need:

ROOT::RDataFrame("oldtree", "oldfile.root")
  .Alias("newbranch", "oldbranch")
  .Snapshot("newtree", "newfile.root", {"newbranch"});

For a TTree solution, we need Philippe.

Hi @eguiraud
i didn’t get any resulte with the commande that you have proposed to me .
the new branch “eventNumber_” still empty .

   ROOT::RDataFrame("E", "mcv5.0.monopole.geamon.jte.jchain.aanet.2.root").Alias("eventNumber_", "id").Snapshot("MONTECARLO1", "aa.root", {"eventNumber_"});

the error i have got

IncrementalExecutor::executeFunction: symbol '_ZN4ROOT8Internal3RDF11RActionBaseD2Ev' unresolved while linking function '_GLOBAL__sub_I_cling_module_8'!
You are probably missing the definition of ROOT::Internal::RDF::RActionBase::~RActionBase()


That’s weird, what ROOT version are you on (i.e. what does root-config --version print)?

Does splitting the command in two parts help?

ROOT::RDataFrame df("E", "mcv5.0.monopole.geamon.jte.jchain.aanet.2.root");
df.Alias("eventNumber_", "id").Snapshot("MONTECARLO1", "aa.root", {"eventNumber_"});

Hi @eguiraud
i am using root version 6.16/00

the splitting commands didn’t help to solve the probleme .
the erreur will shown when i add this ligne !!

.Snapshot("MONTECARLO1", "aa.root", {"eventNumber_"});

i see thet preferable to solve this issue without RDataFrame ;
for your attention the clone of old tree with his branche and the creation of new tree with his branche has been set up well without any probemes ;so i need juste a link btween the old and new branches .
i hope you understood my idea


i wana fill the branche “eventNumber_” with the containt of the old branch called “id”

The fastest is to rename the branch.

TFile *file = TFile::Open(filename, "UPDATE");
TTree *tree = file->Get<TTree>(treename);
TBranch *br = tree->GetBranch( old_branchname );
TLeaf *leaf = br->GetLeaf(old_branchname);


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