24 int Application::main(
int argc,
char**argv ) {
27 auto logger = application.
logger() ;
29 application.
init( argc, argv ) ;
33 logger->log<ERROR>() <<
"MarlinMT main, caught MarlinMT exception " << e.
what() << std::endl ;
34 logger->log<ERROR>() <<
"Exiting with status 1" << std::endl ;
37 catch ( std::exception &e ) {
38 logger->log<ERROR>() <<
"MarlinMT main, caught std::exception " << e.what() << std::endl ;
39 logger->log<ERROR>() <<
"Exiting with status 1" << std::endl ;
43 logger->log<ERROR>() <<
"MarlinMT main, caught unknown exception" << std::endl ;
44 logger->log<ERROR>() <<
"Exiting with status 2" << std::endl ;
52 void Application::init(
int argc,
char **argv ) {
58 _parseResult = parser.
parse( argc, argv ) ;
62 fullCmdLine = std::string(argv[0]) +
" " + fullCmdLine ;
63 auto &cmdLineSection = _configuration.createSection(
"CmdLine" ) ;
64 cmdLineSection.setParameter(
"Program", _parseResult._programName ) ;
65 cmdLineSection.setParameter(
"Arguments", fullCmdLine ) ;
66 cmdLineSection.setParameter(
"Config", _parseResult._config.value() ) ;
67 cmdLineSection.setParameter(
"NThreads", _parseResult._nthreads ) ;
68 auto &argsSection = cmdLineSection.addSection(
"AdditionalArgs" ) ;
69 for(
auto &arg : _parseResult._additionalArgs ) {
70 argsSection.setParameter( arg.first, arg.second ) ;
74 std::filesystem::path progName = _parseResult._programName ;
75 _parseResult._programName = progName.filename().string() ;
78 auto &pluginMgr = PluginManager::instance() ;
79 pluginMgr.logger()->setLevel<MESSAGE>() ;
80 auto libraries = details::split_string<std::string>( details::getenv<std::string>(
"MARLINMT_DLL",
"" ),
":" ) ;
81 pluginMgr.loadLibraries( libraries ) ;
83 if( _parseResult._dumpExample ) {
89 ConfigHelper::readConfig( _parseResult._config.value(), _configuration ) ;
92 _loggerMgr.setup(
this ) ;
93 logger()->log<MESSAGE0>() <<
"============ Configuration ============" << std::endl ;
94 logger()->log<MESSAGE0>() << _configuration << std::endl ;
95 logger()->log<MESSAGE0>() <<
"=======================================" << std::endl ;
98 _bookStoreManager.setup(
this ) ;
101 _geometryMgr.setup(
this ) ;
104 if( 0 == _parseResult._nthreads ) {
107 if( 1 == _parseResult._nthreads ) {
108 logger()->log<MESSAGE>() <<
"Running in single-thread mode" << std::endl ;
109 _scheduler = std::make_shared<SimpleScheduler>() ;
112 logger()->log<MESSAGE>() <<
"Running in multi-thread mode (nthreads=" << _parseResult._nthreads <<
")" << std::endl ;
113 _scheduler = std::make_shared<concurrency::PEPScheduler>() ;
115 _scheduler->setup(
this ) ;
118 auto dstype = configuration().section(
"datasource").parameter<std::string>(
"DatasourceType" ) ;
120 if(
nullptr == _dataSource ) {
121 MARLINMT_THROW(
"Data source of type '" + dstype +
"' not found in plugins" ) ;
123 _dataSource->setup(
this ) ;
124 _dataSource->onEventRead( std::bind( &Application::onEventRead,
this, _1 ) ) ;
125 _dataSource->onRunHeaderRead( std::bind( &Application::onRunHeaderRead,
this, _1 ) ) ;
128 auto &execSection = _configuration.section(
"execute") ;
129 auto procs = execSection.parameterNames() ;
130 for(
auto &proc : procs ) {
131 _conditions[ proc ] = execSection.parameter<std::string>( proc ) ;
133 _initialized = true ;
138 void Application::run() {
139 if( _parseResult._dumpExample ) {
140 logger()->log<MESSAGE>() <<
"Dry run. Output configuration in: " << _parseResult._config.value() << std::endl ;
141 dumpExampleConfig() ;
145 _dataSource->readAll() ;
147 catch( StopProcessingException &e ) {
148 logger()->log<ERROR>() << std::endl
149 <<
" **********************************************************" << std::endl
150 <<
" * *" << std::endl
151 <<
" * Stop of event processing requested by processor : *" << std::endl
152 <<
" * " << e.what() << std::endl
153 <<
" * will call end() method of all processors ! *" << std::endl
154 <<
" * *" << std::endl
155 <<
" **********************************************************" << std::endl
158 _geometryMgr.clear() ;
160 _bookStoreManager.writeToDisk();
166 return _parseResult ;
171 const std::string &Application::programName()
const {
177 bool Application::isInitialized()
const {
178 return _initialized ;
184 return _loggerMgr.mainLogger() ;
190 if ( isInitialized() ) {
191 return _loggerMgr.createLogger( name ) ;
194 return Logging::createLogger( name ) ;
200 void Application::onEventRead( std::shared_ptr<EventStore> event ) {
202 while( _scheduler->freeSlots() == 0 ) {
203 _scheduler->popFinishedEvents( events ) ;
204 if( not events.empty() ) {
205 processFinishedEvents( events ) ;
209 std::this_thread::sleep_for( std::chrono::milliseconds(1) ) ;
213 auto seeds = _randomSeedMgr.generateRandomSeeds( event.get() ) ;
220 _scheduler->pushEvent( event ) ;
222 _scheduler->popFinishedEvents( events ) ;
223 if( not events.empty() ) {
224 processFinishedEvents( events ) ;
230 void Application::onRunHeaderRead( std::shared_ptr<RunHeader> rhdr ) {
231 logger()->log<MESSAGE9>() <<
"New run header no " << rhdr->runNumber() << std::endl ;
232 _scheduler->processRunHeader( rhdr ) ;
238 return _geometryMgr ;
244 return _randomSeedMgr ;
250 return _randomSeedMgr ;
255 void Application::processFinishedEvents(
const EventList &events )
const {
257 for(
auto event : events ) {
258 logger()->log<MESSAGE9>()
259 <<
"Event uid " << event->uid()
260 <<
" finished" << std::endl ;
267 return _bookStoreManager ;
273 return _bookStoreManager ;
279 return _configuration ;
284 void Application::dumpExampleConfig() {
286 auto &pluginMgr = PluginManager::instance() ;
292 _loggerMgr.getParameters( loggingSection ) ;
295 geometrySection.
setParameter<std::string>(
"GeometryType",
"EmptyGeometry" ) ;
297 auto &datasourceSection = config.
createSection(
"datasource") ;
301 datasourceSection.setParameter<std::string>(
"DatasourceType",
"LCIOReader" ) ;
304 datasourceSection.setParameter<std::string>(
"DatasourceType",
"CustomSource" ) ;
305 datasourceSection.setParameter<std::string>(
"InputFile",
"yourinput.dat" ) ;
309 _bookStoreManager.getParameters( bookstoreSection ) ;
312 globalSection.
setParameter(
"RandomSeed", 1234567890 ) ;
315 auto pluginNames = pluginMgr.pluginNames<
Processor>() ;
316 for(
auto pluginName : pluginNames ) {
317 auto processor = pluginMgr.create<
Processor>( pluginName ) ;
318 processor->
setName(
"My" + processor->type() ) ;
319 auto &procSection = procsSection.addSection( processor->name() ) ;
320 procSection.setParameter(
"ProcessorName", processor->name() ) ;
321 procSection.setParameter(
"ProcessorType", processor->type() ) ;
322 auto criticalOpt = processor->runtimeOption( Processor::ERuntimeOption::eCritical ) ;
323 auto cloneOpt = processor->runtimeOption( Processor::ERuntimeOption::eClone ) ;
324 procSection.setParameter(
"ProcessorCritical", criticalOpt.has_value() ? criticalOpt.value() : false ) ;
325 procSection.setParameter(
"ProcessorClone", cloneOpt.has_value() ? cloneOpt.value() : false ) ;
326 processor->getParameters( procSection ) ;
328 ConfigHelper::writeConfig( _parseResult._config.value(), config ) ;
const char * what() const noexcept override
Get the full exception message.
std::string _programName
The program name.
ProcessorConditionsExtension class Event extension providing access to processor runtime conditions (...
ConfigSection & createSection(const std::string &sn)
Create a new section by name.
void setName(const std::string &n)
Set the component name.
RandomSeedExtension class Event extension providing access to random seeds.
ParseResult parse(int argc, char **argv)
Parse the command line.
DataSourcePlugin class Responsible for reading/getting LCEvent and LCRunHeader in the framework for f...
#define MARLINMT_THROW(message)
Application class Base application interface for running a Marlin application.
std::vector< std::shared_ptr< EventStore > > EventList
ConfigSection & setParameter(const std::string &n, const T &val)
Set a parameter value.
ParseResult struct. The result of the command line parsing.
void getParameters(ConfigSection §ion, const std::set< std::string > &exclude={}) const
Get the parameters from configurable object and populate the config section with. ...
void init(int argc, char **argv)
Initialize the application.
void run()
Run the application.
Logger logger() const
Get the application logger instance.
void print_banner(std::ostream &out)
CmdLineParser class Does the main command line parsing for MarlinMT.