Gaudi Framework, version v25r0

Home   Generated: Mon Feb 17 2014
 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 //--------------------------------------------------------------------------------------------
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 ) {
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 }

Generated at Mon Feb 17 2014 14:37:40 for Gaudi Framework, version v25r0 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004