File indexing completed on 2024-11-25 02:29:11
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
0010 import os
0011 import datetime
0012 import subprocess
0013 import argparse
0014
0015 def insert_to_file(template, target, replace_dict):
0016 '''Update the template file based on the replace_dict, and write to the target.'''
0017 with open(template, 'r') as input_file:
0018 config=input_file.read()
0019 with open(target, 'w') as output_file:
0020 for key, value in replace_dict.items():
0021 config = config.replace(key, value)
0022 output_file.write(config)
0023
0024
0025 def main():
0026 parser = argparse.ArgumentParser(description='Run SiStrip DCS O2O by splitting into small intervals.')
0027 parser.add_argument('-i', '--interval',
0028 dest = 'interval',
0029 type = int,
0030 default = 1,
0031 help = 'Interval (in hours) for splitting jobs. Default: %(default)d hours',
0032 )
0033 parser.add_argument('-b', '--begin',
0034 dest = 'begin',
0035 default = '2016-01-01 00:00:00',
0036 help = 'Beginning time of the interval. Format: [YYYY-MM-DD HH:MM:SS]. Default: %(default)s',
0037 )
0038 parser.add_argument('-e', '--end',
0039 dest = 'end',
0040 default = '2016-02-01 00:00:00',
0041 help = 'End time of the interval. Format: [YYYY-MM-DD HH:MM:SS]. Default: %(default)s',
0042 )
0043 parser.add_argument('-t', '--template',
0044 dest = 'template',
0045 default = 'dcs_o2o_template_cfg.py',
0046 help = 'Template config file. Default: %(default)s',
0047 )
0048 parser.add_argument('--db',
0049 dest = 'dbfile',
0050 default = 'SiStripDetVOff.db',
0051 help = 'Output tag. Default: %(default)s',
0052 )
0053 parser.add_argument('--tag',
0054 dest = 'tag',
0055 default = 'SiStripDetVOff',
0056 help = 'Output tag. Default: %(default)s',
0057 )
0058 args = parser.parse_args()
0059
0060
0061 if not os.path.exists(args.dbfile):
0062 with open(args.dbfile, 'w'):
0063 pass
0064
0065 dt_begin = datetime.datetime.strptime(args.begin, '%Y-%m-%d %H:%M:%S')
0066 dt_end = datetime.datetime.strptime(args.end, '%Y-%m-%d %H:%M:%S')
0067
0068 while (dt_end-dt_begin).total_seconds() > 0:
0069 dt_next = dt_begin + datetime.timedelta(hours=args.interval)
0070 tmin_str = dt_begin.strftime('%Y, %-m, %-d, %-H, 0, 0, 0')
0071 tmax_str = dt_next.strftime('%Y, %-m, %-d, %-H, 0, 0, 0')
0072 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'))
0073 replace_dict={'_TMIN_':tmin_str, '_TMAX_':tmax_str, '_DBFILE_':args.dbfile, '_TAG_':args.tag}
0074 insert_to_file(args.template, targetFile, replace_dict)
0075 print('Running %s' % targetFile)
0076 command = 'cmsRun %s' % targetFile
0077
0078 subprocess.Popen(command, shell=True).communicate()
0079 print('='*50)
0080 dt_begin = dt_next
0081
0082 if __name__ == '__main__':
0083 main()