1 #ifndef UTILITYAPPS_SRC_RUN_PLUGIN_H
2 #define UTILITYAPPS_SRC_RUN_PLUGIN_H
20 #include "TTimeStamp.h"
38 #include "DD4hep/PluginService.h"
40 union {
void* p; Detector* l; }
v;
41 v.p = ::dd4hep::PluginService::Create<void*>(
"Detector_constructor",name);
45 dd4hep::except(
"RunPlugin",
"++ Failed to locate plugin to create Detector instance");
48 dd4hep::except(
"RunPlugin",
"++ Exception: %s", e.what());
51 dd4hep::except(
"RunPlugin",
"++ UNKNOWN Exception while creating Detector instance.");
53 dd4hep::except(
"RunPlugin",
"++ UNKNOWN Exception while creating Detector instance.");
59 long run_plugin(
dd4hep::Detector& description,
const char* name,
int argc,
char** argv) {
61 description.
apply(name,argc,argv);
65 dd4hep::except(
"RunPlugin",
"++ Exception while executing plugin %s:\n\t\t%s",
66 name ? name :
"<unknown>", e.what());
69 dd4hep::except(
"RunPlugin",
"++ UNKNOWN Exception while executing plugin %s.",name ? name :
"<unknown>");
76 std::ostream& print_default_args() {
78 " -build_type <number/string> Specify the build type \n"
79 " [OPTIONAL] MUST come immediately after the -compact input.\n"
80 " Default for each file is: BUILD_DEFAULT [=1] \n"
81 " Allowed: BUILD_SIMU [=1], BUILD_RECO [=2], \n"
82 " BUILD_DISPLAY [=3] or BUILD_ENVELOPE [=4] \n"
83 " -destroy [OPTIONAL] Force destruction of the Detector instance \n"
84 " before exiting the application \n"
85 " -no-destroy [OPTIONAL] Inhibit destruction of the Detector instance \n"
86 " -volmgr [OPTIONAL] Load and populate phys.volume manager to \n"
87 " check the volume ids for duplicates etc. \n"
88 " -no-volmgr [OPTIONAL] Inhibit loading phys.volume manager \n"
89 " -interpreter [OPTIONAL] Start ROOT C++ interpreter after execution. \n"
90 " -interactive [OPTIONAL] Alias for -interpreter argument. \n"
91 " -no-interpreter [OPTIONAL] Inhibit ROOT C++ interpreter. \n"
92 " -print <number/string> Specify output level. Default: INFO(=3) \n"
93 " [OPTIONAL] Allowed values: VERBOSE(=1), DEBUG(=2), \n"
94 " INFO(=3), WARNING(=4), ERROR(=5), FATAL(=6) \n"
95 " The lower the level, the more printout... \n"
96 " -dry-run [OPTIONAL] Only load description. No execution. \n"
97 " -ui [OPTIONAL] Install ROOT interpreter UI for dd4hep \n"
98 " Will show up in the global interpreter variable\n"
99 " 'dd4hep::ROOTUI* gdd4hepUI' and allows the user\n"
100 " to interact with the the Detector instance from the\n"
101 " ROOT interactive prompt. \n"
102 " -plugin <name> <args> Execute plugin <name> after loading geometry. \n"
103 " All arguments following until the next \n"
104 " '-plugin' tag are considered as arguments \n"
105 " to the current plugin. \n"
111 void usage_default(
const char* name) {
112 std::cout <<
" " << name <<
" -opt [-opt] \n"
113 " -compact <file> Specify the compact geometry file \n"
114 " [REQUIRED] At least one compact geo file is required! \n";
115 print_default_args() <<
116 " -load_only [OPTIONAL] Dry-run to only load geometry without \n"
117 " starting the display. \n"
124 bool volmgr, dry_run, destroy, interpreter, ui;
125 dd4hep::PrintLevel print;
126 std::vector<const char*> geo_files, build_types;
127 std::vector<std::vector<const char*> > plugins;
136 print = dd4hep::INFO;
140 int handle(
int& i,
int argc,
char** argv) {
141 if ( ::strncmp(argv[i],
"-compact",5)==0 || ::strncmp(argv[i],
"-input",4)==0 ) {
142 geo_files.emplace_back(argv[++i]);
143 if ( argc>i+2 && ::strncmp(argv[i+1],
"-build_type",6)==0 ) {
144 build_types.emplace_back(argv[i+2]);
148 build_types.emplace_back(
"BUILD_DEFAULT");
151 else if ( ::strncmp(argv[i],
"-load_only",5)==0 )
153 else if ( ::strncmp(argv[i],
"-dry-run",5)==0 )
155 else if ( ::strncmp(argv[i],
"-print",4)==0 )
156 dd4hep::setPrintLevel(dd4hep::PrintLevel(print = dd4hep::decodePrintLevel(argv[++i])));
157 else if ( ::strncmp(argv[i],
"-destroy",5)==0 )
159 else if ( ::strncmp(argv[i],
"-no-destroy",8)==0 )
161 else if ( ::strncmp(argv[i],
"-volmgr",4)==0 )
163 else if ( ::strncmp(argv[i],
"-no-volmgr",7)==0 )
165 else if ( ::strncmp(argv[i],
"-interpreter",6)==0 )
167 else if ( ::strncmp(argv[i],
"-no-interpreter",7)==0 )
169 else if ( ::strncmp(argv[i],
"-interactive",6)==0 )
171 else if ( ::strncmp(argv[i],
"-no-interactive",7)==0 )
173 else if ( ::strncmp(argv[i],
"-ui",3)==0 )
175 else if ( ::strncmp(argv[i],
"-no-ui",6)==0 )
177 else if ( ::strncmp(argv[i],
"-plugin",5)==0 ) {
179 plugins.emplace_back(std::vector<const char*>());
180 plugins.back().emplace_back(argv[++i]);
181 for( ++i; i < argc; ++i ) {
182 if ( ::strncmp(argv[i],
"-plugin",5)==0 ) { --i;
break; }
183 if ( ::strncmp(argv[i],
"-end-plugin",4)==0 ) {
break; }
184 plugins.back().emplace_back(argv[i]);
186 plugins.back().emplace_back(
nullptr);
195 std::pair<int, char**> a(0,0);
197 for(
size_t i=0; i<plugins.size(); ++i) {
198 std::vector<const char*>& plug = plugins[i];
200 size_t num_args = plug.size()>2 ? plug.size()-2 : 0;
203 result = run_plugin(description,plug[0],plug.size()-1,(
char**)(num_args>0 ? &plug[1] : 0));
205 ::snprintf(text,
sizeof(text),
"[%8.3f sec]",stop.AsDouble()-start.AsDouble());
206 if ( result == EINVAL ) {
207 std::cout <<
"FAILED to execute dd4hep plugin: '" << plug[0]
208 <<
"' with args (" << num_args <<
") :[ ";
209 for( std::size_t j = 1; j < plug.size(); ++j ) {
210 if ( plug[j] ) std::cout << plug[j] <<
" ";
212 std::cout <<
"]" << std::endl << std::flush;
215 std::cout <<
"run_plugin: " << text <<
" Executed dd4hep plugin: '" << plug[0]
216 <<
"' with args (" << num_args <<
") :[ ";
217 for( std::size_t j=1; j<plug.size(); ++j ) {
218 if ( plug[j] ) std::cout << plug[j] <<
" ";
220 std::cout <<
"]" << std::endl << std::flush;
222 if ( name && name[0] ) {
223 result = run_plugin(description, name, a.first, a.second);
226 std::cout <<
"WARNING: run_plugin: No plugin name supplied. "
227 <<
"Implicitly assuming execution steered by XML." << std::endl;
235 for( std::size_t i=0; i<args.geo_files.size(); ++i ) {
236 const char* argv[] = { args.geo_files[i], args.build_types[i], 0 };
237 run_plugin(description,
"DD4hep_CompactLoader",2,(
char**)argv);
249 for(
int i=1; i<argc;++i ) {
250 if ( argv[i][0]==
'-' ) {
251 if ( args.handle(i, argc, argv) )
257 args.geo_files.emplace_back(argv[i]);
258 args.build_types.emplace_back(
"BUILD_DEFAULT");
261 if ( args.geo_files.empty() )
266 load_compact(description, args);
267 if ( args.ui ) run_plugin(description,
"DD4hep_InteractiveUI",0,0);
269 if ( args.volmgr ) run_plugin(description,
"DD4hep_VolumeManager",0,0);
272 if ( !args.dry_run ) {
274 std::pair<int, char**> a(0,0);
275 if ( args.interpreter ) {
276 TRint app(name, &a.first, a.second);
277 result = args.run(description, name);
278 if ( result != EINVAL ) app.Run();
281 result = args.run(description, name);
283 if ( result == EINVAL ) usage_default(name);
286 dd4hep::printout(INFO,
"Application",
"The geometry was loaded. Application now exiting.");
292 dd4hep::printout(ERROR,
"Application",
"destroyInstance: Uncaught exception: %s",e.what());
296 dd4hep::printout(ERROR,
"Application",
"destroyInstance: UNKNOWN uncaught exception.");
303 std::cout << runner <<
" -opt [-opt] \n"
304 " -input <file> [OPTIONAL] Specify geometry input file. \n"
305 " -plugin <name> <args> [args] [-end-plugin] \n"
306 " [REQUIRED] Plugin to be executed and applied. \n"
307 " -plugin <name> <args> [args] -end-plugin \n"
308 " [OPTIONAL] Next plugin with arguments. \n";
309 print_default_args() << std::endl;
310 if ( exit_program ) ::exit(EINVAL);
316 arguments.interpreter =
false;
318 for(
int i=1; i<argc;++i) {
319 if ( argv[i][0]==
'-' ) {
320 if ( arguments.handle(i,argc,argv) )
327 if ( !arguments.dry_run &&
329 !arguments.interpreter &&
330 arguments.plugins.empty() &&
331 arguments.geo_files.empty() )
335 std::unique_ptr<TRint> interpreter;
338 if ( !arguments.geo_files.empty() ) {
339 load_compact(description, arguments);
342 std::cout <<
"geoPluginRun: No geometry input supplied. "
343 <<
"No geometry will be loaded." << std::endl << std::flush;
346 if ( arguments.ui ) {
347 run_plugin(description,
"DD4hep_InteractiveUI",0,0);
350 if ( arguments.volmgr ) {
351 run_plugin(description,
"DD4hep_VolumeManager",0,0);
353 if ( arguments.interpreter ) {
354 std::pair<int, char**> a(0,0);
355 interpreter.reset(
new TRint(
"geoPluginRun", &a.first, a.second));
358 for(
size_t i=0; i<arguments.plugins.size(); ++i) {
359 std::vector<const char*>& plug = arguments.plugins[i];
360 int num_args = int(plug.size())-2;
363 long result = run_plugin(description, plug[0], num_args, (
char**)&plug[1]);
365 ::snprintf(text,
sizeof(text),
"[%8.3f sec]",stop.AsDouble()-start.AsDouble());
366 if ( result == EINVAL ) {
367 std::cout <<
"geoPluginRun: FAILED to execute dd4hep plugin: '" << plug[0]
368 <<
"' with args (" << num_args <<
") :[ ";
369 for(
size_t j = 1; j < plug.size(); ++j) {
370 if ( plug[j] ) std::cout << plug[j] <<
" ";
372 std::cout <<
"]" << std::endl;
375 std::cout <<
"geoPluginRun: " << text <<
" Executed dd4hep plugin: '" << plug[0]
376 <<
"' with args (" << num_args <<
") :[ ";
377 for(
size_t j=1; j<plug.size(); ++j) {
378 if ( plug[j] ) std::cout << plug[j] <<
" ";
380 std::cout <<
"]" << std::endl << std::flush;
382 if ( arguments.plugins.empty() ) {
384 if ( !arguments.dry_run ) {
386 std::pair<int, char**> a(0,0);
387 if ( arguments.interpreter ) {
388 TRint app(name, &a.first, a.second);
389 result = arguments.run(description, name);
390 if ( result != EINVAL ) app.Run();
393 result = arguments.run(description, name);
394 if ( result == EINVAL ) usage_default(name);
397 std::cout <<
"The geometry was loaded. Application now exiting." << std::endl;
400 if ( !arguments.dry_run && interpreter.get() ) {
407 dd4hep::printout(dd4hep::ERROR,
"Application",
"destroyInstance: Uncaught exception: %s",e.what());
411 dd4hep::printout(dd4hep::ERROR,
"Application",
"destroyInstance: UNKNOWN uncaught exception.");
423 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: Uncaught exception: %s",e.what());
426 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: UNKNOWN uncaught exception.");
437 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: Uncaught exception: %s",e.what());
440 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: UNKNOWN uncaught exception.");