ATLAS Offline Software
Loading...
Searching...
No Matches
RoIsUnpackingEmulationTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
6#include <fstream>
7
8
10 const std::string& name,
11 const IInterface* parent)
12 : RoIsUnpackingToolBase ( type, name, parent ) {}
13
14
17
18 if (readEmulatedData().isFailure() ) {
19 ATH_MSG_ERROR( "Failed to read emulated data" );
20 return StatusCode::FAILURE;
21 }
22
23 return StatusCode::SUCCESS;
24}
25
26
28 ATH_CHECK(decodeMapping([&](const std::string& name){
29 return name.find(m_thresholdPrefix) == 0;
30 }));
31 return StatusCode::SUCCESS;
32}
33
34
36 if ( m_inputFilename.empty() ) {
37 ATH_MSG_WARNING("Provided InputFilename parameter is empty");
38 return StatusCode::RECOVERABLE;
39 }
40 try {
43 }
44 catch (const std::exception& ex) {
45 ATH_MSG_WARNING(ex.what());
46 return StatusCode::RECOVERABLE;
47 }
48
49 ATH_MSG_DEBUG("Input file parsed successfully. Data:");
50
51 for (auto& fragment : m_inputData) {
52 ATH_MSG_DEBUG("Event");
53 for (auto& fakeRoI : fragment) {
54 const char* const delim = ", ";
55 std::ostringstream debugPassedTresholdIDs;
56 std::copy(fakeRoI.passedThresholdIDs.begin(),
57 fakeRoI.passedThresholdIDs.end(),
58 std::ostream_iterator<std::string>(debugPassedTresholdIDs, delim));
59
60 ATH_MSG_DEBUG("Eta: " + std::to_string(fakeRoI.eta) +
61 ", Phi: " + std::to_string(fakeRoI.phi) +
62 ", Word: " + std::to_string(fakeRoI.roIWord) +
63 ", PassedThresholdIDs: " + debugPassedTresholdIDs.str());
64 }
65 }
66
67 return StatusCode::SUCCESS;
68}
69
70
71std::vector<std::vector<RoIsUnpackingEmulationTool::FakeRoI>> RoIsUnpackingEmulationTool::parseInputFile() {
72 const char* whiteSpaceChars = " \t\n\r\f\v";
73 auto rtrim = [whiteSpaceChars](std::string& s) { s.erase(s.find_last_not_of(whiteSpaceChars) + 1); };
74 auto ltrim = [whiteSpaceChars](std::string& s) { s.erase(0, s.find_first_not_of(whiteSpaceChars)); };
75 auto trim = [&rtrim, &ltrim](std::string& s) { ltrim(s); rtrim(s); };
76
77 std::vector<std::vector<RoIsUnpackingEmulationTool::FakeRoI>> result;
78 unsigned lineNumber = 0;
79 std::string line;
80
81 std::ifstream inputFile(m_inputFilename);
82 if (!inputFile.good()) {
83 throw std::invalid_argument("File " + m_inputFilename + "does not exists or is corrupted");
84 }
85
86 while (getline(inputFile, line)) {
87 trim(line);
88 if (line.empty()) {continue;}
89 result.push_back(parseInputFileLine(line, ++lineNumber));
90 }
91
92 if (result.empty()) {
93 throw std::invalid_argument("File " + m_inputFilename + " does not contain any RoI");
94 }
95 ATH_MSG_DEBUG("Read in " << result.size() << " pseudo events from " << m_inputFilename );
96 return result;
97}
98
99
100std::vector<RoIsUnpackingEmulationTool::FakeRoI>
101RoIsUnpackingEmulationTool::parseInputFileLine(const std::string& line, unsigned lineNumber) {
102 std::vector<RoIsUnpackingEmulationTool::FakeRoI> result;
103 unsigned roiNumber = 0;
104 std::stringstream inputLine(line);
105 std::string roi;
106
107 while (getline(inputLine, roi, ';')) {
108 // FIXME: If there aren't as many input fields in roi as parseInputRoI expects,
109 // it'll return uninitialized data (which can lead to a practically infinite
110 // loop when we try to normalize the phi). Add an elementary check here to
111 // fix such a loop observed from creatingEVTest in ViewAlgsTest.
112 if (roi.find (',') != std::string::npos) {
113 result.push_back(parseInputRoI(roi, lineNumber, ++roiNumber));
114 }
115 }
116 return result;
117}
118
119
121RoIsUnpackingEmulationTool::parseInputRoI(const std::string& roi, unsigned lineNumber, unsigned roiNumber) {
123 std::stringstream inputRoi(roi);
124 std::string roiElement;
125 unsigned roiElementNumber = 0;
126
127 std::vector<std::function<void(const std::string&)>> fillResultFunctors;
128 fillResultFunctors.emplace_back([&](const std::string& eta) { result.eta = stod(eta); });
129 fillResultFunctors.emplace_back([&](const std::string& phi) { result.phi = stod(phi); });
130 fillResultFunctors.emplace_back([&](const std::string& word) { result.roIWord = stoul(word); });
131 fillResultFunctors.emplace_back([&](const std::string& passedThresholdID) {
132 result.passedThresholdIDs.push_back(passedThresholdID); });
133
134 while (getline(inputRoi, roiElement, ',')) {
135 if (roiElementNumber > 2) { roiElementNumber = 3; }
136 try{
137 //cppcheck bug: https://trac.cppcheck.net/ticket/11530
138 //cppcheck-suppress containerOutOfBounds
139 fillResultFunctors[roiElementNumber++](roiElement);
140 }
141 catch (const std::exception& ex) {
142 throw std::invalid_argument("Exception encountered while parsing line " + std::to_string(lineNumber) +
143 ", roi " + std::to_string(roiNumber) + ". Exception: " + ex.what());
144 }
145 }
146
147 return result;
148}
149
150
151StatusCode RoIsUnpackingEmulationTool::unpack(const EventContext& ctx,
152 const ROIB::RoIBResult& /*roib*/,
153 const HLT::IDSet& activeChains ) const {
154 using namespace TrigCompositeUtils;
155
156 // create and record the collections needed
159
160 // retrieve fake data for this event
161 if (m_inputData.empty()){
162 ATH_MSG_ERROR("No input dataset found. Cannot decode RoI emulation");
163 return StatusCode::FAILURE;
164 }
165 const size_t line = ctx.evt() % m_inputData.size();
166 ATH_MSG_DEBUG("Getting RoIs for event "<< line);
167 auto FakeRoIs = m_inputData[line];
168
169 for (auto& roi : FakeRoIs) {
170 uint32_t roIWord = roi.roIWord;
171 trigRoIs->push_back( std::make_unique<TrigRoiDescriptor>(
172 roIWord, 0u ,0u,
173 roi.eta, roi.eta-m_roIWidth, roi.eta+m_roIWidth,
174 roi.phi, roi.phi-m_roIWidth, roi.phi+m_roIWidth) );
175
176 ATH_MSG_DEBUG( "RoI word: 0x" << MSG::hex << std::setw(8) << roIWord << MSG::dec );
177
178 // This hltSeedingNodeName() denotes an initial node with no parents
179 auto *decision = TrigCompositeUtils::newDecisionIn( decisionOutput.ptr(), hltSeedingNodeName() );
180 std::vector<TrigCompositeUtils::DecisionID> passedThresholdIDs;
181
182 for ( const auto& th: roi.passedThresholdIDs ) {
183 ATH_MSG_DEBUG( "Passed Threshold " << th << " enabling respective chains " );
184 passedThresholdIDs.push_back( HLT::Identifier(th) );
185 addChainsToDecision( HLT::Identifier( th ), decision, activeChains );
186 decision->setObjectLink( initialRoIString(),
187 ElementLink<TrigRoiDescriptorCollection>(m_trigRoIsKey.key(), trigRoIs->size()-1, ctx) );
188 }
189
190 decision->setDetail( "thresholds", passedThresholdIDs );
191 }
192
193 for ( auto roi: *trigRoIs ) {
194 ATH_MSG_DEBUG("RoI Eta: " << roi->eta() << " Phi: " << roi->phi() << " RoIWord: " << roi->roiWord());
195 }
196
197 return StatusCode::SUCCESS;
198}
Scalar eta() const
pseudorapidity method
Scalar phi() const
phi method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
SG::WriteHandle< DecisionContainer > createAndStore(const SG::WriteHandleKey< DecisionContainer > &key, const EventContext &ctx)
Creates and right away records the DecisionContainer with the key.
string trim(string s)
Class holding the LVL1 RoIB result build by the RoIBuilder.
Definition RoIBResult.h:47
virtual StatusCode initialize() override
virtual StatusCode unpack(const EventContext &ctx, const ROIB::RoIBResult &roib, const HLT::IDSet &activeChains) const override
std::vector< std::vector< FakeRoI > > parseInputFile()
virtual StatusCode start() override
Gaudi::Property< float > m_roIWidth
std::vector< std::vector< RoIsUnpackingEmulationTool::FakeRoI > > m_inputData
Gaudi::Property< std::string > m_thresholdPrefix
Gaudi::Property< std::string > m_inputFilename
static FakeRoI parseInputRoI(const std::string &roi, unsigned lineNumber, unsigned roiNumber)
RoIsUnpackingEmulationTool(const std::string &type, const std::string &name, const IInterface *parent)
static std::vector< FakeRoI > parseInputFileLine(const std::string &line, unsigned lineNumber)
SG::WriteHandleKey< TrigCompositeUtils::DecisionContainer > m_decisionsKey
RoIsUnpackingToolBase(const std::string &type, const std::string &name, const IInterface *parent)
void addChainsToDecision(HLT::Identifier thresholdId, TrigCompositeUtils::Decision *d, const HLT::IDSet &activeChains) const
virtual StatusCode initialize() override
SG::WriteHandleKey< TrigRoiDescriptorCollection > m_trigRoIsKey
StatusCode decodeMapping(std::function< bool(const std::string &)> &&filter)
Fills mapping from L1 threshold -> to HLT chain.
pointer_type ptr()
Dereference the pointer.
std::set< HLT::Identifier > IDSet
SG::WriteHandle< CONT > createAndStoreNoAux(const SG::WriteHandleKey< CONT > &key, const EventContext &ctx)
Creates and right away records the Container CONT with the key.
Decision * newDecisionIn(DecisionContainer *dc, const std::string &name)
Helper method to create a Decision object, place it in the container and return a pointer to it.
const std::string & initialRoIString()
const std::string & hltSeedingNodeName()