ATLAS Offline Software
Loading...
Searching...
No Matches
TrigConfHLTUtils/Root/HLTUtils.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <iostream>
6#include <stdexcept>
7#include <algorithm>
8#include <fstream>
9
11
12using namespace TrigConf;
13
14
17
20
21
22HLTHash HLTUtils::string2hash( std::string_view s, const std::string& category )
23{
24 // Try to find existing hash in category
25 const std::string ss(s);
26 const auto icat = s_hashStore.hashCat.find(category);
27 if (icat != s_hashStore.hashCat.end()) { // category found
28 const HashMap& cat = icat->second;
29 const auto ihash = cat.name2hash.find(ss);
30 if (ihash != cat.name2hash.end()) { // hash found
31 return ihash->second;
32 }
33 }
34 else {
35 s_hashStore.hashCat.emplace(category, std::make_unique<HashMap>());
36 }
37
38 /*********************************************************************
39 This hash function is derived from the ELF hashing function and
40 unchanged since Run-1. For Phase-II we should really switch to a
41 64 bit hash like CxxUtils::crc64 which is safer (and faster).
42 But we need to take care of backwards compatibility with old data.
43 Original author: Tomasz Bold
44 *********************************************************************/
45 HLTHash hash = 0xd2d84a61;
46 for ( int i = (int)s.size()-1; i >= 0; --i )
47 //coverity[INTEGER_OVERFLOW]
48 hash ^= ( hash >> 5) + s[i] + ( hash << 7 );
49
50 for ( int i = 0; i < (int)s.size(); ++i )
51 hash ^= ( hash >> 5) + s[i] + ( hash << 7 );
52 /********************************************************************/
53
54 // Try to insert new hash
55 HashMap& cat = s_hashStore.hashCat.at(category);
56 const auto& [itr, inserted] = cat.hash2name.emplace(hash, ss);
57
58 if ( inserted ) {
59 // also update reverse map
60 cat.name2hash.emplace(ss, hash);
61 }
62 else {
63 // There are two cases where insertion into the hash->name map would fail:
64 // 1) another thread entered the same hash/name pair already
65 // 2) there is a hash collision
66 if ( s != itr->second ) {
67 throw std::domain_error("Hash collision in category " + category +
68 " for elements " + itr->second + " and " + ss);
69 }
70 }
71
72 return hash;
73}
74
75const std::string HLTUtils::hash2string( HLTHash hash, const std::string& category ) {
76
77 const auto& icat = s_hashStore.hashCat.find(category);
78 if (icat == s_hashStore.hashCat.end()) {
79 return "UNKNOWN CATEGORY";
80 }
81
82 const HashMap& cat = icat->second;
83 const auto& h = cat.hash2name.find(hash);
84 if (h == cat.hash2name.end()) {
85 return "UNKNOWN HASH ID";
86 }
87
88 return h->second;
89}
90
91void HLTUtils::hashes2file( const std::string& fileName) {
92 std::ofstream fout(fileName);
93
94 for (const auto [category, hashes] : s_hashStore.hashCat) {
95 fout << s_newCategory << '\n' << category << '\n';
96 for (const auto [hash, nameptr] : hashes.hash2name) {
97 std::string name(nameptr);
98 std::erase(name, '\n'); // Remove any line breaks
99 fout << hash << '\n' << name << '\n';
100 }
101 }
102}
103
104void HLTUtils::file2hashes( const std::string& fileName) {
105 std::ifstream fin(fileName);
106 if (!fin.is_open()) {
107 return;
108 }
109 std::string line;
110 std::string category;
111 // Note: this method is a to aid with development/debugging.
112 // It won't be used in production code, hence it is light on error checking.
113 while(std::getline(fin, line)) {
114 if (line == s_newCategory) {
115 std::getline(fin, category);
116 continue;
117 }
118 HLTHash hash = std::stoul(line);
119 std::string name;
120 std::getline(fin, name);
121 HLTHash check = string2hash(name, category);
122 if (check != hash) {
123 std::cerr << "Inconsistency in file2hashes(" << fileName << ") function,"
124 " item " << name << " has hash " << hash << " not " << check << std::endl;
125 }
126 }
127}
static Double_t ss
const_iterator find(const key_type &key) const
Look up an element in the map.
const_iterator end() const
Iterator at the end of the map.
static void hashes2file(const std::string &fileName="hashes2string.txt")
debugging output of internal dictionary
static const std::string s_newCategory
In-file identifier.
static HLTHash string2hash(std::string_view, const std::string &category=s_defaultCategory)
hash function translating TE names into identifiers
static const std::string hash2string(HLTHash, const std::string &category=s_defaultCategory)
hash function translating identifiers into names (via internal dictionary)
static void file2hashes(const std::string &fileName="hashes2string.txt")
debugging output of internal dictionary
static TFile * fout
Definition listroot.cxx:40
Forward iterator to traverse the main components of the trigger configuration.
Two concurrent maps to store name->hash and hash->name mappings.
Hash2NameMap_t hash2name
hash to name map
Name2HashMap_t name2hash
name to hash map