Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-30 07:55:48

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