All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
EventLoopMgr.cpp
Go to the documentation of this file.
1 #define GAUDISVC_EVENTLOOPMGR_CPP
2 
3 #include "GaudiKernel/SmartIF.h"
4 #include "GaudiKernel/Incident.h"
13 
14 #include "HistogramAgent.h"
15 #include "EventLoopMgr.h"
16 
17 
18 // Instantiation of a static factory class used by clients to create instances of this service
20 
21 
22 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
23 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
24 
25 #define DEBMSG ON_DEBUG debug()
26 #define VERMSG ON_VERBOSE verbose()
27 
28 //--------------------------------------------------------------------------------------------
29 // Standard Constructor
30 //--------------------------------------------------------------------------------------------
31 EventLoopMgr::EventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
32 : MinimalEventLoopMgr(nam, svcLoc)
33 {
35  m_histoPersSvc = 0;
36  m_evtDataMgrSvc = 0;
37  m_evtDataSvc = 0;
38  m_evtSelector = 0;
39  m_evtContext = 0;
40  m_endEventFired = true;
41 
42  // Declare properties
43  declareProperty("HistogramPersistency", m_histPersName = "");
44  declareProperty("EvtSel", m_evtsel );
45  declareProperty("Warnings",m_warnings=true,
46  "Set this property to false to suppress warning messages");
47 }
48 
49 //--------------------------------------------------------------------------------------------
50 // Standard Destructor
51 //--------------------------------------------------------------------------------------------
53  if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
54  if( m_histoPersSvc ) m_histoPersSvc->release();
55  if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
56  if( m_evtDataSvc ) m_evtDataSvc->release();
57  if( m_evtSelector ) m_evtSelector->release();
58  if( m_evtContext ) delete m_evtContext;
59 }
60 
61 //--------------------------------------------------------------------------------------------
62 // implementation of IAppMgrUI::initialize
63 //--------------------------------------------------------------------------------------------
65  // Initialize the base class
67  if( !sc.isSuccess() ) {
68  DEBMSG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
69  return sc;
70  }
71 
72  // Setup access to event data services
73  m_evtDataMgrSvc = serviceLocator()->service("EventDataSvc");
74  if( !m_evtDataMgrSvc.isValid() ) {
75  fatal() << "Error retrieving EventDataSvc interface IDataManagerSvc." << endmsg;
76  return StatusCode::FAILURE;
77  }
78  m_evtDataSvc = serviceLocator()->service("EventDataSvc");
79  if( !m_evtDataSvc.isValid() ) {
80  fatal() << "Error retrieving EventDataSvc interface IDataProviderSvc." << endmsg;
81  return StatusCode::FAILURE;
82  }
83 
84  // Obtain the IProperty of the ApplicationMgr
86  if ( ! m_appMgrProperty.isValid() ) {
87  fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
88  return StatusCode::FAILURE;
89  }
90 
91  // We do not expect a Event Selector necessarily being declared
92  setProperty(m_appMgrProperty->getProperty("EvtSel")).ignore();
93 
94  if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
95  m_evtSelector = serviceLocator()->service("EventSelector");
96  if( m_evtSelector.isValid() ) {
97  // Setup Event Selector
98  sc=m_evtSelector->createContext(m_evtContext);
99  if( !sc.isSuccess() ) {
100  fatal() << "Can not create the event selector Context." << endmsg;
101  return sc;
102  }
103  }
104  else {
105  fatal() << "EventSelector not found." << endmsg;
106  return sc;
107  }
108  }
109  else {
110  m_evtSelector = 0;
111  m_evtContext = 0;
112  if ( m_warnings ) {
113  warning() << "Unable to locate service \"EventSelector\" " << endmsg;
114  warning() << "No events will be processed from external input." << endmsg;
115  }
116  }
117 
118  // Setup access to histogramming services
119  m_histoDataMgrSvc = serviceLocator()->service("HistogramDataSvc");
120  if( !m_histoDataMgrSvc.isValid() ) {
121  fatal() << "Error retrieving HistogramDataSvc." << endmsg;
122  return sc;
123  }
124  // Setup histogram persistency
125  m_histoPersSvc = serviceLocator()->service("HistogramPersistencySvc");
126  if( !m_histoPersSvc.isValid() ) {
127  warning() << "Histograms cannot not be saved - though required." << endmsg;
128  return sc;
129  }
130 
131  return StatusCode::SUCCESS;
132 }
133 //--------------------------------------------------------------------------------------------
134 // implementation of IService::reinitialize
135 //--------------------------------------------------------------------------------------------
137 
138  // Initialize the base class
140  if( !sc.isSuccess() ) {
141  DEBMSG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
142  return sc;
143  }
144 
145  // Check to see whether a new Event Selector has been specified
146  setProperty(m_appMgrProperty->getProperty("EvtSel"));
147  if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
148  SmartIF<IService> theSvc(serviceLocator()->service("EventSelector"));
149  SmartIF<IEvtSelector> theEvtSel(theSvc);
150  if( theEvtSel.isValid() && ( theEvtSel.get() != m_evtSelector.get() ) ) {
151  // Setup Event Selector
152  if ( m_evtSelector.get() && m_evtContext ) {
153  // Need to release context before switching to new event selector
154  m_evtSelector->releaseContext(m_evtContext);
155  m_evtContext = 0;
156  }
157  m_evtSelector = theEvtSel;
158  if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
159  sc = theSvc->reinitialize();
160  if( !sc.isSuccess() ) {
161  error() << "Failure Reinitializing EventSelector "
162  << theSvc->name( ) << endmsg;
163  return sc;
164  }
165  }
166  else {
167  sc = theSvc->sysInitialize();
168  if( !sc.isSuccess() ) {
169  error() << "Failure Initializing EventSelector "
170  << theSvc->name( ) << endmsg;
171  return sc;
172  }
173  }
174  sc = m_evtSelector->createContext(m_evtContext);
175  if( !sc.isSuccess() ) {
176  error() << "Can not create Context " << theSvc->name( ) << endmsg;
177  return sc;
178  }
179  info() << "EventSelector service changed to "
180  << theSvc->name( ) << endmsg;
181  }
182  else if ( m_evtSelector.isValid() ) {
183  if ( m_evtContext ) {
184  m_evtSelector->releaseContext(m_evtContext);
185  m_evtContext = 0;
186  }
187  sc = m_evtSelector->createContext(m_evtContext);
188  if( !sc.isSuccess() ) {
189  error() << "Can not create Context " << theSvc->name( ) << endmsg;
190  return sc;
191  }
192  }
193  }
194  else if ( m_evtSelector.isValid() && m_evtContext ) {
195  m_evtSelector->releaseContext(m_evtContext);
196  m_evtSelector = 0;
197  m_evtContext = 0;
198  }
199  return StatusCode::SUCCESS;
200 }
201 
202 
203 //--------------------------------------------------------------------------------------------
204 // implementation of IService::stop
205 //--------------------------------------------------------------------------------------------
207  if ( ! m_endEventFired ) {
208  // Fire pending EndEvent incident
210  m_endEventFired = true;
211  }
212  return MinimalEventLoopMgr::stop();
213 }
214 
215 //--------------------------------------------------------------------------------------------
216 // implementation of IAppMgrUI::finalize
217 //--------------------------------------------------------------------------------------------
219  StatusCode sc;
220 
221  // Finalize base class
223  if (! sc.isSuccess()) {
224  error() << "Error finalizing base class" << endmsg;
225  return sc;
226  }
227 
228  // Save Histograms Now
229  if ( m_histoPersSvc != 0 ) {
230  HistogramAgent agent;
231  sc = m_histoDataMgrSvc->traverseTree( &agent );
232  if( sc.isSuccess() ) {
233  IDataSelector* objects = agent.selectedObjects();
234  // skip /stat entry!
235  if ( objects->size() > 0 ) {
236  IDataSelector::iterator i;
237  for ( i = objects->begin(); i != objects->end(); i++ ) {
238  IOpaqueAddress* pAddr = 0;
239  StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
240  if ( iret.isSuccess() ) {
241  (*i)->registry()->setAddress(pAddr);
242  }
243  else {
244  sc = iret;
245  }
246  }
247  for ( i = objects->begin(); i != objects->end(); i++ ) {
248  IRegistry* reg = (*i)->registry();
249  StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
250  if ( !iret.isSuccess() ) {
251  sc = iret;
252  }
253  }
254  }
255  if ( sc.isSuccess() ) {
256  info() << "Histograms converted successfully according to request." << endmsg;
257  }
258  else {
259  error() << "Error while saving Histograms." << endmsg;
260  }
261  }
262  else {
263  error() << "Error while traversing Histogram data store" << endmsg;
264  }
265  }
266 
267  // Release event selector context
268  if ( m_evtSelector && m_evtContext ) {
269  m_evtSelector->releaseContext(m_evtContext).ignore();
270  m_evtContext = 0;
271  }
272 
273  // Release all interfaces...
274  m_histoDataMgrSvc = 0;
275  m_histoPersSvc = 0;
276 
277  m_evtSelector = 0;
278  m_evtDataSvc = 0;
279  m_evtDataMgrSvc = 0;
280 
281  return StatusCode::SUCCESS;
282 }
283 
284 //--------------------------------------------------------------------------------------------
285 // executeEvent(void* par)
286 //--------------------------------------------------------------------------------------------
288 
289  // Fire BeginEvent "Incident"
291  // An incident may schedule a stop, in which case is better to exit before the actual execution.
292  if ( m_scheduledStop ) {
293  always() << "Terminating event processing loop due to a stop scheduled by an incident listener" << endmsg;
294  return StatusCode::SUCCESS;
295  }
296 
297  // Execute Algorithms
301 
302  // Check if there was an error processing current event
303  if( UNLIKELY(!sc.isSuccess()) ){
304  error() << "Terminating event processing loop due to errors" << endmsg;
305  }
306  return sc;
307 }
308 
309 //--------------------------------------------------------------------------------------------
310 // IEventProcessing::executeRun
311 //--------------------------------------------------------------------------------------------
313  StatusCode sc;
314  // initialize the base class
315  sc = MinimalEventLoopMgr::executeRun(maxevt);
316  return sc;
317 }
318 
319 //--------------------------------------------------------------------------------------------
320 // implementation of IAppMgrUI::nextEvent
321 //--------------------------------------------------------------------------------------------
323  static int total_nevt = 0;
324  DataObject* pObject = 0;
326 
327  // loop over events if the maxevt (received as input) if different from -1.
328  // if evtmax is -1 it means infinite loop
329  for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt); nevt++, total_nevt++) {
330 
331  // Check if there is a scheduled stop issued by some algorithm/service
332  if ( m_scheduledStop ) {
333  m_scheduledStop = false;
334  always() << "Terminating event processing loop due to scheduled stop" << endmsg;
335  break;
336  }
337  // Clear the event store, if used in the event loop
338  if( 0 != total_nevt ) {
339 
340  if ( ! m_endEventFired ) {
341  // Fire EndEvent "Incident" (it is considered part of the clearing of the TS)
343  m_endEventFired = true;
344  }
345  sc = m_evtDataMgrSvc->clearStore();
346  if( !sc.isSuccess() ) {
347  DEBMSG << "Clear of Event data store failed" << endmsg;
348  }
349  }
350 
351  // Setup event in the event store
352  if( m_evtContext ) {
353  IOpaqueAddress* addr = 0;
354  // Only if there is a EventSelector
355  sc = getEventRoot(addr);
356  if( !sc.isSuccess() ) {
357  info() << "No more events in event selection " << endmsg;
358  break;
359  }
360  // Set root clears the event data store first
361  sc = m_evtDataMgrSvc->setRoot ("/Event", addr);
362  if( !sc.isSuccess() ) {
363  warning() << "Error declaring event root address." << endmsg;
364  continue;
365  }
366  sc = m_evtDataSvc->retrieveObject("/Event", pObject);
367  if( !sc.isSuccess() ) {
368  warning() << "Unable to retrieve Event root object" << endmsg;
369  break;
370  }
371  }
372  else {
373  sc = m_evtDataMgrSvc->setRoot ("/Event", new DataObject());
374  if( !sc.isSuccess() ) {
375  warning() << "Error declaring event root DataObject" << endmsg;
376  }
377  }
378  // Execute event for all required algorithms
379  sc = executeEvent(NULL);
380  m_endEventFired = false;
381  if( !sc.isSuccess() ){
382  error() << "Terminating event processing loop due to errors" << endmsg;
384  return sc;
385  }
386  }
387  return StatusCode::SUCCESS;
388 }
389 
392  refpAddr = 0;
394  if ( !sc.isSuccess() ) {
395  return sc;
396  }
397  // Create root address and assign address to data service
398  sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
399  if( !sc.isSuccess() ) {
400  sc = m_evtSelector->next(*m_evtContext);
401  if ( sc.isSuccess() ) {
402  sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
403  if ( !sc.isSuccess() ) {
404  warning() << "Error creating IOpaqueAddress." << endmsg;
405  }
406  }
407  }
408  return sc;
409 }
const std::string BeginEvent
Processing of a new event has started.
Definition: Incident.h:60
virtual StatusCode executeEvent(void *par)
implementation of IEventProcessor::executeEvent(void* par)
#define UNLIKELY(x)
Definition: Kernel.h:127
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
bool m_scheduledStop
Scheduled stop of event processing.
const std::string BeginProcessing
Incident raised just before entering loop over the algorithms.
Definition: Incident.h:84
SmartIF< IConversionSvc > m_histoPersSvc
Reference to the Histogram Persistency Service.
Definition: EventLoopMgr.h:56
virtual StatusCode setProperty(const Property &p)
Set the property by property.
Definition: Service.cpp:342
virtual StatusCode initialize()
implementation of IService::initialize
const std::string EndProcessing
Incident raised just after the loop over the algorithms (note: before the execution of OutputStreams)...
Definition: Incident.h:86
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
HistogramAgent base in charge of collecting all the refereces to DataObjects in a transient store tha...
std::string m_histPersName
Name of the Hist Pers type.
Definition: EventLoopMgr.h:58
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
virtual StatusCode executeRun(int maxevt)
implementation of IEventProcessor::executeRun()
Class definition of EventLoopMgr.
Definition: EventLoopMgr.h:39
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
const std::string EndEvent
Processing of the last event has finished.
Definition: Incident.h:61
virtual StatusCode executeEvent(void *par)
implementation of IEventProcessor::executeEvent(void* par)
virtual StatusCode initialize()
implementation of IService::initialize
virtual StatusCode reinitialize()
implementation of IService::reinitialize
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
bool m_warnings
Flag to disable warning messages when using external input.
Definition: EventLoopMgr.h:65
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
StatusCode getEventRoot(IOpaqueAddress *&refpAddr)
Create event address using event selector.
list nevt
Definition: ana.py:134
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
IDataSelector * selectedObjects()
Return the set of selected DataObjects.
SmartIF< IIncidentSvc > m_incidentSvc
Reference to the incident service.
StatusCode setAppReturnCode(SmartIF< IProperty > &appmgr, int value, bool force=false)
Set the application return code.
Definition: AppReturnCode.h:50
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual StatusCode reinitialize()
implementation of IService::reinitialize
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
EventLoopMgr(const std::string &nam, ISvcLocator *svcLoc)
Standard Constructor.
virtual StatusCode stop()
implementation of IService::stop
SmartIF< IDataManagerSvc > m_evtDataMgrSvc
Reference to the Event Data Service's IDataManagerSvc interface.
Definition: EventLoopMgr.h:44
virtual StatusCode finalize()
implementation of IService::finalize
virtual StatusCode executeRun(int maxevt)
implementation of IEventProcessor::executeRun( )
SmartIF< IProperty > m_appMgrProperty
Property interface of ApplicationMgr.
Definition: EventLoopMgr.h:60
IEvtSelector::Context * m_evtContext
Event Iterator.
Definition: EventLoopMgr.h:50
SmartIF< IEvtSelector > m_evtSelector
Reference to the Event Selector.
Definition: EventLoopMgr.h:48
virtual StatusCode finalize()
implementation of IService::finalize
#define DEBMSG
Base class for all Incidents (computing events).
Definition: Incident.h:16
virtual ~EventLoopMgr()
Standard Destructor.
bool m_endEventFired
Flag to avoid to fire the EnvEvent incident twice in a row (and also not before the first event) ...
Definition: EventLoopMgr.h:63
SmartIF< IDataProviderSvc > m_evtDataSvc
Reference to the Event Data Service's IDataProviderSvc interface.
Definition: EventLoopMgr.h:46
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:142
This is the default processing manager of the application manager.
virtual StatusCode stop()
implementation of IService::stop
virtual StatusCode nextEvent(int maxevt)
implementation of IService::nextEvent
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:107
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:211
std::string m_evtsel
Event selector.
Definition: EventLoopMgr.h:52
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
const int AlgorithmFailure
Definition: AppReturnCode.h:24
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
list i
Definition: ana.py:128
std::vector< DataObject * > IDataSelector
This is only a placeholder to allow me compiling until the responsible guy does his work! M...
Definition: IDataSelector.h:8
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< IDataManagerSvc > m_histoDataMgrSvc
Reference to the Histogram Data Service.
Definition: EventLoopMgr.h:54
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336