HPS-MC
_pede.py
Go to the documentation of this file.
1 """! running pede from in hps-mc jobs"""
2 
3 import shutil
4 import re
5 import os
6 
7 from hpsmc.component import Component
8 from ._parameter import Parameter
9 from ._pattern import Pattern
10 from ._util import getBeamspotConstraintsFloatingOnly
11 
12 
13 class PEDE(Component):
14  """! Run pede minimizer over input bin files for alignment
15  """
16 
17  def __init__(self, **kwargs):
18  self._pede_steering_file_pede_steering_file = None
19  self.to_floatto_float = []
20  self.param_mapparam_map = None
21  self.pede_minimizationpede_minimization = None
22 
23  self.subitosubito = False
24  self.constraint_fileconstraint_file = None
25  self.previous_fitprevious_fit = None
26  self.beamspot_constraintsbeamspot_constraints = False
27  self.survey_constraintssurvey_constraints = False
28 
29  self.output_extoutput_extoutput_ext = 'res'
30 
31  super().__init__('pede', command='pede', **kwargs)
32 
34  self.loggerlogger.info(f'Parameter Map: {self.param_map}')
35  parameters = Parameter.parse_map_file(self.param_mapparam_map)
36 
37  if self.previous_fitprevious_fit is not None:
38  self.loggerlogger.info(f'Loading previous fit: {self.previous_fit}')
39  Parameter.parse_pede_res(self.previous_fitprevious_fit,
40  destination=parameters,
41  skip_nonfloat=False)
42 
43  # define which parameters are floating
44  # if a parameter matches any of the input patterns
45  # it will be floated
46  patterns = list(map(Pattern, self.to_floatto_float))
47  for parameter in parameters.values():
48  if any(pattern.match(parameter) for pattern in patterns):
49  self.loggerlogger.debug(f'Floating parameter {parameter}')
50  parameter.float()
51 
52  # build steering file for pede
53  pede_steering_file = 'pede-steer.txt'
54  with open(pede_steering_file, 'w') as psf:
55  # write out input mille binary files
56  psf.write('CFiles\n')
57  for ipf in self.inputsinputs:
58  psf.write(ipf+'\n')
59 
60  # external constraint file
61  if self.constraint_fileconstraint_file is not None:
62  self.loggerlogger.info(f'Adding constraint file {self.constraint_file}')
63  psf.write('\n')
64  psf.write('!Constraint file\n')
65  psf.write(constraint_file+'\n')
66 
67  # list parameters
68  psf.write('\nParameter\n')
69  for i, p in parameters.items():
70  psf.write(p.pede_format() + '\n')
71 
72  # survey constraints
73  if self.survey_constraintssurvey_constraints:
74  self.loggerlogger.info('Applying survey constraints')
75  self.loggerlogger.warn('UNTESTED - not sure if this is correct (Tom E)')
76  survey_meas_tu = 0.05
77  psf.write('\n!Survey constraints tu\n')
78  for p, name in param_map.items():
79  if p.module_number() == 0 and p.direction == 1 and p.trans_rot == 1:
80  psf.write('\nMeasurement 0.0 %.3f\n' % survey_meas_tu)
81  psf.write('%s 1.0\n' & p)
82  psf.write("\n\n")
83 
84  # apply beamspotConstraint (This I think is not correct)
85  if self.beamspot_constraintsbeamspot_constraints:
86  self.loggerlogger.error('Beamspot constraints not implemented!')
87  return -1
88  psf.write(getBeamspotConstraintsFloatingOnly(param_map))
89  psf.write("\n\n")
90 
91  psf.write("\n\n")
92  self.loggerlogger.info(f'Appending minimization settings from {self.pede_minimization}')
93  # determine MP minimization settings
94  with open(self.pede_minimizationpede_minimization) as minfile:
95  for line in minfile:
96  psf.write(line)
97 
98  return pede_steering_file
99 
100  def _print_pede_res(self):
101  # print parameters that were floated so user can see results
102  parameters = Parameter.parse_pede_res('millepede.res', skip_nonfloat=True)
103  self.loggerlogger.info('Deduced Parameters')
104  for i, p in parameters.items():
105  if p.active:
106  self.loggerlogger.info(f' {p}')
107  return
108 
110  return ['to_float']
111 
112  def required_config(self):
113  return ['param_map', 'pede_minimization']
114 
116  return ['subito', 'constraint_file', 'previous_fit', 'beamspot_constraints', 'survey_constraints']
117 
118  def cmd_args(self):
119  a = [self._pede_steering_file_pede_steering_file]
120  if self.subitosubito:
121  a += ['-s']
122  return a
123 
124  def setup(self):
125  """pre-run initialization"""
126  self._pede_steering_file_pede_steering_file = self._write_pede_steering_file_write_pede_steering_file()
127 
128  def cleanup(self):
129  """post-run de-initialization"""
130  # copy pede output files to output directory
131  self._print_pede_res_print_pede_res()
Run pede minimizer over input bin files for alignment.
Definition: _pede.py:13
def required_config(self)
Return a list of required configuration settings.
Definition: _pede.py:112
def required_parameters(self)
Return a list of required parameters.
Definition: _pede.py:109
def optional_parameters(self)
Return a list of optional parameters.
Definition: _pede.py:115
def _print_pede_res(self)
Definition: _pede.py:100
def __init__(self, **kwargs)
Definition: _pede.py:17
def _write_pede_steering_file(self)
Definition: _pede.py:33
def cmd_args(self)
Return the command arguments of this component.
Definition: _pede.py:118
Base class for components in a job.
Definition: component.py:15
def getBeamspotConstraintsFloatingOnly(pars)
put beamspot constraints onto floating parameters
Definition: _util.py:32