ATLAS Offline Software
Loading...
Searching...
No Matches
EventInfoByteStreamAuxCnv.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
11
15
17#include "GaudiKernel/MsgStream.h"
18#include "GaudiKernel/StatusCode.h"
19#include "GaudiKernel/DataObject.h"
20#include "GaudiKernel/IRegistry.h"
21
24
26
27#include "eformat/StreamTag.h"
28
29#include <time.h>
30
32 : Converter(storageType(), classID(), svcloc)
33 , AthMessaging(svcloc != nullptr ? msgSvc() : nullptr, "EventInfoByteStreamAuxCnv")
34 , m_robDataProvider("ROBDataProviderSvc", "EventInfoByteStreamAuxCnv")
35 , m_mdSvc("InputMetaDataStore", "EventInfoByteStreamAuxCnv")
36 , m_isSimulation(false)
37 , m_isTestbeam(false)
38 , m_isCalibration(false)
39{
40}
41
46
51
53{
54 ATH_MSG_DEBUG("Initialize");
55
56 CHECK(Converter::initialize());
57
58 CHECK(m_robDataProvider.retrieve());
59 CHECK(m_mdSvc.retrieve());
60
61 SmartIF<IProperty> byteStreamCnvSvc(service("ByteStreamCnvSvc"));
62 CHECK( byteStreamCnvSvc.isValid() );
63
64 SimpleProperty<bool> propIsSimulation("IsSimulation", m_isSimulation);
65 StatusCode sc = byteStreamCnvSvc->getProperty(&propIsSimulation);
66 if (sc.isSuccess()) {
67 m_isSimulation = propIsSimulation.value();
68 ATH_MSG_INFO("IsSimulation : " << m_isSimulation);
69 }
70 else {
71 ATH_MSG_ERROR("Cannot get IsSimulation");
72 return sc;
73 }
74
75 SimpleProperty<bool> propIsTestbeam("IsTestbeam", m_isTestbeam);
76 sc = byteStreamCnvSvc->getProperty(&propIsTestbeam);
77 if (sc.isSuccess()) {
78 m_isTestbeam = propIsTestbeam.value();
79 ATH_MSG_INFO("IsTestbeam : " << m_isTestbeam);
80 }
81 else {
82 ATH_MSG_ERROR("Cannot get IsTestbeam");
83 return sc;
84 }
85
86 SimpleProperty<bool> propIsCalibration("IsCalibration", m_isCalibration);
87 sc = byteStreamCnvSvc->getProperty(&propIsCalibration);
88 if (sc.isSuccess()) {
89 m_isCalibration = propIsCalibration.value();
90 ATH_MSG_INFO("IsCalibration : " << m_isCalibration);
91 }
92 else {
93 ATH_MSG_ERROR("Cannot get IsCalibration");
94 return sc;
95 }
96
97 // Retrieve MC EventInfo decoder tool (optional - only needed for MC BS files)
98 if (m_isSimulation) {
99 if (m_mcEventInfoTool.retrieve().isFailure()) {
100 ATH_MSG_WARNING("Failed to retrieve MCEventInfoByteStreamTool - MC EventInfo will use default values");
101 m_mcEventInfoTool.disable();
102 } else {
103 ATH_MSG_DEBUG("MCEventInfoByteStreamTool retrieved successfully");
104 }
105 } else {
106 m_mcEventInfoTool.disable();
107 }
108
109 return StatusCode::SUCCESS;
110}
111
113{
114 ATH_MSG_DEBUG("Finalize");
115
116 StatusCode sc = Converter::finalize();
117 if (sc.isFailure()) {
118 ATH_MSG_WARNING("Converter::finalize() failed");
119 }
120 return sc;
121}
122
123StatusCode EventInfoByteStreamAuxCnv::createObj(IOpaqueAddress* pAddr, DataObject*& pObj)
124{
125 ByteStreamAddress* pRE_Addr{};
126 ATH_CHECK( (pRE_Addr = dynamic_cast<ByteStreamAddress*>(pAddr)) != nullptr );
127
128 const EventContext& ctx = pRE_Addr->getEventContext();
129 ATH_MSG_DEBUG("Creating Objects");
130
131 // get RawEvent
132 const RawEvent* re = m_robDataProvider->getEvent(ctx);
133 if (!re) {
134 ATH_MSG_ERROR("Can not get RawEvent ");
135 return StatusCode::FAILURE;
136 }
137
138 // Run Number
139 int runNumber = re->run_no();
140
141 // Event Number
142 uint64_t eventNumber;
143 if (re->version() < 0x03010000) {
144 eventNumber=re->lvl1_id();
145 } else {
146 eventNumber=re->global_id();
147 }
148
149 // Time Stamp
150 uint32_t bc_time_sec = re->bc_time_seconds();
151 uint32_t bc_time_ns = re->bc_time_nanoseconds();
152 // bc_time_ns should be lt 1e9.
153 if (bc_time_ns > 1000000000) {
154 if (runNumber < 20920) {
155 // In M4 we saw otherwise, because sec/ns was swapped in raw data.
156 uint32_t temp = bc_time_ns;
157 bc_time_ns = bc_time_sec;
158 bc_time_sec= temp;
159 ATH_MSG_DEBUG("bc_time second/nanosecond swapped, sec/ns = " << bc_time_sec << " " << bc_time_ns);
160 }
161 else {
162 // For later runs, the nanosecond clock sometimes is not reset, making it overrun 1e9. Round it off to 1e9
163 ATH_MSG_WARNING("bc_time nanosecond number larger than 1e9, it is " << bc_time_ns << ", reset it to 1 sec");
164 bc_time_ns = 1000000000;
165 }
166 }
167
168 // luminosity block number
169 uint16_t lumiBlock = re->lumi_block();
170
171 // bunch crossing identifier
172 uint16_t bcID = re->bc_id();
173
174 unsigned int detMask0 = 0xFFFFFFFF, detMask1 = 0xFFFFFFFF, detMask2 = 0xFFFFFFFF, detMask3 = 0xFFFFFFFF;
175 // Get ByteStream Metadata from Input MetaData Store
176 const ByteStreamMetadataContainer* metadatacont{nullptr};
177 StatusCode status = m_mdSvc->retrieve(metadatacont, "ByteStreamMetadata");
178 if (!status.isSuccess()) {
179 ATH_MSG_WARNING("Unable to retrieve Input MetaData for ByteStream");
180 }
181 else {
182 const ByteStreamMetadata* metadata = *(metadatacont->begin());
183 uint64_t detectorMask = metadata->getDetectorMask();
184 detMask0 = (unsigned int)(detectorMask & 0x00000000FFFFFFFF);
185 detMask1 = (unsigned int)(detectorMask >> 32);
186 uint64_t detectorMask2 = metadata->getDetectorMask2();
187 detMask2 = (unsigned int)(detectorMask2 & 0x00000000FFFFFFFF);
188 detMask3 = (unsigned int)(detectorMask2 >> 32);
189 }
190
191
192 xAOD::EventInfo evtInfo;
193 xAOD::EventAuxInfo* pEvtInfoAux = new xAOD::EventAuxInfo();
194 evtInfo.setStore(pEvtInfoAux);
195
196 evtInfo.setRunNumber(runNumber);
197 evtInfo.setEventNumber(eventNumber);
198 evtInfo.setLumiBlock(lumiBlock);
199 evtInfo.setTimeStamp(bc_time_sec);
200 evtInfo.setTimeStampNSOffset(bc_time_ns);
201 evtInfo.setBCID(bcID);
202 evtInfo.setDetectorMask(detMask0,detMask1);
203 evtInfo.setDetectorMaskExt(detMask2,detMask3);
204
205 // Set default MC values (will be overwritten if MC ROB is present)
206 evtInfo.setMCChannelNumber(0);
207 evtInfo.setMCEventNumber(0);
208 evtInfo.setMCEventWeights(std::vector<float>(1,1));
209
210 // Set Event Type
211 uint32_t eventTypeBitmask{0};
212 if (m_isSimulation) {
213 eventTypeBitmask |= xAOD::EventInfo::IS_SIMULATION;
214 }
215 if (m_isTestbeam) {
216 eventTypeBitmask |= xAOD::EventInfo::IS_TESTBEAM;
217 }
218 if (m_isCalibration) {
219 eventTypeBitmask |= xAOD::EventInfo::IS_CALIBRATION;
220 }
221 evtInfo.setEventTypeBitmask(eventTypeBitmask);
222
223 // Try to decode MC EventInfo from dedicated ROB fragment (for MC ByteStream files)
224 bool mcEventInfoDecoded = false;
225 if (m_isSimulation && m_mcEventInfoTool.isEnabled()) {
226 // Request the MC EventInfo ROB
227 std::vector<uint32_t> mcRobIds = m_mcEventInfoTool->robIds();
228 std::vector<const OFFLINE_FRAGMENTS_NAMESPACE::ROBFragment*> mcRobFragments;
229 m_robDataProvider->getROBData(ctx, mcRobIds, mcRobFragments, "EventInfoByteStreamAuxCnv");
230
231 if (!mcRobFragments.empty()) {
232 ATH_MSG_DEBUG("Found MC EventInfo ROB fragment, decoding...");
233 StatusCode sc = m_mcEventInfoTool->convertFromBS(mcRobFragments[0], evtInfo);
234 if (sc.isFailure()) {
235 ATH_MSG_WARNING("Failed to decode MC EventInfo from ROB fragment, using default values");
236 } else {
237 ATH_MSG_DEBUG("Successfully decoded MC EventInfo from ROB fragment");
238 mcEventInfoDecoded = true;
239 }
240 } else {
241 ATH_MSG_DEBUG("No MC EventInfo ROB fragment found, using default values");
242 }
243 }
244
245 // Trigger Info
247 // status element
248 re->status(buffer);
249 uint32_t statusElement = *buffer;
250
251 // extended LVL1ID
252 uint32_t extendedLevel1ID = re->lvl1_id();
253
254 // LVL1 trigger type
255 uint32_t level1TriggerType = re->lvl1_trigger_type();
256
257 // stream tag
258 std::vector<xAOD::EventInfo::StreamTag> streamTags;
259 std::vector<eformat::helper::StreamTag> onl_streamTags;
260 re->stream_tag(buffer);
261 eformat::helper::decode(re->nstream_tag(), buffer, onl_streamTags);
262 for (const eformat::helper::StreamTag& onl_streamTag : onl_streamTags) {
263 std::set<uint32_t> tmp_off_dets = std::set<uint32_t>();
264 if (!onl_streamTag.dets.empty()) {
265 std::set<eformat::SubDetector> tmp_onl_dets = onl_streamTag.dets;
266 for (const eformat::SubDetector& subdet : tmp_onl_dets) {
267 tmp_off_dets.insert((uint32_t) subdet);
268 }
269 }
270 streamTags.push_back(xAOD::EventInfo::StreamTag(onl_streamTag.name
271 , onl_streamTag.type
272 , onl_streamTag.obeys_lumiblock
273 , onl_streamTag.robs
274 , tmp_off_dets)
275 );
276 }
277
278 evtInfo.setStatusElement(statusElement);
279 // Only set extendedLevel1ID from RawEvent header if MC EventInfo wasn't decoded
280 // (MC EventInfo ROB contains the original extendedLevel1ID value)
281 if (!mcEventInfoDecoded) {
282 evtInfo.setExtendedLevel1ID(extendedLevel1ID);
283 }
284 evtInfo.setLevel1TriggerType(level1TriggerType);
285 evtInfo.setStreamTags(streamTags);
286
287 // record EventInfo
288 evtInfo.setEventFlags(xAOD::EventInfo::Core, m_robDataProvider->getEventStatus(ctx));
289 pObj = SG::asStorable(pEvtInfoAux);
290
291 ATH_MSG_DEBUG(" New xAOD::EventAuxInfo made, run/event= " << runNumber
292 << " " << eventNumber
293 << " Time stamp = " << ascTime(bc_time_sec)
294 );
295
296 return StatusCode::SUCCESS;
297}
298
299StatusCode EventInfoByteStreamAuxCnv::createRep(DataObject* /*pObj*/, IOpaqueAddress*& /*pAddr*/)
300{
301 ATH_MSG_DEBUG("createRep for xAOD::EventAuxInfo");
302
303 // For MC ByteStream files, create the MC EventInfo ROB fragment
304 if (m_isSimulation && m_mcEventInfoTool.isEnabled()) {
305 ATH_MSG_DEBUG("Creating MC EventInfo ROB fragment for simulation");
306
307 // Get RawEventWrite via ByteStreamCnvSvc
308 SmartIF<IByteStreamEventAccess> byteStreamCnvSvc(service("ByteStreamCnvSvc"));
309 if (!byteStreamCnvSvc.isValid()) {
310 ATH_MSG_ERROR("Failed to retrieve ByteStreamCnvSvc");
311 return StatusCode::FAILURE;
312 }
313 RawEventWrite* re = byteStreamCnvSvc->getRawEvent();
314 if (!re) {
315 ATH_MSG_ERROR("Failed to get RawEventWrite");
316 return StatusCode::FAILURE;
317 }
318
319 // Create MC EventInfo ROB fragment
320 std::vector<OFFLINE_FRAGMENTS_NAMESPACE_WRITE::ROBFragment*> mcRobs;
321 ATH_CHECK(m_mcEventInfoTool->convertToBS(mcRobs, Gaudi::Hive::currentContext()));
322
323 // Append ROB fragments to the event
324 for (auto* rob : mcRobs) {
325 // Set LVL1 ID and trigger type from the full event
326 rob->rod_lvl1_id(re->lvl1_id());
327 rob->rod_lvl1_type(re->lvl1_trigger_type());
328 re->append(rob);
329 ATH_MSG_DEBUG("Added MC EventInfo ROB fragment to output event");
330 }
331 }
332
333 return StatusCode::SUCCESS;
334}
335
336std::string EventInfoByteStreamAuxCnv::ascTime(unsigned int tstamp)
337{
338 struct tm t;
339 t.tm_sec = tstamp;
340 t.tm_min = 0;
341 t.tm_hour = 0;
342 t.tm_mday = 0;
343 t.tm_mon = 0;
344 t.tm_year = 70;
345 t.tm_wday = 00;
346 t.tm_yday = 00;
347 t.tm_isdst = 0;
348 time_t ct = mktime(&t);
349 struct tm t2;
350 gmtime_r(&ct, &t2);
351 char buf[32];
352 asctime_r (&t2, buf);
353 return std::string (buf);
354}
const boost::regex re(r_e)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
This file contains the class definition for the ByteStreamMetadataContainer class.
This file contains the class definition for the ByteStreamMetadata class.
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
uint32_t CLID
The Class ID type.
static Double_t sc
Tool for encoding/decoding MC EventInfo to/from ByteStream.
OFFLINE_FRAGMENTS_NAMESPACE_WRITE::FullEventFragment RawEventWrite
data type for writing raw event
Definition RawEvent.h:39
OFFLINE_FRAGMENTS_NAMESPACE::FullEventFragment RawEvent
data type for reading raw event
Definition RawEvent.h:37
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
IOpaqueAddress for ByteStreamCnvSvc, with ROB ids.
const EventContext & getEventContext() const
static constexpr long storageType()
This class is the StoreGate data object for bytestream metadata.
This class is the StoreGate data object for bytestream metadata.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
virtual StatusCode createObj(IOpaqueAddress *pAddr, DataObject *&pObj) override
converter method to create object
virtual StatusCode createRep(DataObject *pObj, IOpaqueAddress *&pAddr) override
converter method to write object
virtual StatusCode finalize() override
std::string ascTime(unsigned int t)
convert timestamp to ascii time.
ServiceHandle< IROBDataProviderSvc > m_robDataProvider
RODDataProviderSvc handle.
virtual StatusCode initialize() override
ServiceHandle< StoreGateSvc > m_mdSvc
TDS handle.
EventInfoByteStreamAuxCnv(ISvcLocator *svcloc)
ToolHandle< IMCEventInfoByteStreamTool > m_mcEventInfoTool
Tool for decoding MC EventInfo from ROB.
Class describing a stream tag on the event.
void setBCID(uint32_t value)
Set the bunch crossing ID of the event.
void setDetectorMask(uint32_t mask0, uint32_t mask1)
Set the bit fields indicating with TTC timezones were present.
void setTimeStampNSOffset(uint32_t value)
Set the nanosecond offset wrt. the time stamp.
void setMCEventNumber(uint64_t value)
Set the MC generator's event number.
@ Core
Core flags describing the event.
void setStreamTags(const std::vector< StreamTag > &value)
Set the streams that the event was put in.
void setTimeStamp(uint32_t value)
Set the POSIX time of the event.
void setLevel1TriggerType(uint16_t value)
Set the Level-1 trigger type.
bool setEventFlags(EventFlagSubDet subDet, uint32_t flags)
Set the event flags for a particular sub-detector.
void setDetectorMaskExt(uint32_t mask2, uint32_t mask3)
Set the bit fields indicating with TTC timezones were present.
void setEventNumber(uint64_t value)
Set the current event's event number.
void setEventTypeBitmask(uint32_t value)
Set the event type bitmask.
void setMCChannelNumber(uint32_t value)
Set the MC generator's channel number.
void setExtendedLevel1ID(uint32_t value)
Set the extended Level-1 identifier.
void setMCEventWeights(const std::vector< float > &value)
Set the weights of all the MC events used in the simulation.
void setRunNumber(uint32_t value)
Set the current event's run number.
@ IS_CALIBRATION
true: calibration, false: physics
@ IS_SIMULATION
true: simulation, false: data
@ IS_TESTBEAM
true: testbeam, false: full detector
void setStatusElement(uint32_t value)
Set the trigger status element.
void setLumiBlock(uint32_t value)
Set the current event's luminosity block number.
DataObject * asStorable(SG::DataObjectSharedPtr< T > pObject)
EventInfo_v1 EventInfo
Definition of the latest event info version.
EventAuxInfo_v3 EventAuxInfo
Definition of the latest event auxiliary info version.