Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:25:28

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/unit_test.hpp>
0010 
0011 #include "ActsFatras/EventData/Barcode.hpp"
0012 
0013 #include <array>
0014 #include <sstream>
0015 #include <string>
0016 #include <unordered_map>
0017 #include <vector>
0018 
0019 using ActsFatras::Barcode;
0020 
0021 namespace ActsTests {
0022 
0023 BOOST_AUTO_TEST_SUITE(EventDataSuite)
0024 
0025 BOOST_AUTO_TEST_CASE(BarcodeMakeDescendant) {
0026   // initial barcode with primary particle
0027   auto p3 =
0028       Barcode().withVertexPrimary(1).withVertexSecondary(2).withParticle(3);
0029   BOOST_CHECK_EQUAL(p3.vertexPrimary(), 1u);
0030   BOOST_CHECK_EQUAL(p3.vertexSecondary(), 2u);
0031   BOOST_CHECK_EQUAL(p3.particle(), 3u);
0032   BOOST_CHECK_EQUAL(p3.generation(), 0u);
0033   BOOST_CHECK_EQUAL(p3.subParticle(), 0u);
0034 
0035   // generate two first-generation descendants
0036   auto p30 = p3.makeDescendant(0);
0037   auto p31 = p3.makeDescendant(1);
0038   BOOST_CHECK_EQUAL(p30.vertexPrimary(), 1u);
0039   BOOST_CHECK_EQUAL(p30.vertexSecondary(), 2u);
0040   BOOST_CHECK_EQUAL(p30.particle(), 3u);
0041   BOOST_CHECK_EQUAL(p30.generation(), 1u);
0042   BOOST_CHECK_EQUAL(p30.subParticle(), 0u);
0043   BOOST_CHECK_EQUAL(p31.vertexPrimary(), 1u);
0044   BOOST_CHECK_EQUAL(p31.vertexSecondary(), 2u);
0045   BOOST_CHECK_EQUAL(p31.particle(), 3u);
0046   BOOST_CHECK_EQUAL(p31.generation(), 1u);
0047   BOOST_CHECK_EQUAL(p31.subParticle(), 1u);
0048 
0049   // generate two (overlapping) second-generation descendants.
0050   auto p300 = p30.makeDescendant(0);
0051   auto p310 = p31.makeDescendant(0);
0052   BOOST_CHECK_EQUAL(p300.vertexPrimary(), 1u);
0053   BOOST_CHECK_EQUAL(p300.vertexSecondary(), 2u);
0054   BOOST_CHECK_EQUAL(p300.particle(), 3u);
0055   BOOST_CHECK_EQUAL(p300.generation(), 2u);
0056   BOOST_CHECK_EQUAL(p300.subParticle(), 0u);
0057   BOOST_CHECK_EQUAL(p310.vertexPrimary(), 1u);
0058   BOOST_CHECK_EQUAL(p310.vertexSecondary(), 2u);
0059   BOOST_CHECK_EQUAL(p310.particle(), 3u);
0060   BOOST_CHECK_EQUAL(p310.generation(), 2u);
0061   BOOST_CHECK_EQUAL(p310.subParticle(), 0u);
0062 }
0063 
0064 BOOST_AUTO_TEST_CASE(BarcodeVertex) {
0065   auto p = Barcode()
0066                .withVertexPrimary(1)
0067                .withVertexSecondary(2)
0068                .withParticle(3)
0069                .withGeneration(4)
0070                .withSubParticle(5);
0071   auto vtx = p.vertexId();
0072   BOOST_CHECK_EQUAL(vtx.vertexPrimary(), 1u);
0073   BOOST_CHECK_EQUAL(vtx.vertexSecondary(), 2u);
0074   BOOST_CHECK_EQUAL(vtx.particle(), 0u);
0075   BOOST_CHECK_EQUAL(vtx.generation(), 4u);
0076   BOOST_CHECK_EQUAL(vtx.subParticle(), 0u);
0077 }
0078 
0079 BOOST_AUTO_TEST_CASE(BarcodeWithoutSubparticle) {
0080   auto p1 = Barcode()
0081                 .withVertexPrimary(1)
0082                 .withVertexSecondary(2)
0083                 .withParticle(3)
0084                 .withGeneration(4)
0085                 .withSubParticle(5);
0086   auto p2 = p1.withoutSubparticle();
0087   BOOST_CHECK_EQUAL(p2.vertexPrimary(), 1u);
0088   BOOST_CHECK_EQUAL(p2.vertexSecondary(), 2u);
0089   BOOST_CHECK_EQUAL(p2.particle(), 3u);
0090   BOOST_CHECK_EQUAL(p2.generation(), 4u);
0091   BOOST_CHECK_EQUAL(p2.subParticle(), 0u);
0092 }
0093 
0094 BOOST_AUTO_TEST_CASE(BarcodeConstructors) {
0095   auto p1 = Barcode()
0096                 .withVertexPrimary(1u)
0097                 .withVertexSecondary(2u)
0098                 .withParticle(3u)
0099                 .withGeneration(4u)
0100                 .withSubParticle(5u);
0101   std::vector<std::uint32_t> vectorValues = {1u, 2u, 3u, 4u, 5u};
0102   auto p2 = Barcode().withData(vectorValues);
0103   std::array<std::uint32_t, 5> arrayValues = {1u, 2u, 3u, 4u, 5u};
0104   auto p3 = Barcode().withData(arrayValues);
0105   auto p4 = Barcode::Invalid();
0106   auto p5 = Barcode();
0107   std::vector<std::uint32_t> vectorZeros = {0u, 0u, 0u, 0u, 0u};
0108 
0109   BOOST_CHECK_EQUAL(p1, p2);
0110   BOOST_CHECK_EQUAL(p1, p3);
0111   BOOST_CHECK(p4.asVector() == vectorZeros);
0112   BOOST_CHECK_EQUAL(p4, p5);
0113   BOOST_CHECK(p1.isValid());
0114   BOOST_CHECK(!p4.isValid());
0115 
0116   auto q1 = p1.withVertexPrimary(11u);
0117   auto q2 = p1.withVertexSecondary(22u);
0118   auto q3 = p1.withParticle(33u);
0119   auto q4 = p1.withGeneration(44u);
0120   auto q5 = p1.withSubParticle(55u);
0121 
0122   BOOST_CHECK_NE(p1, q1);
0123   BOOST_CHECK_NE(p1, q2);
0124   BOOST_CHECK_NE(p1, q3);
0125   BOOST_CHECK_NE(p1, q4);
0126   BOOST_CHECK_NE(p1, q5);
0127 
0128   BOOST_CHECK(p1 < q1);
0129   BOOST_CHECK(q2 > q4);
0130   BOOST_CHECK(q3 > q5);
0131   BOOST_CHECK(p1 < q4);
0132   BOOST_CHECK(q5 > p1);
0133 
0134   std::vector<std::uint32_t> badValues = {11u, 12u, 13u, 14u};
0135   auto r1 = Barcode::Invalid();
0136   BOOST_CHECK_THROW(r1 = r1.withData(badValues), std::invalid_argument);
0137 
0138   std::array<std::uint32_t, 6> badValues2 = {11u, 12u, 13u, 14u, 15u, 16u};
0139   Barcode r2;
0140   BOOST_CHECK_THROW(r2 = r2.withData(badValues2), std::invalid_argument);
0141 }
0142 
0143 BOOST_AUTO_TEST_CASE(BarcodeLimits) {
0144   Barcode::PrimaryVertexId primVtx = 1001u;
0145   Barcode::SecondaryVertexId secVtx = 10002u;
0146   Barcode::ParticleId part = 100003u;
0147   Barcode::GenerationId gen = 104u;
0148   Barcode::SubParticleId subPart = 100005u;
0149 
0150   auto p1 = Barcode()
0151                 .withVertexPrimary(primVtx)
0152                 .withVertexSecondary(secVtx)
0153                 .withParticle(part)
0154                 .withGeneration(gen)
0155                 .withSubParticle(subPart);
0156   BOOST_CHECK_EQUAL(p1.vertexPrimary(), 1001u);
0157   BOOST_CHECK_EQUAL(p1.vertexSecondary(), 10002u);
0158   BOOST_CHECK_EQUAL(p1.particle(), 100003u);
0159   BOOST_CHECK_EQUAL(p1.generation(), 104u);
0160   BOOST_CHECK_EQUAL(p1.subParticle(), 100005u);
0161 }
0162 
0163 BOOST_AUTO_TEST_CASE(BarcodeHash) {
0164   auto p1 = Barcode()
0165                 .withVertexPrimary(1u)
0166                 .withVertexSecondary(2u)
0167                 .withParticle(3u)
0168                 .withGeneration(4u)
0169                 .withSubParticle(5u);
0170   BOOST_CHECK_EQUAL(p1.hash(), 6445027996773929525u);
0171 
0172   auto p2 = Barcode()
0173                 .withVertexPrimary(11u)
0174                 .withVertexSecondary(22u)
0175                 .withParticle(33u)
0176                 .withGeneration(44u)
0177                 .withSubParticle(55u);
0178   BOOST_CHECK_EQUAL(p2.hash(), 13009826896635491935u);
0179 
0180   auto p3 = Barcode()
0181                 .withSubParticle(5u)
0182                 .withGeneration(4u)
0183                 .withParticle(2u)
0184                 .withVertexSecondary(3u)
0185                 .withVertexPrimary(1u);
0186   BOOST_CHECK_NE(p1.hash(), p3.hash());
0187 
0188   std::unordered_map<Barcode, int> map;
0189   map.emplace(p1, 101);
0190   map[p2] = 202;
0191 
0192   auto search = map.find(p1);
0193   BOOST_CHECK(search != map.end());
0194   BOOST_CHECK_EQUAL(search->second, 101);
0195   BOOST_CHECK_EQUAL(map[p2], 202);
0196   search = map.find(p3);
0197   BOOST_CHECK(search == map.end());
0198 }
0199 
0200 BOOST_AUTO_TEST_CASE(BarcodeStreamOutput) {
0201   // if anything gets accidentally converted to char,
0202   // it will be A, B, C, D, and E, respectively
0203 
0204   auto p1 = Barcode()
0205                 .withVertexPrimary(65u)
0206                 .withVertexSecondary(66u)
0207                 .withParticle(67u)
0208                 .withGeneration(68u)
0209                 .withSubParticle(69u);
0210   std::string str = "vp=65|vs=66|p=67|g=68|sp=69";
0211 
0212   std::stringstream stream;
0213   stream << p1;
0214   BOOST_CHECK_EQUAL(stream.str(), str);
0215 }
0216 
0217 BOOST_AUTO_TEST_SUITE_END()
0218 
0219 }  // namespace ActsTests