File indexing completed on 2025-01-18 09:13:47
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Factories.h>
0016 #include <DD4hep/Printout.h>
0017
0018
0019 #include <TPython.h>
0020
0021 using namespace std;
0022 using namespace dd4hep;
0023
0024
0025 namespace {
0026
0027 void usage(int argc, char** argv) {
0028 cout <<
0029 "Usage: -plugin <name> -arg [-arg] \n"
0030 " name: factory name DD4hep_Python \n"
0031 " -import <string> import a python module, making its classes available.\n"
0032 " -macro <string> load a python script as if it were a macro. \n"
0033 " -exec <string> execute a python statement (e.g. \"import ROOT\". \n"
0034 " -eval <string> evaluate a python expression (e.g. \"1+1\") \n"
0035 " -prompt enter an interactive python session (exit with ^D) \n"
0036 " -dd4hep Equivalent to -exec \"import dd4hep\" \n"
0037 " -help Show this online help message. \n"
0038 " \n"
0039 " Note: entries can be given multiple times and are executed in exactly the \n"
0040 " order specified at the command line! \n"
0041 " \n"
0042 "\tArguments given: " << arguments(argc,argv) << endl << flush;
0043 ::exit(EINVAL);
0044 }
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 long call_python(Detector& , int argc, char** argv) {
0055 if ( argc > 0 ) {
0056 vector<pair<string, string> > commands;
0057 for(int i = 0; i < argc && argv[i]; ++i) {
0058 if ( 0 == ::strncmp("-import",argv[i],2) )
0059 commands.emplace_back("import",argv[++i]);
0060 else if ( 0 == ::strncmp("-dd4hep", argv[i],2) )
0061 commands.emplace_back("exec","import dd4hep");
0062 else if ( 0 == ::strncmp("-macro", argv[i],2) )
0063 commands.emplace_back("macro",argv[++i]);
0064 else if ( 0 == ::strncmp("-exec", argv[i],2) )
0065 commands.emplace_back("exec",argv[++i]);
0066 else if ( 0 == ::strncmp("-eval", argv[i],2) )
0067 commands.emplace_back("calc",argv[++i]);
0068 else if ( 0 == ::strncmp("-prompt", argv[i],2) )
0069 commands.emplace_back("prompt","");
0070 else
0071 usage(argc, argv);
0072 }
0073 if ( commands.empty() ) {
0074 usage(argc, argv);
0075 }
0076 for(const auto& c : commands) {
0077 Bool_t result = kFALSE;
0078 switch(c.first[0]) {
0079 case 'i':
0080 result = TPython::Import(c.second.c_str());
0081 break;
0082 case 'm':
0083 TPython::LoadMacro(c.second.c_str());
0084 result = kTRUE;
0085 break;
0086 case 'e':
0087 result = TPython::Exec(c.second.c_str());
0088 break;
0089 case 'c':
0090
0091 TPython::Exec(c.second.c_str());
0092 result = kTRUE;
0093 break;
0094 case 'p':
0095 TPython::Prompt();
0096 result = kTRUE;
0097 break;
0098 default:
0099 usage(argc, argv);
0100 ::exit(EINVAL);
0101 break;
0102 }
0103 if ( result != kTRUE ) {
0104 except("DD4hep_Python","+++ Failed to invoke the statement: %s",c.second.c_str());
0105 }
0106 }
0107 return 1;
0108 }
0109 except("DD4hep_Python","+++ No commands file name given.");
0110 return 0;
0111 }
0112 }
0113
0114 DECLARE_APPLY(DD4hep_Python,call_python)