5#define GAUDISVC_EVENTLOOPMGR_CPP
24#include "GaudiKernel/IAlgManager.h"
25#include "GaudiKernel/IAlgorithm.h"
26#include "GaudiKernel/SmartIF.h"
27#include "GaudiKernel/Incident.h"
28#include "GaudiKernel/DataObject.h"
29#include "GaudiKernel/IIncidentSvc.h"
30#include "GaudiKernel/IEvtSelector.h"
31#include "GaudiKernel/IDataManagerSvc.h"
32#include "GaudiKernel/IConversionSvc.h"
33#include "GaudiKernel/GaudiException.h"
34#include "GaudiKernel/EventContext.h"
35#include "GaudiKernel/EventIDBase.h"
36#include "GaudiKernel/ThreadLocalContext.h"
37#include "GaudiKernel/AppReturnCode.h"
41#include "EventInfo/EventInfo.h"
59 : base_class(nam, svcLoc),
74 declareProperty(
"EvtStore",
m_eventStore,
"The StoreGateSvc instance to interact with for event payload" );
76 "Name of Event Selector to use. If empty string (default) "
77 "take value from ApplicationMgr");
79 "Histogram persistency technology to use: ROOT, HBOOK, NONE. "
80 "By default (empty string) get property value from "
83 "histogram write/update interval");
85 "Controls behaviour of event loop depending on return code of"
86 " Algorithms. 0: all non-SUCCESSes terminate job. "
87 "1: RECOVERABLE skips to next event, FAILURE terminates job "
88 "(DEFAULT). 2: RECOVERABLE and FAILURE skip to next events");
90 "Print event heartbeat printouts every m_eventPrintoutInterval events");
92 "heartbeat time interval is seconds rather than events"
93 "you also get a nice event rate printout then");
94 declareProperty(
"DoLiteLoop",
m_liteLoop=
false,
"Runs the bare minimum during executeEvent");
95 declareProperty(
"UseDetailChronoStat",
m_doChrono=
false);
96 declareProperty(
"ClearStorePolicy",
98 "Configure the policy wrt handling of when the "
99 "'clear-the-event-store' event shall happen: at EndEvent "
100 "(default as it is makes things easier for memory management"
101 ") or at BeginEvent (easier e.g. for interactive use)");
102 declareProperty(
"PreSelectTools",
m_tools,
"AlgTools for event pre-selection")->
105 "Require valid input attribute list to be present");
107 "In case of DoubleEventSelector use event number from secondary input");
109 "ServiceHandle for EvtIdModifierSvc");
111 "List of algorithms/sequences to execute during PreFork");
128 m_autoRetrieveTools =
false;
129 m_checkToolDeps =
false;
131 StatusCode
sc = MinimalEventLoopMgr::initialize();
132 if ( !
sc.isSuccess() )
134 ATH_MSG_ERROR (
"Failed to initialize base class MinimalEventLoopMgr" );
143 if( !
sc.isSuccess() )
145 ATH_MSG_FATAL (
"Error retrieving pointer to StoreGateSvc" );
154 if( !
sc.isSuccess() )
163 SmartIF<IProperty> prpMgr(serviceLocator());
164 if ( !prpMgr.isValid() )
166 ATH_MSG_FATAL (
"IProperty interface not found in ApplicationMgr." );
167 return StatusCode::FAILURE;
175 if( !
sc.isSuccess() )
182 if ( histPersName.length() == 0 )
187 if ( histPersName !=
"NONE" ) {
192 if( !sc.isSuccess() ) {
193 ATH_MSG_WARNING (
"Histograms cannot not be saved - though required." );
196 SmartIF<IProperty> histSvc;
197 if (histPersName ==
"ROOT") {
198 histSvc = serviceLocator()->service(
"RootHistSvc");
199 }
else if ( histPersName ==
"HBOOK" ) {
200 histSvc = serviceLocator()->service(
"HbookHistSvc");
204 ATH_MSG_ERROR (
"could not locate actual Histogram persistency service" );
206 const Gaudi::Details::PropertyBase &prop = histSvc->getProperty(
"OutputFile");
209 const StringProperty &sprop =
dynamic_cast<const StringProperty&
>( prop );
214 ATH_MSG_VERBOSE (
"could not dcast OutputFile property to a StringProperty."
215 <<
" Need to fix Gaudi." );
217 val = prop.toString();
222 val !=
"UndefinedROOTOutputFileName" &&
223 val !=
"UndefinedHbookOutputFileName" ) {
237 ATH_MSG_DEBUG (
"EventID modifier Service not set. No run number, ... overrides will be applied." );
240 ATH_MSG_INFO (
"Could not find EventID modifier Service. No run number, ... overrides will be applied." );
246 const std::string& selName(
m_evtsel.value());
253 if( !selName.empty() && selName !=
"NONE") {
254 SmartIF<IEvtSelector> theEvtSel{serviceLocator()->service( selName )};
263 return StatusCode::FAILURE;
265 if (msgLevel(MSG::INFO)) {
266 SmartIF<INamedInterface> named(theEvtSel);
268 ATH_MSG_INFO (
"Setup EventSelector service " << named->name( )
272 }
else if (
sc.isFailure()) {
275 return StatusCode::FAILURE;
285 return StatusCode::FAILURE;
289 m_aess = serviceLocator()->service(
"AlgExecStateSvc");
292 return StatusCode::FAILURE;
316 if ( policyName !=
"BeginEvent" &&
317 policyName !=
"EndEvent" ) {
320 <<
"] for the 'ClearStore-policy !"
322 <<
"Valid values are: BeginEvent, EndEvent"
324 throw GaudiException(
"Can not setup 'ClearStore'-policy",
326 StatusCode::FAILURE);
348 unsigned int toolCtr = 0;
349 for ( ; firstTool != lastTool; ++firstTool )
369 CHECK( MinimalEventLoopMgr::stopRun() );
371 auto appProp = m_appMgrUI.as<IProperty>();
372 CHECK( Gaudi::setAppReturnCode( appProp, Gaudi::ReturnCode::ScheduledStop,
true ) );
373 return StatusCode::SUCCESS;
382 StatusCode
sc = MinimalEventLoopMgr::finalize();
383 if (
sc.isFailure()) {
388 if (sc2.isFailure()) {
404 unsigned int toolCtr = 0;
405 ATH_MSG_INFO (
"Summary of AthenaEvtLoopPreSelectTool invocation: (invoked/success/failure)" );
406 ATH_MSG_INFO (
"-----------------------------------------------------" );
408 for ( ; firstTool != lastTool; ++firstTool ) {
409 ATH_MSG_INFO ( std::setw(2) << std::setiosflags(std::ios_base::right)
410 << toolCtr+1 <<
".) " << std::resetiosflags(std::ios_base::right)
411 << std::setw(48) << std::setfill(
'.')
412 << std::setiosflags(std::ios_base::left)
413 << (*firstTool)->name() << std::resetiosflags(std::ios_base::left)
416 << std::setw(6) << std::setiosflags(std::ios_base::right)
427 return (
sc.isFailure() || sc2.isFailure() ) ? StatusCode::FAILURE :
437 StatusCode
sc (StatusCode::SUCCESS);
440 std::vector<DataObject*> objects;
442 DataObject* obj = reg->object();
443 if ( !obj || obj->clID() == CLID_StatisticsFile )
return false;
444 objects.push_back( obj );
448 if ( !
sc.isSuccess() ) {
449 ATH_MSG_ERROR (
"Error while traversing Histogram data store" );
453 if ( objects.size() > 0) {
456 if (
m_nevt == 1 || force ||
457 (writeInterval != 0 &&
m_nevt%writeInterval == 0) ) {
460 sc = std::accumulate( begin( objects ), end( objects ),
sc, [&]( StatusCode isc,
auto& i ) {
461 IOpaqueAddress* pAddr =
nullptr;
463 if ( iret.isFailure() )
return iret;
464 i->registry()->setAddress( pAddr );
467 sc = std::accumulate( begin( objects ), end( objects ),
sc, [&]( StatusCode isc,
auto& i ) {
468 IRegistry* reg = i->registry();
469 StatusCode iret =
m_histoPersSvc->fillRepRefs( reg->address(), i );
470 return iret.isFailure() ? iret : isc;
472 if ( !
sc.isSuccess() ) {
477 if (force || (writeInterval != 0 &&
m_nevt%writeInterval == 0) ) {
495 ListAlg::iterator ita;
496 for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ++ita )
498 StatusCode
sc = (*ita)->sysInitialize();
509 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ++ita )
511 StatusCode
sc = (*ita)->sysInitialize();
512 if(
sc.isFailure() ) {
520 return StatusCode::SUCCESS;
529 for ( ListAlg::iterator ita = m_topAlgList.begin();
530 ita != m_topAlgList.end();
533 const StatusCode&
sc = (*ita)->sysExecute(ctx);
537 m_aess->algExecState(*ita,ctx).setState(AlgExecState::State::Done,
sc);
538 if ( !
sc.isSuccess() ) {
540 << (*ita)->name() <<
" failed with StatusCode::" <<
sc );
545 return StatusCode::SUCCESS;
556 m_incidentSvc->fireIncident(Incident(
"BeginEvent",IncidentType::BeginEvent));
558 m_incidentSvc->fireIncident(Incident(
"EndEvent",IncidentType::EndEvent));
580 bool toolsPassed=
true;
581 bool eventFailed =
false;
583 unsigned int toolCtr=0;
586 tool_store::iterator theTool =
m_tools.begin();
587 tool_store::iterator lastTool =
m_tools.end();
588 while(toolsPassed && theTool!=lastTool )
590 toolsPassed = (*theTool)->passEvent(ctx.eventID());
599 uint64_t evtNumber = ctx.eventID().event_number();
603 if (doEvtHeartbeat) {
605 ATH_MSG_INFO (
" ===>>> start processing event #" << evtNumber <<
", run #" <<
m_currentRun <<
" " <<
m_nev <<
" events processed so far <<<===" );
614 <<
" events processed so far <<<===" );
624 if ( m_scheduledStop ) {
625 ATH_MSG_ALWAYS (
"A stopRun was requested by an incidentListener. "
626 <<
"Do not process this event." );
627 return (StatusCode::SUCCESS);
635 if(!
sc.isSuccess()) {
637 m_aess->setEventStatus( EventStatus::AlgFail, ctx );
644 <<
"Skipping remaining algorithms." << std::endl
645 <<
"\tNo output will be written for this event, "
646 <<
"but job will continue to next event" );
652 ATH_MSG_INFO (
"Skipping remaining algorithms." << std::endl
653 <<
"\tNo output will be written for this event, "
654 <<
"but job will continue to next event" );
660 m_aess->setEventStatus( EventStatus::Success, ctx );
663 for (ListAlg::iterator ito = m_outStreamList.begin();
664 ito != m_outStreamList.end(); ++ito ) {
665 sc = (*ito)->sysExecute(ctx);
666 if( !
sc.isSuccess() ) {
680 if (doEvtHeartbeat) {
684 <<
" " <<
m_nev <<
" events processed so far <<<===");
688 <<
" events processed so far <<<===");
690 std::ofstream outfile(
"eventLoopHeartBeat.txt");
694 outfile <<
" done processing event #" << evtNumber <<
", run #" <<
m_currentRun
695 <<
" " <<
m_nev <<
" events read so far <<<===" << std::endl;
704 return eventFailed?StatusCode::FAILURE:StatusCode::SUCCESS;
713 if (!(this->
nextEvent(maxevt)).isSuccess())
return StatusCode::FAILURE;
717 return StatusCode::SUCCESS;
726 if (0 == maxevt)
return StatusCode::SUCCESS;
728 static std::atomic<int> total_nevt = 0;
752 while(maxevt == -1 ||
m_nevt < maxevt) {
758 if ( m_scheduledStop ) {
759 m_scheduledStop =
false;
760 ATH_MSG_ALWAYS (
"A stopRun was requested. Terminating event loop." );
773 if( !
sc.isSuccess() ) {
788 IOpaqueAddress* addr =
nullptr;
792 if ( !
sc.isSuccess() )
796 sc = StatusCode::SUCCESS;
807 if (
nullptr != addr) {
810 if( !
sc.isSuccess() ) {
815 if ((
sc=
eventStore()->loadEventProxies()).isFailure()) {
828 if( !
sc.isSuccess() )
830 ATH_MSG_ERROR (
"Terminating event processing loop due to errors" );
839 if( !
sc.isSuccess() ) {
863 ATH_MSG_ERROR (
"Seek failed; unsupported by event selector" );
864 return StatusCode::FAILURE;
869 ATH_MSG_FATAL (
"Can not create the event selector Context." );
870 return StatusCode::FAILURE;
876 if (
sc.isSuccess()) {
901 ATH_MSG_ERROR (
"Collection size unsupported by event selector" );
907 ATH_MSG_FATAL (
"Can not create the event selector Context." );
921 if(inc.type()!=
"BeforeFork")
925 ATH_MSG_WARNING (
"Skipping BeforeFork handler. Begin run has already passed" );
937 ATH_MSG_WARNING (
"Skipping BeforeFork handler. No event selector is provided" );
942 IOpaqueAddress* addr =
nullptr;
944 if(!
sc.isSuccess()) {
949 if (
sc.isFailure()) {
953 if (
nullptr != addr) {
956 if(!
sc.isSuccess()) {
962 if(
eventStore()->loadEventProxies().isFailure()) {
974 throw std::runtime_error(
"Error installing event context object" );
983 ATH_MSG_ERROR (
"Unable to execute requested algorithms/sequences during PreFork!" );
991 if(!
sc.isSuccess()) {
1001 IAlgManager* algMgr = Gaudi::svcLocator()->as<IAlgManager>();
1005 SmartIF<IAlgorithm>& alg = algMgr->algorithm(
name,
false);
1008 sc &= alg->sysExecute(ctx);
1027 std::unique_ptr<EventInfo> eventInfo;
1029 unsigned int conditionsRun = EventIDBase::UNDEFNUM;
1030 bool consume_modifier_stream =
false;
1037 if (pAttrList !=
nullptr && pAttrList->size() > 6) {
1040 unsigned int runNumber = (*pAttrList)[
"RunNumber"].data<
unsigned int>();
1041 unsigned long long eventNumber = (*pAttrList)[
"EventNumber"].data<
unsigned long long>();
1042 unsigned int eventTime = (*pAttrList)[
"EventTime"].data<
unsigned int>();
1043 unsigned int eventTimeNS = (*pAttrList)[
"EventTimeNanoSec"].data<
unsigned int>();
1044 unsigned int lumiBlock = (*pAttrList)[
"LumiBlockN"].data<
unsigned int>();
1045 unsigned int bunchId = (*pAttrList)[
"BunchId"].data<
unsigned int>();
1048 consume_modifier_stream =
true;
1052 unsigned long long eventNumberSecondary{};
1053 if (!(pAttrList->exists(
"hasSecondaryInput") &&
1054 (*pAttrList)[
"hasSecondaryInput"].data<
bool>())) {
1055 ATH_MSG_FATAL(
"Secondary EventNumber requested, but secondary input does not exist!");
1056 return StatusCode::FAILURE;
1058 if (pAttrList->exists(
"EventNumber_secondary")) {
1059 eventNumberSecondary = (*pAttrList)[
"EventNumber_secondary"].data<
unsigned long long>();
1064 if (pEventSecondary) {
1065 eventNumberSecondary = pEventSecondary->
event_ID()->event_number();
1067 ATH_MSG_FATAL(
"Secondary EventNumber requested, but it does not exist!");
1068 return StatusCode::FAILURE;
1071 if (eventNumberSecondary != 0) {
1074 if (doEvtHeartbeat) {
1076 << eventNumberSecondary <<
" instead of #"
1077 << eventNumber <<
" <<<===");
1079 eventNumber = eventNumberSecondary;
1083 eventInfo = std::make_unique<EventInfo>(
1084 std::make_unique<EventID>(runNumber, eventNumber, eventTime,
1085 eventTimeNS, lumiBlock, bunchId),
1087 eventID = *(eventInfo->event_ID());
1089 if (!
m_evtIdModSvc.isSet() && pAttrList->exists(
"ConditionsRun")) {
1090 conditionsRun = (*pAttrList)[
"ConditionsRun"].data<
unsigned int>();
1092 conditionsRun = runNumber;
1110 ATH_MSG_FATAL(
"Valid input attribute list required but not present!");
1111 return StatusCode::FAILURE;
1125 if (xAODEvent ==
nullptr) {
1126 ATH_MSG_ERROR(
"Failed to get EventID from input. Tried old-style and xAOD::EventInfo");
1127 return StatusCode::FAILURE;
1131 eventInfo = std::make_unique<EventInfo>(
1134 eventID = *(eventInfo->event_ID());
1136 if (!
sc.isSuccess()) {
1138 return StatusCode::FAILURE;
1145 eventInfo = std::make_unique<EventInfo>(
1146 std::make_unique<EventID>(1,
m_nevt, 0), std::make_unique<EventType>());
1147 eventInfo->event_ID()->set_lumi_block(
m_nevt);
1148 eventID = *(eventInfo->event_ID());
1150 if (!
sc.isSuccess()) {
1152 return (StatusCode::FAILURE);
1156 ctx.setEventID( eventID );
1162 Gaudi::Hive::setCurrentContext( ctx );
1165 if (
eventStore()->record(std::make_unique<EventContext> ( ctx ),
1166 "EventContext").isFailure())
1169 return (StatusCode::FAILURE);
1172 return StatusCode::SUCCESS;
1182 if (msgLevel(MSG::DEBUG)) {
1183 unsigned int oldrunnr=eID.run_number();
1184 unsigned int oldLB=eID.lumi_block();
1185 unsigned int oldTS=eID.time_stamp();
1186 unsigned int oldTSno=eID.time_stamp_ns_offset();
1187 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc runnr=" << oldrunnr <<
" -> " << new_eID.run_number() );
1188 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc LB=" << oldLB <<
" -> " << new_eID.lumi_block() );
1189 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc TimeStamp=" << oldTS <<
" -> " << new_eID.time_stamp() );
1190 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc TimeStamp ns Offset=" << oldTSno <<
" -> " << new_eID.time_stamp_ns_offset() );
1192 ctx.setEventID( new_eID );
1197 ctx.setEventID( eID );
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_ALWAYS(x)
#define ATH_MSG_WARNING(x)
The default ATLAS batch event loop manager.
ClearStorePolicy::Type clearStorePolicy(const std::string &policyName, MsgStream &msg)
returns the enum-version of the policy (by name)
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
Assign a CLID to EventContext.
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
EventID eventIDFromxAOD(const xAOD::EventInfo *xaod)
Create EventID object from xAOD::EventInfo.
EventType eventTypeFromxAOD(const xAOD::EventInfo *xaod)
Create EventType object from xAOD::EventInfo.
This class provides general information about an event.
Extension to IEvtSelector to allow for seeking.
void setProperty(columnar::PythonToolHandle &self, const std::string &key, nb::object value)
An AttributeList represents a logical row of attributes in a metadata table.
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
An AttributeList represents a logical row of attributes in a metadata table.
virtual ~AthenaEventLoopMgr()
Standard Destructor.
UnsignedIntegerProperty m_eventPrintoutInterval
IntegerProperty m_failureMode
IEvtSelector::Context * m_evtSelCtxt
Gaudi EventSelector Context (may be used as a cursor by the evt selector)
virtual int curEvent() const override
Return the current event count.
tool_stats m_toolInvoke
tool called counter
IIncidentSvc_t m_incidentSvc
Reference to the incident service.
unsigned int m_nev
events processed
StoreGateSvc_t m_eventStore
Reference to StoreGateSvc;.
bool m_useSecondaryEventNumber
read event number from secondary input
tool_stats m_toolAccept
tool returns StatusCode::SUCCESS counter
virtual void handle(const Incident &inc) override
IIncidentListenet interfaces.
bool m_requireInputAttributeList
require input attribute list
SmartIF< IAlgExecStateSvc > m_aess
Reference to the Algorithm Execution State Svc.
virtual StatusCode writeHistograms(bool force=false)
Dump out histograms as needed.
StringProperty m_histPersName
tool_stats m_toolReject
tool returns StatusCode::FAILURE counter
AthenaEventLoopMgr()
no implementation
StatusCode execAtPreFork(const EventContext &ctx) const
Execute certain algorithms/sequences in PreFork.
IConversionSvc_t m_histoPersSvc
IEvtSelector * m_evtSelector
Reference to the Event Selector.
virtual StatusCode executeEvent(EventContext &&ctx) override
implementation of IEventProcessor::executeEvent(EventContext&& ctx)
virtual StatusCode initialize() override
implementation of IAppMgrUI::initalize
virtual StatusCode seek(int evt) override
Seek to a given event.
virtual int size() override
Return the size of the collection.
IEvtIdModifierSvc_t m_evtIdModSvc
StoreGateSvc * eventStore() const
virtual StatusCode finalize() override
implementation of IAppMgrUI::finalize
IntegerProperty m_writeInterval
StringProperty m_clearStorePolicy
ServiceHandle< IConversionSvc > IConversionSvc_t
StatusCode installEventContext(EventContext &ctx)
virtual StatusCode stopRun() override
Called from ApplicationMgr::stopRun() to terminate the loop.
void setupPreSelectTools(Gaudi::Details::PropertyBase &)
property update handler:sets up the Pre-selection tools
virtual const std::string & name() const override
virtual StatusCode executeAlgorithms(const EventContext &)
Run the algorithms for the current event.
StringArrayProperty m_execAtPreFork
virtual void modifyEventContext(EventContext &ctx, const EventID &eID, bool consume_modifier_stream)
number_type m_currentRun
current run number
unsigned int m_intervalInSeconds
StatusCode initializeAlgorithms()
Initialize all algorithms and output streams.
tool_store::const_iterator tool_iterator
tool_store m_tools
internal tool store
virtual StatusCode executeRun(int maxevt) override
implementation of IEventProcessor::executeRun(int maxevt)
virtual StatusCode nextEvent(int maxevt) override
implementation of IAppMgrUI::nextEvent. maxevt==0 returns immediately
ServiceHandle< IChronoStatSvc > m_chronoStatSvc
ServiceHandle< Athena::IConditionsCleanerSvc > m_conditionsCleaner
IDataManagerSvc_t m_histoDataMgrSvc
Reference to the Histogram Data Service.
void setClearStorePolicy(Gaudi::Details::PropertyBase &clearStorePolicy)
property update handler:set the clear-store policy value and check its value.
void resetTimeout(Timeout &instance)
Reset timeout.
static Timeout & instance()
Get reference to Timeout singleton.
void setConditionsRun(EventIDBase::number_type conditionsRun)
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
EventID * event_ID()
the unique identification of the event.
Abstract interface for seeking for an event selector.
virtual StatusCode seek(IEvtSelector::Context &c, int evtnum) const =0
Seek to a given event number.
virtual int size(IEvtSelector::Context &c) const =0
Return the size of the collection, or -1 if we can't get the size.
The Athena Transient Store API.
StatusCode recordAddress(const std::string &skey, CxxUtils::RefCountedPtr< IOpaqueAddress > pAddress, bool clearAddressFlag=true)
Create a proxy object using an IOpaqueAddress and a transient key.
StatusCode record(T *p2BRegistered, const TKEY &key)
Record an object with a key.
const T * tryConstRetrieve() const
virtual StatusCode clearStore(bool forceRemove=false) override final
clear DataStore contents: called by the event loop mgrs
uint32_t runNumber() const
The current event's run number.
void setExtendedEventContext(EventContext &ctx, ExtendedEventContext &&ectx)
Move an extended context into a context object.
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
thread_local event_number_t eventIndex
EventInfo_v1 EventInfo
Definition of the latest event info version.