ATLAS Offline Software
RoIsUnpackingEmulationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
6 #include <fstream>
7 
8 
10  const std::string& name,
11  const IInterface* 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 
71 std::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 
100 std::vector<RoIsUnpackingEmulationTool::FakeRoI>
101 RoIsUnpackingEmulationTool::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 
121 RoIsUnpackingEmulationTool::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 
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(),
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 }
RoIsUnpackingEmulationTool::parseInputRoI
static FakeRoI parseInputRoI(const std::string &roi, unsigned lineNumber, unsigned roiNumber)
Definition: RoIsUnpackingEmulationTool.cxx:121
RoIsUnpackingEmulationTool::m_inputData
std::vector< std::vector< RoIsUnpackingEmulationTool::FakeRoI > > m_inputData
Definition: RoIsUnpackingEmulationTool.h:52
checkFileSG.line
line
Definition: checkFileSG.py:75
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
get_generator_info.result
result
Definition: get_generator_info.py:21
RoIsUnpackingToolBase
Base class for RoI unpackers.
Definition: RoIsUnpackingToolBase.h:32
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
ROIB::RoIBResult
Class holding the LVL1 RoIB result build by the RoIBuilder.
Definition: RoIBResult.h:47
TrigCompositeUtils::hltSeedingNodeName
const std::string & hltSeedingNodeName()
Definition: TrigCompositeUtilsRoot.cxx:892
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
TrigCompositeUtils::newDecisionIn
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.
Definition: TrigCompositeUtilsRoot.cxx:46
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
RoIsUnpackingEmulationTool::readEmulatedData
StatusCode readEmulatedData()
Definition: RoIsUnpackingEmulationTool.cxx:35
RoIsUnpackingToolBase::addChainsToDecision
void addChainsToDecision(HLT::Identifier thresholdId, TrigCompositeUtils::Decision *d, const HLT::IDSet &activeChains) const
Definition: RoIsUnpackingToolBase.cxx:88
RoIsUnpackingEmulationTool::m_roIWidth
Gaudi::Property< float > m_roIWidth
Definition: RoIsUnpackingEmulationTool.h:28
RoIsUnpackingEmulationTool::m_currentRowNumber
unsigned m_currentRowNumber
Definition: RoIsUnpackingEmulationTool.h:53
TrigCompositeUtils::createAndStore
SG::WriteHandle< DecisionContainer > createAndStore(const SG::WriteHandleKey< DecisionContainer > &key, const EventContext &ctx)
Creates and right away records the DecisionContainer with the key.
Definition: TrigCompositeUtilsRoot.cxx:30
RoIsUnpackingEmulationTool::FakeRoI
Definition: RoIsUnpackingEmulationTool.h:41
RoIsUnpackingEmulationTool::initialize
virtual StatusCode initialize() override
Definition: RoIsUnpackingEmulationTool.cxx:15
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
python.TriggerHandler.th
th
Definition: TriggerHandler.py:296
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
RoIsUnpackingToolBase::decodeMapping
StatusCode decodeMapping(std::function< bool(const std::string &)> &&filter)
Fills mapping from L1 threshold -> to HLT chain.
Definition: RoIsUnpackingToolBase.cxx:52
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
RoIsUnpackingEmulationTool.h
TrigCompositeUtils::initialRoIString
const std::string & initialRoIString()
Definition: TrigCompositeUtilsRoot.cxx:868
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
calibdata.exception
exception
Definition: calibdata.py:496
RoIsUnpackingEmulationTool::m_thresholdPrefix
Gaudi::Property< std::string > m_thresholdPrefix
Definition: RoIsUnpackingEmulationTool.h:34
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TrigCompositeUtils::createAndStoreNoAux
SG::WriteHandle< CONT > createAndStoreNoAux(const SG::WriteHandleKey< CONT > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Creates and right away records the Container CONT with the key.
RoIsUnpackingToolBase::m_decisionsKey
SG::WriteHandleKey< TrigCompositeUtils::DecisionContainer > m_decisionsKey
Definition: RoIsUnpackingToolBase.h:51
HLT::Identifier
Definition: TrigCompositeUtils/TrigCompositeUtils/HLTIdentifier.h:20
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
xAOD::roiNumber
setTeId setLumiBlock setRoiId setRoiSubsystem roiNumber
Definition: L2StandAloneMuon_v1.cxx:339
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
TrigT1CaloDefs.h
RoIsUnpackingToolBase::m_trigRoIsKey
SG::WriteHandleKey< TrigRoiDescriptorCollection > m_trigRoIsKey
Definition: RoIsUnpackingToolBase.h:57
SG::WriteHandle< TrigRoiDescriptorCollection >
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
RoIsUnpackingEmulationTool::parseInputFile
std::vector< std::vector< FakeRoI > > parseInputFile()
Definition: RoIsUnpackingEmulationTool.cxx:71
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
RoIsUnpackingEmulationTool::unpack
virtual StatusCode unpack(const EventContext &ctx, const ROIB::RoIBResult &roib, const HLT::IDSet &activeChains) const override
Definition: RoIsUnpackingEmulationTool.cxx:151
HLT::IDSet
std::set< HLT::Identifier > IDSet
Definition: TrigCompositeUtils/TrigCompositeUtils/HLTIdentifier.h:63
GRLStrUtil::trim
void trim(std::string &input)
Definition: StrUtil.cxx:12
RoIsUnpackingEmulationTool::start
virtual StatusCode start() override
Definition: RoIsUnpackingEmulationTool.cxx:27
RoIsUnpackingToolBase::initialize
virtual StatusCode initialize() override
Definition: RoIsUnpackingToolBase.cxx:16
TrigCompositeUtils
Definition: Event/xAOD/xAODTrigger/xAODTrigger/TrigComposite.h:19
RoIsUnpackingEmulationTool::m_inputFilename
Gaudi::Property< std::string > m_inputFilename
Definition: RoIsUnpackingEmulationTool.h:31
calibdata.copy
bool copy
Definition: calibdata.py:27
RoIsUnpackingEmulationTool::parseInputFileLine
static std::vector< FakeRoI > parseInputFileLine(const std::string &line, unsigned lineNumber)
Definition: RoIsUnpackingEmulationTool.cxx:101
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
RoIsUnpackingEmulationTool::RoIsUnpackingEmulationTool
RoIsUnpackingEmulationTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: RoIsUnpackingEmulationTool.cxx:9