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,
help;
125 dd4hep::PrintLevel print;
126 std::vector<const char*> geo_files, build_types;
127 std::vector<std::vector<const char*> > plugins;
137 print = dd4hep::INFO;
141 int handle(
int& i,
int argc,
char** argv) {
142 if ( ::strncmp(argv[i],
"-compact",5)==0 || ::strncmp(argv[i],
"-input",4)==0 ) {
143 geo_files.emplace_back(argv[++i]);
144 if ( argc>i+2 && ::strncmp(argv[i+1],
"-build_type",6)==0 ) {
145 build_types.emplace_back(argv[i+2]);
149 build_types.emplace_back(
"BUILD_DEFAULT");
152 else if ( ::strncmp(argv[i],
"-help",5)==0 )
154 else if ( ::strncmp(argv[i],
"--help",6)==0 )
156 else if ( ::strncmp(argv[i],
"-load_only",5)==0 )
158 else if ( ::strncmp(argv[i],
"-dry-run",5)==0 )
160 else if ( ::strncmp(argv[i],
"-print",4)==0 )
161 dd4hep::setPrintLevel(dd4hep::PrintLevel(print = dd4hep::decodePrintLevel(argv[++i])));
162 else if ( ::strncmp(argv[i],
"-destroy",5)==0 )
164 else if ( ::strncmp(argv[i],
"-no-destroy",8)==0 )
166 else if ( ::strncmp(argv[i],
"-volmgr",4)==0 )
168 else if ( ::strncmp(argv[i],
"-no-volmgr",7)==0 )
170 else if ( ::strncmp(argv[i],
"-interpreter",6)==0 )
172 else if ( ::strncmp(argv[i],
"-no-interpreter",7)==0 )
174 else if ( ::strncmp(argv[i],
"-interactive",6)==0 )
176 else if ( ::strncmp(argv[i],
"-no-interactive",7)==0 )
178 else if ( ::strncmp(argv[i],
"-ui",3)==0 )
180 else if ( ::strncmp(argv[i],
"-no-ui",6)==0 )
182 else if ( ::strncmp(argv[i],
"-plugin",5)==0 ) {
184 plugins.emplace_back(std::vector<const char*>());
185 plugins.back().emplace_back(argv[++i]);
186 for( ++i; i < argc; ++i ) {
187 if ( ::strncmp(argv[i],
"-plugin",5)==0 ) { --i;
break; }
188 if ( ::strncmp(argv[i],
"-end-plugin",4)==0 ) {
break; }
189 plugins.back().emplace_back(argv[i]);
191 plugins.back().emplace_back(
nullptr);
200 std::pair<int, char**> a(0,0);
202 for(
size_t i=0; i<plugins.size(); ++i) {
203 std::vector<const char*>& plug = plugins[i];
205 size_t num_args = plug.size()>2 ? plug.size()-2 : 0;
208 result = run_plugin(description,plug[0],plug.size()-1,(
char**)(num_args>0 ? &plug[1] : 0));
210 ::snprintf(text,
sizeof(text),
"[%8.3f sec]",stop.AsDouble()-start.AsDouble());
211 if ( result == EINVAL ) {
212 std::cout <<
"FAILED to execute dd4hep plugin: '" << plug[0]
213 <<
"' with args (" << num_args <<
") :[ ";
214 for( std::size_t j = 1; j < plug.size(); ++j ) {
215 if ( plug[j] ) std::cout << plug[j] <<
" ";
217 std::cout <<
"]" << std::endl << std::flush;
220 std::cout <<
"run_plugin: " << text <<
" Executed dd4hep plugin: '" << plug[0]
221 <<
"' with args (" << num_args <<
") :[ ";
222 for( std::size_t j=1; j<plug.size(); ++j ) {
223 if ( plug[j] ) std::cout << plug[j] <<
" ";
225 std::cout <<
"]" << std::endl << std::flush;
227 if ( name && name[0] ) {
228 result = run_plugin(description, name, a.first, a.second);
231 std::cout <<
"WARNING: run_plugin: No plugin name supplied. "
232 <<
"Implicitly assuming execution steered by XML." << std::endl;
240 for( std::size_t i=0; i<args.geo_files.size(); ++i ) {
241 const char* argv[] = { args.geo_files[i], args.build_types[i], 0 };
242 run_plugin(description,
"DD4hep_CompactLoader",2,(
char**)argv);
254 for(
int i=1; i<argc;++i ) {
255 if ( argv[i][0]==
'-' ) {
256 if ( args.handle(i, argc, argv) )
262 args.geo_files.emplace_back(argv[i]);
263 args.build_types.emplace_back(
"BUILD_DEFAULT");
266 if ( args.geo_files.empty() )
271 load_compact(description, args);
272 if ( args.ui ) run_plugin(description,
"DD4hep_InteractiveUI",0,0);
274 if ( args.volmgr ) run_plugin(description,
"DD4hep_VolumeManager",0,0);
277 if ( !args.dry_run ) {
279 std::pair<int, char**> a(0,0);
280 if ( args.interpreter ) {
281 TRint app(name, &a.first, a.second);
282 result = args.run(description, name);
283 if ( result != EINVAL ) app.Run();
286 result = args.run(description, name);
288 if ( result == EINVAL ) usage_default(name);
291 dd4hep::printout(INFO,
"Application",
"The geometry was loaded. Application now exiting.");
297 dd4hep::printout(ERROR,
"Application",
"destroyInstance: Uncaught exception: %s",e.what());
301 dd4hep::printout(ERROR,
"Application",
"destroyInstance: UNKNOWN uncaught exception.");
308 std::cout << runner <<
" -opt [-opt] \n"
309 " -input <file> [OPTIONAL] Specify geometry input file. \n"
310 " -plugin <name> <args> [args] [-end-plugin] \n"
311 " [REQUIRED] Plugin to be executed and applied. \n"
312 " -plugin <name> <args> [args] -end-plugin \n"
313 " [OPTIONAL] Next plugin with arguments. \n";
314 print_default_args() << std::endl;
315 if ( exit_program ) ::exit(EINVAL);
321 arguments.interpreter =
false;
323 for(
int i=1; i < argc; ++i ) {
324 if( argv[i][0]==
'-' ) {
325 if ( arguments.handle(i,argc,argv) )
332 if( !arguments.dry_run &&
334 !arguments.interpreter &&
335 arguments.plugins.empty() &&
336 arguments.geo_files.empty() )
340 if( arguments.help ) {
343 std::unique_ptr<TRint> interpreter;
346 if( !arguments.geo_files.empty() ) {
347 load_compact(description, arguments);
350 std::cout <<
"geoPluginRun: No geometry input supplied. "
351 <<
"No geometry will be loaded." << std::endl << std::flush;
355 run_plugin(description,
"DD4hep_InteractiveUI",0,0);
358 if( arguments.volmgr ) {
359 run_plugin(description,
"DD4hep_VolumeManager",0,0);
361 if( arguments.interpreter ) {
362 std::pair<int, char**> a(0,0);
363 interpreter.reset(
new TRint(
"geoPluginRun", &a.first, a.second));
366 for(
size_t i=0; i < arguments.plugins.size(); ++i ) {
367 std::vector<const char*>& plug = arguments.plugins[i];
368 int num_args = int(plug.size())-2;
371 long result = run_plugin(description, plug[0], num_args, (
char**)&plug[1]);
373 ::snprintf(text,
sizeof(text),
"[%8.3f sec]",stop.AsDouble()-start.AsDouble());
374 if (result == EINVAL ) {
375 std::cout <<
"geoPluginRun: FAILED to execute 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;
383 std::cout <<
"geoPluginRun: " << text <<
" Executed dd4hep plugin: '" << plug[0]
384 <<
"' with args (" << num_args <<
") :[ ";
385 for(
size_t j=1; j < plug.size(); ++j ) {
386 if ( plug[j] ) std::cout << plug[j] <<
" ";
388 std::cout <<
"]" << std::endl << std::flush;
390 if( arguments.plugins.empty() ) {
392 if( !arguments.dry_run ) {
394 std::pair<int, char**> a(0,0);
395 if( arguments.interpreter ) {
396 TRint app(name, &a.first, a.second);
397 result = arguments.run(description, name);
398 if( result != EINVAL ) app.Run();
401 result = arguments.run(description, name);
402 if( result == EINVAL ) usage_default(name);
405 std::cout <<
"The geometry was loaded. Application now exiting." << std::endl;
408 if ( !arguments.dry_run && interpreter.get() ) {
415 dd4hep::printout(dd4hep::ERROR,
"Application",
"destroyInstance: Uncaught exception: %s",e.what());
419 dd4hep::printout(dd4hep::ERROR,
"Application",
"destroyInstance: UNKNOWN uncaught exception.");
431 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: Uncaught exception: %s",e.what());
434 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: UNKNOWN uncaught exception.");
445 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: Uncaught exception: %s",e.what());
448 dd4hep::printout(dd4hep::ERROR,
"Application",
"geoPluginRun: UNKNOWN uncaught exception.");