ATLAS Offline Software
Loading...
Searching...
No Matches
PU1SuppTestBench.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
13
14#include "PU1SuppTestBench.h"
15
16#include <algorithm>
17#include <bitset>
18#include <cctype>
19#include <fstream>
20#include <sstream>
21#include <boost/algorithm/string/trim.hpp>
22
23namespace GlobalSim {
24
27 ISvcLocator* pSvcLocator)
28 : AthAlgorithm(name, pSvcLocator) {}
29
32 ATH_MSG_INFO("Initializing PU1SuppTestBench");
33
34 CHECK(m_suppFIFO_WriteKey.initialize());
36
37 if (m_testsFileName.empty()) {
38 ATH_MSG_FATAL("testsFileName is empty");
39 return StatusCode::FAILURE;
40 }
41
43
44 ATH_MSG_INFO("Loaded " << m_fifos.size() << " FIFOs for input");
45 return StatusCode::SUCCESS;
46}
47
50 if (m_fifo_ptr >= m_fifos.size()) {
51 ATH_MSG_ERROR("No more FIFO data to write");
52 return StatusCode::FAILURE;
53 }
54
55 // Write TOB FIFO to event store
57 CHECK(h_write.record(std::move(m_fifos[m_fifo_ptr])));
58
59 // Write dummy expectation to event store (placeholder)
60 auto expectations = std::make_unique<PU1SuppExpectations>("0000", "0000");
63 CHECK(h_write_exp.record(std::move(expectations)));
64
65 ++m_fifo_ptr;
66 return StatusCode::SUCCESS;
67}
68
71 std::ifstream tobFile(m_testsFileName);
72 std::ifstream rhoFile(m_rhoFileName);
73
74 if (!tobFile) {
75 ATH_MSG_FATAL("Failed to open TOB input file: " << m_testsFileName);
76 return StatusCode::FAILURE;
77 }
78 if (!rhoFile) {
79 ATH_MSG_FATAL("Failed to open rho input file: " << m_rhoFileName);
80 return StatusCode::FAILURE;
81 }
82
83 std::string tobLine, rhoLine;
84 int lineNum = 0;
85
86 while (std::getline(tobFile, tobLine) && std::getline(rhoFile, rhoLine)) {
87 ++lineNum;
88
89 std::string tobStr = trim(tobLine);
90 std::string rhoStr = trim(rhoLine);
91
92 const bool isHex = (tobStr.length() == 64 &&
93 std::all_of(tobStr.begin(), tobStr.end(), ::isxdigit));
94
95 const bool isBinary =
96 (tobStr.length() == 256 &&
97 std::all_of(tobStr.begin(), tobStr.end(),
98 [](char c) { return c == '0' || c == '1'; }));
99
100 if (!isHex && !isBinary) {
101 ATH_MSG_WARNING("Skipping bad TOB line "
102 << lineNum
103 << ": expected 64 length hex or 256 length bit");
104 continue;
105 }
106
107 auto fifo = std::make_unique<GepAlgoPU1SuppFIFO>();
108 PU1SuppPortsIn ports_in;
109
110 const int chunkSize = isHex ? 16 : 64;
111 std::string_view tobView = tobStr;
112 bool parseError = false;
113
114 for (int i=0; i < 4; ++i) {
115 auto tobWord = tobView.substr(i * chunkSize, chunkSize);
116 uint64_t value = 0;
117 auto [ptr, ec] = std::from_chars(
118 tobWord.data(),
119 tobWord.data() + tobWord.size(),
120 value,
121 isHex ? 16 : 2
122 );
123 if (ec != std::errc{}) {
124 ATH_MSG_WARNING("Skipping bad TOB line " << lineNum
125 << ": failed to parse section " << i);
126 parseError = true;
127 break;
128 }
129 ports_in.m_I_PU1TobData[i] = value;
130 }
131
132 if (parseError) continue;
133
134 ports_in.m_rho = static_cast<uint16_t>(std::stoul(rhoStr, nullptr, 2));
135
136 fifo->push_back(ports_in);
137 m_fifos.push_back(std::move(fifo));
138 }
139
140 return StatusCode::SUCCESS;
141}
142
144std::string PU1SuppTestBenchAlg::trim(const std::string& s) {
145 std::string result = s;
146 boost::algorithm::trim(result);
147 return result;
148}
149
150} // namespace GlobalSim
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define CHECK(...)
Evaluate an expression and check for errors.
Declares the PU1SuppTestBenchAlg, an Athena algorithm for testing PU1 suppression logic.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
StatusCode init_from_file()
Populate m_fifos from file.
std::vector< std::unique_ptr< GepAlgoPU1SuppFIFO > > m_fifos
Preloaded list of TOB FIFOs from file.
PU1SuppTestBenchAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
virtual StatusCode execute() override
Athena execute hook (called once per event).
SG::WriteHandleKey< GepAlgoPU1SuppFIFO > m_suppFIFO_WriteKey
Write handle key for TOB FIFO.
Gaudi::Property< std::string > m_testsFileName
Path to input file containing 256-bit TOBs as 64-character hex strings.
SG::WriteHandleKey< PU1SuppExpectations > m_PU1SuppExpectations_WriteKey
Write handle key for dummy expectations.
std::size_t m_fifo_ptr
Index of next FIFO to write during execution.
Gaudi::Property< std::string > m_rhoFileName
Path to input file containing rho values, one per line.
std::string trim(const std::string &s)
Strip whitespace from both ends of a string.
virtual StatusCode initialize() override
Athena initialize hook.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
AlgTool to read in LArStripNeighborhoods, and run the eRatio Algorithm.
Data structure representing a single PU1 suppression input.
uint16_t m_rho
Rho value as a 16-bit initialized unsigned integer.
std::array< uint64_t, 4 > m_I_PU1TobData
256-bit TOB stored as four 64-bit unsigned integers