15#include "GaudiKernel/ClassID.h"
16#include "GaudiKernel/FileIncident.h"
17#include "GaudiKernel/IIncidentSvc.h"
18#include "GaudiKernel/IIoComponentMgr.h"
27#include "eformat/StreamTag.h"
31 const std::string stringTypeStr{
"string"};
34 long eventNumber,
const void* source,
35 size_t nbytes,
unsigned int status) {
45 : base_class(name, svcloc) {
65 if (this->FSMState() != Gaudi::StateMachine::OFFLINE) {
66 this->
reinit(lock).ignore();
86 m_autoRetrieveTools =
false;
87 m_checkToolDeps =
false;
90 ATH_MSG_DEBUG(
"Initializing secondary event selector " << name());
100 return StatusCode::FAILURE;
103 std::sort(m_skipEventSequence.begin(), m_skipEventSequence.end());
109 return StatusCode::FAILURE;
121 if (!m_eventStreamingTool.empty()) {
122 ATH_CHECK(m_eventStreamingTool.retrieve());
133 if (!iomgr->io_register(
this, IIoComponentMgr::IoMode::READ, input).isSuccess()) {
134 ATH_MSG_FATAL(
"could not register [" << input <<
"] for output !");
137 ATH_MSG_VERBOSE(
"io_register[" << this->name() <<
"](" << input <<
") [ok]");
141 return(StatusCode::FAILURE);
150 StatusCode risc = this->
reinit(lock);
164 m_firstEvt.resize(1);
170 bool retError =
false;
172 for (ToolHandle<IAthenaSelectorTool>& tool :
m_helperTools) {
173 if (!tool->postInitialize().isSuccess()) {
174 ATH_MSG_FATAL(
"Failed to postInitialize() " << tool->name());
181 return(StatusCode::FAILURE);
187 FileIncident firstInputFileIncident(name(),
"FirstInputFile",
"BSF:" + *m_inputCollectionsIterator);
196 return StatusCode::SUCCESS;
208 return StatusCode::SUCCESS;
216 m_inputFileGuard.reset();
217 m_pendingFile.reset();
222 return StatusCode::SUCCESS;
232 for (ToolHandle<IAthenaSelectorTool>& tool :
m_helperTools) {
233 if (!tool->preFinalize().isSuccess()) {
241 return AthService::finalize();
247 m_inputFileGuard.reset();
248 m_pendingFile.reset();
249 ++m_inputCollectionsIterator;
256 ATH_MSG_ERROR(
"cannot open new run for non-filebased inputs");
257 return StatusCode::FAILURE;
262 return StatusCode::FAILURE;
264 std::string blockname = *m_inputCollectionsIterator;
268 ATH_MSG_FATAL(
"Unable to access file " << *m_inputCollectionsIterator <<
", stopping here");
281 ATH_MSG_WARNING(
"skipping more events " <<
m_skipEvents.value() - m_NumEvents <<
"(" << nev <<
") than in file " << *m_inputCollectionsIterator <<
", try next");
283 m_numEvt[m_fileCount] = nev;
291 m_firstEvt[m_fileCount] = m_NumEvents;
292 m_numEvt[m_fileCount] = nev;
294 return StatusCode::SUCCESS;
299 if (nevguid.first != -1) {
300 m_pendingFile.emplace(
"BSF:" + fileName, nevguid.second);
302 return nevguid.first;
309 m_pendingFile->first, m_pendingFile->second,
311 m_pendingFile.reset();
317 return StatusCode::SUCCESS;
327 static std::atomic<int> n_bad_events = 0;
329 if (!m_eventStreamingTool.empty() && m_eventStreamingTool->isClient()) {
332 ATH_CHECK(m_eventStreamingTool->getLockedEvent(&source, status));
334 return StatusCode::SUCCESS;
337 for (
const ToolHandle<IAthenaSelectorTool>& tool :
m_helperTools) {
338 if (!
tool->preNext().isSuccess()) {
351 if (
sc.isRecoverable()) {
353 }
else if (
sc.isFailure()) {
354 return StatusCode::FAILURE;
362 int nbad = ++n_bad_events;
366 ATH_MSG_ERROR(
"Cannot continue processing: bad event handling disabled or limit exceeded (" << nbad <<
" events)");
368 return StatusCode::FAILURE;
375 (m_skipEventSequence.empty() || m_NumEvents != m_skipEventSequence.front()) ) {
379 if (rec_sg != StatusCode::SUCCESS) {
380 ATH_MSG_ERROR(
"Fail to record BS DataHeader in StoreGate. Skipping events?! " << rec_sg);
387 for (
const ToolHandle<IAthenaSelectorTool>& tool :
m_helperTools) {
389 if (toolStatus.isRecoverable()) {
391 status = StatusCode::RECOVERABLE;
392 }
else if (toolStatus.isFailure()) {
394 status = StatusCode::FAILURE;
397 if (
status.isRecoverable()) {
399 }
else if (
status.isFailure()) {
411 catch (
const ByteStreamExceptions::badFragmentData&) {
412 int nbad = ++n_bad_events;
413 ATH_MSG_WARNING(
"Bad fragment data encountered, current count at " << nbad);
417 ATH_MSG_ERROR(
"Cannot continue processing: bad event handling disabled or limit exceeded (" << nbad <<
" events)");
419 return StatusCode::FAILURE;
426 if (!m_skipEventSequence.empty() && m_NumEvents == m_skipEventSequence.front()) {
427 m_skipEventSequence.erase(m_skipEventSequence.begin());
429 if ( m_NumEvents % 1'000 == 0 ) {
437 if (!m_eventStreamingTool.empty() && m_eventStreamingTool->isServer()) {
440 while ( (
sc = putEvent_ST(*m_eventStreamingTool,
441 m_NumEvents - 1, pre->start(),
442 pre->fragment_size_word() *
sizeof(uint32_t),
448 return StatusCode::SUCCESS;
465 unsigned int cntr = m_NumEvents;
467 while (m_NumEvents+1 <= cntr + jump) {
472 return StatusCode::SUCCESS;
477 return StatusCode::SUCCESS;
496 ATH_MSG_DEBUG(
"Event source found no more valid files left in input list");
498 return StatusCode::FAILURE;
506 return StatusCode::FAILURE;
517 if (pre ==
nullptr) {
519 return StatusCode::FAILURE;
528 return badEvent ? StatusCode::RECOVERABLE : StatusCode::SUCCESS;
533 if (rec_sg != StatusCode::SUCCESS) {
534 ATH_MSG_ERROR(
"Fail to record BS DataHeader in StoreGate. Skipping events?! " << rec_sg);
537 return StatusCode::SUCCESS;
552 if (
sc.isRecoverable()) {
555 if (
sc.isFailure()) {
556 return StatusCode::FAILURE;
566 (m_skipEventSequence.empty() || m_NumEvents != m_skipEventSequence.front()) ) {
567 return StatusCode::SUCCESS;
569 if (!m_skipEventSequence.empty() && m_NumEvents == m_skipEventSequence.front()) {
570 m_skipEventSequence.erase(m_skipEventSequence.begin());
573 ATH_MSG_INFO(
"skipping secondary event " << m_NumEvents);
580 return StatusCode::SUCCESS;
592 bool badEvent(
false);
598 catch (
const ByteStreamExceptions::readError&) {
600 return StatusCode::FAILURE;
602 catch (
const ByteStreamExceptions::badFragment&) {
606 catch (
const ByteStreamExceptions::badFragmentData&) {
613 return StatusCode::FAILURE;
617 ATH_MSG_FATAL(
"Attempt to read previous data on invalid reader");
618 return StatusCode::FAILURE;
627 return StatusCode::FAILURE;
634 if (rec_sg != StatusCode::SUCCESS) {
635 ATH_MSG_ERROR(
"Fail to record BS DataHeader in StoreGate. Skipping events?! " << rec_sg);
638 return StatusCode::SUCCESS;
652 for (
int i = 0;
i < jump;
i++) {
655 return StatusCode::SUCCESS;
657 return StatusCode::FAILURE;
661 if (it.identifier() ==
m_endIter->identifier()) {
663 return StatusCode::SUCCESS;
665 return StatusCode::FAILURE;
670 return StatusCode::FAILURE;
675 return StatusCode::SUCCESS;
683 ATH_MSG_ERROR(
"Input not seekable, choose different input svc");
684 return StatusCode::FAILURE;
688 if (fileNum == -1 && evtNum >= m_firstEvt[m_fileCount] && evtNum < m_NumEvents) {
689 fileNum = m_fileCount;
694 return StatusCode::RECOVERABLE;
697 if (fileNum != m_fileCount) {
702 m_fileCount = fileNum;
706 ATH_MSG_FATAL(
"Unable to open file with seeked event " << evtNum <<
" file " << fileName);
707 return StatusCode::FAILURE;
709 int delta = evtNum - m_firstEvt[m_fileCount];
717 int delta = (evtNum - m_firstEvt[m_fileCount] + 1) -
m_eventSource->positionInBlock();
718 ATH_MSG_DEBUG(
"Seeking event " << evtNum <<
" in current file with delta " << delta);
722 else if ( delta > 0 ) {
726 else if ( delta < 0 ) {
731 return StatusCode::SUCCESS;
741 std::string listName(
"EventInfoAtts");
750 auto attrList = std::make_unique<AthenaAttributeList>();
758 return StatusCode::SUCCESS;
769 attrList->extend(
"RunNumber" + suffix,
"unsigned int");
770 attrList->extend(
"EventNumber" + suffix,
"unsigned long long");
771 attrList->extend(
"LumiBlockN" + suffix,
"unsigned int");
772 attrList->extend(
"BunchId" + suffix,
"unsigned int");
773 attrList->extend(
"EventTime" + suffix,
"unsigned int");
774 attrList->extend(
"EventTimeNanoSec" + suffix,
"unsigned int");
779 (*attrList)[
"RunNumber" + suffix].data<
unsigned int>() = event->run_no();
780 if (event->version() < 0x03010000) {
781 (*attrList)[
"EventNumber" + suffix].data<
unsigned long long>() = event->lvl1_id();
783 (*attrList)[
"EventNumber" + suffix].data<
unsigned long long>() = event->global_id();
785 (*attrList)[
"LumiBlockN" + suffix].data<
unsigned int>() = event->lumi_block();
786 (*attrList)[
"BunchId" + suffix].data<
unsigned int>() = event->bc_id();
788 unsigned int bc_time_sec =
event->bc_time_seconds();
789 unsigned int bc_time_ns =
event->bc_time_nanoseconds();
791 if (bc_time_ns > 1000000000) {
793 ATH_MSG_WARNING(
" bc_time nanosecond number larger than 1e9, it is " << bc_time_ns <<
", reset it to 1 sec");
794 bc_time_ns = 1000000000;
796 (*attrList)[
"EventTime" + suffix].data<
unsigned int>() = bc_time_sec;
797 (*attrList)[
"EventTimeNanoSec" + suffix].data<
unsigned int>() = bc_time_ns;
801 event->status(buffer);
802 attrList->extend(
"TriggerStatus" + suffix,
"unsigned int");
803 (*attrList)[
"TriggerStatus" + suffix].data<
unsigned int>() = *buffer;
805 attrList->extend(
"ExtendedL1ID" + suffix,
"unsigned int");
806 attrList->extend(
"L1TriggerType" + suffix,
"unsigned int");
807 (*attrList)[
"ExtendedL1ID" + suffix].data<
unsigned int>() = event->lvl1_id();
808 (*attrList)[
"L1TriggerType" + suffix].data<
unsigned int>() = event->lvl1_trigger_type();
811 event->lvl1_trigger_info(buffer);
812 for (uint32_t iT1 = 0; iT1 <
event->nlvl1_trigger_info(); ++iT1) {
813 std::stringstream name;
814 name <<
"L1TriggerInfo_" << iT1;
815 attrList->extend(name.str() + suffix,
"unsigned int");
816 (*attrList)[name.str() + suffix].data<
unsigned int>() = *buffer;
821 event->lvl2_trigger_info(buffer);
822 for (uint32_t iT1 = 0; iT1 <
event->nlvl2_trigger_info(); ++iT1) {
824 std::stringstream name;
825 name <<
"L2TriggerInfo_" << iT1;
826 attrList->extend(name.str() + suffix,
"unsigned int");
827 (*attrList)[name.str() + suffix].data<
unsigned int>() = *buffer;
833 event->event_filter_info(buffer);
834 for (uint32_t iT1 = 0; iT1 <
event->nevent_filter_info(); ++iT1) {
836 std::stringstream name;
837 name <<
"EFTriggerInfo_" << iT1;
838 attrList->extend(name.str() + suffix,
"unsigned int");
839 (*attrList)[name.str() + suffix].data<
unsigned int>() = *buffer;
845 event->stream_tag(buffer);
846 std::vector<eformat::helper::StreamTag> onl_streamTags;
847 eformat::helper::decode(event->nstream_tag(), buffer, onl_streamTags);
848 for (std::vector<eformat::helper::StreamTag>::const_iterator itS = onl_streamTags.begin(),
849 itSE = onl_streamTags.end(); itS != itSE; ++itS) {
850 attrList->extend(itS->name + suffix, stringTypeStr);
851 (*attrList)[itS->name + suffix].data<std::string>() = itS->type;
854 return StatusCode::SUCCESS;
866 if (m_numEvt[i] == -1) {
870 long nev = nevguid.first;
877 m_firstEvt[i] = m_firstEvt[i - 1] + m_numEvt[i - 1];
885 if (evtNum >= m_firstEvt[i] && evtNum < m_firstEvt[i] + m_numEvt[i]) {
899 return int(m_NumEvents);
910 if (m_eventStreamingTool.empty()) {
911 return StatusCode::FAILURE;
913 return m_eventStreamingTool->makeServer(1,
"");
919 if (m_eventStreamingTool.empty()) {
920 return StatusCode::FAILURE;
922 std::string dummyStr;
923 return m_eventStreamingTool->makeClient(0, dummyStr);
929 if (m_eventStreamingTool.empty()) {
930 return StatusCode::FAILURE;
932 if (m_eventStreamingTool->isClient()) {
933 StatusCode
sc = m_eventStreamingTool->lockEvent(evtNum);
934 while (
sc.isRecoverable()) {
936 sc = m_eventStreamingTool->lockEvent(evtNum);
940 return StatusCode::FAILURE;
946 if (m_eventStreamingTool.empty()) {
947 ATH_MSG_ERROR(
"No AthenaSharedMemoryTool configured for readEvent()");
948 return StatusCode::FAILURE;
951 for (
int i = 0; i < maxevt || maxevt == -1; ++i) {
956 if (m_NumEvents == -1) {
957 ATH_MSG_VERBOSE(
"Called read Event and read last event from input: " << i);
960 ATH_MSG_ERROR(
"Unable to retrieve next event for " << i <<
"/" << maxevt);
961 return StatusCode::FAILURE;
963 if (m_eventStreamingTool->isServer()) {
965 while ( (
sc = putEvent_ST(*m_eventStreamingTool,
968 pre->fragment_size_word() *
sizeof(uint32_t),
977 while ( (
sc = putEvent_ST(*m_eventStreamingTool, 0, 0, 0, 0)).isRecoverable() ) {
981 return StatusCode::SUCCESS;
986 IOpaqueAddress*& iop)
const {
989 iop = proxy->address();
990 return StatusCode::SUCCESS;
993 return StatusCode::FAILURE;
1000 return StatusCode::SUCCESS;
1009 if (!iomgr->io_hasitem(
this)) {
1010 ATH_MSG_FATAL(
"IoComponentMgr does not know about myself !");
1011 return StatusCode::FAILURE;
1014 for (std::size_t i = 0,
imax = inputCollections.size(); i !=
imax; ++i) {
1015 ATH_MSG_INFO(
"I/O reinitialization, file = " << inputCollections[i]);
1016 std::string &fname = inputCollections[i];
1017 if (!iomgr->io_contains(
this, fname)) {
1018 ATH_MSG_ERROR(
"IoComponentMgr does not know about [" << fname <<
"] !");
1019 return StatusCode::FAILURE;
1021 ATH_CHECK(iomgr->io_retrieve(
this, fname));
1024 m_inputFileGuard.reset();
1025 m_pendingFile.reset();
1032 [] (Gaudi::Details::PropertyBase&) {}
1037 return this->
reinit(lock);
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
This file contains the class definition for the EventContextByteStream class.
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
OFFLINE_FRAGMENTS_NAMESPACE::FullEventFragment RawEvent
data type for reading raw event
size_t size() const
Number of registered mappings.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
An AttributeList represents a logical row of attributes in a metadata table.
This class provides the Context for EventSelectorByteStream.
virtual StatusCode initialize() override
Implementation of Service base class methods.
virtual StatusCode stop() override
Gaudi::Property< bool > m_isSecondary
IsSecondary, know if this is an instance of secondary event selector.
virtual int size(Context &it) const override
Always returns -1.
void fileTransition(lock_t &lock) const
EndInputFile for the file begun last and BeginInputFile for the file opened since,...
virtual StatusCode fillAttributeList(coral::AttributeList *attrList, const std::string &suffix, bool copySource) const override
Fill AttributeList with specific items from the selector and a suffix.
Gaudi::Property< std::string > m_eventSourceName
ToolHandle< IAthenaSelectorTool > m_counterTool
virtual StatusCode share(int evtnum) override
Request to share a given event number.
Gaudi::Property< bool > m_filebased
virtual StatusCode resetCriteria(const std::string &criteria, Context &context) const override
Set a selection criteria.
std::lock_guard< mutex_t > lock_t
SmartIF< IByteStreamInputSvc > m_eventSource
Gaudi::CheckedProperty< uint32_t > m_firstLBNo
virtual StatusCode readEvent(int maxevt) override
Read the next maxevt events.
EventContextByteStream * m_endIter
int findEvent(int evtNum, lock_t &lock) const
Search for event with number evtNum.
virtual StatusCode releaseContext(Context *&it) const override
virtual StatusCode makeServer(int num) override
Make this a server.
virtual bool disconnectIfFinished(const SG::SourceID &fid) const override
Gaudi::CheckedProperty< uint64_t > m_eventsPerRun
virtual StatusCode nextWithSkip(IEvtSelector::Context &ctxt) const override
Go to next event and skip if necessary.
ToolHandleArray< IAthenaSelectorTool > m_helperTools
HelperTools, vector of names of AlgTools that are executed by the EventSelector.
virtual StatusCode finalize() override
StatusCode previousImpl(Context &it, lock_t &lock) const
virtual StatusCode last(Context &it) const override
long openBlock(const std::string &fileName, lock_t &lock) const
Open fileName as the current file, without input file incidents (see fileTransition()).
Gaudi::CheckedProperty< uint32_t > m_initTimeStamp
Gaudi::CheckedProperty< uint32_t > m_eventsPerLB
Gaudi::Property< bool > m_procBadEvent
process bad events, which fail check_tree().
Gaudi::CheckedProperty< uint32_t > m_runNo
virtual StatusCode createContext(Context *&it) const override
create context
virtual StatusCode createAddress(const Context &it, IOpaqueAddress *&iop) const override
virtual StatusCode makeClient(int num) override
Make this a client.
virtual int curEvent(const Context &it) const override
Return the current event number.
ServiceHandle< IIncidentSvc > m_incidentSvc
StatusCode nextImpl(Context &it, lock_t &lock) const
StatusCode fillAttributeListImpl(coral::AttributeList *attrList, const std::string &suffix, bool copySource, lock_t &lock) const
void nextFile(lock_t &lock) const
StatusCode nextHandleFileTransitionImpl(IEvtSelector::Context &ctxt, lock_t &lock) const
virtual StatusCode rewind(Context &it) const override
StatusCode reinit(lock_t &lock)
Reinitialize the service when a fork() occurred/was-issued.
Gaudi::Property< std::vector< std::string > > m_inputCollectionsProp
virtual StatusCode next(Context &it) const override
int m_fileCount ATLAS_THREAD_SAFE
number of files to process.
StatusCode openNewRun(lock_t &lock) const
virtual StatusCode io_reinit() override
Callback method to reinitialize the internal state of the component for I/O purposes (e....
virtual ~EventSelectorByteStream()
Standard Destructor.
Gaudi::Property< std::vector< long > > m_skipEventSequenceProp
StoreGateSvc * eventStore() const
void inputCollectionsHandler(Gaudi::Details::PropertyBase &)
virtual StatusCode start() override
virtual StatusCode recordAttributeList() const override
Record AttributeList in StoreGate.
virtual StatusCode seek(Context &, int evtnum) const override
Seek to a given event number.
StatusCode nextWithSkipImpl(IEvtSelector::Context &ctxt, lock_t &lock) const
StatusCode recordAttributeListImpl(lock_t &lock) const
Gaudi::CheckedProperty< uint64_t > m_firstEventNo
EventContextByteStream * m_beginIter
virtual StatusCode nextHandleFileTransition(IEvtSelector::Context &ctxt) const override
Handle file transition at the next iteration.
Gaudi::Property< long > m_skipEvents
virtual StatusCode previous(Context &it) const override
Gaudi::Property< int > m_maxBadEvts
number of bad events allowed before quitting.
EventSelectorByteStream(const std::string &name, ISvcLocator *svcloc)
Standard Constructor.
The Athena Transient Store API.
static StoreGateSvc * currentStoreGate()
get current StoreGate
virtual SG::DataProxy * proxy(const void *const pTransient) const override final
get proxy for a given data object address in memory
bool contains(const std::string &s, const std::string ®x)
does a string contain the substring
::StatusCode StatusCode
StatusCode definition for legacy code.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
static constexpr CLID ID()