ATLAS Offline Software
Loading...
Searching...
No Matches
TOBTextReader.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3 */
4#include "TOBTextReader.h"
5
6#include "SpecRegistry.h"
7
10
11#include <fstream>
12#include <sstream>
13
14namespace GlobalSim {
15
17 ATH_CHECK(m_outKey.initialize());
18
19 const auto entry = specRegistry().find(m_specName.value());
20 if (entry == specRegistry().end()) {
21 ATH_MSG_ERROR("Unknown BitSpec '" << m_specName.value()
22 << "'. Known specs are: " << knownSpecNames());
23 return StatusCode::FAILURE;
24 }
25 m_width = entry->second.width;
26 m_unpack = entry->second.unpack;
27
28 // The whole file is read here so that execute() can stay const and
29 // reentrant: after this point m_events is only ever read.
30 std::ifstream in(m_inputFile.value());
31 if (!in) {
32 ATH_MSG_ERROR("Could not open input file '" << m_inputFile.value() << "'");
33 return StatusCode::FAILURE;
34 }
35
36 const std::size_t expected = m_width / 4;
37 std::string line;
38 std::size_t lineNo{0};
39
40 while (std::getline(in, line)) {
41 ++lineNo;
42 std::istringstream ss(line);
43 std::string token;
44 std::vector<std::string> tokens;
45
46 while (ss >> token) {
47 // Checked here rather than at decode time: a token of the wrong
48 // length would be silently truncated by std::bitset, which is how a
49 // file written for one spec gets read as another without complaint.
50 if (token.size() != expected) {
51 ATH_MSG_ERROR("line " << lineNo << ": token '" << token << "' has "
52 << token.size() << " hex characters, but "
53 << m_specName.value() << " needs " << expected);
54 return StatusCode::FAILURE;
55 }
56 tokens.push_back(token);
57 }
58
59 if (!tokens.empty()) { m_events.push_back(std::move(tokens)); }
60 }
61
62 if (m_events.empty()) {
63 ATH_MSG_ERROR("No events read from '" << m_inputFile.value() << "'");
64 return StatusCode::FAILURE;
65 }
66
67 ATH_MSG_INFO("Loaded " << m_events.size() << " events from '"
68 << m_inputFile.value() << "' as " << m_specName.value()
69 << ", " << expected << " hex characters per TOB");
70 return StatusCode::SUCCESS;
71 }
72
73 StatusCode TOBTextReader::execute(const EventContext& ctx) const {
74 // event N -> line N, wrapping round if the job runs more events than the
75 // file has lines.
76 const std::size_t idx = ctx.evt() % m_events.size();
77 const std::vector<std::string>& tokens = m_events[idx];
78
79 auto tobs = std::make_unique<xAOD::BaseContainer>();
80 auto tobsAux = std::make_unique<xAOD::AuxContainerBase>();
81 tobs->setStore(tobsAux.get());
82
83 // Assigning the whole word fills every field the spec declares, so the
84 // objects carry the flags as well as the kinematics.
85 for (const std::string& token : tokens) {
86 tobs->push_back(std::make_unique<SG::AuxElement>());
87 m_unpack(*tobs->back(), token);
88 }
89
90 ATH_MSG_INFO("event " << ctx.evt() << ": produced " << tobs->size()
91 << " TOBs into '" << m_outKey.key() << "'");
92
94 ATH_CHECK(h.record(std::move(tobs), std::move(tobsAux)));
95 return StatusCode::SUCCESS;
96 }
97
98} // namespace GlobalSim
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_INFO(x,...)
Base class for elements of a container that can have aux data.
static Double_t ss
Header file for AthHistogramAlgorithm.
SG::WriteHandleKey< xAOD::BaseContainer > m_outKey
std::vector< std::vector< std::string > > m_events
Gaudi::Property< std::string > m_specName
virtual StatusCode initialize() override
Gaudi::Property< std::string > m_inputFile
std::function< void(SG::AuxElement &, const std::string &)> m_unpack
virtual StatusCode execute(const EventContext &ctx) const override
AlgTool to read in LArStripNeighborhoods, and run the BDT Algorithm.
Definition BitSpec.h:23
std::string knownSpecNames()
Comma-separated list of the known names, for error messages.
const std::map< std::string, SpecEntry > & specRegistry()
The known specs, by name. Built on first use.