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/Algorithm.h"
41 #include "EventInfo/EventInfo.h"
62 : base_class(nam, svcLoc),
64 m_incidentSvc (
"IncidentSvc", nam ),
65 m_eventStore(
"StoreGateSvc", nam ),
66 m_evtSelector(nullptr), m_evtSelCtxt(nullptr),
67 m_histoDataMgrSvc(
"HistogramDataSvc", nam ),
68 m_histoPersSvc (
"HistogramPersistencySvc", nam ),
69 m_evtIdModSvc (
"", nam ),
71 m_currentRun(0), m_firstRun(true), m_tools(this),
72 m_nevt(0), m_writeHists(false),
73 m_nev(0), m_proc(0), m_useTools(false),
74 m_chronoStatSvc(
"ChronoStatSvc", nam ),
75 m_conditionsCleaner(
"Athena::ConditionsCleanerSvc", nam )
77 declareProperty(
"EvtStore",
m_eventStore,
"The StoreGateSvc instance to interact with for event payload" );
79 "Name of Event Selector to use. If empty string (default) "
80 "take value from ApplicationMgr");
82 "Histogram persistency technology to use: ROOT, HBOOK, NONE. "
83 "By default (empty string) get property value from "
86 "histogram write/update interval");
88 "Controls behaviour of event loop depending on return code of"
89 " Algorithms. 0: all non-SUCCESSes terminate job. "
90 "1: RECOVERABLE skips to next event, FAILURE terminates job "
91 "(DEFAULT). 2: RECOVERABLE and FAILURE skip to next events");
93 "Print event heartbeat printouts every m_eventPrintoutInterval events");
95 "heartbeat time interval is seconds rather than events"
96 "you also get a nice event rate printout then");
97 declareProperty(
"DoLiteLoop",
m_liteLoop=
false,
"Runs the bare minimum during executeEvent");
98 declareProperty(
"UseDetailChronoStat",
m_doChrono=
false);
99 declareProperty(
"ClearStorePolicy",
101 "Configure the policy wrt handling of when the "
102 "'clear-the-event-store' event shall happen: at EndEvent "
103 "(default as it is makes things easier for memory management"
104 ") or at BeginEvent (easier e.g. for interactive use)");
105 declareProperty(
"PreSelectTools",
m_tools,
"AlgTools for event pre-selection")->
108 "Require valid input attribute list to be present");
110 "In case of DoubleEventSelector use event number from secondary input");
112 "ServiceHandle for EvtIdModifierSvc");
114 "List of algorithms/sequences to execute during PreFork");
131 m_autoRetrieveTools =
false;
132 m_checkToolDeps =
false;
135 if ( !
sc.isSuccess() )
137 ATH_MSG_ERROR (
"Failed to initialize base class MinimalEventLoopMgr" );
146 if( !
sc.isSuccess() )
148 ATH_MSG_FATAL (
"Error retrieving pointer to StoreGateSvc" );
157 if( !
sc.isSuccess() )
166 SmartIF<IProperty> prpMgr(serviceLocator());
167 if ( !prpMgr.isValid() )
169 ATH_MSG_FATAL (
"IProperty interface not found in ApplicationMgr." );
170 return StatusCode::FAILURE;
178 if( !
sc.isSuccess() )
185 if ( histPersName.length() == 0 )
187 CHECK(setProperty(prpMgr->getProperty(
"HistogramPersistency")));
190 if ( histPersName !=
"NONE" ) {
195 if( !sc.isSuccess() ) {
196 ATH_MSG_WARNING (
"Histograms cannot not be saved - though required." );
200 if (histPersName ==
"ROOT") {
201 histSvc = serviceLocator()->service(
"RootHistSvc");
202 }
else if ( histPersName ==
"HBOOK" ) {
203 histSvc = serviceLocator()->service(
"HbookHistSvc");
207 ATH_MSG_ERROR (
"could not locate actual Histogram persistency service" );
209 const Gaudi::Details::PropertyBase &prop =
histSvc->getProperty(
"OutputFile");
212 const StringProperty &sprop =
dynamic_cast<const StringProperty&
>( prop );
217 ATH_MSG_VERBOSE (
"could not dcast OutputFile property to a StringProperty."
218 <<
" Need to fix Gaudi." );
220 val = prop.toString();
225 val !=
"UndefinedROOTOutputFileName" &&
226 val !=
"UndefinedHbookOutputFileName" ) {
240 ATH_MSG_DEBUG (
"EventID modifier Service not set. No run number, ... overrides will be applied." );
243 ATH_MSG_INFO (
"Could not find EventID modifier Service. No run number, ... overrides will be applied." );
252 sc = setProperty(prpMgr->getProperty(
"EvtSel"));
257 SmartIF<IEvtSelector> theEvtSel{serviceLocator()->service(
selName )};
266 return StatusCode::FAILURE;
268 if (msgLevel(MSG::INFO)) {
269 SmartIF<INamedInterface> named(theEvtSel);
271 ATH_MSG_INFO (
"Setup EventSelector service " << named->name( )
275 }
else if (
sc.isFailure()) {
278 return StatusCode::FAILURE;
288 return StatusCode::FAILURE;
292 m_aess = serviceLocator()->service(
"AlgExecStateSvc");
295 return StatusCode::FAILURE;
319 if ( policyName !=
"BeginEvent" &&
320 policyName !=
"EndEvent" ) {
323 <<
"] for the 'ClearStore-policy !"
325 <<
"Valid values are: BeginEvent, EndEvent"
327 throw GaudiException(
"Can not setup 'ClearStore'-policy",
329 StatusCode::FAILURE);
351 unsigned int toolCtr = 0;
352 for ( ; firstTool != lastTool; ++firstTool )
373 if (
sc.isFailure()) {
378 if (sc2.isFailure()) {
394 unsigned int toolCtr = 0;
395 ATH_MSG_INFO (
"Summary of AthenaEvtLoopPreSelectTool invocation: (invoked/success/failure)" );
396 ATH_MSG_INFO (
"-----------------------------------------------------" );
398 for ( ; firstTool != lastTool; ++firstTool ) {
399 ATH_MSG_INFO ( std::setw(2) << std::setiosflags(std::ios_base::right)
400 << toolCtr+1 <<
".) " << std::resetiosflags(std::ios_base::right)
401 << std::setw(48) << std::setfill(
'.')
402 << std::setiosflags(std::ios_base::left)
403 << (*firstTool)->name() << std::resetiosflags(std::ios_base::left)
406 << std::setw(6) << std::setiosflags(std::ios_base::right)
417 return (
sc.isFailure() || sc2.isFailure() ) ? StatusCode::FAILURE :
430 std::vector<DataObject*>
objects;
432 DataObject*
obj = reg->object();
433 if ( !
obj ||
obj->clID() == CLID_StatisticsFile )
return false;
438 if ( !
sc.isSuccess() ) {
439 ATH_MSG_ERROR (
"Error while traversing Histogram data store" );
447 (writeInterval != 0 &&
m_nevt%writeInterval == 0) ) {
451 IOpaqueAddress* pAddr =
nullptr;
453 if ( iret.isFailure() )
return iret;
454 i->registry()->setAddress( pAddr );
458 IRegistry* reg =
i->registry();
460 return iret.isFailure() ? iret : isc;
462 if ( !
sc.isSuccess() ) {
467 if (
force || (writeInterval != 0 &&
m_nevt%writeInterval == 0) ) {
486 for ( ita = m_topAlgList.begin(); ita != m_topAlgList.end(); ++ita )
499 for (ita = m_outStreamList.begin(); ita != m_outStreamList.end(); ++ita )
502 if(
sc.isFailure() ) {
510 return StatusCode::SUCCESS;
520 ita != m_topAlgList.end();
527 m_aess->algExecState(*ita,ctx).setState(AlgExecState::State::Done,
sc);
528 if ( !
sc.isSuccess() ) {
530 << (*ita)->name() <<
" failed with StatusCode::" <<
sc );
535 return StatusCode::SUCCESS;
546 m_incidentSvc->fireIncident(Incident(
"BeginEvent",IncidentType::BeginEvent));
548 m_incidentSvc->fireIncident(Incident(
"EndEvent",IncidentType::EndEvent));
570 bool toolsPassed=
true;
571 bool eventFailed =
false;
573 unsigned int toolCtr=0;
578 while(toolsPassed && theTool!=lastTool )
580 toolsPassed = (*theTool)->passEvent(ctx.eventID());
589 uint64_t evtNumber = ctx.eventID().event_number();
593 if (doEvtHeartbeat) {
595 ATH_MSG_INFO (
" ===>>> start processing event #" << evtNumber <<
", run #" <<
m_currentRun <<
" " <<
m_nev <<
" events processed so far <<<===" );
604 <<
" events processed so far <<<===" );
614 if ( m_scheduledStop ) {
615 ATH_MSG_ALWAYS (
"A stopRun was requested by an incidentListener. "
616 <<
"Do not process this event." );
617 return (StatusCode::SUCCESS);
625 if(!
sc.isSuccess()) {
627 m_aess->setEventStatus( EventStatus::AlgFail, ctx );
634 <<
"Skipping remaining algorithms." << std::endl
635 <<
"\tNo output will be written for this event, "
636 <<
"but job will continue to next event" );
642 ATH_MSG_INFO (
"Skipping remaining algorithms." << std::endl
643 <<
"\tNo output will be written for this event, "
644 <<
"but job will continue to next event" );
650 m_aess->setEventStatus( EventStatus::Success, ctx );
654 ito != m_outStreamList.end(); ++ito ) {
655 sc = (*ito)->sysExecute(ctx);
656 if( !
sc.isSuccess() ) {
670 if (doEvtHeartbeat) {
674 <<
" " <<
m_nev <<
" events processed so far <<<===");
678 <<
" events processed so far <<<===");
680 std::ofstream
outfile(
"eventLoopHeartBeat.txt");
685 <<
" " <<
m_nev <<
" events read so far <<<===" << std::endl;
694 return eventFailed?StatusCode::FAILURE:StatusCode::SUCCESS;
703 if (!(this->
nextEvent(maxevt)).isSuccess())
return StatusCode::FAILURE;
707 return StatusCode::SUCCESS;
716 if (0 == maxevt)
return StatusCode::SUCCESS;
718 static std::atomic<int> total_nevt = 0;
742 while(maxevt == -1 ||
m_nevt < maxevt) {
748 if ( m_scheduledStop ) {
749 m_scheduledStop =
false;
750 ATH_MSG_ALWAYS (
"A stopRun was requested. Terminating event loop." );
763 if( !
sc.isSuccess() ) {
778 IOpaqueAddress* addr =
nullptr;
782 if ( !
sc.isSuccess() )
786 sc = StatusCode::SUCCESS;
797 if (
nullptr != addr) {
800 if( !
sc.isSuccess() ) {
805 if ((
sc=
eventStore()->loadEventProxies()).isFailure()) {
818 if( !
sc.isSuccess() )
820 ATH_MSG_ERROR (
"Terminating event processing loop due to errors" );
829 if( !
sc.isSuccess() ) {
853 ATH_MSG_ERROR (
"Seek failed; unsupported by event selector" );
854 return StatusCode::FAILURE;
859 ATH_MSG_FATAL (
"Can not create the event selector Context." );
860 return StatusCode::FAILURE;
866 if (
sc.isSuccess()) {
891 ATH_MSG_ERROR (
"Collection size unsupported by event selector" );
897 ATH_MSG_FATAL (
"Can not create the event selector Context." );
911 if(inc.type()!=
"BeforeFork")
915 ATH_MSG_WARNING (
"Skipping BeforeFork handler. Begin run has already passed" );
927 ATH_MSG_WARNING (
"Skipping BeforeFork handler. No event selector is provided" );
932 IOpaqueAddress* addr =
nullptr;
934 if(!
sc.isSuccess()) {
939 if (
sc.isFailure()) {
943 if (
nullptr != addr) {
946 if(!
sc.isSuccess()) {
952 if(
eventStore()->loadEventProxies().isFailure()) {
964 throw std::runtime_error(
"Error installing event context object" );
973 ATH_MSG_ERROR (
"Unable to execute requested algorithms/sequences during PreFork!" );
981 if(!
sc.isSuccess()) {
991 IAlgManager* algMgr = Gaudi::svcLocator()->as<IAlgManager>();
995 SmartIF<IAlgorithm>&
alg = algMgr->algorithm(
name,
false);
998 sc &=
alg->sysExecute(ctx);
1017 std::unique_ptr<EventInfo> eventInfo;
1019 unsigned int conditionsRun = EventIDBase::UNDEFNUM;
1020 bool consume_modifier_stream =
false;
1027 if (pAttrList !=
nullptr && pAttrList->size() > 6) {
1030 unsigned int runNumber = (*pAttrList)[
"RunNumber"].data<
unsigned int>();
1031 unsigned long long eventNumber = (*pAttrList)[
"EventNumber"].data<
unsigned long long>();
1032 unsigned int eventTime = (*pAttrList)[
"EventTime"].data<
unsigned int>();
1033 unsigned int eventTimeNS = (*pAttrList)[
"EventTimeNanoSec"].data<
unsigned int>();
1034 unsigned int lumiBlock = (*pAttrList)[
"LumiBlockN"].data<
unsigned int>();
1035 unsigned int bunchId = (*pAttrList)[
"BunchId"].data<
unsigned int>();
1038 consume_modifier_stream =
true;
1042 unsigned long long eventNumberSecondary{};
1043 if (!(pAttrList->exists(
"hasSecondaryInput") &&
1044 (*pAttrList)[
"hasSecondaryInput"].data<
bool>())) {
1045 ATH_MSG_FATAL(
"Secondary EventNumber requested, but secondary input does not exist!");
1046 return StatusCode::FAILURE;
1048 if (pAttrList->exists(
"EventNumber_secondary")) {
1049 eventNumberSecondary = (*pAttrList)[
"EventNumber_secondary"].data<
unsigned long long>();
1054 if (pEventSecondary) {
1055 eventNumberSecondary = pEventSecondary->
event_ID()->event_number();
1057 ATH_MSG_FATAL(
"Secondary EventNumber requested, but it does not exist!");
1058 return StatusCode::FAILURE;
1061 if (eventNumberSecondary != 0) {
1064 if (doEvtHeartbeat) {
1066 << eventNumberSecondary <<
" instead of #"
1073 eventInfo = std::make_unique<EventInfo>(
1077 eventID = *(eventInfo->event_ID());
1079 if (!
m_evtIdModSvc.isSet() && pAttrList->exists(
"ConditionsRun")) {
1080 conditionsRun = (*pAttrList)[
"ConditionsRun"].data<
unsigned int>();
1100 ATH_MSG_FATAL(
"Valid input attribute list required but not present!");
1101 return StatusCode::FAILURE;
1115 if (xAODEvent ==
nullptr) {
1116 ATH_MSG_ERROR(
"Failed to get EventID from input. Tried old-style and xAOD::EventInfo");
1117 return StatusCode::FAILURE;
1121 eventInfo = std::make_unique<EventInfo>(
1124 eventID = *(eventInfo->event_ID());
1126 if (!
sc.isSuccess()) {
1128 return StatusCode::FAILURE;
1135 eventInfo = std::make_unique<EventInfo>(
1136 std::make_unique<EventID>(1,
m_nevt, 0), std::make_unique<EventType>());
1137 eventInfo->event_ID()->set_lumi_block(
m_nevt);
1138 eventID = *(eventInfo->event_ID());
1140 if (!
sc.isSuccess()) {
1142 return (StatusCode::FAILURE);
1146 ctx.setEventID( eventID );
1152 Gaudi::Hive::setCurrentContext( ctx );
1155 if (
eventStore()->record(std::make_unique<EventContext> ( ctx ),
1156 "EventContext").isFailure())
1159 return (StatusCode::FAILURE);
1162 return StatusCode::SUCCESS;
1173 unsigned int oldrunnr=eID.run_number();
1174 unsigned int oldLB=eID.lumi_block();
1175 unsigned int oldTS=eID.time_stamp();
1176 unsigned int oldTSno=eID.time_stamp_ns_offset();
1177 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc runnr=" << oldrunnr <<
" -> " << new_eID.run_number() );
1178 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc LB=" << oldLB <<
" -> " << new_eID.lumi_block() );
1179 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc TimeStamp=" << oldTS <<
" -> " << new_eID.time_stamp() );
1180 ATH_MSG_DEBUG (
"modifyEventContext: use evtIdModSvc TimeStamp ns Offset=" << oldTSno <<
" -> " << new_eID.time_stamp_ns_offset() );
1182 ctx.setEventID( new_eID );
1187 ctx.setEventID( eID );