I was working with the tmva, after doing a classification I have a root file with two trees
TrainingTree;
-mass
-BDT
TestTree;
-mass
-BDT
and I want to merge the branches from the 2 tree in a new tree with only 2 branches
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
Ideally you - don’t do it! 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?
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.
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!