ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MaterialCompositionTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MaterialCompositionTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2018 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 
13 
14 namespace Acts {
15 namespace Test {
16 
17 BOOST_AUTO_TEST_SUITE(material_composition)
18 
19 constexpr float eps = 1.0f / 255u;
20 
21 BOOST_AUTO_TEST_CASE(construct_element_fraction) {
22  // carbon parameters, atomic charge is Z
23  unsigned int carbonZ = 12u;
24  // a fraction between 0 and 255
25  unsigned int carbonWeight = 46u;
26  float carbonFraction = float(carbonWeight) / 255u;
27 
28  ElementFraction a(carbonZ, carbonFraction);
29  BOOST_TEST(a.element(), carbonZ);
30  CHECK_CLOSE_REL(a.fraction(), carbonFraction, eps);
31 
32  ElementFraction b(carbonZ, carbonWeight);
33  BOOST_TEST(b.element(), carbonZ);
34  CHECK_CLOSE_REL(b.fraction(), carbonFraction, eps);
35 }
36 
37 BOOST_AUTO_TEST_CASE(construct_with_fractions) {
38  ElementFraction carbon(12u, 0.45f);
39  ElementFraction silicon(14u, 0.125f);
40  ElementFraction titanium(22u, 0.25f);
41  ElementFraction copper(29u, 0.175f);
42 
43  MaterialComposition compound({silicon, carbon, titanium, copper});
44  BOOST_TEST(!!compound);
45  BOOST_TEST(compound.size() == 4u);
46 
47  float totalFraction = 0.0f;
48  for (const auto& eFraction : compound) {
49  totalFraction += eFraction.fraction();
50  }
51  // to better fit we need to implement some proper weight scaling
52  CHECK_CLOSE_REL(totalFraction, 1.0f, compound.size() * eps);
53 
54  // input order should not matter
55  MaterialComposition shuffled({carbon, silicon, titanium, copper});
56  // check if the sorting worked
57  BOOST_TEST(compound.size() == shuffled.size());
58  BOOST_TEST(compound == shuffled);
59 }
60 
61 BOOST_AUTO_TEST_CASE(construct_with_weights) {
62  ElementFraction carbon(12u, 128u);
63  ElementFraction silicon(14u, 64u);
64  ElementFraction titanium(22u, 32u);
65  ElementFraction copper(29u, 31u);
66 
67  MaterialComposition compound({silicon, carbon, titanium, copper});
68  BOOST_TEST(!!compound);
69  BOOST_TEST(compound.size() == 4u);
70 
71  float totalFraction = 0.0f;
72  for (const auto& eFraction : compound) {
73  totalFraction += eFraction.fraction();
74  }
75  // to better fit we need to implement some proper weight scaling
76  CHECK_CLOSE_REL(totalFraction, 1.0f, compound.size() * eps);
77 }
78 
79 BOOST_AUTO_TEST_SUITE_END()
80 
81 } // namespace Test
82 } // namespace Acts