Merge TTrees and edit some branches

I have several ROOT files with TTrees containing multiple branches. I need to merge the trees and also update one of the branches. For concreteness, let’s say I’m merging different Monte Carlo samples, and I need to update the weight branch. These files have a lot of branches, and I want my code to be agnostic to the types written to the other branches except for the weight branch. The extra branches may even change in the future, and I don’t want my code to stop working because of that.

I can’t do something like this, because I need explicit access to the weight branch, so I can compute a different value for every entry.

How can I do this?

EDIT: This basically does what I need to do. But how can I do this in C++?

Translation to C++ is fairly straight forward. Change the comment, add “;” at the end of each line, change the for loop… etc …The file is short. It should not take long to translate.