00001
00009 #include "FluxSvc.h"
00010 #include "FluxSvc/IRegisterSource.h"
00011
00012
00013 #include "FluxSvc/../src/test/flux/rootplot.h"
00014
00015 #include "GaudiKernel/SvcFactory.h"
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/IToolSvc.h"
00018 #include "GaudiKernel/GaudiException.h"
00019 #include "GaudiKernel/IObjManager.h"
00020 #include "GaudiKernel/IToolFactory.h"
00021 #include "GaudiKernel/IAlgManager.h"
00022 #include "GaudiKernel/Algorithm.h"
00023 #include "GaudiKernel/IAppMgrUI.h"
00024 #include "GaudiKernel/IParticlePropertySvc.h"
00025
00026 #include "Flux.h"
00027
00028 #include "FluxMgr.h"
00029 #include <algorithm>
00030
00031
00032 static SvcFactory<FluxSvc> a_factory;
00033 const ISvcFactory& FluxSvcFactory = a_factory;
00034
00035
00036 static std::string default_source_library("$(FLUXSVCROOT)/xml/source_library.xml");
00037 static std::string default_dtd_file("$(FLUXSVCROOT)/xml/source.dtd");
00038
00039
00040
00041
00043
00044 : Service(name,svc), m_currentFlux(0)
00045 {
00046
00047 declareProperty("source_lib" , m_source_lib);
00048 declareProperty("dtd_file" , m_dtd_file=default_dtd_file);
00049 declareProperty("EvtMax" , m_evtMax=0);
00050 declareProperty("StartTime" , m_startTime=0);
00051 declareProperty("EndTime", m_endTime=0);
00052
00053 }
00054
00055 std::list<std::string> FluxSvc::fluxNames()const{
00056 return m_fluxMgr->sourceList();
00057 }
00058
00059 StatusCode FluxSvc::source(std::string name, IFlux*& flux) {
00060 std::list<std::string> source_list( fluxNames() );
00061 std::list<std::string> source_list2( SpectrumFactoryTable::instance()->spectrumList() );
00062
00063 if( std::find(source_list.begin(), source_list.end(), name) == source_list.end()
00064 &&(std::find(source_list2.begin(), source_list2.end(), name) == source_list2.end()))
00065
00066 return StatusCode::FAILURE;
00067 flux = new Flux(name);
00068 m_currentFlux = flux;
00069 return StatusCode::SUCCESS;
00070 }
00071
00073 FluxSvc::~FluxSvc()
00074 {
00075 }
00076
00077
00078
00079 StatusCode FluxSvc::initialize ()
00080 {
00081 StatusCode status = Service::initialize ();
00082
00083
00084 setProperties ();
00085
00086
00087 MsgStream log( msgSvc(), name() );
00088
00089 status = serviceLocator()->queryInterface(IID_IAppMgrUI, (void**)&m_appMgrUI);
00090
00091
00092 if( m_source_lib.empty() ){
00093 m_source_lib.push_back(default_source_library);
00094 log << MSG::INFO << "Set source library list to " << default_source_library << endreq;
00095 }
00096
00097
00098
00099 m_fluxMgr = new FluxMgr(m_source_lib, m_dtd_file);
00100
00101 Flux::mgr(m_fluxMgr);
00102
00103
00104 if( m_fluxMgr->sourceList().empty()) {
00105 log << MSG::ERROR << "Did not initialize properly: no sources detected" << endreq;
00106 status = StatusCode::FAILURE;
00107 }
00108
00109 if ( service("ParticlePropertySvc", m_partSvc).isFailure() ){
00110 log << MSG::ERROR << "Couldn't find the ParticlePropertySvc!" << endreq;
00111 return StatusCode::FAILURE;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121 IObjManager* objManager=0;
00122
00123
00124 status = serviceLocator()->service("ApplicationMgr", objManager );
00125 if( status.isFailure()) {
00126 log << MSG::ERROR << "Unable to locate ObjectManager Service" << endreq;
00127 return status;
00128 }
00129
00130
00131 IToolSvc* tsvc =0;
00132 status = service( "ToolSvc", tsvc, true );
00133 if( status.isFailure() ) {
00134 log << MSG::ERROR << "Unable to locate Tool Service" << endreq;
00135 return status;
00136 }
00137
00138 IToolFactory* toolfactory = 0;
00139
00140
00141 for(IObjManager::ObjIterator it = objManager->objBegin(); it !=objManager->objEnd(); ++ it){
00142
00143 std::string tooltype= (*it)->ident();
00144
00145 const IFactory* factory = objManager->objFactory( tooltype );
00146 IFactory* fact = const_cast<IFactory*>(factory);
00147 status = fact->queryInterface( IID_IToolFactory, (void**)&toolfactory );
00148 if( status.isSuccess() ) {
00149
00150 IAlgTool* itool;
00151 status = tsvc->retrieveTool(tooltype, itool);
00152 if( status.isSuccess()) {
00153 status =itool->queryInterface( IRegisterSource::interfaceID(), (void**)&itool);
00154 if( status.isSuccess() ){
00155 log << MSG::INFO << "Registering sources in " << tooltype << endreq;
00156 dynamic_cast<IRegisterSource*>(itool)->registerMe(this);
00157 }else{
00158 tsvc->releaseTool(itool);
00159 }
00160
00161 }
00162
00163 }
00164
00165 }
00166
00167
00168 return StatusCode::SUCCESS;
00169 }
00170
00171
00172 StatusCode FluxSvc::finalize ()
00173 {
00174 StatusCode status = StatusCode::SUCCESS;
00175
00176 delete m_fluxMgr;
00177 return status;
00178 }
00179
00181 StatusCode FluxSvc::queryInterface(const IID& riid, void** ppvInterface) {
00182 if ( IID_IFluxSvc.versionMatch(riid) ) {
00183 *ppvInterface = (IFluxSvc*)this;
00184 }else if (IID_IRunable.versionMatch(riid) ) {
00185 *ppvInterface = (IRunable*)this;
00186 } else {
00187 return Service::queryInterface(riid, ppvInterface);
00188 }
00189
00190 addRef();
00191 return SUCCESS;
00192 }
00193
00194
00196 void FluxSvc::addFactory(std::string name, const ISpectrumFactory* factory ){
00197 m_fluxMgr->addFactory(name, factory);
00198 }
00199
00200
00202 void FluxSvc::pass ( double t){
00203 m_fluxMgr->pass(t);
00204 }
00205
00206 void FluxSvc::rootDisplay(std::vector<char*> arguments){
00207 rootplot abc(arguments);
00208 }
00209
00211 IFlux* FluxSvc::currentFlux(){
00212 return m_currentFlux;
00213 }
00214
00216 std::string FluxSvc::fluxName()const{
00217 return m_currentFlux->name();
00218 }
00219
00221 std::string FluxSvc::uniqueIDString()const{
00222 std::strstream t;
00223 t << m_currentFlux->numSource();
00224 return m_currentFlux->name() + t.str();
00225 }
00226
00227
00228 void FluxSvc::setOrientation(std::pair<double,double> ang){
00229 m_fluxMgr->setOrientation(ang);
00230 }
00231
00233 std::pair<double,double> FluxSvc::getOrientation(){
00234 return m_fluxMgr->getOrientation();
00235 }
00236
00237 HepRotation FluxSvc::transformGlastToGalactic(double time)const{
00238 return m_fluxMgr->transformGlastToGalactic(time);
00239 }
00240
00241 std::pair<double,double> FluxSvc::location(){
00242 return m_fluxMgr->location();
00243 }
00245 void FluxSvc::setRockType(int rockType){
00246 m_fluxMgr->setRockType(rockType);
00247 }
00248
00249 std::vector<std::pair< std::string ,std::list<std::string> > > FluxSvc::sourceOriginList() const{
00250 return m_fluxMgr->sourceOriginList();
00251 }
00252
00253
00254 StatusCode FluxSvc::run(){
00255 StatusCode status = StatusCode::FAILURE;
00256 MsgStream log( msgSvc(), name() );
00257
00258 if ( 0 == m_appMgrUI ) return status;
00259
00260 IProperty* propMgr=0;
00261 status = serviceLocator()->service("ApplicationMgr", propMgr );
00262 if( status.isFailure()) {
00263 log << MSG::ERROR << "Unable to locate PropertyManager Service" << endreq;
00264 return status;
00265 }
00266
00267 IntegerProperty evtMax("EvtMax",0);
00268 status = propMgr->getProperty( &evtMax );
00269 if (status.isFailure()) return status;
00270
00271 setProperty(evtMax);
00272
00273
00274
00275 IAlgManager* theAlgMgr;
00276 status = serviceLocator( )->getService( "ApplicationMgr",
00277 IID_IAlgManager,
00278 (IInterface*&)theAlgMgr );
00279 IAlgorithm* theIAlg;
00280 Algorithm* theAlgorithm=0;
00281 IntegerProperty errorProperty("ErrorCount",0);
00282
00283 status = theAlgMgr->getAlgorithm( "Top", theIAlg );
00284 if ( status.isSuccess( ) ) {
00285 try{
00286 theAlgorithm = dynamic_cast<Algorithm*>(theIAlg);
00287 } catch(...){
00288 status = StatusCode::FAILURE;
00289 }
00290 }
00291 if ( status.isFailure( ) ) {
00292 log << MSG::WARNING << "Could not find algorithm 'Top'; will not monitor errors" << endreq;
00293 }
00294
00295
00296
00297 IFlux* flux=currentFlux();
00298 int eventNumber= 0;
00299 double currentTime=m_startTime;
00300
00301 { bool noend=true;
00302 log << MSG::INFO << "Runable interface starting event loop as :" ;
00303 if( m_evtMax>0) { log << " MaxEvt = " << m_evtMax; noend=false; }
00304 if( m_endTime>0) { log << " EndTime= " << m_endTime; noend=false; }
00305 log << endreq;
00306
00307 if(noend) {
00308 log << MSG::WARNING << "No end condition specified: will not process any events!" << endreq;
00309 }
00310 }
00311 while( m_evtMax>0 && eventNumber < m_evtMax
00312 || m_endTime>0 && currentTime< m_endTime ) {
00313
00314 status = m_appMgrUI->nextEvent(1);
00315
00316
00317 if( theAlgorithm !=0) theAlgorithm->getProperty(&errorProperty);
00318 if( status.isFailure() || errorProperty.value() > 0){
00319 status = StatusCode::FAILURE;
00320 }
00321
00322 if( status.isFailure()) break;
00323 currentTime = flux->gpsTime();
00324 eventNumber ++;
00325 }
00326 if( status.isFailure()){
00327 log << MSG::ERROR << "Terminating FluxSvc loop due to error" << endreq;
00328
00329 }else if( m_endTime>0 && currentTime >= m_endTime ) {
00330 log << MSG::INFO << "Loop terminated by time " << endreq;
00331 }else {
00332 log << MSG::INFO << "Processing loop terminated by event count" << endreq;
00333 }
00334 return status;
00335
00336 }
00337