ATLAS Offline Software
Loading...
Searching...
No Matches
eFexByteStreamTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5//***************************************************************************
6// eFexByteStreamTool - description
7// -------------------
8// begin : 11 05 2022
9// email : will@cern.ch
10// ***************************************************************************/
11
12#include "eFexByteStreamTool.h"
13#include "CxxUtils/span.h"
14#include "eformat/SourceIdentifier.h"
15#include "eformat/Status.h"
16
25
28
29namespace {
30 // Helper method for creating the containers
31 template<typename T, typename W>
32 StatusCode addContainer(
33 std::map <std::pair<L1CaloRdoFexTob::TobSource, bool>, SG::WriteHandle<T>> &map,
34 L1CaloRdoFexTob::TobSource source, bool inTime, const SG::WriteHandleKey <T> &handle,
35 const EventContext &ctx) {
36 if (!handle.empty()) {
37 return map.emplace(std::make_pair(source, inTime),
38 SG::WriteHandle<T>(handle, ctx)).first->second.record(
39 std::make_unique<T>(), std::make_unique<W>());
40 }
41 return StatusCode::SUCCESS;
42 }
43}
44
46 const std::string& name,
47 const IInterface* parent)
48 : base_class(type, name, parent) {}
49
51
52 // Initialise eEM data handle keys
53 ATH_CHECK(m_eEMReadKey.initialize(!m_eEMReadKey.empty()));
54 ATH_CHECK(m_eEMWriteKey.initialize(!m_eEMWriteKey.empty()));
55
56 // Initialise eTAU data handle keys
57 ATH_CHECK(m_eTAUReadKey.initialize(!m_eTAUReadKey.empty()));
58 ATH_CHECK(m_eTAUWriteKey.initialize(!m_eTAUWriteKey.empty()));
59
60 // write keys for xTOBs
61 ATH_CHECK(m_eEMxWriteKey.initialize(!m_eEMxWriteKey.empty()));
62 ATH_CHECK(m_eTAUxWriteKey.initialize(!m_eTAUxWriteKey.empty()));
63
64 // multislice TOBs
67
68 // Initialize eTower handle key
69 ATH_CHECK(m_eTowerWriteKey.initialize(!m_eTowerWriteKey.empty()));
70
71 m_decoder = std::make_unique<L1CaloBsDecoderRun3>();
72
73 // Initialize monitoring tool if not empty
74 if (!m_monTool.empty()) {
75 ATH_CHECK(m_monTool.retrieve());
76 m_decoder->setLogger( std::make_unique<MonitoredLogging>(m_monTool) );
77 ATH_MSG_INFO("Logging errors to " << m_monTool.name() << " monitoring tool");
78 }
79
80
81 return StatusCode::SUCCESS;
82}
83
84void eFexByteStreamTool::MonitoredLogging::err(const std::string& location, const std::string& title, const std::string&) const {
85
87 Monitored::Scalar("efexDecoderErrorLocation",std::string("lb=") + std::to_string(Gaudi::Hive::currentContext().eventID().lumi_block()) + "," + (location.empty() ? std::string("UNKNOWN") : location)),
88 Monitored::Scalar("efexDecoderErrorTitle",title.empty() ? std::string("UNKNOWN") : title)
89 );
90
91 Monitored::Group(m_monTool, Monitored::Scalar("lbn",Gaudi::Hive::currentContext().eventID().lumi_block()),
92 Monitored::Scalar("decoderError",(title.empty() ? std::string("UNKNOWN") : title) + ":" + (location.empty() ? std::string("UNKNOWN") : location))
93 );
94}
95
96// BS->xAOD conversion
97StatusCode eFexByteStreamTool::convertFromBS(const std::vector<const ROBF*>& vrobf, const EventContext& ctx) const {
98
99 // indices are source (tob or xtob) and bool for in/out of time
100 std::map<std::pair<L1CaloRdoFexTob::TobSource,bool>,SG::WriteHandle<xAOD::eFexEMRoIContainer>> eContainers;
101 std::map<std::pair<L1CaloRdoFexTob::TobSource,bool>,SG::WriteHandle<xAOD::eFexTauRoIContainer>> tContainers;
102
103 bool multislice=false;
104
105 if (!m_eEMWriteKey.empty()) {
106 ATH_CHECK(StatusCode(addContainer<xAOD::eFexEMRoIContainer, xAOD::eFexEMRoIAuxContainer>(
107 eContainers, L1CaloRdoFexTob::TobSource::EfexTob, false, m_eEMWriteKey, ctx)));
108 }
109 if (!m_eTAUWriteKey.empty()) {
110 ATH_CHECK(StatusCode(addContainer<xAOD::eFexTauRoIContainer, xAOD::eFexTauRoIAuxContainer>(
111 tContainers, L1CaloRdoFexTob::TobSource::EfexTob, false, m_eTAUWriteKey, ctx)));
112 }
113 if (!m_eEMxWriteKey.empty()) {
114 ATH_CHECK( StatusCode(addContainer<xAOD::eFexEMRoIContainer,xAOD::eFexEMRoIAuxContainer>(
116 }
117 if (!m_eTAUxWriteKey.empty()) {
118 ATH_CHECK( StatusCode(addContainer<xAOD::eFexTauRoIContainer,xAOD::eFexTauRoIAuxContainer>(
120 }
121 if (!m_eEMSliceWriteKey.empty()) {
122 ATH_CHECK( StatusCode(addContainer<xAOD::eFexEMRoIContainer,xAOD::eFexEMRoIAuxContainer>(
124 multislice=true;
125 }
126 if (!m_eTAUSliceWriteKey.empty()) {
127 ATH_CHECK( StatusCode(addContainer<xAOD::eFexTauRoIContainer,xAOD::eFexTauRoIAuxContainer>(
129 multislice=true;
130 }
131
132
134 if(!m_eTowerWriteKey.empty()) {
136 ATH_CHECK( eTowers.record(std::make_unique<xAOD::eFexTowerContainer>(),std::make_unique<xAOD::eFexTowerAuxContainer>()) );
137 }
138
139 std::list<L1CaloRdoRodInfo> rodInfos;
140 std::list<L1CaloRdoEfexTob> efexTobs;
141 std::list<L1CaloRdoEfexTower> efexTowers;
142 L1CaloBsDecoderRun3& decoder = *m_decoder;
143 std::map<std::tuple<int,int,int,int,int>,size_t> towerMap; // maps {crate,module,fpga,eta,phi} onto index in vector
144 for (const ROBF* rob : vrobf) {
145 // Iterate over ROD words and decode
146 ATH_MSG_DEBUG("Decoding " << rob->rod_ndata() << " ROD words from ROB 0x" << std::hex << rob->rob_source_id() << std::dec);
147 L1CaloBsDecoderUtil::decodeRodInfo( rob, rodInfos );
148
149 if(rob->rod_ndata()==0) continue;
150
151 CxxUtils::span data{rob->rod_data(), rob->rod_ndata()};
152 auto lastRod = rodInfos.end(); lastRod--;
153
154 if ( (rob->rob_source_id() >> 16) == eformat::TDAQ_CALO_FEAT_EXTRACT_DAQ && !m_eTowerWriteKey.empty() ) {
155 // decoding raw input data
156 // tower et counts should be in order: PS, L1, L2, L3, Had
157 // towers with all et counts 0 will still be zero-suppressed
158 efexTowers.clear();
159 decoder.decodeEfexData(data.begin(), data.end(), efexTowers, lastRod );
160 for(auto& t : efexTowers) {
161 if (t.getLayer() != 0) continue; // do hadronic in next loop
162 towerMap[std::make_tuple(t.getCrate(),t.getModule(),t.getFpgaNumber(),t.getRegion().getEtaIndex(),t.getRegion().getPhiIndex())] = eTowers->size();
163 eTowers->push_back( std::make_unique<xAOD::eFexTower>() );
164 // in bytestream cell orders are L2,PS,L1,L3 so reorder to usual PS,L1,L2,L3 order
165 std::vector<uint16_t> counts(11,xAOD::eFexTower::c_missingCountCode);
166 counts[0] = t.getSupercells().at(4);
167 for(size_t idx = 0;idx<4;idx++) {
168 counts[idx+1] = t.getSupercells().at(idx+5); // L1
169 counts[idx+5] = t.getSupercells().at(idx); // L2
170 }
171 counts[9] = t.getSupercells().at(9);
172
173 eTowers->back()->initialize(t.getRegion().getEtaIndex()*0.1 + 0.05,2.*ROOT::Math::Pi()*(0.5 + t.getRegion().getPhiIndex() - 64*(t.getRegion().getPhiIndex()>=32))/64,
174 counts,
175 t.getModule() + t.getCrate()*12,
176 t.getFpgaNumber(),
177 t.getFlag(),0 /* hadronic status flag */);
178 }
179
180 for(auto& t : efexTowers) {
181 if (t.getLayer() == 0) continue; // do hadronic towers now ... add to existing towers
182 size_t idx = eTowers->size();
183 if(auto itr = towerMap.find({t.getCrate(), t.getModule(), t.getFpgaNumber(),t.getRegion().getEtaIndex(),t.getRegion().getPhiIndex()}); itr != towerMap.end()) {
184 idx = itr->second;
185 } else {
186 // possible that ecal tower was zero-suppressed but hcal has energy, so create such a tower
187 towerMap[std::make_tuple(t.getCrate(),t.getModule(),t.getFpgaNumber(),t.getRegion().getEtaIndex(),t.getRegion().getPhiIndex())] = eTowers->size();
188 eTowers->push_back( std::make_unique<xAOD::eFexTower>() );
189 eTowers->back()->initialize(t.getRegion().getEtaIndex()*0.1 + 0.05,2.*ROOT::Math::Pi()*(0.5 + t.getRegion().getPhiIndex() - 64*(t.getRegion().getPhiIndex()>=32))/64,
190 std::vector<uint16_t>(11,xAOD::eFexTower::c_missingCountCode),
191 t.getModule() + t.getCrate()*12,t.getFpgaNumber(),0,t.getFlag());
192 }
193 auto tower = eTowers->at(idx);
194 tower->setHad_status(t.getFlag());
195 auto et_count = tower->et_count();
196 et_count.at(10) = t.getValue();
197 tower->setEt_count(et_count);
198 }
199
200
201
202 } else if ( (rob->rob_source_id() >> 16) == eformat::TDAQ_CALO_FEAT_EXTRACT_ROI ) {
203 // decoding tobs
204 efexTobs.clear();
205 decoder.decodeEfexTobs(data.begin(), data.end(), efexTobs, lastRod);
206
207 for (const L1CaloRdoEfexTob &tob: efexTobs) {
208 // loop over slices ... create separate tobs for each, where there's a word defined
209 for (size_t slice = 0; slice < tob.numSlices(); slice++) {
210 if (!multislice && int(slice) != tob.getL1aPos())
211 continue; // ignore out-of-time slices if multiSlice option = false
212 if (tob.getWord0(slice) == 0) continue; // this tob isn't in this slice
213 if (tob.getTobType() == L1CaloRdoFexTob::TobType::EM) {
214 auto cont = eContainers.find({tob.getTobSource(), int(slice) != tob.getL1aPos()});
215 if (cont == eContainers.end()) continue; // not writing this tob collection
216 cont->second->push_back(std::make_unique<xAOD::eFexEMRoI>());
217 if (tob.getTobSource() == L1CaloRdoFexTob::TobSource::EfexTob) {
218 cont->second->back()->initialize(tob.getModule(), tob.getCrate(), tob.getWord0(slice));
219 } else {
220 cont->second->back()->initialize(tob.getWord0(slice), tob.getWord1(slice));
221 }
222 } else if (tob.getTobType() == L1CaloRdoFexTob::TobType::Tau) {
223 auto cont = tContainers.find({tob.getTobSource(), int(slice) != tob.getL1aPos()});
224 if (cont == tContainers.end()) continue; // not writing this tob collection
225 cont->second->push_back(std::make_unique<xAOD::eFexTauRoI>());
226 if (tob.getTobSource() == L1CaloRdoFexTob::TobSource::EfexTob) {
227 cont->second->back()->initialize(tob.getModule(), tob.getCrate(), tob.getWord0(slice));
228 } else {
229 cont->second->back()->initialize(tob.getWord0(slice), tob.getWord1(slice));
230 }
231 }
232 } // timeslice loop
233 } // tob loop
234 } // tob data if block
235 } // fragment loop
236
237 if(msgLevel(MSG::DEBUG)) {
238 std::stringstream msg;
239 for (auto&[k, v]: eContainers) {
240 msg << v->size() << " " << (k.second ? "out-of-time " : "in-time ") << "eg" <<
241 (k.first == L1CaloRdoFexTob::TobSource::EfexTob ? "" : "x") << "TOB, ";
242 }
243 for (auto&[k, v]: tContainers) {
244 msg << v->size() << " " << (k.second ? "out-of-time " : "in-time ") << "tau" <<
245 (k.first == L1CaloRdoFexTob::TobSource::EfexTob ? "" : "x") << "TOB, ";
246 }
247 ATH_MSG_DEBUG("Decoded: " << msg.str());
248 if (!m_eTowerWriteKey.empty()) {
249 ATH_MSG_DEBUG("Decoded: " << eTowers->size() << " eTowers");
250 }
251 }
252 // this is how to print the in-time em xTOBs:
253// for(const auto& tob : *(eContainers[{L1CaloRdoFexTob::TobSource::EfexXtob,false}])) {
254// std::cout << tob->eFexNumber() << "." << tob->fpga() << ": " << tob->fpgaEta() << " " << tob->fpgaPhi() << " " << tob->et() << " " << tob->etXTOB() << " " << tob->etTOB() << " " << std::endl;
255// }
256
257 return StatusCode::SUCCESS;
258}
259
260StatusCode eFexByteStreamTool::convertToBS(std::vector<OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment*>& , const EventContext& ) {
261 // not supported yet
262 return StatusCode::FAILURE;
263}
264
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment ROBF
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
Simplified version of the C++20 std::span.
Definition span.h:61
Decoder for Run 3 data formats: eFEX, jFEX, gFEX and phase 1 Topo.
static void decodeRodInfo(const eformat::ROBFragment< const uint32_t * > *rod, std::list< L1CaloRdoRodInfo > &dat)
L1CaloRdoFexTob subclass for eFEX EM/Tau TOBs or XTOBs.
Group of local monitoring quantities and retain correlation when filling histograms
Declare a monitored scalar variable.
bool empty() const
Test if the key is blank.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ToolHandle< GenericMonitoringTool > m_monTool
virtual void err(const std::string &location, const std::string &title, const std::string &detail) const override
SG::WriteHandleKey< xAOD::eFexTowerContainer > m_eTowerWriteKey
SG::WriteHandleKey< xAOD::eFexTauRoIContainer > m_eTAUxWriteKey
SG::WriteHandleKey< xAOD::eFexTauRoIContainer > m_eTAUWriteKey
std::unique_ptr< L1CaloBsDecoderRun3 > m_decoder
virtual StatusCode convertToBS(std::vector< OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment * > &vrobf, const EventContext &eventContext) override
xAOD->BS conversion
SG::ReadHandleKey< xAOD::eFexTauRoIContainer > m_eTAUReadKey
virtual StatusCode convertFromBS(const std::vector< const OFFLINE_FRAGMENTS_NAMESPACE::ROBFragment * > &vrobf, const EventContext &eventContext) const override
BS->xAOD conversion.
SG::ReadHandleKey< xAOD::eFexEMRoIContainer > m_eEMReadKey
SG::WriteHandleKey< xAOD::eFexEMRoIContainer > m_eEMxWriteKey
eFexByteStreamTool(const std::string &type, const std::string &name, const IInterface *parent)
SG::WriteHandleKey< xAOD::eFexEMRoIContainer > m_eEMSliceWriteKey
SG::WriteHandleKey< xAOD::eFexTauRoIContainer > m_eTAUSliceWriteKey
ToolHandle< GenericMonitoringTool > m_monTool
virtual StatusCode initialize() override
SG::WriteHandleKey< xAOD::eFexEMRoIContainer > m_eEMWriteKey
STL class.
static const int c_missingCountCode
OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment WROBF
eformat::write::ROBFragment ROBFragment
Definition RawEvent.h:33
eformat::ROBFragment< PointerType > ROBFragment
Definition RawEvent.h:27
Simplified version of the C++20 std::span.
MsgStream & msg
Definition testRead.cxx:32