EinarElen / prototype-geometry

1 stars 0 forks source link

Templated geometries #13

Closed EinarElen closed 2 years ago

EinarElen commented 2 years ago

We want to be able to customize the position of the horizontal bars of the geometry without having to have multiple GDML files as well as being able to switch the trigger scintillator geometry between the various versions. To do this, we will have to adopt some form of templating. If we use the built in rudimentary templating support that comes with the Python string library, then a workflow that looks something like the following would work

Most of the files remain as simple whatever.gdml files.

Files where we want to be able to change some parameter are changed to be called whatever.gdml.in. The actual file that is used in LDMX-sw (whatever.gdml) is generated at runtime by filling in the templated values. Template variables are defined by prepending $.

EinarElen commented 2 years ago

Quick demo of what template usage might look like

from string import Template
# Assuming the shift variable is defined as $shift

default_template = {
    'shift' : 0.,
}

template = {
    'shift' : 33,
    'ignoreme': 44,
}
input_gdml='/home/einarelen/.local/ldmx/src/ldmx-sw/Detectors/data/ldmx-hcal-prototype-v1.0/constants.gdml.in'
output_gdml='/home/einarelen/.local/ldmx/src/ldmx-sw/Detectors/data/ldmx-hcal-prototype-v1.0/constants.gdml'
result=''
with open(input_gdml, 'r') as f:
    src = Template(f.read())
    result = src.substitute(template)
    print(result)
    print(type(result))

if result == '':
    print("Warning: Substitution of {} into {} failed, output file will be empty!".format(template,input_gdml))

with open(output_gdml, 'w') as f:
    comment='    <!-- This file was generated from the template {} -->\n'.format(template)
    f.write(comment)
    f.write(result)
EinarElen commented 2 years ago

Note: LDMX-sw would have to add the gdml.in files to the (most likely cmake) script that copies detector files

EinarElen commented 2 years ago

No longer required after the testbeam, closing the issue