BOSS 7.0.6
BESIII Offline Software System
Loading...
Searching...
No Matches
RootIoSvc.cxx
Go to the documentation of this file.
1/**
2* @file RootIoSvc.cxx
3* @brief definition of the class RootIoSvc
4*
5* $Header: /bes/bes/BossCvs/Event/RootIO/src/RootIoSvc.cxx,v 1.4 2011/02/17 01:27:10 maqm Exp $
6* Original author: Heather Kelly [email protected]
7*/
8
9#include "GaudiKernel/SvcFactory.h"
10#include "GaudiKernel/MsgStream.h"
11#include "GaudiKernel/GaudiException.h"
12//#include "GaudiKernel/IObjManager.h"
13//#include "GaudiKernel/IToolFactory.h"
14#include "GaudiKernel/IAlgManager.h"
15#include "GaudiKernel/Algorithm.h"
16#include "GaudiKernel/IAppMgrUI.h"
17#include "GaudiKernel/IIncidentSvc.h"
18#include "GaudiKernel/IIncidentListener.h"
19
20#include "CLHEP/Random/Random.h"
21
22#include <vector>
23#include <algorithm>
24
25/**
26* \class RootIoSvc
27*
28* \brief Service that implements the IRunable interface, to control the event loop.
29*Based on RootIoSvc of Glast.
30*/
31
32// includes
33#include "GaudiKernel/Service.h"
34#include "GaudiKernel/IRunable.h"
35#include "GaudiKernel/Property.h"
36#include "RootIO/IRootIoSvc.h"
37#include "TSystem.h"
38
39//forward declarations
40template <class TYPE> class SvcFactory;
41class IAppMgrUI;
42
43
44class RootIoSvc :
45 virtual public Service,
46 virtual public IIncidentListener,
47 virtual public IRootIoSvc,
48 virtual public IRunable
49{
50public:
51
52
53 /// for the IRunnable interfce
54 virtual StatusCode run();
55
56 //------------------------------------------------------------------
57 // stuff required by a Service
58
59 /// perform initializations for this service.
60 virtual StatusCode initialize ();
61
62 /// perform the finalization, as required for a service.
63 virtual StatusCode finalize ();
64
65 /// Query interface
66 virtual StatusCode queryInterface( const InterfaceID& riid, void** ppvUnknown );
67
68 /// Handles incidents, implementing IIncidentListener interface
69 virtual void handle(const Incident& inc);
70
71 virtual int getEvtMax() { return m_evtMax; };
72
73 virtual void setRootEvtMax(unsigned int max);
74
75 virtual void setRootTimeMax(unsigned int max);
76
77 virtual void registerRootTree(TChain *ch);
78
79 virtual bool setIndex(int i);
80 virtual int index() { return m_index; };
81
82 virtual bool setRunEventPair(std::pair<int,int> ids);
83 virtual std::pair<int,int> runEventPair() { return m_runEventPair; };
84
85 virtual int getAutoSaveInterval() { return m_autoSaveInterval; };
86
87protected:
88
89 /// Standard Constructor
90 RootIoSvc ( const std::string& name, ISvcLocator* al );
91
92 /// destructor
93 virtual ~RootIoSvc();
94
95private:
96
97 void beginEvent();
98 void endEvent();
99
100 /// Allow SvcFactory to instantiate the service.
101 friend class SvcFactory<RootIoSvc>;
102
103 /// Reference to application manager UI
104 IAppMgrUI* m_appMgrUI;
105 IntegerProperty m_evtMax;
106 IntegerProperty m_autoSaveInterval;
107
108 // starting and ending times for orbital simulation
109 DoubleProperty m_startTime;
110 DoubleProperty m_endTime;
111
112 unsigned int m_rootEvtMax;
113 int m_index;
114 std::pair<int, int> m_runEventPair;
115 std::vector<TChain *> m_chainCol;
116
117};
118
119// declare the service factories for the RootIoSvc
120//static SvcFactory<RootIoSvc> a_factory;
121//const ISvcFactory& RootIoSvcFactory = a_factory;
122
123// ------------------------------------------------
124// Implementation of the RootIoSvc class
125// ------------------------------------------------
126/// Standard Constructor
127RootIoSvc::RootIoSvc(const std::string& name,ISvcLocator* svc)
128: Service(name,svc)
129{
130
131 declareProperty("EvtMax" , m_evtMax=0);
132 declareProperty("StartTime" , m_startTime=0);
133 declareProperty("EndTime", m_endTime=0);
134 declareProperty("AutoSaveInterval", m_autoSaveInterval=1000);
135 declareProperty("StartingIndex", m_index=-1);
136 m_rootEvtMax = 0;
137 //m_index = -1;
138 m_runEventPair = std::pair<int,int>(-1,-1);
139 m_chainCol.clear();
140}
141
142
143/// Standard Destructor
145{
146 m_chainCol.clear();
147}
148
149
150// initialize
152{
153 StatusCode status = Service::initialize ();
154
155 // bind all of the properties for this service
156 setProperties ();
157
158 // open the message log
159 MsgStream log( msgSvc(), name() );
160
161 status = serviceLocator()->queryInterface(IAppMgrUI::interfaceID(), (void**)&m_appMgrUI);
162
163 // use the incident service to register begin, end events
164 IIncidentSvc* incsvc = 0;
165 status = service ("IncidentSvc", incsvc, true);
166
167 if( status.isFailure() ) return status;
168
169 incsvc->addListener(this, "BeginEvent", 100);
170 incsvc->addListener(this, "EndEvent", 0);
171
172 // Tell ROOT to reset signals to their default behavior
173 gSystem->ResetSignal(kSigBus);
174 gSystem->ResetSignal(kSigSegmentationViolation);
175 gSystem->ResetSignal(kSigIllegalInstruction);
176 gSystem->ResetSignal(kSigFloatingException);
177
178 return StatusCode::SUCCESS;
179}
180
181
182// finalize
184{
185 StatusCode status = StatusCode::SUCCESS;
186 return status;
187}
188
189/// Query interface
190StatusCode RootIoSvc::queryInterface(const InterfaceID& riid, void** ppvInterface) {
191 if ( IID_IRootIoSvc.versionMatch(riid) ) {
192 *ppvInterface = (IRootIoSvc*)this;
193 }else if (IRunable::interfaceID().versionMatch(riid) ) {
194 *ppvInterface = (IRunable*)this;
195 } else if (IIncidentListener::interfaceID().versionMatch(riid) ) {
196 *ppvInterface = (IIncidentListener*)this;
197 } else {
198 return Service::queryInterface(riid, ppvInterface);
199 }
200
201 addRef();
202 return SUCCESS;
203}
204
205
206void RootIoSvc::setRootEvtMax(unsigned int max) {
207 // Purpose and Method: Allow users of the RootIoSvc to specify the number
208 // of events found in their ROOT files
209 if (m_rootEvtMax == 0) {
210 m_rootEvtMax = max;
211 return;
212 }
213
214 if (m_rootEvtMax > max) m_rootEvtMax = max;
215}
216
217void RootIoSvc::setRootTimeMax(unsigned int max) {
218 // Not yet used
219 return;
220}
221
223 m_chainCol.push_back(ch);
224}
225
227 if (i < 0) return false;
228 std::vector<TChain*>::iterator it;
229 for(it = m_chainCol.begin(); it != m_chainCol.end(); it++) {
230 if (i >= (*it)->GetEntries()) return false;
231 }
232 m_index = i;
233 m_runEventPair = std::pair<int, int>(-1,-1);
234 return true;
235}
236
237
238bool RootIoSvc::setRunEventPair(std::pair<int, int> ids) {
239 std::vector<TChain*>::iterator it;
240 for(it = m_chainCol.begin(); it != m_chainCol.end(); it++) {
241 int readInd = (*it)->GetEntryNumberWithIndex(ids.first, ids.second);
242 if ( (readInd < 0) || (readInd >= (*it)->GetEntries()) ) return false;
243 }
244 m_runEventPair = ids;
245 m_index=-1;
246 return true;
247}
248
249// handle "incidents"
250void RootIoSvc::handle(const Incident &inc)
251{
252 if( inc.type()=="BeginEvent")beginEvent();
253 else if(inc.type()=="EndEvent")endEvent();
254}
255
256
257void RootIoSvc::beginEvent() // should be called at the beginning of an event
258{
259}
260
261void RootIoSvc::endEvent() // must be called at the end of an event to update, allow pause
262{
263 m_index = -1;
264 m_runEventPair = std::pair<int, int>(-1,-1);
265}
266
267StatusCode RootIoSvc::run(){
268 // Purpose and Method: Control the event loop
269
270 StatusCode status = StatusCode::FAILURE;
271 MsgStream log( msgSvc(), name() );
272
273 if ( 0 == m_appMgrUI ) return status;
274
275 IProperty* propMgr=0;
276 status = serviceLocator()->service("ApplicationMgr", propMgr );
277 if( status.isFailure()) {
278 log << MSG::ERROR << "Unable to locate PropertyManager Service" << endreq;
279 return status;
280 }
281
282 IntegerProperty evtMax("EvtMax",0);
283 status = propMgr->getProperty( &evtMax );
284 if (status.isFailure()) return status;
285
286 // Determine if the min number of ROOT events is less than the
287 // requested number of events in the jobOptions file
288 IntegerProperty rootEvtMax("EvtMax", m_rootEvtMax);
289 if (rootEvtMax < evtMax) setProperty(rootEvtMax);
290 else setProperty(evtMax);
291
292 // now find the top alg so we can monitor its error count
293 //
294 IAlgManager* theAlgMgr;
295 status = serviceLocator( )->getService( "ApplicationMgr",
296 IAlgManager::interfaceID(),
297 (IInterface*&)theAlgMgr );
298 IAlgorithm* theIAlg;
299 Algorithm* theAlgorithm=0;
300 IntegerProperty errorProperty("ErrorCount",0);
301
302 status = theAlgMgr->getAlgorithm( "Top", theIAlg );
303 if ( status.isSuccess( ) ) {
304 try{
305 theAlgorithm = dynamic_cast<Algorithm*>(theIAlg);
306 } catch(...){
307 status = StatusCode::FAILURE;
308 }
309 }
310 if ( status.isFailure( ) ) {
311 log << MSG::WARNING << "Could not find algorithm 'Top'; will not monitor errors" << endreq;
312 }
313
314
315 // loop over the events
316
317 int eventNumber= 0;
318 double currentTime=m_startTime;
319
320 { bool noend=true;
321 log << MSG::INFO << "Runable interface starting event loop as :" ;
322 if( m_evtMax>0) { log << " MaxEvt = " << m_evtMax; noend=false; }
323 if( m_endTime>0) { log << " EndTime= " << m_endTime; noend=false; }
324 log << endreq;
325
326 if(noend) {
327 log << MSG::WARNING << "No end condition specified: will not process any events!" << endreq;
328 }
329 }
330 // Not yet using time as a control on the event loop for ROOT
331 while( m_evtMax>0 && eventNumber < m_evtMax
332 || m_endTime>0 && currentTime< m_endTime ) {
333
334 status = m_appMgrUI->nextEvent(1); // currently, always success
335
336 // the single event may have created a failure. Check the ErrorCount propery of the Top alg.
337 if( theAlgorithm !=0) theAlgorithm->getProperty(&errorProperty);
338 if( status.isFailure() || errorProperty.value() > 0){
339 status = StatusCode::FAILURE;
340 }
341
342 if( status.isFailure()) break;
343 //if(flux!=0){
344 // currentTime = flux->gpsTime();
345 // }
346 eventNumber ++;
347 }
348 if( status.isFailure()){
349 log << MSG::ERROR << "Terminating RootIoSvc loop due to error" << endreq;
350
351 }else if( m_endTime>0 && currentTime >= m_endTime ) {
352 log << MSG::INFO << "Loop terminated by time " << endreq;
353 }else {
354 log << MSG::INFO << "Processing loop terminated by event count" << endreq;
355 }
356 return status;
357}
358
definition of the interface for IRootIoSvc
IMessageSvc * msgSvc()
The RootIoSvc gaudi service interface.
Definition: IRootIoSvc.h:23
Service that implements the IRunable interface, to control the event loop. Based on RootIoSvc of Glas...
Definition: RootIoSvc.cxx:49
virtual int index()
Definition: RootIoSvc.cxx:80
virtual bool setRunEventPair(std::pair< int, int > ids)
Definition: RootIoSvc.cxx:238
virtual std::pair< int, int > runEventPair()
Definition: RootIoSvc.cxx:83
virtual void registerRootTree(TChain *ch)
Definition: RootIoSvc.cxx:222
virtual StatusCode run()
for the IRunnable interfce
Definition: RootIoSvc.cxx:267
virtual StatusCode initialize()
perform initializations for this service.
Definition: RootIoSvc.cxx:151
virtual void setRootEvtMax(unsigned int max)
Definition: RootIoSvc.cxx:206
virtual void setRootTimeMax(unsigned int max)
Definition: RootIoSvc.cxx:217
RootIoSvc(const std::string &name, ISvcLocator *al)
Standard Constructor.
Definition: RootIoSvc.cxx:127
virtual int getEvtMax()
Definition: RootIoSvc.cxx:71
virtual bool setIndex(int i)
Definition: RootIoSvc.cxx:226
virtual StatusCode finalize()
perform the finalization, as required for a service.
Definition: RootIoSvc.cxx:183
virtual int getAutoSaveInterval()
Definition: RootIoSvc.cxx:85
virtual void handle(const Incident &inc)
Handles incidents, implementing IIncidentListener interface.
Definition: RootIoSvc.cxx:250
virtual ~RootIoSvc()
destructor
Definition: RootIoSvc.cxx:144
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
Query interface.
Definition: RootIoSvc.cxx:190
Forward and external declarations.
Definition: CalibDataSvc.h:35