Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryVisualisationAndMaterialHandling.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryVisualisationAndMaterialHandling.py
1 import json
2 
3 import json
4 
5 
6 def dumper(obj):
7  try:
8  return obj.toJSON()
9  except:
10  return obj.__dict__
11 
12 
13 class index_info(object):
14  def __init__(self, index):
15  self.index = index
16  self.boundaries = []
19  self.approaches = []
26 
27  def __repr__(self):
28  return repr(
29  (
30  self.index,
31  self.boundaries,
32  self.layers_no_approach,
34  self.approaches,
39  self.activeBinningRorZ,
40  self.activeBinningPhi,
41  )
42  )
43 
44 
45 def append_index_if_missing(dictionary, name, index):
46  if name not in dictionary:
47  dictionary[name] = index_info(index)
48 
49 
50 def extract_coords(coords, is_disc):
51  x_values = [coords[1], coords[2]]
52  y_values = [coords[0], coords[0]]
53  if is_disc:
54  x_values = [coords[2], coords[2]]
55  y_values = [coords[1], coords[0]]
56  return x_values, y_values
57 
58 
59 def dump_geo(filename, plot, output_folder, dump_steering, steering_file):
60  f = open(filename)
61  data = json.load(f)
62 
63  # First you want to read the Volume entries and save the relevant quantities
64  # - names
65  index_to_names = []
66  for entry in data["Volumes"]["entries"]:
67  index_to_names.append(entry["value"]["NAME"])
68 
69  # In case volume names are not found check in the volume names with dummy values
70  if not index_to_names:
71  for entry in data["Surfaces"]["entries"]:
72  if "volume" in entry:
73  vol = entry["volume"]
74  if "volume" + str(vol) not in index_to_names:
75  index_to_names.append("volume" + str(vol))
76 
77  # Stores the information to easily decide on which layers you want the material to be mapped
78  steering_map = {}
79 
80  # Once you have read the volumes extensions, you read the surfaces representing layers and boundaries.
81  # All these surfaces belong to a volume, they have therefore a volume index.
82  # You are interested only in:
83  # - surfaces with layer index (NO approach index):
84  # -- even layer indices represent "active" layers, i.e. the ones corresponding to sensitive layers
85  # -- odd event indices represent navigation layers
86  # - surfaces with layer index AND approach index:
87  # -- indicate approach layers to "active" layers:
88  # e.g. it can happen that for cylinders: 1 sits in front of the active layer,
89  # 2 sits in the middle of the layer,
90  # 3 sits behind the layer.
91  # ...
92  # - surfaces with boundary index (no layer index in this case):
93  # -- they indicate boundaries between volumes. You are interested in boundaries between volumes
94  # containing at least an active layer.
95 
96  index_to_extends_layers_bounds_cylinders = [[] for _ in range(len(index_to_names))]
97  index_to_extends_layers_bounds_discs = [[] for _ in range(len(index_to_names))]
98 
99  index_to_extends_layers_cylinders = [[] for _ in range(len(index_to_names))]
100  index_to_extends_layers_discs = [[] for _ in range(len(index_to_names))]
101 
102  for entry in data["Surfaces"]["entries"]:
103  if "layer" in entry:
104  extends = []
105  vol = entry["volume"]
106 
107  if "sensitive" in entry:
108  continue
109 
110  # Filling extends with the following quantities:
111  # for cylinders:
112  # radius, z min, z max, approach index, layer index
113  # for discs:
114  # inner radius, outer radius, z position, approach index, layer index
115 
116  z_shift = 0.0
117  if entry["value"]["transform"]["translation"] != None:
118  z_shift = entry["value"]["transform"]["translation"][2]
119 
120  approach_index = -1
121  if "approach" in entry:
122  approach_index = entry["approach"]
123 
124  if entry["value"]["type"] == "CylinderSurface":
125  # radius, z min, z max, approach index, layer index
126  extends = [
127  entry["value"]["bounds"]["values"][0],
128  z_shift - entry["value"]["bounds"]["values"][1],
129  z_shift + entry["value"]["bounds"]["values"][1],
130  approach_index,
131  entry["layer"],
132  ]
133  index_to_extends_layers_cylinders[vol - 1].append(extends)
134  elif entry["value"]["type"] == "DiscSurface":
135  # inner radius, outer radius, z position, approach index, layer index
136  extends = [
137  entry["value"]["bounds"]["values"][0],
138  entry["value"]["bounds"]["values"][1],
139  z_shift,
140  approach_index,
141  entry["layer"],
142  ]
143  index_to_extends_layers_discs[vol - 1].append(extends)
144  else:
145  print(
146  "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
147  )
148 
149  if "boundary" in entry:
150  extends = []
151  vol = entry["volume"]
152 
153  # Filling extends with the following quantities:
154  # for cylinders:
155  # radius, z min, z max, boundary index
156  # for discs:
157  # inner radius, outer radius, z position, boundary index
158 
159  z_shift = 0.0
160  if entry["value"]["transform"]["translation"] != None:
161  z_shift = entry["value"]["transform"]["translation"][2]
162 
163  if entry["value"]["type"] == "CylinderSurface":
164  extends = [
165  entry["value"]["bounds"]["values"][0],
166  z_shift - entry["value"]["bounds"]["values"][1],
167  z_shift + entry["value"]["bounds"]["values"][1],
168  entry["boundary"],
169  ]
170  index_to_extends_layers_bounds_cylinders[vol - 1].append(extends)
171  elif entry["value"]["type"] == "DiscSurface":
172  extends = [
173  entry["value"]["bounds"]["values"][0],
174  entry["value"]["bounds"]["values"][1],
175  z_shift,
176  entry["boundary"],
177  ]
178  index_to_extends_layers_bounds_discs[vol - 1].append(extends)
179  else:
180  print(
181  "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
182  )
183 
184  # Steering the information and collect it into an output file if needed
185  from itertools import chain
186 
187  interesting_volumes = []
188  v_index = 0
189  is_disc = False
190  for elements in chain(
191  index_to_extends_layers_cylinders, index_to_extends_layers_discs
192  ):
193  for coords in elements:
194  if coords[3] > 0:
195  continue
196  if v_index not in interesting_volumes:
197  interesting_volumes.append(v_index)
198  append_index_if_missing(steering_map, index_to_names[v_index], v_index + 1)
199  steering_map[index_to_names[v_index]].layers_no_approach.append(coords[4])
200  v_index = v_index + 1
201  if not is_disc and v_index == len(index_to_extends_layers_cylinders):
202  v_index = 0
203  is_disc = True
204 
205  v_index = 0
206  is_disc = False
207  for elements in chain(
208  index_to_extends_layers_bounds_cylinders, index_to_extends_layers_bounds_discs
209  ):
210  for coords in elements:
211  if v_index in interesting_volumes:
213  steering_map, index_to_names[v_index], v_index + 1
214  )
215  steering_map[index_to_names[v_index]].boundaries.append(coords[3])
216  v_index = v_index + 1
217  if not is_disc and v_index == len(index_to_extends_layers_bounds_cylinders):
218  v_index = 0
219  is_disc = True
220 
221  v_index = 0
222  is_disc = False
223  for elements in chain(
224  index_to_extends_layers_cylinders, index_to_extends_layers_discs
225  ):
226  for coords in elements:
227  if coords[3] == -1:
228  continue
229  if (
230  coords[4]
231  not in steering_map[index_to_names[v_index]].layers_with_approach
232  ):
233  steering_map[index_to_names[v_index]].layers_with_approach.append(
234  coords[4]
235  )
236  if coords[3] not in steering_map[index_to_names[v_index]].approaches:
237  steering_map[index_to_names[v_index]].approaches.append(coords[3])
238  v_index = v_index + 1
239  if not is_disc and v_index == len(index_to_extends_layers_bounds_cylinders):
240  v_index = 0
241  is_disc = True
242 
243  if dump_steering:
244  output_map = {"SteeringField": steering_map}
245  with open(steering_file, "w", encoding="utf-8") as f:
246  json.dump(output_map, f, default=dumper, ensure_ascii=False, indent=4)
247 
248  # Once you have all the data in hands, you make a few nice plots to show volumes and surfaces
249  if plot:
250  import matplotlib.pyplot as plt
251 
252  plt.rcParams.update({"figure.max_open_warning": 0})
253  from matplotlib.pyplot import cm
254  from itertools import cycle
255  import numpy as np
256 
257  color = cm.rainbow(np.linspace(0, 1, len(index_to_extends_layers_cylinders)))
258 
259  is_in_legend = []
260 
261  plt.figure(figsize=(20, 10))
262 
263  # Plot all layers (w/o approach layers) + boundaries
264  v_index = 0
265  is_disc = False
266  for elements in chain(
267  index_to_extends_layers_cylinders, index_to_extends_layers_discs
268  ):
269  for coords in elements:
270  # skip approach layers
271  if coords[3] > 0:
272  continue
273  x_values, y_values = extract_coords(coords, is_disc)
274  if index_to_names[v_index] not in is_in_legend:
275  plt.plot(
276  x_values,
277  y_values,
278  c=color[v_index],
279  label="v: " + str(v_index + 1) + ", " + index_to_names[v_index],
280  )
281  is_in_legend.append(index_to_names[v_index])
282  else:
283  plt.plot(x_values, y_values, c=color[v_index])
284  v_index = v_index + 1
285  if not is_disc and v_index == len(index_to_extends_layers_cylinders):
286  v_index = 0
287  is_disc = True
288 
289  v_index = 0
290  is_disc = False
291  for elements in chain(
292  index_to_extends_layers_bounds_cylinders,
293  index_to_extends_layers_bounds_discs,
294  ):
295  for coords in elements:
296  if v_index in interesting_volumes:
297  x_values, y_values = extract_coords(coords, is_disc)
298  plt.plot(x_values, y_values, c=color[v_index])
299  v_index = v_index + 1
300  if not is_disc and v_index == len(index_to_extends_layers_bounds_cylinders):
301  v_index = 0
302  is_disc = True
303 
304  plt.xlabel("z [mm]")
305  plt.ylabel("R [mm]")
306  plt.title("Volumes and Layers (no approach layers)")
307  plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
308  plt.savefig(output_folder + "/volumes_and_layers.png")
309 
310  # Plot each volume: layers + approach layers
311  v_index = 0
312  is_disc = False
313  approach_colors = ["black", "blue", "red", "green", "orange", "purple", "pink"]
314  for elements in chain(
315  index_to_extends_layers_cylinders, index_to_extends_layers_discs
316  ):
317  l_index = 0
318  if not elements:
319  v_index = v_index + 1
320  continue
321  plt.figure(figsize=(20, 10))
322  color_layers = cm.rainbow(np.linspace(0, 1, len(elements)))
323  has_elements = False
324  for coords in elements:
325  if coords[3] > 0:
326  continue
327  has_elements = True
328  x_values, y_values = extract_coords(coords, is_disc)
329  plt.plot(
330  x_values,
331  y_values,
332  c=color_layers[l_index],
333  label="l: " + str(coords[4]),
334  )
335  l_index = l_index + 1
336 
337  a_is_disc = False
338  count = 0
339  for a_coords in chain(
340  index_to_extends_layers_cylinders[v_index],
341  index_to_extends_layers_discs[v_index],
342  ):
343  if a_coords[4] == coords[4] and a_coords[3] > 0:
344  ax_values, ay_values = extract_coords(a_coords, a_is_disc)
345  plt.plot(
346  ax_values,
347  ay_values,
348  linestyle=(0, (5, 10)),
349  c=approach_colors[a_coords[3]],
350  label="l: " + str(coords[4]) + ", a: " + str(a_coords[3]),
351  )
352  count = count + 1
353  if count == len(index_to_extends_layers_cylinders[v_index]):
354  a_is_disc = True
355 
356  v_index = v_index + 1
357  if not is_disc and v_index == len(index_to_extends_layers_cylinders):
358  v_index = 0
359  is_disc = True
360 
361  if not has_elements:
362  continue
363 
364  plt.xlabel("z [mm]")
365  plt.ylabel("R [mm]")
366  plt.title(index_to_names[v_index - 1])
367  plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
368  plt.savefig(output_folder + "/layers_for_volume_" + str(v_index) + ".png")
369 
370  plt.figure(figsize=(20, 10))
371 
372  # Plot boundaries
373  v_index = 0
374  is_disc = False
375  for elements in chain(
376  index_to_extends_layers_bounds_cylinders,
377  index_to_extends_layers_bounds_discs,
378  ):
379  for coords in elements:
380  x_values, y_values = extract_coords(coords, is_disc)
381  if v_index in interesting_volumes:
382  plt.plot(
383  x_values,
384  y_values,
385  linestyle="--",
386  c=color[v_index],
387  label="v: " + str(v_index + 1) + ", b: " + str(coords[3]),
388  )
389  v_index = v_index + 1
390  if not is_disc and v_index == len(index_to_extends_layers_bounds_cylinders):
391  v_index = 0
392  is_disc = True
393 
394  plt.xlabel("z [mm]")
395  plt.ylabel("R [mm]")
396  plt.title("Boundary surfaces")
397  plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
398  plt.savefig(output_folder + "/boundaries.png")
399 
400  plt.figure(figsize=(20, 10))
401 
402  # Plot all approach layers
403  v_index = 0
404  is_disc = False
405  add_to_legend = []
406  for elements in chain(
407  index_to_extends_layers_cylinders, index_to_extends_layers_discs
408  ):
409  if not elements:
410  v_index = v_index + 1
411  continue
412  for coords in elements:
413  if coords[3] == -1:
414  continue
415  x_values, y_values = extract_coords(coords, is_disc)
416  if coords[3] not in add_to_legend:
417  plt.plot(
418  x_values,
419  y_values,
420  c=approach_colors[coords[3]],
421  linestyle="--",
422  label="approach index = " + str(coords[3]),
423  )
424  add_to_legend.append(coords[3])
425  else:
426  plt.plot(
427  x_values, y_values, c=approach_colors[coords[3]], linestyle="--"
428  )
429  v_index = v_index + 1
430  if not is_disc and v_index == len(index_to_extends_layers_bounds_cylinders):
431  v_index = 0
432  is_disc = True
433 
434  plt.xlabel("z [mm]")
435  plt.ylabel("R [mm]")
436  plt.title("Approach layers")
437  plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
438  plt.savefig(output_folder + "/approach_layers.png")
439 
440 
441 def read_and_modify(filename, plot, output_folder, steering_file, output_file):
442  f = open(filename)
443  layers = open(steering_file)
444  data = json.load(f)
445  full_data = json.load(layers)
446  layer_data = full_data["SteeringField"]
447 
448  index_to_names = []
449 
450  # Allows to dump the binning defined for the material layers
451  dump_binning_for_material = False
452 
453  # Allows to check which layers are configured to carry material
454  check_material_layers = True
455 
456  # First you want to read the Volume entries and save the relevant quantities
457  # - names
458  for entry in data["Volumes"]["entries"]:
459  index_to_names.append(entry["value"]["NAME"])
460 
461  # In case volume names are not found check in the volume names with dummy values
462  if not index_to_names:
463  for entry in data["Surfaces"]["entries"]:
464  if "volume" in entry:
465  vol = entry["volume"]
466  if "volume" + str(vol) not in index_to_names:
467  index_to_names.append("volume" + str(vol))
468 
469  for entry in data["Surfaces"]["entries"]:
470  if "layer" in entry:
471  vol = entry["volume"]
472 
473  if index_to_names[vol - 1] in layer_data:
474  if "approach" in entry:
475  if (
476  entry["layer"]
477  in layer_data[index_to_names[vol - 1]]["layers_with_approach"]
478  and entry["approach"]
479  in layer_data[index_to_names[vol - 1]]["approaches"]
480  ):
481  entry["value"]["material"]["mapMaterial"] = True
482  else:
483  if (
484  entry["layer"]
485  in layer_data[index_to_names[vol - 1]]["layers_no_approach"]
486  ):
487  entry["value"]["material"]["mapMaterial"] = True
488 
489  if entry["value"]["material"]["mapMaterial"]:
490  for val in entry["value"]["material"]["binUtility"]["binningdata"]:
491  if val["value"] == "binZ" or val["value"] == "binR":
492  val["bins"] = layer_data[index_to_names[vol - 1]][
493  "activeBinningRorZ"
494  ]
495  else:
496  val["bins"] = layer_data[index_to_names[vol - 1]][
497  "activeBinningPhi"
498  ]
499  if val["bins"] == 0:
500  print(
501  "ERROR!!! Using binning value == 0! Check you input for",
502  index_to_names[vol - 1],
503  )
504  return
505 
506  approach_index = "None"
507  if "approach" in entry:
508  approach_index = entry["approach"]
509 
510  if dump_binning_for_material and entry["value"]["material"]["mapMaterial"]:
511  print(
512  "Volume: ",
513  entry["volume"],
514  index_to_names[vol - 1],
515  " - Layer: ",
516  entry["layer"],
517  " - Approach:",
518  approach_index,
519  )
520  for val in entry["value"]["material"]["binUtility"]["binningdata"]:
521  print("-->", val["value"], ": ", val["bins"])
522 
523  if "boundary" in entry:
524  extends = []
525  vol = entry["volume"]
526 
527  if (
528  index_to_names[vol - 1] in layer_data
529  and entry["boundary"]
530  in layer_data[index_to_names[vol - 1]]["boundaries"]
531  ):
532  entry["value"]["material"]["mapMaterial"] = True
533  for val in entry["value"]["material"]["binUtility"]["binningdata"]:
534  if entry["value"]["type"] == "CylinderSurface":
535  if val["value"] == "binZ":
536  val["bins"] = layer_data[index_to_names[vol - 1]][
537  "passiveCylinderBinningZ"
538  ]
539  else:
540  val["bins"] = layer_data[index_to_names[vol - 1]][
541  "passiveCylinderBinningPhi"
542  ]
543  elif entry["value"]["type"] == "DiscSurface":
544  if val["value"] == "binR":
545  val["bins"] = layer_data[index_to_names[vol - 1]][
546  "passiveDiscBinningR"
547  ]
548  else:
549  val["bins"] = layer_data[index_to_names[vol - 1]][
550  "passiveDiscBinningPhi"
551  ]
552  else:
553  print(
554  "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
555  )
556  if val["bins"] == 0:
557  print(
558  "ERROR!!! Using binning value == 0! Check you input for",
559  index_to_names[vol - 1],
560  )
561  return
562 
563  if dump_binning_for_material and entry["value"]["material"]["mapMaterial"]:
564  print(
565  "Volume: ",
566  entry["volume"],
567  index_to_names[vol - 1],
568  " - Boundary:",
569  entry["boundary"],
570  )
571  for val in entry["value"]["material"]["binUtility"]["binningdata"]:
572  print("-->", val["value"], ": ", val["bins"])
573 
574  # Once you have all the data in hands, you make a few nice plots to show volumes and surfaces
575 
576  with open(output_file, "w", encoding="utf-8") as f:
577  json.dump(data, f, ensure_ascii=False, indent=4)
578 
579  if plot and check_material_layers:
580  import matplotlib.pyplot as plt
581  from matplotlib.pyplot import cm
582  from itertools import cycle
583  import numpy as np
584 
585  plt.figure(figsize=(20, 10))
586 
587  material_layer_cylinders = [[] for _ in range(len(index_to_names))]
588  material_layer_discs = [[] for _ in range(len(index_to_names))]
589 
590  material_approach_cylinders = [[] for _ in range(len(index_to_names))]
591  material_approach_discs = [[] for _ in range(len(index_to_names))]
592 
593  material_boundary_cylinders = [[] for _ in range(len(index_to_names))]
594  material_boundary_discs = [[] for _ in range(len(index_to_names))]
595 
596  for entry in data["Surfaces"]["entries"]:
597 
598  if not entry["value"]["material"]["mapMaterial"]:
599  continue
600 
601  z_shift = 0.0
602  if entry["value"]["transform"]["translation"] != None:
603  z_shift = entry["value"]["transform"]["translation"][2]
604 
605  if "layer" in entry:
606  extends = []
607  vol = entry["volume"]
608 
609  if entry["value"]["type"] == "CylinderSurface":
610  extends = [
611  entry["value"]["bounds"]["values"][0],
612  z_shift - entry["value"]["bounds"]["values"][1],
613  z_shift + entry["value"]["bounds"]["values"][1],
614  ]
615  if "approach" in entry:
616  material_approach_cylinders[vol - 1].append(extends)
617  else:
618  material_layer_cylinders[vol - 1].append(extends)
619 
620  elif entry["value"]["type"] == "DiscSurface":
621  extends = [
622  entry["value"]["bounds"]["values"][0],
623  entry["value"]["bounds"]["values"][1],
624  z_shift,
625  ]
626  if "approach" in entry:
627  material_approach_discs[vol - 1].append(extends)
628  else:
629  material_layer_discs[vol - 1].append(extends)
630  else:
631  print(
632  "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
633  )
634 
635  if "boundary" in entry:
636  extends = []
637  vol = entry["volume"]
638 
639  if entry["value"]["type"] == "CylinderSurface":
640  extends = [
641  entry["value"]["bounds"]["values"][0],
642  z_shift - entry["value"]["bounds"]["values"][1],
643  z_shift + entry["value"]["bounds"]["values"][1],
644  ]
645  material_boundary_cylinders[vol - 1].append(extends)
646 
647  elif entry["value"]["type"] == "DiscSurface":
648  extends = [
649  entry["value"]["bounds"]["values"][0],
650  entry["value"]["bounds"]["values"][1],
651  z_shift,
652  ]
653  material_boundary_discs[vol - 1].append(extends)
654  else:
655  print(
656  "WARNING: Processing surface with unknown type. Only CylinderSurface and DiscSurface are considered."
657  )
658 
659  from itertools import chain
660 
661  v_index = 0
662  is_first = True
663  is_disc = False
664  for elements in chain(material_layer_cylinders, material_layer_discs):
665  l_index = 0
666  for coords in elements:
667  x_values, y_values = extract_coords(coords, is_disc)
668  if is_first:
669  plt.plot(x_values, y_values, c="black", label="layer")
670  is_first = False
671  else:
672  plt.plot(x_values, y_values, c="black")
673  l_index = l_index + 1
674  v_index = v_index + 1
675  if not is_disc and v_index == len(material_layer_cylinders):
676  is_disc = True
677  v_index = 0
678 
679  v_index = 0
680  is_first = True
681  is_disc = False
682  for elements in chain(material_approach_cylinders, material_approach_discs):
683  l_index = 0
684  for coords in elements:
685  x_values, y_values = extract_coords(coords, is_disc)
686  if is_first:
687  plt.plot(x_values, y_values, c="red", label="approach")
688  is_first = False
689  else:
690  plt.plot(x_values, y_values, c="red")
691  l_index = l_index + 1
692  v_index = v_index + 1
693  if not is_disc and v_index == len(material_approach_cylinders):
694  is_disc = True
695  v_index = 0
696 
697  v_index = 0
698  is_first = True
699  is_disc = False
700  for elements in chain(material_boundary_cylinders, material_boundary_discs):
701  l_index = 0
702  for coords in elements:
703  x_values, y_values = extract_coords(coords, is_disc)
704  if is_first:
705  plt.plot(x_values, y_values, c="blue", label="boundary")
706  is_first = False
707  else:
708  plt.plot(x_values, y_values, c="blue")
709  l_index = l_index + 1
710  v_index = v_index + 1
711  if not is_disc and v_index == len(material_boundary_cylinders):
712  is_disc = True
713  v_index = 0
714 
715  plt.xlabel("z [mm]")
716  plt.ylabel("R [mm]")
717  plt.title("Layers with material")
718  plt.legend(loc="center left", bbox_to_anchor=(1, 0.5))
719  plt.savefig(output_folder + "/material_layers.png")
720 
721 
722 import argparse
723 import os
724 
725 # Initialize parser
726 parser = argparse.ArgumentParser()
727 parser.add_argument(
728  "--geometry",
729  default="",
730  type=str,
731  help="Specify the input file for geometry visualisation",
732 )
733 parser.add_argument(
734  "--plot",
735  default=True,
736  action="store_true",
737  help="Enable plot creation for geometry visualisation (Default : True)",
738 )
739 parser.add_argument(
740  "--output_folder",
741  default="plots",
742  type=str,
743  help="Specify the output folder for plots (Default : plots)",
744 )
745 parser.add_argument(
746  "--dump_steering",
747  default=False,
748  action="store_true",
749  help="Enable production of steering file for material mapping (Default : False)",
750 )
751 parser.add_argument(
752  "--edit",
753  default=False,
754  action="store_true",
755  help="Enable editing of input file for creation of json for material mapping (Default : False)",
756 )
757 parser.add_argument(
758  "--steering_file",
759  default="",
760  type=str,
761  help="Specify the steering file to guide editing of json for material mapping",
762 )
763 parser.add_argument(
764  "--output_map",
765  default="",
766  type=str,
767  help="Specify the output json for material mapping",
768 )
769 args = parser.parse_args()
770 
771 print(" --- Geometry visualisation and material map fie creation --- ")
772 print()
773 
774 if not args.geometry:
775  print("Error: Missing input geometry file. Please specify --geometry.")
776  exit()
777 
778 if not os.path.exists(args.geometry):
779  print("Error: Invalid file path/name in --geometry. Please check your input!")
780  exit()
781 
782 if not args.geometry.endswith(".json"):
783  print("Error: Invalid file format in --geometry. Please check your input!")
784  exit()
785 
786 print("\t parsing file : ", args.geometry)
787 if args.plot:
788  print("\t job configured to produce plots in output folder: ", args.output_folder)
789  if not os.path.exists(args.output_folder):
790  os.mkdir(args.output_folder)
791 
792 if args.dump_steering and args.edit:
793  print(
794  "Error: Wrong job configuration. --dump_steering and --edit can't be \
795  both true at the same time."
796  )
797  print(
798  "\t Decide if you want to dump the steering file OR to read an existing file for editing the geometry file."
799  )
800  exit()
801 
802 if args.dump_steering:
803  if not args.steering_file:
804  print("Error: Missing output steering file. Please specify --steering_file.")
805  exit()
806  if not args.steering_file.endswith(".json"):
807  print("Error: Invalid file format in --steering_file. It must end with .json!")
808  exit()
809  print(
810  "\t job configured to produce steering file for material mapping with name: ",
811  args.steering_file,
812  )
813 
814 if args.edit:
815  print("\t job configured to edit the input geometry file following a steering file")
816  if not args.steering_file:
817  print("Error: Missing input steering file. Please specify --steering_file.")
818  exit()
819  if not os.path.exists(args.steering_file):
820  print(
821  "Error: Invalid file path/name in --steering_file. Please check your input!"
822  )
823  exit()
824  if not args.steering_file.endswith(".json"):
825  print("Error: Invalid file format in --steering_file. Please check your input!")
826  exit()
827  if not args.output_map:
828  print("Error: Missing output map file. Please specify --output_map.")
829  exit()
830  if not args.output_map.endswith(".json"):
831  print("Error: Invalid file format in --output_map. Please check your input!")
832  exit()
833 
834 
835 if args.plot or args.dump_steering:
836  dump_geo(
837  args.geometry,
838  args.plot,
839  args.output_folder,
840  args.dump_steering,
841  args.steering_file,
842  )
843 
844 if args.edit:
846  args.geometry,
847  args.plot,
848  args.output_folder,
849  args.steering_file,
850  args.output_map,
851  )