ATLAS Offline Software
Loading...
Searching...
No Matches
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
14import sys, os
15import getopt, string
16
17
18
19_useropts = "bidc:hv"
20_userlongopts = [ "batch", "interactive", "no-display", "debug=", "command=",
21 "help", "version",]
22
23
24
25def _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
40scripts,opts = [],[]
41runBatch = 0 # batch mode is NOT the default
42display = None # only useful in interactive mode: no display
43command = "" # optional one-line command
44userOpts = [] # left-over opts after '-'
45
46
47
48for 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
58try:
59 optlist, args = getopt.getopt( opts, _useropts, _userlongopts )
60except getopt.error:
61 print (sys.exc_value)
63
64if args:
65 print ("Unhandled arguments:", args)
67
68for 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
82if optlist: del opt, arg
83del args, optlist, opts
84del _useropts, _userlongopts, string, getopt
85
86
87if not display and '-b' not in sys.argv:
88 sys.argv = sys.argv[:1] + ['-b'] + sys.argv[1:]
89del display
90
91
92
93if not os.getcwd() in sys.path:
94 sys.path = [ os.getcwd() ] + sys.path
95
96if '' not in sys.path:
97 sys.path = [ '' ] + sys.path
98
99sys.ps1 = 'pyroot> '
100fhistory = os.path.expanduser( '~/.pyroot.history' )
101
102
103if 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')
109else:
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
125print (sys.ps1+"loading ROOT...")
126import ROOT
127print (sys.ps1+"loading ROOT... [ok]")
128ROOT.gROOT.SetStyle("Plain")
129ROOT.gStyle.SetPalette(1) # less ugly palette colors
130ROOT.gStyle.SetOptStat(111111) #
131print (sys.ps1+"loaded pyroot style... [ok]")
132
133
134if not runBatch:
135 import atexit, readline
136
137 # history support
138 atexit.register( readline.write_history_file, fhistory )
139 del readline, atexit
140
141del fhistory
142
143if command:
144 print (sys.ps1+'executing CLI (-c) command: "%s"' % command)
145 exec (command)
146del command
147
148for 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
165else:
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
_help_and_exit(reason=None)
explanation of the options --------------------------------------------—
Definition pyroot.py:25
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)