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