DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
PythonPlugin.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Factories.h>
16 #include <DD4hep/Printout.h>
17 
18 // ROOT includes
19 #include <TPython.h>
20 
21 using namespace std;
22 using namespace dd4hep;
23 
25 namespace {
26 
27  void usage(int argc, char** argv) {
28  cout <<
29  "Usage: -plugin <name> -arg [-arg] \n"
30  " name: factory name DD4hep_Python \n"
31  " -import <string> import a python module, making its classes available.\n"
32  " -macro <string> load a python script as if it were a macro. \n"
33  " -exec <string> execute a python statement (e.g. \"import ROOT\". \n"
34  " -eval <string> evaluate a python expression (e.g. \"1+1\") \n"
35  " -prompt enter an interactive python session (exit with ^D) \n"
36  " -dd4hep Equivalent to -exec \"import dd4hep\" \n"
37  " -help Show this online help message. \n"
38  " \n"
39  " Note: entries can be given multiple times and are executed in exactly the \n"
40  " order specified at the command line! \n"
41  " \n"
42  "\tArguments given: " << arguments(argc,argv) << endl << flush;
43  ::exit(EINVAL);
44  }
45 
47 
54  long call_python(Detector& /* description */, int argc, char** argv) {
55  if ( argc > 0 ) {
56  vector<pair<string, string> > commands;
57  for(int i = 0; i < argc && argv[i]; ++i) {
58  if ( 0 == ::strncmp("-import",argv[i],2) )
59  commands.emplace_back("import",argv[++i]);
60  else if ( 0 == ::strncmp("-dd4hep", argv[i],2) )
61  commands.emplace_back("exec","import dd4hep");
62  else if ( 0 == ::strncmp("-macro", argv[i],2) )
63  commands.emplace_back("macro",argv[++i]);
64  else if ( 0 == ::strncmp("-exec", argv[i],2) )
65  commands.emplace_back("exec",argv[++i]);
66  else if ( 0 == ::strncmp("-eval", argv[i],2) )
67  commands.emplace_back("calc",argv[++i]);
68  else if ( 0 == ::strncmp("-prompt", argv[i],2) )
69  commands.emplace_back("prompt","");
70  else
71  usage(argc, argv);
72  }
73  if ( commands.empty() ) {
74  usage(argc, argv);
75  }
76  for(const auto& c : commands) {
77  Bool_t result = kFALSE;
78  switch(c.first[0]) {
79  case 'i':
80  result = TPython::Import(c.second.c_str());
81  break;
82  case 'm':
83  TPython::LoadMacro(c.second.c_str());
84  result = kTRUE;
85  break;
86  case 'e':
87  result = TPython::Exec(c.second.c_str());
88  break;
89  case 'c':
90  // we do not care about the result
91  TPython::Exec(c.second.c_str());
92  result = kTRUE;
93  break;
94  case 'p':
95  TPython::Prompt();
96  result = kTRUE;
97  break;
98  default:
99  usage(argc, argv);
100  ::exit(EINVAL);
101  break;
102  }
103  if ( result != kTRUE ) {
104  except("DD4hep_Python","+++ Failed to invoke the statement: %s",c.second.c_str());
105  }
106  }
107  return 1;
108  }
109  except("DD4hep_Python","+++ No commands file name given.");
110  return 0;
111  }
112 }
113 
114 DECLARE_APPLY(DD4hep_Python,call_python)
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
Factories.h
std
Definition: Plugins.h:30
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41
Printout.h