Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:34

0001 # This file is part of the ACTS project.
0002 #
0003 # Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 #
0005 # This Source Code Form is subject to the terms of the Mozilla Public
0006 # License, v. 2.0. If a copy of the MPL was not distributed with this
0007 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 
0010 # each volume configuration is one logical block
0011 #
0012 #   --digi-smear-volume=8
0013 #   --digi-smear-indices=0:1:5 # loc0, loc1, and time
0014 #   --digi-smear-types=0:0:3   # loc{0,1} uses gaussian, time uses uniform
0015 #   # parameter 0: loc0 gaussian width
0016 #   # parameter 1: loc1 gaussian width
0017 #   # parameter 2-4: time pitch,min,max
0018 #   --digi-smear-parameters=10:20:2.5:-25:25
0019 #
0020 # which can be repeated as often as needed
0021 #
0022 #   --digi-smear-volume=11
0023 #   --digi-smear-indices=1       # loc1
0024 #   --digi-smear-types=0         # loc1 uses gaussian
0025 #   --digi-smear-parameters=12.5 # loc1 gaussian width
0026 #
0027 
0028 
0029 import argparse
0030 import json
0031 import sys
0032 
0033 
0034 def add_switch(i, argv, current):
0035     fields = argv[i].split("=")
0036 
0037     if len(fields) == 1:
0038         # --foo bar
0039         current.append(argv[i])
0040         current.append(argv[i + 1])
0041         i += 2
0042 
0043     elif len(fields) == 2:
0044         # --foo=bar
0045         current.append(argv[i])
0046         i += 1
0047 
0048     else:
0049         raise RuntimeError(f"Invalid argument: {argv[i]}")
0050 
0051     return i
0052 
0053 
0054 def get_args_blocks():
0055     argv = sys.argv[1:]
0056     blocks = []
0057     current = []
0058 
0059     i = 0
0060     while i < len(argv):
0061         if argv[i].startswith("--digi-smear-volume"):
0062             if current:
0063                 blocks.append(current)
0064                 current = []
0065         i = add_switch(i, argv, current)
0066     if current:
0067         blocks.append(current)
0068 
0069     return blocks
0070 
0071 
0072 def arg_parser():
0073     argp = argparse.ArgumentParser()
0074     argp.add_argument(
0075         "--digi-smear-volume", help="Sensitive volume identifiers", required=True
0076     )
0077     argp.add_argument(
0078         "--digi-smear-indices",
0079         help="Smear parameter indices for this volume",
0080         required=True,
0081     )
0082     argp.add_argument(
0083         "--digi-smear-types",
0084         help="Smear function types as 0 (gauss), 1 (truncated gauss), 2 (clipped gauss), 3 (uniform), 4 (digital)",
0085         required=True,
0086     )
0087     argp.add_argument(
0088         "--digi-smear-parameters",
0089         help="Smear parameters depending on the smearing type, 1 parameter for simple gauss, 3 for all others (1 parameter, 2 range values)",
0090         required=True,
0091     )
0092     return argp
0093 
0094 
0095 def get_args():
0096     return [arg_parser().parse_args(block) for block in get_args_blocks()]
0097 
0098 
0099 def get_n_params(type_id):
0100     if type_id == 0:
0101         return 1
0102     return 3
0103 
0104 
0105 def get_param_blocks(types_ids, params):
0106     blocks = []
0107     icur = 0
0108     for x in types_ids:
0109         n = get_n_params(x)
0110         blocks.append(params[icur : icur + n])
0111         icur += n
0112     return blocks
0113 
0114 
0115 def block_to_json(args):
0116     top_data = {"volume": int(args.digi_smear_volume), "value": {"smearing": []}}
0117 
0118     indices = [int(x) for x in args.digi_smear_indices.split(":")]
0119     types = [int(x) for x in args.digi_smear_types.split(":")]
0120     params = [float(x) for x in args.digi_smear_parameters.split(":")]
0121     param_blocks = get_param_blocks(types, params)
0122 
0123     for i, t, ps in zip(indices, types, param_blocks):
0124         data = {"index": i}
0125         if t == 0:
0126             data["mean"] = 0.0
0127             data["stddev"] = ps[0]
0128             data["type"] = "Gauss"
0129         elif t == 1:
0130             data["mean"] = 0.0
0131             data["stddev"] = ps[0]
0132             data["range"] = ps[1:]
0133             data["type"] = "GaussTrunc"
0134         elif t == 2:
0135             data["mean"] = 0.0
0136             data["stddev"] = ps[0]
0137             data["range"] = ps[1:]
0138             data["type"] = "GaussClipped"
0139         elif t in [3, 4]:
0140             data["type"] = "Uniform" if t == 3 else "Digital"
0141 
0142             pitch = ps[0]
0143             low = ps[1]
0144             high = ps[2]
0145 
0146             data["bindata"] = [
0147                 0,  # Acts::Open,
0148                 0,  # Acts::AxisDirection::AxisX,
0149                 (high - low) / pitch,
0150                 low,
0151                 high,
0152             ]
0153         else:
0154             raise RuntimeError(f"Unrecognized type: {t}")
0155 
0156         top_data["value"]["smearing"].append(data)
0157 
0158     return top_data
0159 
0160 
0161 def get_json_data():
0162     return {
0163         "acts-geometry-hierarchy-map": {
0164             "format-version": 0,
0165             "value-identifier": "digitization-configuration",
0166         },
0167         "entries": [block_to_json(x) for x in get_args()],
0168     }
0169 
0170 
0171 def main():
0172     print(json.dumps(get_json_data(), indent=4))
0173 
0174 
0175 if __name__ == "__main__":
0176     main()