| The Gaudi Framework v32r1 (f65d50dc)

Application.cpp
Go to the documentation of this file.
1 #include <Gaudi/Application.h>
2 #include <GaudiKernel/AppReturnCode.h>
3 #include <GaudiKernel/Bootstrap.h>
4 #include <GaudiKernel/IAppMgrUI.h>
5 #include <GaudiKernel/IEventProcessor.h>
6 #include <GaudiKernel/IJobOptionsSvc.h>
7 #include <GaudiKernel/IMessageSvc.h>
8 #include <GaudiKernel/INamedInterface.h>
9 #include <GaudiKernel/IProperty.h>
10 #include <GaudiKernel/ISvcLocator.h>
11 #include <GaudiKernel/Property.h>
12 #include <gsl/span>
13
14 #define GAUDI_ASSERT_THROW_NAME( cond, msg, name ) \
15 if ( !cond ) throw GaudiException{msg, name, StatusCode::FAILURE};
16
17 #define GAUDI_ASSERT_THROW( cond, msg ) \
18 if ( !cond ) throw GaudiException{msg, this->name(), StatusCode::FAILURE};
19
20 namespace {
21 void consumeOptions( SmartIF<IProperty> prop, Gaudi::Application::Options& options ) {
22 GAUDI_ASSERT_THROW_NAME( prop, "invalid IProperty", "Gaudi::Application" );
23 std::string prefix = SmartIF<INamedInterface>( prop )->name() + '.';
24 const auto prefix_size = prefix.size();
25
26 auto opt = options.upper_bound( prefix );
27 while ( opt != end( options ) && std::string_view( opt->first ).substr( 0, prefix_size ) == prefix ) {
28 GAUDI_ASSERT_THROW_NAME( prop->setProperty( opt->first.substr( prefix_size ), opt->second ),
29 "failure setting " + opt->first + " to " + opt->second, "Gaudi::Application" );
30 // drop processed option and increase iterator
31 opt = options.erase( opt );
32 }
33 }
34 } // namespace
35
36 Gaudi::Application::Factory::ReturnType Gaudi::Application::create( std::string_view type, Options opts ) {
37 if ( type == "Gaudi::Application" ) { return std::make_unique<Application>( std::move( opts ) ); }
38 return Factory::create( type, std::move( opts ) );
39 }
40
41 Gaudi::Application::Application( Gaudi::Application::Options options ) {
42 // # Prepare the application
43 // - instantiate
44 app = Gaudi::createApplicationMgr();
45 GAUDI_ASSERT_THROW_NAME( app, "failure instantiating ApplicationMgr", "Gaudi::Application" );
46
47 // - main configuration
48 consumeOptions( app.as<IProperty>(), options );
49 // - start minimal services
50 GAUDI_ASSERT_THROW_NAME( app->configure(), "failure creating basic services", "Gaudi::Application" );
51
52 consumeOptions( SmartIF<IMessageSvc>{app}.as<IProperty>(), options );
53
54 // - prepare job configuration
55 {
56 auto sloc = app.as<ISvcLocator>();
57 auto jos = sloc->service<IJobOptionsSvc>( "JobOptionsSvc" );
58 std::for_each( begin( options ), end( options ), [&jos]( const auto& item ) {
59 std::string_view name = item.first;
60 const auto sep_pos = name.find_last_of( '.' );
61 std::string_view client = name.substr( 0, sep_pos );
62 name.remove_prefix( sep_pos + 1 );
63 jos->addPropertyToCatalogue( std::string{client}, Gaudi::Property<std::string>{std::string{name}, item.second} );
64 } );
65 }
66 }
67
68 Gaudi::Application::~Application() { app->terminate().ignore(); }
69
70 int Gaudi::Application::run() {
71 auto prop = app.as<IProperty>();
72 auto processor = app.as<IEventProcessor>();
73
74 Gaudi::Property<int> evtMax;
75 evtMax.assign( prop->getProperty( "EvtMax" ) );
76
77 // - get ready to process events
78 if ( app->initialize() ) {
79 if ( app->start() ) {
80 // - main processing loop
81 if ( !processor->executeRun( evtMax ) ) setAppReturnCode( prop, Gaudi::ReturnCode::GenericFailure );
82
83 app->stop().ignore();
84 } else
85 setAppReturnCode( prop, Gaudi::ReturnCode::GenericFailure );
86 app->finalize().ignore();
87 } else
88 setAppReturnCode( prop, Gaudi::ReturnCode::GenericFailure );
89 return getAppReturnCode( prop );
90 }
91
92 namespace {
93 struct c_opt_t {
94 char* key;
95 char* value;
96 };
97 } // namespace
98
99 #ifdef GAUDI_HASCLASSVISIBILITY
100 # pragma GCC visibility push( default )
101 #endif
102
103 extern "C" {
104 // helper to invoke the factory from Python
105 Gaudi::Application* _py_Gaudi__Application__create( const char* name, const c_opt_t* options, long n ) {
106 Gaudi::Application::Options opts;
107 gsl::span py_opts{options, n};
108 for ( auto& opt : py_opts ) { opts[opt.key] = opt.value; }
109 return Gaudi::Application::create( name, std::move( opts ) ).release();
110 }
111 int _py_Gaudi__Application__run( Gaudi::Application* self ) { return self->run(); }
112 void _py_Gaudi__Application__delete( Gaudi::Application* self ) { delete self; }
113 }
114
115 #ifdef GAUDI_HASCLASSVISIBILITY
116 # pragma GCC visibility pop
117 #endif
Gaudi::getAppReturnCode
int getAppReturnCode(const SmartIF< IProperty > &appmgr)
Get the application (current) return code.
Definition: AppReturnCode.h:69
_py_Gaudi__Application__create
Gaudi::Application * _py_Gaudi__Application__create(const char *name, const c_opt_t *options, long n)
Definition: Application.cpp:105
SmartIF< IProperty >
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
IEventProcessor.h
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
GAUDI_ASSERT_THROW_NAME
#define GAUDI_ASSERT_THROW_NAME(cond, msg, name)
Definition: Application.cpp:14
Gaudi.Application::run
virtual int run()
Implement the application main logic:
Definition: Application.cpp:70
IAppMgrUI.h
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:107
Bootstrap.h
Gaudi::ReturnCode::GenericFailure
constexpr int GenericFailure
Definition: AppReturnCode.h:18
std::map< std::string, std::string >
gaudirun.opts
opts
Definition: gaudirun.py:307
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
std::string
STL class.
gaudirun.options
options
Definition: gaudirun.py:285
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
IProperty::setProperty
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
INamedInterface.h
IJobOptionsSvc
Main interface for the JobOptions service.
Definition: IJobOptionsSvc.h:20
gaudirun.type
type
Definition: gaudirun.py:142
gaudirun.prefix
string prefix
Definition: gaudirun.py:331
IMessageSvc.h
IOTest.end
def end
Definition: IOTest.py:112
Gaudi.Application.create
def create(cls, appType, opts)
Definition: __init__.py:83
Gaudi::setAppReturnCode
StatusCode setAppReturnCode(SmartIF< IProperty > &appmgr, int value, bool force=false)
Set the application return code.
Definition: AppReturnCode.h:49
std::move
T move(T... args)
Gaudi.Application
Gaudi application entry point.
Definition: __init__.py:62
Gaudi.Application::Application
Application(Options opts)
Construct and configure the application from the provided options.
Definition: Application.cpp:41
AppReturnCode.h
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
IProperty.h
_py_Gaudi__Application__run
int _py_Gaudi__Application__run(Gaudi::Application *self)
Definition: Application.cpp:111
Gaudi.Application::~Application
virtual ~Application()
Definition: Application.cpp:68
Property.h
IEventProcessor
The IEventProcessor is the interface to process events.
Definition: IEventProcessor.h:14
AtlasMCRecoFullPrecedenceDump.evtMax
int evtMax
Definition: AtlasMCRecoFullPrecedenceDump.py:13
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
std::for_each
T for_each(T... args)
ISvcLocator.h
Gaudi.c_opt_t
Definition: __init__.py:58
_py_Gaudi__Application__delete
void _py_Gaudi__Application__delete(Gaudi::Application *self)
Definition: Application.cpp:112
Gaudi::createApplicationMgr
GAUDI_API IAppMgrUI * createApplicationMgr(const std::string &dllname, const std::string &factname)
Application.h
IJobOptionsSvc.h
Gaudi.Application::Options
std::map< std::string, std::string > Options
Definition: Application.h:19