Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExaTrkXMetricHookTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ExaTrkXMetricHookTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
12 
13 #include <cassert>
14 #include <iostream>
15 
16 #include <torch/torch.h>
17 
18 void testTruthTestGraph(std::vector<int64_t> &truthGraph,
19  std::vector<int64_t> &testGraph,
20  const std::string &resStr) {
21  std::stringstream ss;
23 
25 
26  auto opts = torch::TensorOptions().dtype(torch::kInt64);
27  const auto edgeTensor =
28  torch::from_blob(testGraph.data(),
29  {static_cast<long>(testGraph.size() / 2), 2}, opts)
30  .transpose(0, 1);
31 
32  hook({}, edgeTensor);
33 
34  const auto str = ss.str();
35 
36  auto begin = str.begin() + str.find("Efficiency");
37  BOOST_CHECK(std::string(begin, str.end() - 1) == resStr);
38 }
39 
40 BOOST_AUTO_TEST_CASE(same_graph) {
41  // clang-format off
42  std::vector<int64_t> truthGraph = {
43  1,2,
44  2,3,
45  3,4,
46  4,5,
47  };
48  // clang-format on
49 
50  // clang-format off
51  std::vector<int64_t> testGraph = {
52  3,4,
53  4,5,
54  1,2,
55  2,3,
56  };
57  // clang-format on
58 
59  testTruthTestGraph(truthGraph, testGraph, "Efficiency=1, purity=1");
60 }
61 
62 // Test large numbers because overflows are a danger for cantor pairing
63 BOOST_AUTO_TEST_CASE(same_graph_large_numbers) {
64  // clang-format off
65  int64_t k = 100'000;
66 
67  std::vector<int64_t> truthGraph = {
68  1,2,
69  2,3,
70  3,4,
71  4,5,
72  };
73  // clang-format on
74  std::transform(truthGraph.begin(), truthGraph.end(), truthGraph.begin(),
75  [&](auto i) { return k + i; });
76 
77  // clang-format off
78  std::vector<int64_t> testGraph = {
79  3,4,
80  4,5,
81  1,2,
82  2,3,
83  };
84  // clang-format on
85  std::transform(testGraph.begin(), testGraph.end(), testGraph.begin(),
86  [&](auto i) { return k + i; });
87 
88  testTruthTestGraph(truthGraph, testGraph, "Efficiency=1, purity=1");
89 }
90 
91 BOOST_AUTO_TEST_CASE(fifty_fifty) {
92  // clang-format off
93  std::vector<int64_t> truthGraph = {
94  1,2,
95  2,3,
96  3,4,
97  4,5,
98  };
99  // clang-format on
100 
101  // clang-format off
102  std::vector<int64_t> testGraph = {
103  3,4,
104  4,5,
105  6,9,
106  5,1,
107  };
108  // clang-format on
109 
110  testTruthTestGraph(truthGraph, testGraph, "Efficiency=0.5, purity=0.5");
111 }