ATLAS Offline Software
ByteStreamCnvSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "ByteStreamCnvSvc.h"
9 
10 #include "StoreGate/StoreGateSvc.h"
13 
14 #include "eformat/SourceIdentifier.h"
15 #include "eformat/StreamTag.h"
16 
17 #include "GaudiKernel/IClassIDSvc.h"
18 
20 
21 #include <algorithm>
22 
23 namespace {
25  std::set<eformat::SubDetector> detsOnline(const std::set<uint32_t>& detsOffline) {
26  std::set<eformat::SubDetector> out;
27  for (const uint32_t detid : detsOffline)
28  out.insert(static_cast<eformat::SubDetector>(detid));
29  return out;
30  }
31 }
32 
34 ByteStreamCnvSvc::ByteStreamCnvSvc(const std::string& name, ISvcLocator* pSvcLocator)
35  : base_class(name, pSvcLocator),
36  m_evtStore ("StoreGateSvc", name)
37 {
38  declareProperty("ByteStreamOutputSvc", m_ioSvcName);
39  declareProperty("UserType", m_userType = "RawEvent");
40  declareProperty("EventStore", m_evtStore);
41 }
42 
45 }
46 
48 {
49  const EventContext& ctx = Gaudi::Hive::currentContext();
50  const auto& feaMap = m_slots.get(ctx)->m_feaMap;
51  const auto& fea = feaMap.find(name);
52  return fea!=feaMap.end() ? fea->second.get() : nullptr;
53 }
54 
55 StatusCode ByteStreamCnvSvc::storeFullEventAssembler(std::unique_ptr<FullEventAssemblerBase> fea, const std::string& name)
56 {
57  const EventContext& ctx = Gaudi::Hive::currentContext();
58  m_slots.get(ctx)->m_feaMap[name] = std::move(fea);
59  return StatusCode::SUCCESS;
60 }
61 
64  if (!ByteStreamCnvSvcBase::initialize().isSuccess()) {
65  ATH_MSG_FATAL("ByteStreamCnvSvcBase::initialize() failed");
66  return(StatusCode::FAILURE);
67  }
68 
69  ATH_CHECK( m_evtStore.retrieve() );
70 
71  // get ready for output
72  std::vector<std::string> ioSvcNames = m_ioSvcNameList.value();
73  if (!m_ioSvcName.empty()) {
74  // add ioSvcName if ioSvcNameList is missing it
75  std::vector<std::string>::iterator it = find(ioSvcNames.begin(), ioSvcNames.end(), m_ioSvcName);
76  if (it == ioSvcNames.end()) {
77  ioSvcNames.push_back(m_ioSvcName);
78  }
79  }
80  if (!ioSvcNames.empty()) {
81  // Check ByteStreamCnvSvc
82  for (const std::string& svcName : ioSvcNames) {
83  ATH_MSG_DEBUG("Retrieving " << svcName);
84  // get service
85  SmartIF<IByteStreamOutputSvc> ioSvc{service(svcName)};
86  ATH_CHECK( ioSvc.isValid() );
87 
88  // get stream name
89  std::string bsOutputStreamName;
90  SimpleProperty<std::string> propBSO("BSOutputStreamName", bsOutputStreamName);
91  ATH_CHECK(ioSvc.as<IProperty>()->getProperty(&propBSO));
92  bsOutputStreamName = propBSO.value();
93  // append
94  m_ioSvcMap[bsOutputStreamName] = ioSvc;
95  }
96  }
97  return(StatusCode::SUCCESS);
98 }
99 
102 }
103 
104 StatusCode ByteStreamCnvSvc::connectOutput(const std::string& t, const std::string& /*mode*/) {
105  return(connectOutput(t));
106 }
107 
108 StatusCode ByteStreamCnvSvc::connectOutput(const std::string& /*t*/) {
109  ATH_MSG_DEBUG("In connectOutput");
110 
111  const EventContext& ctx = Gaudi::Hive::currentContext();
112  SlotData& slot = *m_slots.get(ctx);
113 
114  // Get the EventInfo obj for run/event number
115  const xAOD::EventInfo* evtInfo{nullptr};
116  ATH_CHECK( m_evtStore->retrieve(evtInfo) );
117  uint64_t event = evtInfo->eventNumber();
118  uint32_t run_no = evtInfo->runNumber();
119  uint32_t bc_time_sec = evtInfo->timeStamp();
120  uint32_t bc_time_ns = evtInfo->timeStampNSOffset();
121  uint32_t run_type = 0;
122  uint32_t lvl1_id = evtInfo->extendedLevel1ID();
123  if (lvl1_id == 0) {
124  lvl1_id = event;
125  }
126  uint8_t lvl1_type = evtInfo->level1TriggerType();
127  uint64_t global_id = event;
128  uint16_t lumi_block = evtInfo->lumiBlock();
129  uint16_t bc_id = evtInfo->bcid();
130  uint8_t nevt = 0;
131  // create an empty RawEvent
132  eformat::helper::SourceIdentifier sid = eformat::helper::SourceIdentifier(eformat::FULL_SD_EVENT, nevt);
133  RawEventWrite* re = setRawEvent (std::make_unique<RawEventWrite>(sid.code(), bc_time_sec, bc_time_ns, global_id, run_type, run_no, lumi_block, lvl1_id, bc_id, lvl1_type));
134 
135  // set stream tags
136  std::vector<eformat::helper::StreamTag> on_streamTags;
137  for (const auto& sTag : evtInfo->streamTags()) {
138  on_streamTags.emplace_back(sTag.name(), sTag.type(), sTag.obeysLumiblock(), sTag.robs(), detsOnline(sTag.dets()));
139  }
140  uint32_t nStreamTagWords = eformat::helper::size_word(on_streamTags);
141  slot.m_tagBuff.resize (nStreamTagWords);
142  eformat::helper::encode(on_streamTags, nStreamTagWords, slot.m_tagBuff.data());
143  re->stream_tag(nStreamTagWords, slot.m_tagBuff.data());
144 
145  // Nothing left to do, unless processing trigger bits
146  if (!m_fillTriggerBits.value()) {
147  return StatusCode::SUCCESS;
148  }
149 
150  // try to get TrigDecision
151  const xAOD::TrigDecision *trigDecision{nullptr};
152  if (m_evtStore->retrieve(trigDecision, "xTrigDecision") != StatusCode::SUCCESS) {
153  ATH_MSG_WARNING("Property " << m_fillTriggerBits.name() << " set to True but failed to retrieve xAOD::TrigDecision. Leaving empty trigger bits in the event header.");
154  return StatusCode::SUCCESS;
155  }
156 
157  // LVL1 info
158  const std::vector<uint32_t> &tbp = trigDecision->tbp();
159  const std::vector<uint32_t> &tap = trigDecision->tap();
160  const std::vector<uint32_t> &tav = trigDecision->tav();
161  const size_t l1TotSize = tbp.size() + tap.size() + tav.size();
162  if (l1TotSize > 0) {
163  slot.m_l1Buff.resize (l1TotSize);
164  size_t l1Size{0};
165  for (const uint32_t tb : tbp) {
166  slot.m_l1Buff[l1Size++] = tb;
167  }
168  for (const uint32_t tb : tap) {
169  slot.m_l1Buff[l1Size++] = tb;
170  }
171  for (const uint32_t tb : tav) {
172  slot.m_l1Buff[l1Size++] = tb;
173  }
174  re->lvl1_trigger_info(l1TotSize, slot.m_l1Buff.data());
175  }
176 
177  // LVL2 info
178  const std::vector<uint32_t>& lvl2PP = trigDecision->lvl2PassedPhysics();
179  if (lvl2PP.size() > 0) {
180  slot.m_l2Buff = lvl2PP;
181  re->lvl2_trigger_info(lvl2PP.size(), slot.m_l2Buff.data());
182  }
183 
184  // EF info
185  const std::vector<uint32_t>& efPP = trigDecision->efPassedPhysics();
186  if (efPP.size() > 0) {
187  slot.m_efBuff = efPP;
188  re->event_filter_info(efPP.size(), slot.m_efBuff.data());
189  }
190 
191  return(StatusCode::SUCCESS);
192 }
193 
194 StatusCode ByteStreamCnvSvc::commitOutput(const std::string& outputConnection, bool /*b*/) {
195  ATH_MSG_DEBUG("In flushOutput " << outputConnection);
196 
197  const EventContext& ctx = Gaudi::Hive::currentContext();
198  SlotData& slot = *m_slots.get(ctx);
199 
200  if (m_ioSvcMap.size() == 0) {
201  ATH_MSG_ERROR("ByteStreamCnvSvc not configure for output");
202  return(StatusCode::FAILURE);
203  }
204 
205  writeFEA (slot);
206 
207  // convert RawEventWrite to RawEvent
208  RawEventWrite* re = slot.m_rawEventWrite.get();
209  uint32_t rawSize = re->size_word();
210  std::vector<uint32_t> buffer (rawSize);
211  uint32_t count = eformat::write::copy(*(re->bind()), buffer.data(), rawSize);
212  if (count != rawSize) {
213  ATH_MSG_ERROR("Memcopy failed");
214  return(StatusCode::FAILURE);
215  }
216  RawEvent rawEvent(buffer.data());
217  // check validity
218  try {
219  rawEvent.check_tree();
220  } catch (...) {
221  ATH_MSG_ERROR("commitOutput failed, because FullEventFragment invalid");
222  return(StatusCode::FAILURE);
223  }
224  ATH_MSG_DEBUG("commitOutput: Size of Event (words) = " << rawEvent.fragment_size_word());
225  // put event to OutputSvc
226  if ((m_ioSvcMap.size() == 1) or (m_ioSvcMap.count(outputConnection) > 0)) {
228  // for backward compatibility
229  if (itSvc == m_ioSvcMap.end()) {
230  itSvc = m_ioSvcMap.begin();
231  }
232  // put
233  if (!itSvc->second->putEvent(&rawEvent)) {
234  ATH_MSG_ERROR("commitOutput failed to send output");
235  return(StatusCode::FAILURE);
236  }
237  }
238  // Clear slot-specific data.
239  slot.clear();
240  return(StatusCode::SUCCESS);
241 }
242 
244 {
245  const auto& feaMap = slot.m_feaMap;
246  ATH_MSG_DEBUG("before FEAMAP size = " << feaMap.size());
247  for (auto& p : feaMap) {
248  MsgStream log(msgSvc(), name());
249  p.second->fill(slot.m_rawEventWrite.get(), log);
250  }
251  ATH_MSG_DEBUG("after FEAMAP size = " << feaMap.size());
252 }
253 
255 ByteStreamCnvSvc::setRawEvent (std::unique_ptr<RawEventWrite> rawEventWrite)
256 {
257  RawEventWrite* ptr = rawEventWrite.get();
258  m_slots->m_rawEventWrite = std::move (rawEventWrite);
259  // cppcheck-suppress returnDanglingLifetime
260  return ptr;
261 }
262 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ByteStreamCnvSvc::commitOutput
virtual StatusCode commitOutput(const std::string &outputConnection, bool b) override
Implements ConversionSvc's commitOutput.
Definition: ByteStreamCnvSvc.cxx:194
ByteStreamCnvSvc::SlotData
Slot-specific state.
Definition: ByteStreamCnvSvc.h:90
RawEventWrite
OFFLINE_FRAGMENTS_NAMESPACE_WRITE::FullEventFragment RawEventWrite
data type for writing raw event
Definition: RawEvent.h:39
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ByteStreamCnvSvc::SlotData::m_feaMap
std::map< std::string, std::unique_ptr< FullEventAssemblerBase > > m_feaMap
Definition: ByteStreamCnvSvc.h:92
ByteStreamCnvSvc::SlotData::m_efBuff
std::vector< uint32_t > m_efBuff
Definition: ByteStreamCnvSvc.h:96
ByteStreamCnvSvc::SlotData::clear
void clear()
Definition: ByteStreamCnvSvc.h:98
TrigDecision.h
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:557
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
ByteStreamCnvSvc::~ByteStreamCnvSvc
virtual ~ByteStreamCnvSvc()
Standard Destructor.
Definition: ByteStreamCnvSvc.cxx:44
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
RawEvent
OFFLINE_FRAGMENTS_NAMESPACE::FullEventFragment RawEvent
data type for reading raw event
Definition: RawEvent.h:37
skel.it
it
Definition: skel.GENtoEVGEN.py:396
ByteStreamCnvSvc::m_evtStore
ServiceHandle< StoreGateSvc > m_evtStore
Event store.
Definition: ByteStreamCnvSvc.h:83
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
ByteStreamCnvSvc::m_slots
SG::SlotSpecificObj< SlotData > m_slots
Definition: ByteStreamCnvSvc.h:108
ByteStreamCnvSvc::ByteStreamCnvSvc
ByteStreamCnvSvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
Definition: ByteStreamCnvSvc.cxx:34
RunTileTBRec.run_type
run_type
Definition: RunTileTBRec.py:65
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
ByteStreamCnvSvc::SlotData::m_l1Buff
std::vector< uint32_t > m_l1Buff
Definition: ByteStreamCnvSvc.h:94
xAOD::tap
setBGCode tap
Definition: TrigDecision_v1.cxx:43
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
ByteStreamCnvSvc::m_ioSvcName
std::string m_ioSvcName
name of the service
Definition: ByteStreamCnvSvc.h:71
Execution.tb
tb
Definition: Execution.py:15
FullEventAssemblerBase
base class for assembling a full atlas raw event from subfragments
Definition: FullEventAssembler.h:23
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
ByteStreamCnvSvc::findFullEventAssembler
virtual FullEventAssemblerBase * findFullEventAssembler(const std::string &name) const override
Implementation of IByteStreamCnvSvc interface.
Definition: ByteStreamCnvSvc.cxx:47
IByteStreamOutputSvc.h
This file contains the interface for the ByteStreamOutputSvc classes.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
AtlasMcWeight::encode
number_type encode(double weight)
Definition: AtlasMcWeight.cxx:65
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ByteStreamCnvSvc::SlotData::m_l2Buff
std::vector< uint32_t > m_l2Buff
Definition: ByteStreamCnvSvc.h:95
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
ByteStreamCnvSvc::SlotData::m_rawEventWrite
std::unique_ptr< RawEventWrite > m_rawEventWrite
Definition: ByteStreamCnvSvc.h:91
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ByteStreamCnvSvc::m_userType
std::string m_userType
user type
Definition: ByteStreamCnvSvc.h:86
ByteStreamCnvSvc::initialize
virtual StatusCode initialize() override
Gaudi Service Interface method implementations:
Definition: ByteStreamCnvSvc.cxx:63
ByteStreamAddress.h
ByteStreamCnvSvc.h
FullEventAssembler.h
ByteStreamCnvSvc::finalize
virtual StatusCode finalize() override
Definition: ByteStreamCnvSvc.cxx:100
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
ActsGeantFollowing_jobOptions_ITk.svcName
svcName
Definition: ActsGeantFollowing_jobOptions_ITk.py:196
ByteStreamCnvSvc::writeFEA
void writeFEA(SlotData &slot)
Write the FEA to RawEvent.
Definition: ByteStreamCnvSvc.cxx:243
ByteStreamCnvSvc::m_fillTriggerBits
Gaudi::Property< bool > m_fillTriggerBits
fill trigger bits
Definition: ByteStreamCnvSvc.h:77
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ByteStreamCnvSvc::SlotData::m_tagBuff
std::vector< uint32_t > m_tagBuff
Definition: ByteStreamCnvSvc.h:93
re
const boost::regex re(r_e)
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
ByteStreamCnvSvc::storeFullEventAssembler
virtual StatusCode storeFullEventAssembler(std::unique_ptr< FullEventAssemblerBase > fea, const std::string &name) override
Definition: ByteStreamCnvSvc.cxx:55
calibdata.copy
bool copy
Definition: calibdata.py:27
ByteStreamCnvSvc::setRawEvent
RawEventWrite * setRawEvent(std::unique_ptr< RawEventWrite > rawEventWrite)
Definition: ByteStreamCnvSvc.cxx:255
ByteStreamCnvSvc::connectOutput
virtual StatusCode connectOutput(const std::string &t, const std::string &mode) override
Implements ConversionSvc's connectOutput.
Definition: ByteStreamCnvSvc.cxx:104
ByteStreamCnvSvc::m_ioSvcNameList
Gaudi::Property< std::vector< std::string > > m_ioSvcNameList
list of service names
Definition: ByteStreamCnvSvc.h:74
StoreGateSvc.h
ByteStreamCnvSvc::m_ioSvcMap
std::map< std::string, IByteStreamOutputSvc * > m_ioSvcMap
Services for writing output.
Definition: ByteStreamCnvSvc.h:80
xAOD::TrigDecision_v1
Interface to the raw trigger decision information of the event.
Definition: TrigDecision_v1.h:44
ByteStreamCnvSvcBase::initialize
virtual StatusCode initialize() override
Required of all Gaudi Services.
Definition: ByteStreamCnvSvcBase.cxx:27