5 from __future__
import print_function
7 import sys, os, re, getopt, json, subprocess
8 from collections
import OrderedDict
14 """- convert TGeo response file options to ACTS v13.0.0 JSON format
19 +
""" [OPTIONS] tgeo.response
21 ACTS v13.0.0 (PR #884) changed the way the TGeo detector configuration is specified.
22 A JSON file is now used, instead of the previous method using Boost options,
23 which were often collected together in a response file (--response-file).
24 This script converts an old response file to the new JSON file.
26 To include all the required settings, this script needs to know the defaults for
27 any options not specified in the response file. These defaults can be obtained
28 by running a TGeo example with the --geo-tgeo-dump-jsonconfig=def.json option.
29 This script includes a hardcoded copy of these defaults (produced with ACTS v13.0.0).
30 These are used by default, but the latest defaults can be regenerated and used by
31 specifying the -d (or -c) option.
33 The JSON file is written to stdout.
36 -h display this help and exit
38 -d use ActsExampleGeometryTGeo --geo-tgeo-dump-jsonconfig to get list of default options
39 -c CMD run CMD --geo-tgeo-dump-jsonconfig instead
40 -f JSON read list of default options from JSON file
41 -n don't add any defaults
43 If none of -dcfn options is specified, then use hardcoded default options.
45 AUTHOR: Tim Adye <tim.adye@cern.ch>""",
49 prog = os.path.basename(sys.argv[0])
56 with
open(filename)
as f:
59 print(prog +
":", e, file=sys.stderr)
65 optlist, args = getopt.getopt(sys.argv[1:],
"hvdc:f:n")
66 except getopt.GetoptError
as e:
67 print(prog +
":", e, file=sys.stderr)
70 if "-h" in opt
or len(sys.argv) <= 1:
85 print(
str(iline) +
":" + line, end=
"", file=sys.stderr)
86 line = re.sub(
r"#.*",
"", line).strip()
90 if re.match(
r"--geo-[\w-]+-loglevel\s\d+$", line):
93 m = re.match(
r"--(geo-tgeo-[\w-]+)\s+(.*)$", line)
96 "%s:%d: unrecognised type of option: %s" % (f.name, iline, line),
102 if o ==
"geo-tgeo-filename" or o ==
"geo-tgeo-worldvolume":
106 if o ==
"geo-tgeo-unit-scalor":
110 if o ==
"geo-tgeo-beampipe-parameters":
111 cfg[
"geo-tgeo-build-beampipe"] =
True
112 cfg[o] = [float(x)
for x
in v.split(
":")]
115 if o ==
"geo-tgeo-volume":
117 cfg[
"Volumes"] = vols
119 vol[
"geo-tgeo-volume-name"] = v
125 "%s:%d: unrecognised global option: %s" % (f.name, iline, line),
130 if re.match(
"geo-tgeo-sfbin-(r|z|phi)-tolerance$", o):
131 vv = [float(x)
for x
in v.split(
":")]
132 vol[o] = OrderedDict([(
"lower", vv[0]), (
"upper", vv[1])])
135 m = re.match(
"geo-tgeo-([ncp])(.*)$", o)
138 "%s:%d: unrecognised option: %s" % (f.name, iline, line),
143 side, oo = m.groups()
144 side = {
"n":
"negative",
"c":
"central",
"p":
"positive"}[side]
145 oo =
"geo-tgeo-" + oo
148 if oo ==
"geo-tgeo-layers":
149 oo =
"geo-tgeo-volume-layers"
152 elif oo ==
"geo-tgeo-volume-name":
153 oo =
"geo-tgeo-subvolume-names"
155 elif oo ==
"geo-tgeo-module-name":
156 oo =
"geo-tgeo-sensitive-names"
159 elif oo ==
"geo-tgeo-module-axes":
160 oo =
"geo-tgeo-sensitive-axes"
162 elif re.match(
"geo-tgeo-layer-[rz]-split$", oo):
165 elif re.match(
"geo-tgeo-layer-[rz]-range$", oo):
167 vv = [float(x)
for x
in vv.split(
":")]
168 vv = OrderedDict([(
"lower", vv[0]), (
"upper", vv[1])])
172 "%s:%d: unrecognised option: %s" % (f.name, iline, line),
178 vol[oo] = OrderedDict()
188 with
open(opt[
"-f"])
as ef:
190 empty = json.loads(ecfg, object_pairs_hook=OrderedDict)
197 for o, v
in empty.items():
200 if len(empty.get(
"Volumes", [])):
202 for o, v
in empty[
"Volumes"][0].
items():
206 print(json.dumps(cfg, indent=2))
210 cmd +=
" --geo-tgeo-dump-jsonconfig=/dev/stdout"
212 print(
"+", cmd, file=sys.stderr)
213 cfg = subprocess.check_output(cmd, shell=
True)
215 print(prog +
": command failed: " + cmd, file=sys.stderr)
218 print(cfg, file=sys.stderr)
219 return json.loads(cfg, object_pairs_hook=OrderedDict)
226 "geo-tgeo-unit-scalor": 1.0,
227 "geo-tgeo-build-beampipe": false,
228 "geo-tgeo-beampipe-parameters": [
235 "geo-tgeo-volume-name": "",
236 "geo-tgeo-sfbin-r-tolerance": {
240 "geo-tgeo-sfbin-z-tolerance": {
244 "geo-tgeo-sfbin-phi-tolerance": {
248 "geo-tgeo-volume-layers": {
253 "geo-tgeo-subvolume-names": {
258 "geo-tgeo-sensitive-names": {
263 "geo-tgeo-sensitive-axes": {
268 "geo-tgeo-layer-r-ranges": {
282 "geo-tgeo-layer-z-ranges": {
296 "geo-tgeo-layer-r-split": {
301 "geo-tgeo-layer-z-split": {
306 "geo-tgeo-cyl-disc-split": false,
309 "geo-tgeo-cyl-nphi-segs": 0,
310 "geo-tgeo-cyl-nz-segs": 0,
311 "geo-tgeo-disc-nphi-segs": 0,
312 "geo-tgeo-disc-nr-segs": 0
319 object_pairs_hook=OrderedDict,
323 if __name__ ==
"__main__":