Skip to content

Commit

Permalink
switch alignment components to use component logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tomeichlersmith committed Mar 28, 2023
1 parent 96e2b0c commit 441e5ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
21 changes: 9 additions & 12 deletions python/hpsmc/alignment/_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import shutil
import re
import os
import logging
import json

logger = logging.getLogger('alignment.apply')

from hpsmc.component import Component
from ._parameter import Parameter
from ._pattern import Pattern
Expand Down Expand Up @@ -64,14 +61,14 @@ def _deduce_next_detector(self, bump = False) :
a valid parameter for some components inheriting from this function.
"""
if bump or self.next_detector is not None :
logger.info('Creating new detector directory.')
self.logger.info('Creating new detector directory.')
# deduce source directory and check that it exists
src_path = self._detector_dir(self.detector)
if not os.path.isdir(src_path) :
raise ValueError(f'Detector {self.detector} is not in hps-java')

if self.next_detector is None :
logger.info('Deducing next detector name from current name')
self.logger.info('Deducing next detector name from current name')
# deduce iter value, using iter0 if there is no iter suffix
matches = re.search('.*iter([0-9]*)', self.detector)
if matches is None :
Expand All @@ -80,9 +77,9 @@ def _deduce_next_detector(self, bump = False) :
i = int(matches.group(1))
self.next_detector = self.detector.replace(f'_iter{i}',f'_iter{i+1}')

logger.info(f'Creating new detector named "{self.next_detector}"')
self.logger.info(f'Creating new detector named "{self.next_detector}"')
else :
logger.info(f'Operating on assumed-existing detector "{self.detector}"')
self.logger.info(f'Operating on assumed-existing detector "{self.detector}"')
self.next_detector = self.detector

def _to_compact(self, parameter_set, detname, save_prev = True, prev_ext = 'prev'):
Expand Down Expand Up @@ -146,7 +143,7 @@ def _change_xml_value(line, key, new_val, append = True) :
dest = os.path.join(self._detector_dir(detname),'compact.xml')
if not os.path.isfile(dest) :
raise ValueError(f'{detname} does not have a compact.xml to modify.')
logger.info(f'Writing compact.xml at {dest}')
self.logger.info(f'Writing compact.xml at {dest}')
original_cp = dest + '.' + prev_ext
shutil.copy2(dest, original_cp)
f = open(dest,'w')
Expand All @@ -155,7 +152,7 @@ def _change_xml_value(line, key, new_val, append = True) :
for line in og :
if 'info name' in line :
# update detector name
logger.debug(f'Changing detector name to {detname}')
self.logger.debug(f'Changing detector name to {detname}')
f.write(_change_xml_value(line, 'name', detname, append = False))
line_edited = True
continue
Expand All @@ -168,7 +165,7 @@ def _change_xml_value(line, key, new_val, append = True) :
for i in parameter_set :
if str(i) in line :
# the parameter with ID i is being set on this line
logger.debug(f'Changing parameter {i}')
self.logger.debug(f'Changing parameter {i}')
f.write(_change_xml_value(
line, 'value', parameter_set[i].compact_value(), append = True
))
Expand All @@ -191,7 +188,7 @@ def _update_readme(self, detname, msg) :

# update/create a README to log how this detector has evolved
log_path = os.path.join(self._detector_dir(detname), 'README.md')
logger.info(f'Updating README.md at {log_path}')
self.logger.info(f'Updating README.md at {log_path}')
with open(log_path, 'a') as log :
from datetime import datetime
log.write(f'# {detname}\n')
Expand Down Expand Up @@ -273,7 +270,7 @@ def execute(self, log_out, log_err) :
# get list of parameters and their MP values
parameters = Parameter.parse_pede_res(self.res_file, skip_nonfloat=True)

logger.debug(f'Applying pede results: {parameters}')
self.logger.debug(f'Applying pede results: {parameters}')

self._to_compact(parameters, self.next_detector)
self._update_readme(self.next_detector, f"""
Expand Down
23 changes: 10 additions & 13 deletions python/hpsmc/alignment/_pede.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import shutil
import re
import os
import logging

from hpsmc.component import Component
from ._parameter import Parameter
Expand All @@ -14,8 +13,6 @@ class PEDE(Component):
"""! Run pede minimizer over input bin files for alignment
"""

logger = logging.getLogger('hpsmc.tools.PEDE')

def __init__(self, **kwargs) :
self._pede_steering_file = None
self.to_float = []
Expand All @@ -33,11 +30,11 @@ def __init__(self, **kwargs) :
super().__init__('pede', command='pede', **kwargs)

def _write_pede_steering_file(self) :
PEDE.logger.info(f'Parameter Map: {self.param_map}')
self.logger.info(f'Parameter Map: {self.param_map}')
parameters = Parameter.parse_map_file(self.param_map)

if self.previous_fit is not None :
PEDE.logger.info(f'Loading previous fit: {self.previous_fit}')
self.logger.info(f'Loading previous fit: {self.previous_fit}')
Parameter.parse_pede_res(self.previous_fit,
destination = parameters,
skip_nonfloat = False)
Expand All @@ -48,7 +45,7 @@ def _write_pede_steering_file(self) :
patterns = list(map(Pattern, self.to_float))
for parameter in parameters.values() :
if any(pattern.match(parameter) for pattern in patterns) :
PEDE.logger.debug(f'Floating parameter {parameter}')
self.logger.debug(f'Floating parameter {parameter}')
parameter.float()

# build steering file for pede
Expand All @@ -61,7 +58,7 @@ def _write_pede_steering_file(self) :

# external constraint file
if self.constraint_file is not None :
PEDE.logger.info(f'Adding constraint file {self.constraint_file}')
self.logger.info(f'Adding constraint file {self.constraint_file}')
psf.write('\n')
psf.write('!Constraint file\n')
psf.write(constraint_file+'\n')
Expand All @@ -73,8 +70,8 @@ def _write_pede_steering_file(self) :

# survey constraints
if self.survey_constraints :
PEDE.logger.info('Applying survey constraints')
PEDE.logger.warn('UNTESTED - not sure if this is correct (Tom E)')
self.logger.info('Applying survey constraints')
self.logger.warn('UNTESTED - not sure if this is correct (Tom E)')
survey_meas_tu = 0.05
psf.write('\n!Survey constraints tu\n')
for p, name in param_map.items() :
Expand All @@ -85,13 +82,13 @@ def _write_pede_steering_file(self) :

# apply beamspotConstraint (This I think is not correct)
if self.beamspot_constraints:
PEDE.logger.error('Beamspot constraints not implemented!')
self.logger.error('Beamspot constraints not implemented!')
return -1
psf.write(getBeamspotConstraintsFloatingOnly(param_map))
psf.write("\n\n")

psf.write("\n\n")
PEDE.logger.info(f'Appending minimization settings from {self.pede_minimization}')
self.logger.info(f'Appending minimization settings from {self.pede_minimization}')
# determine MP minimization settings
with open(self.pede_minimization) as minfile :
for line in minfile :
Expand All @@ -103,10 +100,10 @@ def _write_pede_steering_file(self) :
def _print_pede_res(self) :
# print parameters that were floated so user can see results
parameters = Parameter.parse_pede_res('millepede.res', skip_nonfloat=True)
PEDE.logger.info('Deduced Parameters')
self.logger.info('Deduced Parameters')
for i, p in parameters.items() :
if p.active :
PEDE.logger.info(f' {p}')
self.logger.info(f' {p}')
return

def required_parameters(self) :
Expand Down

0 comments on commit 441e5ec

Please sign in to comment.