Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-12-02 04:24:08

0001 #include "CalibFormats/SiStripObjects/interface/SiStripHashedDetId.h"
0002 #include "CalibFormats/SiStripObjects/interface/SiStripDetInfo.h"
0003 #include "CalibTracker/SiStripCommon/interface/SiStripDetInfoFileReader.h"
0004 #include "FWCore/ParameterSet/interface/FileInPath.h"
0005 #include "catch.hpp"
0006 
0007 #include <set>
0008 #include <vector>
0009 #include <algorithm>
0010 #include <iostream>
0011 
0012 TEST_CASE("SiStripHashedDetId testing", "[SiStripHashedDetId]") {
0013   //_____________________________________________________________
0014   SECTION("Check constructing SiStripHashedDetId from DetId list") {
0015     const auto& detInfo =
0016         SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0017     const auto& detIds = detInfo.getAllDetIds();
0018     SiStripHashedDetId hash(detIds);
0019     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0020               << " Successfully created hash!" << std::endl;
0021     REQUIRE(true);
0022   }
0023 
0024   //_____________________________________________________________
0025   SECTION("Check SiStripHashedDetId copy constructor") {
0026     const auto& detInfo =
0027         SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0028     const auto& dets = detInfo.getAllDetIds();
0029 
0030     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0031               << " dets.size(): " << dets.size() << std::endl;
0032 
0033     SiStripHashedDetId hash(dets);
0034 
0035     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0036               << " hash.size(): " << hash.size() << std::endl;
0037 
0038     // Retrieve hashed indices
0039     std::vector<uint32_t> hashes;
0040     hashes.clear();
0041     hashes.reserve(dets.size());
0042     for (const auto& idet : dets) {
0043       hashes.push_back(hash.hashedIndex(idet));
0044     }
0045 
0046     std::sort(hashes.begin(), hashes.end());
0047 
0048     SiStripHashedDetId hash2(hash);
0049 
0050     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0051               << " Successfully copied hash map!" << std::endl;
0052 
0053     // Retrieve hashed indices
0054     std::vector<uint32_t> hashes2;
0055 
0056     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0057               << " hashs2.size(): " << hash2.size() << std::endl;
0058 
0059     hashes2.clear();
0060     hashes2.reserve(dets.size());
0061     for (const auto& idet : dets) {
0062       hashes2.push_back(hash2.hashedIndex(idet));
0063     }
0064 
0065     std::sort(hashes2.begin(), hashes2.end());
0066 
0067     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0068               << " Successfully sorted second hash map!" << std::endl;
0069 
0070     // Convert vectors to sets for easy set operations
0071     std::set<uint32_t> set1(hashes.begin(), hashes.end());
0072     std::set<uint32_t> set2(hashes2.begin(), hashes2.end());
0073 
0074     std::vector<uint32_t> diff1to2, diff2to1;
0075 
0076     // Find elements in vec1 that are not in vec2
0077     std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), std::inserter(diff1to2, diff1to2.begin()));
0078 
0079     // Find elements in vec2 that are not in vec1
0080     std::set_difference(set2.begin(), set2.end(), set1.begin(), set1.end(), std::inserter(diff2to1, diff2to1.begin()));
0081 
0082     // Output the differences
0083     if (!diff1to2.empty()) {
0084       std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0085                 << " Elements in hash that are not in hash2: ";
0086       for (const auto& elem : diff1to2) {
0087         std::cout << elem << " ";
0088       }
0089       std::cout << std::endl;
0090     }
0091 
0092     if (!diff2to1.empty()) {
0093       std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0094                 << " Elements in hash2 that are not in hash: ";
0095       for (const auto& elem : diff2to1) {
0096         std::cout << elem << " ";
0097       }
0098       std::cout << std::endl;
0099     }
0100 
0101     REQUIRE(hashes == hashes2);
0102   }
0103 
0104   //_____________________________________________________________
0105   SECTION("Check SiStripHashedDetId assignment operator") {
0106     const auto& detInfo =
0107         SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0108     const auto& dets = detInfo.getAllDetIds();
0109 
0110     SiStripHashedDetId hash(dets);
0111     SiStripHashedDetId hash2;
0112 
0113     // Retrieve hashed indices
0114     std::vector<uint32_t> hashes;
0115     hashes.clear();
0116     hashes.reserve(dets.size());
0117     for (const auto& idet : dets) {
0118       hashes.push_back(hash.hashedIndex(idet));
0119     }
0120 
0121     std::sort(hashes.begin(), hashes.end());
0122 
0123     // assign hash to hash2
0124     hash2 = hash;
0125 
0126     // Retrieve hashed indices
0127     std::vector<uint32_t> hashes2;
0128     hashes2.clear();
0129     hashes2.reserve(dets.size());
0130     for (const auto& idet : dets) {
0131       hashes2.push_back(hash2.hashedIndex(idet));
0132     }
0133 
0134     std::sort(hashes2.begin(), hashes2.end());
0135 
0136     if (hashes == hashes2) {
0137       std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0138                 << " Assigned SiStripHashedDetId matches original one!" << std::endl;
0139     } else {
0140       std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0141                 << " Assigned SiStripHashedDetId does not match the original one!" << std::endl;
0142     }
0143 
0144     REQUIRE(hashes == hashes2);
0145   }
0146 
0147   //_____________________________________________________________
0148   SECTION("Check manipulating SiStripHashedDetId") {
0149     const auto& detInfo =
0150         SiStripDetInfoFileReader::read(edm::FileInPath(SiStripDetInfoFileReader::kDefaultFile).fullPath());
0151 
0152     const auto& unsortedDets = detInfo.getAllDetIds();
0153 
0154     // unfortunately SiStripDetInfo::getAllDetIds() returns a const vector
0155     // so in order to sory we're gonna need to copy it first
0156 
0157     std::vector<uint32_t> dets;
0158     dets.reserve(unsortedDets.size());
0159     std::copy(unsortedDets.begin(), unsortedDets.end(), std::back_inserter(dets));
0160 
0161     // sort the vector of detIds (otherwise the test won't work!)
0162     std::sort(dets.begin(), dets.end());
0163 
0164     SiStripHashedDetId hash(dets);
0165 
0166     // Retrieve hashed indices
0167     std::vector<uint32_t> hashes;
0168     uint32_t istart = time(NULL);
0169     hashes.clear();
0170     hashes.reserve(dets.size());
0171     for (const auto& idet : dets) {
0172       hashes.push_back(hash.hashedIndex(idet));
0173     }
0174 
0175     // Some debug
0176     std::stringstream ss;
0177     ss << "[testSiStripHashedDetId::" << __func__ << "]";
0178     uint16_t cntr1 = 0;
0179     for (const auto& ii : hashes) {
0180       if (ii == sistrip::invalid32_) {
0181         cntr1++;
0182         ss << std::endl << " Invalid index " << ii;
0183         continue;
0184       }
0185       uint32_t detid = hash.unhashIndex(ii);
0186       std::vector<uint32_t>::const_iterator iter = find(dets.begin(), dets.end(), detid);
0187       if (iter == dets.end()) {
0188         cntr1++;
0189         ss << std::endl << " Did not find value " << detid << " at index " << ii - *(hashes.begin()) << " in vector!";
0190       } else if (ii != static_cast<uint32_t>(iter - dets.begin())) {
0191         cntr1++;
0192         ss << std::endl
0193            << " Found same value " << detid << " at different indices " << ii << " and " << iter - dets.begin();
0194       }
0195     }
0196 
0197     if (cntr1) {
0198       ss << std::endl << " Found " << cntr1 << " incompatible values!";
0199     } else {
0200       ss << " Found no incompatible values!";
0201     }
0202     std::cout << ss.str() << std::endl;
0203 
0204     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0205               << " Processed " << hashes.size() << " DetIds in " << (time(NULL) - istart) << " seconds" << std::endl;
0206 
0207     REQUIRE(cntr1 == 0);
0208 
0209     // Retrieve DetIds
0210     std::vector<uint32_t> detids;
0211     uint32_t jstart = time(NULL);
0212     // meaasurement!
0213     detids.clear();
0214     detids.reserve(dets.size());
0215     for (uint16_t idet = 0; idet < dets.size(); ++idet) {
0216       detids.push_back(hash.unhashIndex(idet));
0217     }
0218 
0219     // Some debug
0220     std::stringstream sss;
0221     sss << "[testSiStripHashedDetId::" << __func__ << "]";
0222     uint16_t cntr2 = 0;
0223     std::vector<uint32_t>::const_iterator iii = detids.begin();
0224     for (; iii != detids.end(); ++iii) {
0225       if (*iii != dets.at(iii - detids.begin())) {
0226         cntr2++;
0227         sss << std::endl
0228             << " Diff values " << *iii << " and " << dets.at(iii - detids.begin()) << " found at index "
0229             << iii - detids.begin() << " ";
0230       }
0231     }
0232     if (cntr2) {
0233       sss << std::endl << " Found " << cntr2 << " incompatible values!";
0234     } else {
0235       sss << " Found no incompatible values!";
0236     }
0237     std::cout << sss.str() << std::endl;
0238 
0239     std::cout << "[testSiStripHashedDetId::" << __func__ << "]"
0240               << " Processed " << detids.size() << " hashed indices in " << (time(NULL) - jstart) << " seconds"
0241               << std::endl;
0242 
0243     REQUIRE(cntr2 == 0);
0244 
0245     REQUIRE(true);
0246   }
0247 }