ATLAS Offline Software
pyroot.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 "exec" "`which python`" "-tt" "$0" "$@"
4 
5 # File: pyroot.py
6 # Author: Sebastien Binet (binet@cern.ch)
7 
8 # This script allows you to run ROOT from python.
9 # It has been heavily based (err... stolen) on athena.py from Wim
10 
11 __author__ = 'Sebastien Binet (binet@cern.ch)'
12 __doc__ = 'For details about pyroot.py, run "less `which pyroot.py`"'
13 
14 import sys, os
15 import getopt, string
16 
17 
18 
19 _useropts = "bidc:hv"
20 _userlongopts = [ "batch", "interactive", "no-display", "debug=", "command=",
21  "help", "version",]
22 
23 
24 
25 def _help_and_exit( reason = None ):
26  print ("""Accepted command line options:
27  -b, --batch ... batch mode
28  -i, --interactive ... interactive mode [default]
29  --no-display prompt, but no graphics display
30  -c, --command ... one-liner, runs before any scripts
31  -h, --help ... print this help message
32  -,-- [arg1,...] ... additional arguments passed directly
33  to user scripts (left untouched)
34  [<file1>.py [<file2>.py [...]]] ... scripts to run""")
35 
36  sys.exit( 1 )
37 
38 
39 
40 scripts,opts = [],[]
41 runBatch = 0 # batch mode is NOT the default
42 display = None # only useful in interactive mode: no display
43 command = "" # optional one-line command
44 userOpts = [] # left-over opts after '-'
45 
46 
47 
48 for arg in sys.argv[1:]:
49  if arg[-3:] == ".py":
50  scripts.append( arg )
51  elif arg in ('-','--'): # rest are user opts, save and done
52  userOpts += sys.argv[ sys.argv.index( arg )+1: ]
53  break
54  else:
55  opts.append( arg )
56 
57 
58 try:
59  optlist, args = getopt.getopt( opts, _useropts, _userlongopts )
60 except getopt.error:
61  print (sys.exc_value)
63 
64 if args:
65  print ("Unhandled arguments:", args)
67 
68 for opt, arg in optlist:
69  if opt in ("-b", "--batch"):
70  runBatch = 1
71  elif opt in ("-i", "--interactive"):
72  runBatch = 0
73  defOptions = ""
74  if display is None: display = 1
75  elif opt in ("--no-display",):
76  display = 0
77  elif opt in ("-c", "--command"):
78  command = string.strip( arg )
79  elif opt in ("-h", "--help"):
81 
82 if optlist: del opt, arg
83 del args, optlist, opts
84 del _useropts, _userlongopts, string, getopt
85 
86 
87 if not display and '-b' not in sys.argv:
88  sys.argv = sys.argv[:1] + ['-b'] + sys.argv[1:]
89 del display
90 
91 
92 
93 if not os.getcwd() in sys.path:
94  sys.path = [ os.getcwd() ] + sys.path
95 
96 if '' not in sys.path:
97  sys.path = [ '' ] + sys.path
98 
99 sys.ps1 = 'pyroot> '
100 fhistory = os.path.expanduser( '~/.pyroot.history' )
101 
102 
103 if runBatch:
104  # in batch there is no need for stdin
105  if os.isatty( sys.stdin.fileno() ):
106  os.close( sys.stdin.fileno() )
107  if 'PYTHONINSPECT' in os.environ:
108  os.unsetenv('PYTHONINSPECT')
109 else:
110  os.environ['PYTHONINSPECT'] = '1'
111  # readline support
112  import rlcompleter, readline # noqa: F401
113 
114  readline.parse_and_bind( 'tab: complete' )
115  readline.parse_and_bind( 'set show-all-if-ambiguous On' )
116 
117  # history support
118  if os.path.exists( fhistory ):
119  readline.read_history_file( fhistory )
120  readline.set_history_length( 1024 )
121 
122  del readline, rlcompleter
123 
124 
125 print (sys.ps1+"loading ROOT...")
126 import ROOT
127 print (sys.ps1+"loading ROOT... [ok]")
128 ROOT.gROOT.SetStyle("Plain")
129 ROOT.gStyle.SetPalette(1) # less ugly palette colors
130 ROOT.gStyle.SetOptStat(111111) #
131 print (sys.ps1+"loaded pyroot style... [ok]")
132 
133 
134 if not runBatch:
135  import atexit, readline
136 
137  # history support
138  atexit.register( readline.write_history_file, fhistory )
139  del readline, atexit
140 
141 del fhistory
142 
143 if command:
144  print (sys.ps1+'executing CLI (-c) command: "%s"' % command)
145  exec (command)
146 del command
147 
148 for script in scripts:
149  try:
150  exec (open(script).read(), globals())
151  except Exception as e:
152  if isinstance(e,SystemExit):
153  raise
154 
155  import traceback
156  traceback.print_exc()
157 
158  if runBatch:
159  import sys
160  sys.exit(2)
161 
162  # for interactive: drop into prompt
163  break
164 
165 else:
166 
167  del scripts
168 
169 
170  if runBatch:
171  import sys
172  sys.exit(0)
173  else:
174  # done, back to the user
175  print (sys.ps1+"entering interactive session...")
176  pass
read
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)
Definition: openCoraCool.cxx:569
pyroot._help_and_exit
def _help_and_exit(reason=None)
explanation of the options --------------------------------------------—
Definition: pyroot.py:25
Trk::open
@ open
Definition: BinningType.h:40