Skip to content

Commit bae1440

Browse files
authored
DPL Analysis: simple helper to create combinations (#2861)
This is not particularly optimized, as it creates in memory a table with all the pairs, however we will be able to optimize it later using C++20 coreroutines.
1 parent 44f36e3 commit bae1440

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ foreach(t
126126
AnalysisTask
127127
AnalysisDataModel
128128
ASoA
129+
ASoAHelpers
129130
BoostOptionsRetriever
130131
CallbackRegistry
131132
ChannelSpecHelpers
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#ifndef O2_FRAMEWORK_ASOAHELPERS_H_
12+
#define O2_FRAMEWORK_ASOAHELPERS_H_
13+
14+
#include "Framework/ASoA.h"
15+
16+
namespace o2::soa
17+
{
18+
19+
/// @return a vector of pairs with all the possible
20+
/// combinations of the rows of the table T.
21+
/// FIXME: move to coroutines once we have C++20
22+
template <typename T>
23+
std::vector<std::pair<typename T::iterator, typename T::iterator>>
24+
combinations(T const& table)
25+
{
26+
std::vector<std::pair<typename T::iterator, typename T::iterator>> result;
27+
result.reserve((table.size() + 1) * table.size() / 2);
28+
for (auto t0 = table.begin(); t0 + 1 != table.end(); ++t0) {
29+
for (auto t1 = t0 + 1; t1 != table.end(); ++t1) {
30+
result.push_back(std::make_pair(t0, t1));
31+
}
32+
}
33+
return result;
34+
};
35+
36+
} // namespace o2::soa
37+
38+
#endif // O2_FRAMEWORK_ASOAHELPERS_H_
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
#define BOOST_TEST_MODULE Test Framework ASoAHelpers
12+
#define BOOST_TEST_MAIN
13+
#define BOOST_TEST_DYN_LINK
14+
15+
#include "Framework/ASoAHelpers.h"
16+
#include "Framework/TableBuilder.h"
17+
#include <boost/test/unit_test.hpp>
18+
19+
using namespace o2::framework;
20+
using namespace o2::soa;
21+
22+
namespace test
23+
{
24+
DECLARE_SOA_COLUMN(X, x, int32_t, "x");
25+
DECLARE_SOA_COLUMN(Y, y, int32_t, "y");
26+
DECLARE_SOA_COLUMN(Z, z, int32_t, "z");
27+
DECLARE_SOA_DYNAMIC_COLUMN(Sum, sum, [](int32_t x, int32_t y) { return x + y; });
28+
} // namespace test
29+
30+
BOOST_AUTO_TEST_CASE(Combinations)
31+
{
32+
TableBuilder builderA;
33+
auto rowWriterA = builderA.persist<int32_t, int32_t>({"x", "y"});
34+
rowWriterA(0, 0, 0);
35+
rowWriterA(0, 1, 0);
36+
rowWriterA(0, 2, 0);
37+
rowWriterA(0, 3, 0);
38+
rowWriterA(0, 4, 0);
39+
rowWriterA(0, 5, 0);
40+
rowWriterA(0, 6, 0);
41+
rowWriterA(0, 7, 0);
42+
auto tableA = builderA.finalize();
43+
BOOST_REQUIRE_EQUAL(tableA->num_rows(), 8);
44+
45+
using TestA = o2::soa::Table<o2::soa::Index<>, test::X, test::Y>;
46+
47+
TestA tests{tableA};
48+
49+
BOOST_REQUIRE_EQUAL(8, tests.size());
50+
auto c = combinations(tests);
51+
BOOST_CHECK_EQUAL(c.size(), 7 * (7 + 1) / 2);
52+
53+
int n = 0;
54+
int i = 0;
55+
int j = 1;
56+
for (auto [t1, t2] : combinations(tests)) {
57+
BOOST_CHECK_EQUAL(t1.x(), i);
58+
BOOST_CHECK_EQUAL(t2.x(), j);
59+
if (j == 7) {
60+
++i;
61+
j = i + 1;
62+
continue;
63+
}
64+
n++;
65+
j++;
66+
}
67+
//BOOST_CHECK_EQUAL(i, 28);
68+
}

0 commit comments

Comments
 (0)