Logo ROOT   6.16/01
Reference Guide
vo005_Combinations.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_vecops
3## \notebook -nodraw
4## In this tutorial we learn how combinations of RVecs can be build.
5##
6## \macro_code
7## \macro_output
8##
9## \date August 2018
10## \author Stefan Wunsch
11
12import ROOT
13from ROOT.VecOps import RVec, Take, Combinations
14
15# RVec can be sorted in Python with the inbuilt sorting function because
16# PyROOT implements a Python iterator
17v1 = RVec("double")(3)
18v1[0], v1[1], v1[2] = 1, 2, 3
19v2 = RVec("double")(2)
20v2[0], v2[1] = -4, -5
21
22# To get the indices, which result in all combinations, you can call the
23# following helper.
24idx = Combinations(v1, v2)
25
26# Next, the respective elements can be taken via the computed indices.
27c1 = Take(v1, idx[0])
28c2 = Take(v2, idx[1])
29
30# Finally, you can perform any set of operations conveniently.
31v3 = c1 * c2
32
33print("Combinations of {} and {}:".format(v1, v2))
34for i in range(len(v3)):
35 print("{} * {} = {}".format(c1[i], c2[i], v3[i]))
36print
37
38# However, if you want to compute operations on unique combinations of a
39# single RVec, you can perform this as follows.
40
41# Get the indices of unique triples for the given vector.
42v4 = RVec("double")(4)
43v4[0], v4[1], v4[2], v4[3] = 1, 2, 3, 4
44idx2 = Combinations(v4, 3)
45
46# Take the elements and compute any operation on the returned collections.
47c3 = Take(v4, idx2[0])
48c4 = Take(v4, idx2[1])
49c5 = Take(v4, idx2[2])
50
51v5 = c3 * c4 * c5
52
53print("Unique triples of {}:".format(v4))
54for i in range(len(v5)):
55 print("{} * {} * {} = {}".format(c3[i], c4[i], c5[i], v5[i]))
RVec< RVec< typename RVec< T1 >::size_type > > Combinations(const RVec< T1 > &v1, const RVec< T2 > &v2)
Return the indices that represent all combinations of the elements of two RVecs.
Definition: RVec.hxx:1008
RVec< T > Take(const RVec< T > &v, const RVec< typename RVec< T >::size_type > &i)
Return elements of a vector at given indices.
Definition: RVec.hxx:882