Analysis Software
Documentation for sPHENIX simulation 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 #include <vector>
15 
16 namespace Acts {
17 namespace Test {
18 
19 BOOST_AUTO_TEST_SUITE(material_composition)
20 
21 constexpr float eps = 1.0f / 255u;
22 
23 BOOST_AUTO_TEST_CASE(construct_element_fraction) {
24  // carbon parameters, atomic charge is Z
25  unsigned int carbonZ = 12u;
26  // a fraction between 0 and 255
27  unsigned int carbonWeight = 46u;
28  float carbonFraction = float(carbonWeight) / 255u;
29 
30  ElementFraction a(carbonZ, carbonFraction);
31  BOOST_CHECK_EQUAL(a.element(), carbonZ);
32  CHECK_CLOSE_REL(a.fraction(), carbonFraction, eps);
33 
34  ElementFraction b(carbonZ, carbonWeight);
35  BOOST_CHECK_EQUAL(b.element(), carbonZ);
36  CHECK_CLOSE_REL(b.fraction(), carbonFraction, eps);
37 }
38 
39 BOOST_AUTO_TEST_CASE(construct_with_fractions) {
40  ElementFraction carbon(12u, 0.45f);
41  ElementFraction silicon(14u, 0.125f);
42  ElementFraction titanium(22u, 0.25f);
43  ElementFraction copper(29u, 0.175f);
44 
45  MaterialComposition compound({silicon, carbon, titanium, copper});
46  BOOST_CHECK(!!compound);
47  BOOST_CHECK_EQUAL(compound.size(), 4u);
48 
49  float totalFraction = 0.0f;
50  for (const auto& eFraction : compound) {
51  totalFraction += eFraction.fraction();
52  }
53  // to better fit we need to implement some proper weight scaling
54  CHECK_CLOSE_REL(totalFraction, 1.0f, compound.size() * eps);
55 
56  // input order should not matter
57  MaterialComposition shuffled({carbon, silicon, titanium, copper});
58  // check if the sorting worked
59  BOOST_CHECK_EQUAL(compound.size(), shuffled.size());
60  BOOST_CHECK_EQUAL(compound, shuffled);
61 }
62 
63 BOOST_AUTO_TEST_CASE(construct_with_weights) {
64  ElementFraction carbon(12u, 128u);
65  ElementFraction silicon(14u, 64u);
66  ElementFraction titanium(22u, 32u);
67  ElementFraction copper(29u, 31u);
68 
69  MaterialComposition compound({silicon, carbon, titanium, copper});
70  BOOST_CHECK(!!compound);
71  BOOST_CHECK_EQUAL(compound.size(), 4u);
72 
73  float totalFraction = 0.0f;
74  for (const auto& eFraction : compound) {
75  totalFraction += eFraction.fraction();
76  }
77  // to better fit we need to implement some proper weight scaling
78  CHECK_CLOSE_REL(totalFraction, 1.0f, compound.size() * eps);
79 }
80 
81 BOOST_AUTO_TEST_SUITE_END()
82 
83 } // namespace Test
84 } // namespace Acts