File indexing completed on 2023-03-17 10:44:28
0001
0002
0003 """ This script can be used to run manually the dcs o2o over an interval of time,
0004 dividing it in smaller intervals. Running on an interval too big can cause
0005 a result of the query to the database so big that the machine runs out of memory.
0006 By splitting it in smaller intervals of a given DeltaT it is possible to keep
0007 under control the memory used.
0008 """
0009 from __future__ import print_function
0010
0011 import os
0012 import datetime
0013 import subprocess
0014 import argparse
0015
0016 def insert_to_file(template, target, replace_dict):
0017 '''Update the template file based on the replace_dict, and write to the target.'''
0018 with open(template, 'r') as input_file:
0019 config=input_file.read()
0020 with open(target, 'w') as output_file:
0021 for key, value in replace_dict.items():
0022 config = config.replace(key, value)
0023 output_file.write(config)
0024
0025
0026 def main():
0027 parser = argparse.ArgumentParser(description='Run SiStrip DCS O2O by splitting into small intervals.')
0028 parser.add_argument('-i', '--interval',
0029 dest = 'interval',
0030 type = int,
0031 default = 1,
0032 help = 'Interval (in hours) for splitting jobs. Default: %(default)d hours',
0033 )
0034 parser.add_argument('-b', '--begin',
0035 dest = 'begin',
0036 default = '2016-01-01 00:00:00',
0037 help = 'Beginning time of the interval. Format: [YYYY-MM-DD HH:MM:SS]. Default: %(default)s',
0038 )
0039 parser.add_argument('-e', '--end',
0040 dest = 'end',
0041 default = '2016-02-01 00:00:00',
0042 help = 'End time of the interval. Format: [YYYY-MM-DD HH:MM:SS]. Default: %(default)s',
0043 )
0044 parser.add_argument('-t', '--template',
0045 dest = 'template',
0046 default = 'dcs_o2o_template_cfg.py',
0047 help = 'Template config file. Default: %(default)s',
0048 )
0049 parser.add_argument('--db',
0050 dest = 'dbfile',
0051 default = 'SiStripDetVOff.db',
0052 help = 'Output tag. Default: %(default)s',
0053 )
0054 parser.add_argument('--tag',
0055 dest = 'tag',
0056 default = 'SiStripDetVOff',
0057 help = 'Output tag. Default: %(default)s',
0058 )
0059 args = parser.parse_args()
0060
0061
0062 if not os.path.exists(args.dbfile):
0063 with open(args.dbfile, 'w'):
0064 pass
0065
0066 dt_begin = datetime.datetime.strptime(args.begin, '%Y-%m-%d %H:%M:%S')
0067 dt_end = datetime.datetime.strptime(args.end, '%Y-%m-%d %H:%M:%S')
0068
0069 while (dt_end-dt_begin).total_seconds() > 0:
0070 dt_next = dt_begin + datetime.timedelta(hours=args.interval)
0071 tmin_str = dt_begin.strftime('%Y, %-m, %-d, %-H, 0, 0, 0')
0072 tmax_str = dt_next.strftime('%Y, %-m, %-d, %-H, 0, 0, 0')
0073 targetFile = 'dcs_%s_to_%s_cfg.py' % (dt_begin.strftime('%Y-%m-%d__%H_%M_%S'), dt_next.strftime('%Y-%m-%d__%H_%M_%S'))
0074 replace_dict={'_TMIN_':tmin_str, '_TMAX_':tmax_str, '_DBFILE_':args.dbfile, '_TAG_':args.tag}
0075 insert_to_file(args.template, targetFile, replace_dict)
0076 print('Running %s' % targetFile)
0077 command = 'cmsRun %s' % targetFile
0078
0079 subprocess.Popen(command, shell=True).communicate()
0080 print('='*50)
0081 dt_begin = dt_next
0082
0083 if __name__ == '__main__':
0084 main()