Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:10

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 "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Plugins/Hashing/HashingAlgorithm.hpp"
0014 #include "Acts/Plugins/Hashing/HashingAlgorithmConfig.hpp"
0015 #include "Acts/Plugins/Hashing/HashingAnnoy.hpp"
0016 #include "Acts/Plugins/Hashing/HashingTraining.hpp"
0017 #include "Acts/Plugins/Hashing/HashingTrainingConfig.hpp"
0018 #include "Acts/Seeding/BinnedGroup.hpp"
0019 #include "Acts/Seeding/detail/UtilityFunctions.hpp"
0020 
0021 #include <cmath>
0022 #include <cstdlib>
0023 #include <memory>
0024 #include <vector>
0025 
0026 #include <annoy/annoylib.h>
0027 #include <annoy/kissrandom.h>
0028 
0029 #include "SpacePoint.hpp"
0030 
0031 using namespace Acts::UnitLiterals;
0032 
0033 // Function to create and initialize the test vector
0034 std::vector<std::unique_ptr<const SpacePoint>> createTestVector() {
0035   std::optional<float> t, varianceT;
0036   std::vector<std::unique_ptr<const SpacePoint>> testVector;
0037   testVector.reserve(6);  // Reserve space for efficiency
0038 
0039   testVector.push_back(std::make_unique<SpacePoint>(
0040       27.2535, -18.0088, -146.526, 29.0, 1, 0.00520833, 0.5, t, varianceT));
0041   testVector.push_back(std::make_unique<SpacePoint>(
0042       42.9126, -27.3057, -171.477, 50.0, 1, 0.0133333, 0.8, t, varianceT));
0043   testVector.push_back(std::make_unique<SpacePoint>(
0044       42.7087, -27.4589, -171.557, 50.0, 1, 0.0133333, 0.4, t, varianceT));
0045   testVector.push_back(std::make_unique<SpacePoint>(
0046       74.3652, -45.8552, -221.277, 80.0, 1, 0.0133333, 0.4, t, varianceT));
0047   testVector.push_back(std::make_unique<SpacePoint>(
0048       104.12, -63.4203, -268.468, 110.0, 1, 0.0133333, 0.4, t, varianceT));
0049   testVector.push_back(std::make_unique<SpacePoint>(
0050       104.412, -63.1851, -268.468, 110.0, 1, 0.0133333, 0.4, t, varianceT));
0051   return testVector;
0052 }
0053 namespace Acts::Test {
0054 BOOST_AUTO_TEST_CASE(HashingBucketCreationTest) {
0055   using SpacePointPtrVector = std::vector<const SpacePoint*>;
0056 
0057   // Initialize testVector using the createTestVector function
0058   auto testVector = createTestVector();
0059 
0060   // Extract raw pointers from unique_ptrs for the test
0061   SpacePointPtrVector spVec;
0062   spVec.reserve(testVector.size());
0063   for (const auto& sp : testVector) {
0064     spVec.push_back(sp.get());
0065   }
0066 
0067   /// Random seed for Annoy
0068   unsigned int annoySeed = 123456789;
0069 
0070   /// Number of features to use
0071   std::int32_t nf = 1;
0072 
0073   /// Size of the buckets = number of spacepoints in the bucket
0074   unsigned int bucketSize = 100;
0075   /// Number of zBins
0076   unsigned int zBins = 10000;
0077   /// Number of phiBins
0078   unsigned int phiBins = 0;
0079 
0080   /// Layer selection
0081   double layerRMin = 25;
0082   double layerRMax = 40;
0083   double layerZMin = -550;
0084   double layerZMax = 550;
0085 
0086   Acts::HashingAlgorithmConfig hashingConfig;
0087   hashingConfig.bucketSize = bucketSize;
0088   hashingConfig.zBins = zBins;
0089   hashingConfig.phiBins = phiBins;
0090   hashingConfig.layerRMin = layerRMin;
0091   hashingConfig.layerRMax = layerRMax;
0092   hashingConfig.layerZMin = layerZMin;
0093   hashingConfig.layerZMax = layerZMax;
0094 
0095   Acts::HashingTrainingConfig hashingTrainingConfig;
0096   hashingTrainingConfig.annoySeed = annoySeed;
0097   hashingTrainingConfig.f = nf;
0098 
0099   Acts::HashingAlgorithm<const SpacePoint*, SpacePointPtrVector> hashing =
0100       Acts::HashingAlgorithm<const SpacePoint*, SpacePointPtrVector>(
0101           hashingConfig);
0102   Acts::HashingTrainingAlgorithm<SpacePointPtrVector> hashingTraining =
0103       Acts::HashingTrainingAlgorithm<SpacePointPtrVector>(
0104           hashingTrainingConfig);
0105 
0106   // Hashing Training
0107   Acts::AnnoyModel annoyModel = hashingTraining.execute(spVec);
0108 
0109   // Hashing
0110   std::vector<SpacePointPtrVector> bucketsPtrs;
0111   bucketsPtrs.clear();
0112   hashing.execute(spVec, &annoyModel, bucketsPtrs);
0113 
0114   // Check the number of buckets created
0115   BOOST_CHECK_GT(bucketsPtrs.size(), 0);
0116 }
0117 
0118 BOOST_AUTO_TEST_CASE(HashingBucketContentTest) {
0119   using SpacePointPtrVector = std::vector<const SpacePoint*>;
0120 
0121   // Initialize testVector using the createTestVector function
0122   auto testVector = createTestVector();
0123 
0124   // Extract raw pointers from unique_ptrs for the test
0125   SpacePointPtrVector spVec;
0126   spVec.reserve(testVector.size());
0127   for (const auto& sp : testVector) {
0128     spVec.push_back(sp.get());
0129   }
0130 
0131   /// Random seed for Annoy
0132   unsigned int annoySeed = 123456789;
0133 
0134   /// Number of features to use
0135   std::int32_t nf = 1;
0136 
0137   /// Size of the buckets = number of spacepoints in the bucket
0138   unsigned int bucketSize = 100;
0139   /// Number of zBins
0140   unsigned int zBins = 10000;
0141   /// Number of phiBins
0142   unsigned int phiBins = 0;
0143 
0144   /// Layer selection
0145   double layerRMin = 25;
0146   double layerRMax = 40;
0147   double layerZMin = -550;
0148   double layerZMax = 550;
0149 
0150   Acts::HashingAlgorithmConfig hashingConfig;
0151   hashingConfig.bucketSize = bucketSize;
0152   hashingConfig.zBins = zBins;
0153   hashingConfig.phiBins = phiBins;
0154   hashingConfig.layerRMin = layerRMin;
0155   hashingConfig.layerRMax = layerRMax;
0156   hashingConfig.layerZMin = layerZMin;
0157   hashingConfig.layerZMax = layerZMax;
0158 
0159   Acts::HashingTrainingConfig hashingTrainingConfig;
0160   hashingTrainingConfig.annoySeed = annoySeed;
0161   hashingTrainingConfig.f = nf;
0162 
0163   Acts::HashingAlgorithm<const SpacePoint*, SpacePointPtrVector> hashing =
0164       Acts::HashingAlgorithm<const SpacePoint*, SpacePointPtrVector>(
0165           hashingConfig);
0166   Acts::HashingTrainingAlgorithm<SpacePointPtrVector> hashingTraining =
0167       Acts::HashingTrainingAlgorithm<SpacePointPtrVector>(
0168           hashingTrainingConfig);
0169 
0170   // Hashing Training
0171   Acts::AnnoyModel annoyModel = hashingTraining.execute(spVec);
0172 
0173   // Hashing
0174   std::vector<SpacePointPtrVector> bucketsPtrs;
0175   bucketsPtrs.clear();
0176   hashing.execute(spVec, &annoyModel, bucketsPtrs);
0177 
0178   // Validate bucket content
0179   for (const auto& bucket : bucketsPtrs) {
0180     BOOST_CHECK_LE(bucket.size(), bucketSize);
0181     for (const auto* sp : bucket) {
0182       BOOST_CHECK(sp != nullptr);
0183     }
0184   }
0185 }
0186 }  // namespace Acts::Test