Merge two trees into a chain using some ratio

Hi, ROOT developers!

My aim is to add two trees (root files) with some ratio into a chain.
More detailed:
For example I have tree_1 with 10000 events and tree_2 with 7000 events.
The structure of two trees is identical, that is they contain the same branches.
I would like to merge these two trees into one chain using some ratio (for example 70:30 ratio).
So I need to have a chain with
10000 + 7000*3/7 = 13000 events.
And it doesn’t matter what exactly events from tree_2 will be stored in a chain,
these may be 3000 first or 3000 last events from tree_2.

The question is: how can I implement the corresponding code quickly and
without using such methods as SetBranchStatus, SetBranchAddress and without method Fill within loop?
I mean is there something like chain.Add(tree_1); chain.Add(tree_2*3/7); ?



Not directly but close. You could for example use a combination of CopyEntries and CopyTree.

You can also do it from the command line with:

rooteventselector -s "rand()/2147483647 < .7" tree_2.root:tree trimmed_2.root hadd -f final.root tree_1.root trimmed_2.root

Dear Philippe

Thank you for answer!


Dear @pcanal ,

I have a confusion in understanding this.

In the original root file tree_2.root. We want to apply a weight of 3/7 (= 0.43).

So, rooteventselector -s "rand()/2147483647 <= .43". Right?

Another question: Can’t we just apply the scale factor like `rooteventselector -s “(0.43)”? I tried this but its not working.

Not quite. rooteventselector which select a sub-set of the entries but will not apply any weight per se.
So in

rooteventselector -s "rand()/2147483647 <= .43"

43% of the entries (selected at randon because of the call to rand) will be selected (if the original files at 10000 entries, the result files has roughly 4300 entries).

What do you mean by “applying a scale factor”?

I mean “applying a weight”. I have a root file. I want to get a new root file after applying a weight to each branch. Is there any quick way to do this?

More concretely do you mean recording a new column/branch with the weight, or modifying the value of some columns or duplication some entries?