EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
writeMapConfig.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file writeMapConfig.py
1 # This file is part of the Acts project.
2 #
3 # Copyright (C) 2020 CERN for the benefit of the Acts project
4 #
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 import json
10 import sys
11 
12 # Should be run with Python 3 if possible
13 # Script that parse a Json surfaces map to create an easy to use json config file for the mapping
14 # Take two arguments in input : The path to the surfaces map and the path of the json config file
15 # By default the input is : 'surfaces-map.json' and the output is : 'config-map.json'
16 # The config file can be used to define a binning for all the surfaces in a given volume
17 # It can also be used to define the binning for volume mapping
18 
19 def getSurfaceMateral ( mat ):
20  outputmat = {}
21  outputmat['bin0'] = mat['bin0']
22  outputmat['bin1'] = mat['bin1']
23  outputmat['mapMaterial'] = mat['mapMaterial']
24  return outputmat
25 
26 
27 def getVolumeMateral ( mat ):
28  outputmat = {}
29  for bin in mat :
30  if bin == 'bin0' :
31  outputmat['bin0'] = mat['bin0']
32  if bin == 'bin1' :
33  outputmat['bin1'] = mat['bin1']
34  if bin == 'bin2' :
35  outputmat['bin2'] = mat['bin2']
36  outputmat['mapMaterial'] = mat['mapMaterial']
37  return outputmat
38 
39 
40 if sys.version_info[0] < 3:
41  print('Using Python 2')
42  print('To obtain the proper ordering in the Json files Python 3 is recomanded')
43 
44 if len(sys.argv) < 2 :
45  inFileName = 'surfaces-map.json'
46 else :
47  inFileName = sys.argv[1]
48 
49 
50 with open(inFileName,'r') as json_file:
51  config = {}
52  data = json.load(json_file)
53 
54  for kvol in data['volumes']:
55  vconfig = {}
56  bconfig = {}
57  rconfig = {}
58  aconfig = {}
59  abin = {}
60  sconfig = {}
61 
62  if 'boundaries' in data['volumes'][kvol] :
63  for kbound in data['volumes'][kvol]['boundaries'] :
64  dbound = data['volumes'][kvol]['boundaries'][kbound]
65  if not dbound['stype'] in bconfig :
66  bconfig[dbound['stype']] = getSurfaceMateral(dbound)
67  vconfig['boundaries']=bconfig
68 
69 
70  if 'layers' in data['volumes'][kvol] :
71  for klay in data['volumes'][kvol]['layers'] :
72 
73  if 'representing' in data['volumes'][kvol]['layers'][klay] :
74  drep = data['volumes'][kvol]['layers'][klay]['representing']
75  if not drep['stype'] in rconfig :
76  rconfig[drep['stype']] = getSurfaceMateral(drep)
77  vconfig['representing'] = rconfig
78 
79  if 'approach' in data['volumes'][kvol]['layers'][klay] :
80  for kapp in data['volumes'][kvol]['layers'][klay]['approach'] :
81  dapp = data['volumes'][kvol]['layers'][klay]['approach'][kapp]
82  abin[kapp] = getSurfaceMateral(dapp)
83  aconfig[dapp['stype']] = abin
84  vconfig['approach'] = aconfig
85 
86  if 'sensitive' in data['volumes'][kvol]['layers'][klay] :
87  for ksen in data['volumes'][kvol]['layers'][klay]['sensitive'] :
88  dsen = data['volumes'][kvol]['layers'][klay]['sensitive'][ksen]
89  if not dsen['stype'] in sconfig :
90  sconfig[dsen['stype']] = getSurfaceMateral(dsen)
91  vconfig['sensitive'] = sconfig
92 
93 
94  if 'material' in data['volumes'][kvol] :
95  vconfig['material'] = getVolumeMateral(data['volumes'][kvol]['material'])
96 
97  config[data['volumes'][kvol]['Name']] = vconfig
98 
99 
100 if len(sys.argv) < 3 :
101  outFileName = 'config-map.json'
102 else :
103  outFileName = sys.argv[2]
104 
105 with open(outFileName, 'w') as outfile:
106  json.dump(config, outfile, indent=4)