BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
OfflineEventLoopMgr.cxx
Go to the documentation of this file.
1// $Id: OfflineEventLoopMgr.cxx,v 1.11 2011/02/15 06:57:12 dengzy Exp $
2//#define GAUDISVC_EVENTLOOPMGR_CPP
3
4#include "GaudiKernel/SmartIF.h"
5#include "GaudiKernel/Incident.h"
6#include "GaudiKernel/MsgStream.h"
7#include "GaudiKernel/SvcFactory.h"
8#include "GaudiKernel/DataObject.h"
9#include "GaudiKernel/IIncidentSvc.h"
10#include "GaudiKernel/IEvtSelector.h"
11#include "GaudiKernel/IDataManagerSvc.h"
12#include "GaudiKernel/IDataProviderSvc.h"
13#include "GaudiKernel/IConversionSvc.h"
14#include "GaudiKernel/SmartDataPtr.h"
15#include "GaudiKernel/IAlgorithm.h"
16#include "ApplicationMgr/HistogramAgent.h"
19#include "EventModel/Event.h"
21
22// Instantiation of a static factory class used by clients to create instances of this service
23//static SvcFactory<OfflineEventLoopMgr> s_EventLoopMgrFactory;
24//const IFactory& OfflineEventLoopMgrFactory = s_EventLoopMgrFactory;
25extern const CLID& CLID_Event;
26
27//--------------------------------------------------------------------------------------------
28// Standard Constructor
29//--------------------------------------------------------------------------------------------
30OfflineEventLoopMgr::OfflineEventLoopMgr(const std::string& nam, ISvcLocator* svcLoc)
31: MinimalEventLoopMgr(nam, svcLoc)
32{
35 m_incidentSvc = 0;
37 m_evtDataSvc = 0;
38 m_evtSelector = 0;
39 m_evtContext = 0;
40 m_runNo = -1.1;
41 // Declare properties
42 declareProperty("HistogramPersistency", m_histPersName = "");
43 declareProperty( "EvtSel", m_evtsel );
44}
45
46//--------------------------------------------------------------------------------------------
47// Standard Destructor
48//--------------------------------------------------------------------------------------------
50 if( m_histoDataMgrSvc ) m_histoDataMgrSvc->release();
51 if( m_histoPersSvc ) m_histoPersSvc->release();
52 if( m_incidentSvc ) m_incidentSvc->release();
53 if( m_evtDataMgrSvc ) m_evtDataMgrSvc->release();
54 if( m_evtDataSvc ) m_evtDataSvc->release();
55 if( m_evtSelector ) m_evtSelector->release();
56 if( m_evtContext ) delete m_evtContext;
57}
58
59//--------------------------------------------------------------------------------------------
60// implementation of IAppMgrUI::initalize
61//--------------------------------------------------------------------------------------------
63 MsgStream log(msgSvc(), name());
64
65 // initilaize the base class
66 StatusCode sc = MinimalEventLoopMgr::initialize();
67 if( !sc.isSuccess() ) {
68 log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endreq;
69 return sc;
70 }
71
72 // Setup access to event data services
73 sc = serviceLocator()->service("EventDataSvc", m_evtDataMgrSvc, true);
74 if( !sc.isSuccess() ) {
75 log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataManagerSvc." << endreq;
76 return sc;
77 }
78 sc = serviceLocator()->service("EventDataSvc", m_evtDataSvc, true);
79 if( !sc.isSuccess() ) {
80 log << MSG::FATAL << "Error retrieving EventDataSvc interface IDataProviderSvc." << endreq;
81 return sc;
82 }
83
84 // Get the references to the services that are needed by the ApplicationMgr itself
85 sc = serviceLocator()->service("IncidentSvc", m_incidentSvc, true);
86 if( !sc.isSuccess() ) {
87 log << MSG::FATAL << "Error retrieving IncidentSvc." << endreq;
88 return sc;
89 }
90
91 // Obtain the IProperty of the ApplicationMgr
92 // SmartIF<IProperty> prpMgr(IID_IProperty, serviceLocator());
93 SmartIF<IProperty> prpMgr(serviceLocator());
94 if ( ! prpMgr.isValid() ) {
95 log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endreq;
96 return StatusCode::FAILURE;
97 }
98 else {
99 m_appMgrProperty = prpMgr;
100 }
101
102 // We do not expect a Event Selector necessarily being declared
103 setProperty(m_appMgrProperty->getProperty("EvtSel"));
104
105 if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
106 sc = serviceLocator()->service( "EventSelector", m_evtSelector, true );
107 if( sc.isSuccess() ) {
108 // Setup Event Selector
109 sc=m_evtSelector->createContext(m_evtContext);
110 if( !sc.isSuccess() ) {
111 log << MSG::FATAL << "Can not create the event selector Context." << endreq;
112 return sc;
113 }
114 }
115 else {
116 log << MSG::FATAL << "EventSelector not found." << endreq;
117 return sc;
118 }
119 }
120 else {
121 m_evtSelector = 0;
122 m_evtContext = 0;
123 log << MSG::WARNING << "Unable to locate service \"EventSelector\" " << endreq;
124 log << MSG::WARNING << "No events will be processed from external input." << endreq;
125 }
126
127 // Setup access to histogramming services
128 sc = serviceLocator()->service("HistogramDataSvc", m_histoDataMgrSvc, true);
129 if( !sc.isSuccess() ) {
130 log << MSG::FATAL << "Error retrieving HistogramDataSvc." << endreq;
131 return sc;
132 }
133 // Setup histogram persistency
134 sc = serviceLocator()->service("HistogramPersistencySvc", m_histoPersSvc, true );
135 if( !sc.isSuccess() ) {
136 log << MSG::WARNING << "Histograms cannot not be saved - though required." << endreq;
137 return sc;
138 }
139
140 return StatusCode::SUCCESS;
141}
142//--------------------------------------------------------------------------------------------
143// implementation of IService::reinitialize
144//--------------------------------------------------------------------------------------------
146 MsgStream log(msgSvc(), name());
147
148 // initilaize the base class
149 StatusCode sc = MinimalEventLoopMgr::reinitialize();
150 if( !sc.isSuccess() ) {
151 log << MSG::DEBUG << "Error Initializing base class MinimalEventLoopMgr." << endreq;
152 return sc;
153 }
154
155 // Check to see whether a new Event Selector has been specified
156 setProperty(m_appMgrProperty->getProperty("EvtSel"));
157 if( m_evtsel != "NONE" || m_evtsel.length() == 0) {
158 IEvtSelector* theEvtSel;
159 IService* theSvc;
160 sc = serviceLocator()->service( "EventSelector", theEvtSel );
161 sc = serviceLocator()->service( "EventSelector", theSvc );
162 if( sc.isSuccess() && ( theEvtSel != m_evtSelector ) ) {
163 // Setup Event Selector
164 m_evtSelector = theEvtSel;
165 // if (theSvc->state() == IService::INITIALIZED) {
166 if (theSvc->FSMState() == Gaudi::StateMachine::INITIALIZED) {
167 sc = theSvc->reinitialize();
168 if( !sc.isSuccess() ) {
169 log << MSG::ERROR << "Failure Reinitializing EventSelector "
170 << theSvc->name( ) << endreq;
171 return sc;
172 }
173 }
174 else {
175 sc = theSvc->initialize();
176 if( !sc.isSuccess() ) {
177 log << MSG::ERROR << "Failure Initializing EventSelector "
178 << theSvc->name( ) << endreq;
179 return sc;
180 }
181 }
182 sc = m_evtSelector->createContext(m_evtContext);
183 if( !sc.isSuccess() ) {
184 log << MSG::ERROR << "Can not create Context "
185 << theSvc->name( ) << endreq;
186 return sc;
187 }
188 log << MSG::INFO << "EventSelector service changed to "
189 << theSvc->name( ) << endreq;
190 }
191 else if ( m_evtSelector && m_evtContext ) {
192 m_evtSelector->releaseContext(m_evtContext);
193 m_evtContext = 0;
194 sc = m_evtSelector->createContext(m_evtContext);
195 if( !sc.isSuccess() ) {
196 log << MSG::ERROR << "Can not create Context "
197 << theSvc->name( ) << endreq;
198 return sc;
199 }
200 }
201 }
202 else if ( m_evtSelector && m_evtContext ) {
203 m_evtSelector->releaseContext(m_evtContext);
204 m_evtSelector = 0;
205 m_evtContext = 0;
206 }
207 return StatusCode::SUCCESS;
208}
209
210//--------------------------------------------------------------------------------------------
211// implementation of IAppMgrUI::finalize
212//--------------------------------------------------------------------------------------------
214 StatusCode sc;
215 MsgStream log(msgSvc(), name());
216
217 // Finalize base class
218 MinimalEventLoopMgr::finalize();
219
220 // Save Histograms Now
221 if ( 0 != m_histoPersSvc ) {
222 HistogramAgent agent;
223 sc = m_histoDataMgrSvc->traverseTree( &agent );
224 if( sc.isSuccess() ) {
225 IDataSelector* objects = agent.selectedObjects();
226 // skip /stat entry!
227 if ( objects->size() > 0 ) {
228 IDataSelector::iterator i;
229 for ( i = objects->begin(); i != objects->end(); i++ ) {
230 IOpaqueAddress* pAddr = 0;
231 StatusCode iret = m_histoPersSvc->createRep(*i, pAddr);
232 if ( iret.isSuccess() ) {
233 (*i)->registry()->setAddress(pAddr);
234 }
235 else {
236 sc = iret;
237 }
238 }
239 for ( i = objects->begin(); i != objects->end(); i++ ) {
240 IRegistry* reg = (*i)->registry();
241 StatusCode iret = m_histoPersSvc->fillRepRefs(reg->address(), *i);
242 if ( !iret.isSuccess() ) {
243 sc = iret;
244 }
245 }
246 }
247 if ( sc.isSuccess() ) {
248 log << MSG::INFO << "Histograms converted successfully according to request." << endreq;
249 }
250 else {
251 log << MSG::ERROR << "Error while saving Histograms." << endreq;
252 }
253 }
254 else {
255 log << MSG::ERROR << "Error while traversing Histogram data store" << endreq;
256 }
257 }
258
259 // Release evemt selector context
260 if ( m_evtSelector && m_evtContext ) {
261 m_evtSelector->releaseContext(m_evtContext);
262 m_evtContext = 0;
263 }
264
265 // Release all interfaces...
266 m_histoDataMgrSvc = releaseInterface(m_histoDataMgrSvc);
267 m_histoPersSvc = releaseInterface(m_histoPersSvc);
268
269 m_evtSelector = releaseInterface(m_evtSelector);
270 m_incidentSvc = releaseInterface(m_incidentSvc);
271 m_evtDataSvc = releaseInterface(m_evtDataSvc);
272 m_evtDataMgrSvc = releaseInterface(m_evtDataMgrSvc);
273
274 return StatusCode::SUCCESS;
275}
276
277
278//--------------------------------------------------------------------------------------------
279// executeEvent(void* par)
280//--------------------------------------------------------------------------------------------
282 MsgStream log(msgSvc(), name());
283 // Fire BeginEvent "Incident"
284 m_incidentSvc->fireIncident(Incident(name(),IncidentType::BeginEvent));
285 int runNo;
286 SmartDataPtr<Event::EventHeader> evt(m_evtDataSvc,"/Event/EventHeader");
287 if( evt ){
288 runNo = evt -> runNumber();
289 log << MSG::INFO <<"The runNumber of current event is "<<runNo<<endreq;
290 }
291
292 else{
293 log << MSG::ERROR << "Error accessing Event" <<endreq;
294 return StatusCode::FAILURE;
295 }
296 if(runNo!=m_runNo) {
297 log << MSG::INFO <<"fireIncident of begin run"<<endreq;
298 m_incidentSvc->fireIncident(Incident(name(),"NewRun"));
299 if(m_runNo==-1.1){
300 ListAlg::iterator it_topAlg = m_topAlgList.begin();
301 for (;it_topAlg!=m_topAlgList.end();it_topAlg++){
302 (*it_topAlg)->beginRun();}
303 }
304 m_runNo = runNo;
305 }
306
307 // Execute Algorithms
308 StatusCode sc = MinimalEventLoopMgr::executeEvent(par);
309 // Fire EndEvent "Incident"
310 m_incidentSvc->fireIncident(Incident(name(),IncidentType::EndEvent));
311
312 // Check if there was an error processing current event
313 if( !sc.isSuccess() ){
314 MsgStream log( msgSvc(), name() );
315 log << MSG::ERROR << "Terminating event processing loop due to errors" << endreq;
316 }
317
318 return sc;
319}
320
321//--------------------------------------------------------------------------------------------
322// IEventProcessing::executeRun
323//--------------------------------------------------------------------------------------------
324StatusCode OfflineEventLoopMgr::executeRun( int maxevt ) {
325 StatusCode sc;
326 // initialize the base class
327 sc = MinimalEventLoopMgr::executeRun(maxevt);
328 return sc;
329}
330
331//--------------------------------------------------------------------------------------------
332// implementation of IAppMgrUI::nextEvent
333//--------------------------------------------------------------------------------------------
334StatusCode OfflineEventLoopMgr::nextEvent(int maxevt) {
335 static int total_nevt = 0;
336 DataObject* pObject = 0;
337 StatusCode sc;
338 MsgStream log( msgSvc(), name() );
339
340 //yzhang add
341 //begin run
342/* ListAlg::iterator it_topAlg = m_topAlgList.begin();
343 for (;it_topAlg!=m_topAlgList.end();it_topAlg++){
344 (*it_topAlg)->beginRun();
345 }*/
346 //zhangy add
347 // loop over events if the maxevt (received as input) if different from -1.
348 // if evtmax is -1 it means infinite loop
349 for( int nevt = 0; (maxevt == -1 ? true : nevt < maxevt); nevt++, total_nevt++) {
350 // Check if there is a scheduled stop issued by some algorithm/sevice
351 if ( m_scheduledStop ) {
352 m_scheduledStop = false;
353 log << MSG::ALWAYS << "Terminating event processing loop due to scheduled stop" << endreq;
354 break;
355 }
356 // Clear the event store, if used in the event loop
357 if( 0 != total_nevt ) {
358 sc = m_evtDataMgrSvc->clearStore();
359 if( !sc.isSuccess() ) {
360 log << MSG::DEBUG << "Clear of Event data store failed" << endreq;
361 }
362 }
363
364 // Setup event in the event store
365 if( m_evtContext ) {
366 IOpaqueAddress* addr = 0;
367 // Only if there is a EventSelector
368 sc = getEventRoot(addr);
369 if( !sc.isSuccess() ) {
370 log << MSG::INFO << "No more events in event selection " << endreq;
371 break;
372 }
373 // Set root clears the event data store first
374 sc = m_evtDataMgrSvc->setRoot ("/Event", addr);
375 if( !sc.isSuccess() ) {
376 log << MSG::WARNING << "Error declaring event root address." << endreq;
377 continue;
378 }
379 // register event header for mc
380 SmartDataPtr<Event::EventHeader> evt(m_evtDataSvc,"/Event/EventHeader");
381 if(!evt) {
382 sc = m_evtDataMgrSvc->registerAddress ("/Event/EventHeader", addr);
383 if( !sc.isSuccess() ) {
384 log << MSG::WARNING << "Error register EventHeader address." << endreq;
385 }
386 }
387 sc = m_evtDataSvc->retrieveObject("/Event", pObject);
388 if( !sc.isSuccess() ) {
389 log << MSG::WARNING << "Unable to retrieve Event root object" << endreq;
390 break;
391 }
392 }
393 else {
394 sc = m_evtDataMgrSvc->setRoot ("/Event", new DataObject());
395 if( !sc.isSuccess() ) {
396 log << MSG::WARNING << "Error declaring event root DataObject" << endreq;
397 }
398 }
399
400
401
402 // Execute event for all required algorithms
403 sc = executeEvent(NULL);
404 if( !sc.isSuccess() ){
405 log << MSG::ERROR << "Terminating event processing loop due to errors" << endreq;
406 break;
407 }
408 }
409 //yzhang add
410 //end run
411 ListAlg::iterator it_topAlg = m_topAlgList.begin();
412 for (;it_topAlg!=m_topAlgList.end();it_topAlg++){
413 (*it_topAlg)->endRun();
414 }
415 //zhangy add
416 return StatusCode::SUCCESS;
417}
418
419/// Create event address using event selector
420StatusCode OfflineEventLoopMgr::getEventRoot(IOpaqueAddress*& refpAddr) {
421 refpAddr = 0;
422 StatusCode sc = m_evtSelector->next(*m_evtContext);
423 if ( !sc.isSuccess() ) {
424 return sc;
425 }
426 // Create root address and assign address to data service
427 sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
428 if( !sc.isSuccess() ) {
429 sc = m_evtSelector->next(*m_evtContext);
430 if ( sc.isSuccess() ) {
431 sc = m_evtSelector->createAddress(*m_evtContext,refpAddr);
432 if ( !sc.isSuccess() ) {
433 MsgStream log( msgSvc(), name() );
434 log << MSG::WARNING << "Error creating IOpaqueAddress." << endreq;
435 }
436 }
437 }
438 return sc;
439}
int runNo
Definition: DQA_TO_DB.cxx:12
const CLID & CLID_Event
Definition: EventModel.cxx:193
IMessageSvc * msgSvc()
virtual StatusCode reinitialize()
implementation of IService::reinitalize
IDataProviderSvc * m_evtDataSvc
Reference to the Event Data Service's IDataProviderSvc interface.
IDataManagerSvc * m_histoDataMgrSvc
Reference to the Histogram Data Service.
virtual StatusCode executeEvent(void *par)
implementation of IEventProcessor::executeEvent(void* par)
virtual StatusCode executeRun(int maxevt)
implementation of IEventProcessor::executeRun()
StatusCode getEventRoot(IOpaqueAddress *&refpAddr)
Create event address using event selector.
IProperty * m_appMgrProperty
Property interface of ApplicationMgr.
std::string m_evtsel
Event selector.
virtual StatusCode finalize()
implementation of IService::finalize
IConversionSvc * m_histoPersSvc
Reference to the Histogram Persistency Service.
OfflineEventLoopMgr(const std::string &nam, ISvcLocator *svcLoc)
Standard Constructor.
virtual StatusCode initialize()
implementation of IService::initalize
std::string m_histPersName
Name of the Hist Pers type.
virtual StatusCode nextEvent(int maxevt)
implementation of IService::nextEvent
IEvtSelector::Context * m_evtContext
Event Iterator.
IDataManagerSvc * m_evtDataMgrSvc
Reference to the Event Data Service's IDataManagerSvc interface.
IEvtSelector * m_evtSelector
Reference to the Event Selector.
IIncidentSvc * m_incidentSvc
Reference to the indicent service.
virtual ~OfflineEventLoopMgr()
Standard Destructor.