Gaudi Framework, version v23r6

Home   Generated: Wed Jan 30 2013
 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"
14 
15 #include "HistogramAgent.h"
16 #include "EventLoopMgr.h"
17 
18 
19 // Instantiation of a static factory class used by clients to create instances of this service
21 
22 
23 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
24 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
25 
26 #define DEBMSG ON_DEBUG debug()
27 #define VERMSG ON_VERBOSE verbose()
28 
29 //--------------------------------------------------------------------------------------------
30 // Standard Constructor
31 //--------------------------------------------------------------------------------------------
33 : MinimalEventLoopMgr(nam, svcLoc)
34 {
36  m_histoPersSvc = 0;
37  m_evtDataMgrSvc = 0;
38  m_evtDataSvc = 0;
39  m_evtSelector = 0;
40  m_evtContext = 0;
41  m_endEventFired = true;
42 
43  // Declare properties
44  declareProperty("HistogramPersistency", m_histPersName = "");
45  declareProperty("EvtSel", m_evtsel );
46  declareProperty("Warnings",m_warnings=true,
47  "Set this property to false to suppress warning messages");
48 }
49 
50 //--------------------------------------------------------------------------------------------
51 // Standard Destructor
52 //--------------------------------------------------------------------------------------------
54  if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
55  if( m_histoPersSvc ) m_histoPersSvc->release();
56  if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
57  if( m_evtDataSvc ) m_evtDataSvc->release();
58  if( m_evtSelector ) m_evtSelector->release();
59  if( m_evtContext ) delete m_evtContext;
60 }
61 
62 //--------------------------------------------------------------------------------------------
63 // implementation of IAppMgrUI::initialize
64 //--------------------------------------------------------------------------------------------
66  // Initialize the base class
68  if( !sc.isSuccess() ) {
69  DEBMSG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
70  return sc;
71  }
72 
73  // Setup access to event data services
74  m_evtDataMgrSvc = serviceLocator()->service("EventDataSvc");
75  if( !m_evtDataMgrSvc.isValid() ) {
76  fatal() << "Error retrieving EventDataSvc interface IDataManagerSvc." << endmsg;
77  return StatusCode::FAILURE;
78  }
79  m_evtDataSvc = serviceLocator()->service("EventDataSvc");
80  if( !m_evtDataSvc.isValid() ) {
81  fatal() << "Error retrieving EventDataSvc interface IDataProviderSvc." << endmsg;
82  return StatusCode::FAILURE;
83  }
84 
85  // Obtain the IProperty of the ApplicationMgr
87  if ( ! m_appMgrProperty.isValid() ) {
88  fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
89  return StatusCode::FAILURE;
90  }
91 
92  // We do not expect a Event Selector necessarily being declared
93  setProperty(m_appMgrProperty->getProperty("EvtSel")).ignore();
94 
95  if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
96  m_evtSelector = serviceLocator()->service("EventSelector");
97  if( m_evtSelector.isValid() ) {
98  // Setup Event Selector
99  sc=m_evtSelector->createContext(m_evtContext);
100  if( !sc.isSuccess() ) {
101  fatal() << "Can not create the event selector Context." << endmsg;
102  return sc;
103  }
104  }
105  else {
106  fatal() << "EventSelector not found." << endmsg;
107  return sc;
108  }
109  }
110  else {
111  m_evtSelector = 0;
112  m_evtContext = 0;
113  if ( m_warnings ) {
114  warning() << "Unable to locate service \"EventSelector\" " << endmsg;
115  warning() << "No events will be processed from external input." << endmsg;
116  }
117  }
118 
119  // Setup access to histogramming services
120  m_histoDataMgrSvc = serviceLocator()->service("HistogramDataSvc");
121  if( !m_histoDataMgrSvc.isValid() ) {
122  fatal() << "Error retrieving HistogramDataSvc." << endmsg;
123  return sc;
124  }
125  // Setup histogram persistency
126  m_histoPersSvc = serviceLocator()->service("HistogramPersistencySvc");
127  if( !m_histoPersSvc.isValid() ) {
128  warning() << "Histograms cannot not be saved - though required." << endmsg;
129  return sc;
130  }
131 
132  return StatusCode::SUCCESS;
133 }
134 //--------------------------------------------------------------------------------------------
135 // implementation of IService::reinitialize
136 //--------------------------------------------------------------------------------------------
138 
139  // Initialize the base class
141  if( !sc.isSuccess() ) {
142  DEBMSG << "Error Initializing base class MinimalEventLoopMgr." << endmsg;
143  return sc;
144  }
145 
146  // Check to see whether a new Event Selector has been specified
147  setProperty(m_appMgrProperty->getProperty("EvtSel"));
148  if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
149  SmartIF<IService> theSvc(serviceLocator()->service("EventSelector"));
150  SmartIF<IEvtSelector> theEvtSel(theSvc);
151  if( theEvtSel.isValid() && ( theEvtSel.get() != m_evtSelector.get() ) ) {
152  // Setup Event Selector
153  if ( m_evtSelector.get() && m_evtContext ) {
154  // Need to release context before switching to new event selector
155  m_evtSelector->releaseContext(m_evtContext);
156  m_evtContext = 0;
157  }
158  m_evtSelector = theEvtSel;
159  if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
160  sc = theSvc->reinitialize();
161  if( !sc.isSuccess() ) {
162  error() << "Failure Reinitializing EventSelector "
163  << theSvc->name( ) << endmsg;
164  return sc;
165  }
166  }
167  else {
168  sc = theSvc->sysInitialize();
169  if( !sc.isSuccess() ) {
170  error() << "Failure Initializing EventSelector "
171  << theSvc->name( ) << endmsg;
172  return sc;
173  }
174  }
175  sc = m_evtSelector->createContext(m_evtContext);
176  if( !sc.isSuccess() ) {
177  error() << "Can not create Context " << theSvc->name( ) << endmsg;
178  return sc;
179  }
180  info() << "EventSelector service changed to "
181  << theSvc->name( ) << endmsg;
182  }
183  else if ( m_evtSelector.isValid() ) {
184  if ( m_evtContext ) {
185  m_evtSelector->releaseContext(m_evtContext);
186  m_evtContext = 0;
187  }
188  sc = m_evtSelector->createContext(m_evtContext);
189  if( !sc.isSuccess() ) {
190  error() << "Can not create Context " << theSvc->name( ) << endmsg;
191  return sc;
192  }
193  }
194  }
195  else if ( m_evtSelector.isValid() && m_evtContext ) {
196  m_evtSelector->releaseContext(m_evtContext);
197  m_evtSelector = 0;
198  m_evtContext = 0;
199  }
200  return StatusCode::SUCCESS;
201 }
202 
203 
204 //--------------------------------------------------------------------------------------------
205 // implementation of IService::stop
206 //--------------------------------------------------------------------------------------------
208  if ( ! m_endEventFired ) {
209  // Fire pending EndEvent incident
211  m_endEventFired = true;
212  }
213  return MinimalEventLoopMgr::stop();
214 }
215 
216 //--------------------------------------------------------------------------------------------
217 // implementation of IAppMgrUI::finalize
218 //--------------------------------------------------------------------------------------------
220  StatusCode sc;
221 
222  // Finalize base class
224  if (! sc.isSuccess()) {
225  error() << "Error finalizing base class" << endmsg;
226  return sc;
227  }
228 
229  // Save Histograms Now
230  if ( m_histoPersSvc != 0 ) {
231  HistogramAgent agent;
232  sc = m_histoDataMgrSvc->traverseTree( &agent );
233  if( sc.isSuccess() ) {
234  IDataSelector* objects = agent.selectedObjects();
235  // skip /stat entry!
236  if ( objects->size() > 0 ) {
238  for ( i = objects->begin(); i != objects->end(); i++ ) {
239  IOpaqueAddress* pAddr = 0;
240  StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
241  if ( iret.isSuccess() ) {
242  (*i)->registry()->setAddress(pAddr);
243  }
244  else {
245  sc = iret;
246  }
247  }
248  for ( i = objects->begin(); i != objects->end(); i++ ) {
249  IRegistry* reg = (*i)->registry();
250  StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
251  if ( !iret.isSuccess() ) {
252  sc = iret;
253  }
254  }
255  }
256  if ( sc.isSuccess() ) {
257  info() << "Histograms converted successfully according to request." << endmsg;
258  }
259  else {
260  error() << "Error while saving Histograms." << endmsg;
261  }
262  }
263  else {
264  error() << "Error while traversing Histogram data store" << endmsg;
265  }
266  }
267 
268  // Release event selector context
269  if ( m_evtSelector && m_evtContext ) {
270  m_evtSelector->releaseContext(m_evtContext).ignore();
271  m_evtContext = 0;
272  }
273 
274  // Release all interfaces...
275  m_histoDataMgrSvc = 0;
276  m_histoPersSvc = 0;
277 
278  m_evtSelector = 0;
279  m_evtDataSvc = 0;
280  m_evtDataMgrSvc = 0;
281 
282  return StatusCode::SUCCESS;
283 }
284 
285 //--------------------------------------------------------------------------------------------
286 // executeEvent(void* par)
287 //--------------------------------------------------------------------------------------------
289 
290  // Fire BeginEvent "Incident"
292  // An incident may schedule a stop, in which case is better to exit before the actual execution.
293  if ( m_scheduledStop ) {
294  always() << "Terminating event processing loop due to a stop scheduled by an incident listener" << endmsg;
295  return StatusCode::SUCCESS;
296  }
297 
298  // Execute Algorithms
302 
303  // Check if there was an error processing current event
304  if( UNLIKELY(!sc.isSuccess()) ){
305  error() << "Terminating event processing loop due to errors" << endmsg;
306  }
307  return sc;
308 }
309 
310 //--------------------------------------------------------------------------------------------
311 // IEventProcessing::executeRun
312 //--------------------------------------------------------------------------------------------
314  StatusCode sc;
315  // initialize the base class
316  sc = MinimalEventLoopMgr::executeRun(maxevt);
317  return sc;
318 }
319 
320 //--------------------------------------------------------------------------------------------
321 // implementation of IAppMgrUI::nextEvent
322 //--------------------------------------------------------------------------------------------
324  static int total_nevt = 0;
325  DataObject* pObject = 0;
327 
328  // loop over events if the maxevt (received as input) if different from -1.
329  // if evtmax is -1 it means infinite loop
330  for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt); nevt++, total_nevt++) {
331 
332  // Check if there is a scheduled stop issued by some algorithm/service
333  if ( m_scheduledStop ) {
334  m_scheduledStop = false;
335  always() << "Terminating event processing loop due to scheduled stop" << endmsg;
336  break;
337  }
338  // Clear the event store, if used in the event loop
339  if( 0 != total_nevt ) {
340 
341  if ( ! m_endEventFired ) {
342  // Fire EndEvent "Incident" (it is considered part of the clearing of the TS)
344  m_endEventFired = true;
345  }
346  sc = m_evtDataMgrSvc->clearStore();
347  if( !sc.isSuccess() ) {
348  DEBMSG << "Clear of Event data store failed" << endmsg;
349  }
350  }
351 
352  // Setup event in the event store
353  if( m_evtContext ) {
354  IOpaqueAddress* addr = 0;
355  // Only if there is a EventSelector
356  sc = getEventRoot(addr);
357  if( !sc.isSuccess() ) {
358  info() << "No more events in event selection " << endmsg;
359  break;
360  }
361  // Set root clears the event data store first
362  sc = m_evtDataMgrSvc->setRoot ("/Event", addr);
363  if( !sc.isSuccess() ) {
364  warning() << "Error declaring event root address." << endmsg;
365  continue;
366  }
367  sc = m_evtDataSvc->retrieveObject("/Event", pObject);
368  if( !sc.isSuccess() ) {
369  warning() << "Unable to retrieve Event root object" << endmsg;
370  break;
371  }
372  }
373  else {
374  sc = m_evtDataMgrSvc->setRoot ("/Event", new DataObject());
375  if( !sc.isSuccess() ) {
376  warning() << "Error declaring event root DataObject" << endmsg;
377  }
378  }
379  // Execute event for all required algorithms
380  sc = executeEvent(NULL);
381  m_endEventFired = false;
382  if( !sc.isSuccess() ){
383  error() << "Terminating event processing loop due to errors" << endmsg;
385  return sc;
386  }
387  }
388  return StatusCode::SUCCESS;
389 }
390 
393  refpAddr = 0;
395  if ( !sc.isSuccess() ) {
396  return sc;
397  }
398  // Create root address and assign address to data service
399  sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
400  if( !sc.isSuccess() ) {
401  sc = m_evtSelector->next(*m_evtContext);
402  if ( sc.isSuccess() ) {
403  sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
404  if ( !sc.isSuccess() ) {
405  warning() << "Error creating IOpaqueAddress." << endmsg;
406  }
407  }
408  }
409  return sc;
410 }

Generated at Wed Jan 30 2013 17:13:38 for Gaudi Framework, version v23r6 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004