Merge 2 branches in 1 new branch

hello root family,

I was working with the tmva, after doing a classification I have a root file with two trees

  1. TrainingTree;
    -mass
    -BDT
  2. TestTree;
    -mass
    -BDT

and I want to merge the branches from the 2 tree in a new tree with only 2 branches

  1. NewTree
    -mass : merge from mass tree 1 and mass tree 2
    -BDT: merge from BDT tree1 and BDT tree 2

I want to know the best way to do that, I tried with a loop importing the values and assigned to a new tree and branches; but I think the result its not okay

Thanks for your time,


Please read tips for efficient and successful posting and posting code

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


Ideally you - don’t do it! :slight_smile: You can use TChain to Add these two files together, one after the other. The chain behaves like a TTree, it just iterate over both files. Makes sense? Is this what you’re after?

Yes, or I think that this is what I want haha… I was trying with the TChain, adding friends

TChain* fchain = new TChain("dataset1/TrainTree", "A chain");
    fchain->Add("TMVA1.root");
    TChain* fchain2 = new TChain("dataset1/TestTree", "an othe chain");
    fchain2->Add("TMVA1.root");
    TChain* fchain3 = new TChain("Tree", "another chain");
    fchain3->Add("cutvar.root");
    fchain2->SetMakeClass(1);

    fchain->AddFriend(fchain2);
    fchain->AddFriend(fchain3);
    fchain->SetBranchStatus("*",0);
    fchain2->SetBranchStatus("*",0);
    fchain3->SetBranchStatus("*",0);

    fchain->SetBranchStatus("BDT",1);
    fchain2->SetBranchStatus("BDT",1);
    fchain3->SetBranchStatus("DP_Mass",1);


    TFile newfile("chain1.root", "recreate");
    TTree* newtree = fchain->CloneTree(0);

but it does not work

Why do you add friends? Friends are for adding new branches, IIUC you don’t want to add new branches but extend the existing ones with more entries. Did I get that right? Then you just call chain->Add("TrainingTree.root"); chain->Add("TestTree.root");

If the two trees have different names in the two files then please consider updating the code that writes them out, such that they share the same name.

Yes, the problem is that the trees have different names, but I’m going to update the code or change the name of the Trees.

Thanks Axel,

I change the names, but its’s not writing any value

my code is

TChain* fchain = new TChain("Tree", "A chain");
    fchain->Add("bdttest.root");
    fchain->Add("bdttrain.root");
    fchain->SetBranchStatus("BDT",1);

    TFile newfile("chain1.root", "recreate");
    TTree* newtree = fchain->CloneTree(0);

    newtree->Write();
    newtree->Print();
    newfile.Write();
    newfile.Close();

and my original trees are

Do you really need one file? If so, just use hadd: $ hadd chain1.root bdttest.root bdttrain.root

Your code needs to call newtree->Fill after calling fchain->GetEntry(...) for each entry you want to have in the new tree.

But in general there’s no point in going through the exercise of rewriting that data. You know how to create a TChain, just use that in the code that is accessing these combined files, instead of using a TTree read from chain1.root!

TChain* fchain = new TChain("Tree", "A chain");
    fchain->Add("bdttest.root");
    fchain->Add("bdttrain.root");
    fchain->SetBranchStatus("BDT",1);

// here comes the analysis which is simply using fchain instead of newtree!

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