test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
roll_playback.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 
3 # TODO: automatically determine from which LS to start (currently this is hard-coded to 1)
4 
5 
6 import os
7 import sys
8 import time
9 import re
10 import shutil
11 import json
12 
13 
14 dat_source = '/fff/ramdisk/playback_files/run224380'
15 pb_source = '/fff/ramdisk/playback_files/run225044_pb'
16 destination = '/fff/ramdisk'
17 lumi_len = 23 # in seconds
18 run_padding = 6
19 lumi_padding = 4
20 files_copied_buffer_len = 20 # the number of file to keep in the destination directory
21 
22 
23 def dat_sanity_check(dat_source):
24  dat_jsn_files = []
25  dat_files = []
26  dat_run_number = None
27 
28  # find the dat json files
29  files = os.listdir(dat_source)
30  dat_jsn_pattern = re.compile(r'run([0-9]+)_ls([0-9]+)_streamDQM_StorageManager.jsn')
31  dat_jsn_files = sorted(filter(lambda x: dat_jsn_pattern.match(x), files))
32  if len(dat_jsn_files) < 1:
33  print('No dat json files are found in "{0}"'.format(dat_source))
34  return False, dat_jsn_files, dat_files, dat_run_number
35 
36  # check if the dat files exist
37  jsn_files_tobe_removed = []
38  for jsn_file in dat_jsn_files:
39  dat_file = jsn_file.replace('.jsn','.dat')
40  if not os.path.exists(dat_source + '/' + dat_file):
41  print('The dat file {0} does NOT exist! Removing the corresponding json file.'.format(dat_file))
42  jsn_files_tobe_removed.append(jsn_file)
43 
44  # remove the json files that don't have corresponding dat file
45  dat_jsn_files = [x for x in dat_jsn_files if x not in jsn_files_tobe_removed]
46 
47  # create a list of dat files
48  dat_files = map(lambda x: x.replace('.jsn','.dat'), dat_jsn_files)
49 
50 
51  dat_run_number = int(dat_jsn_pattern.match(dat_jsn_files[0]).group(1))
52  # check for run_number consistency
53  for i in range(1,len(dat_jsn_files)):
54  run_number_current = int(dat_jsn_pattern.match(dat_jsn_files[i]).group(1))
55  if run_number_current != dat_run_number:
56  print('Non consistent run numbers: "{0}" - expected, "{1}" - found'.format(run_nummber, run_nummber_current))
57  print('\t "{0}" - will be used as a run number'.format(run_nummber))
58 
59  return True, dat_jsn_files, dat_files, dat_run_number
60 
61 
62 def pb_sanity_check(pb_source):
63  pb_jsn_files = []
64  pb_files = []
65  pb_run_number = None
66 
67  # find the pb json files
68  files = os.listdir(pb_source)
69  pb_jsn_pattern = re.compile(r'run([0-9]+)_ls([0-9]+)_streamDQMHistograms_StorageManager.jsn')
70  pb_jsn_files = sorted(filter(lambda x: pb_jsn_pattern.match(x), files))
71 
72  # check if the pb files exist
73  jsn_files_tobe_removed = []
74  for jsn_file in pb_jsn_files:
75  pb_file = jsn_file.replace('.jsn','.pb')
76  if not os.path.exists(pb_source + '/' + pb_file):
77  print('The pb file {0} does NOT exist! Removing the corresponding json file.'.format(pb_file))
78  jsn_files_tobe_removed.append(jsn_file)
79 
80  # remove the json files that don't have corresponding pb file
81  pb_jsn_files = [x for x in pb_jsn_files if x not in jsn_files_tobe_removed]
82 
83  if len(pb_jsn_files) < 1:
84  print('No pb json files are found in "{0}"'.format(pb_source))
85  return False, pb_jsn_files, pb_files, pb_run_number
86 
87  # create a list of pb files
88  pb_files = map(lambda x: x.replace('.jsn','.pb'), pb_jsn_files)
89 
90  pb_run_number = int(pb_jsn_pattern.match(pb_jsn_files[0]).group(1))
91  # check for run_number consistency
92  for i in range(1,len(pb_jsn_files)):
93  run_number_current = int(pb_jsn_pattern.match(pb_jsn_files[i]).group(1))
94  if run_number_current != pb_run_number:
95  print('Non consistent run numbers: "{0}" - expected, "{1}" - found'.format(run_nummber, run_nummber_current))
96  print('\t "{0}" - will be used as a run number'.format(run_nummber))
97 
98  return True, pb_jsn_files, pb_files, pb_run_number
99 
100 
101 def copy_next_lumi(jsn_files, files, run_number, current_lumi, source, destination):
102  assert(len(jsn_files) == len(files))
103 
104  index = current_lumi % len(jsn_files)
105 
106  # copy the file
107  input_fn = source + '/' + files[index]
108  output_fn = files[index]
109  run_start = output_fn.find('run') + 3
110  output_fn = output_fn[:run_start] + str(run_number).zfill(run_padding) + output_fn[run_start + run_padding:]
111  lumi_start = output_fn.find('ls') + 2
112  output_fn = destination + '/' + output_fn[:lumi_start] + str(current_lumi).zfill(lumi_padding) + output_fn[lumi_start + lumi_padding:]
113  os.link(input_fn, output_fn) # instead of copying the file create a hard link
114  print(input_fn + ' -> ' + output_fn)
115 
116  # modyfy and copy the json file
117  input_jsn_fn = source + '/' + jsn_files[index]
118  input_jsn = open(input_jsn_fn, 'r')
119  jsn_data = json.load(input_jsn)
120  input_jsn.close()
121 
122  # generate the output jsn file name
123  output_jsn_fn = jsn_files[index]
124  run_start = output_jsn_fn.find('run') + 3
125  output_jsn_fn = output_jsn_fn[:run_start] + str(run_number).zfill(run_padding) + output_jsn_fn[run_start + run_padding:]
126  lumi_start = output_jsn_fn.find('ls') + 2
127  output_jsn_fn = destination + '/' + output_jsn_fn[:lumi_start] + str(current_lumi).zfill(lumi_padding) + output_jsn_fn[lumi_start + lumi_padding:]
128 
129  # modify the json file contents
130  jsn_data['data'][3] = output_fn[output_fn.rfind('/')+1:]
131 
132  # create the outpuf jsn file
133  output_jsn = open(output_jsn_fn, 'w')
134  output_jsn.write(json.dumps(jsn_data, indent=4))
135  output_jsn.close()
136 
137  print(input_jsn_fn + ' -> ' + output_jsn_fn)
138 
139  return output_jsn_fn, output_fn
140 
141 
142 
143 if __name__ == '__main__':
144  dat_dir_ok, dat_jsn_files, dat_files, dat_run_number = dat_sanity_check(dat_source)
145  pb_dir_ok, pb_jsn_files, pb_files, pb_run_number = pb_sanity_check(pb_source)
146 
147  if dat_dir_ok and pb_dir_ok:
148  run_number = int(dat_run_number)
149  if run_number != int(pb_run_number):
150  print('The dat run number "{0}" differs from the PB run number "{1}".'.format(run_number, pb_run_number))
151  print('"{0}" is going to be used as a run number.'.format(run_number))
152 
153 
154  output_dir = destination + '/' + 'run' + str(dat_run_number).zfill(run_padding)
155  if not os.path.exists(output_dir): os.mkdir(output_dir)
156 
157  time.sleep(1) # a hack in order python inotify to work correctly
158 
159  current_lumi = 1
160  files_copied = []
161  while True:
162  files_copied += copy_next_lumi(dat_jsn_files, dat_files, run_number, current_lumi, dat_source, output_dir)
163 
164  files_copied += copy_next_lumi(pb_jsn_files, pb_files, run_number, current_lumi, pb_source, output_dir)
165 
166  print('******************************************************************************************')
167 
168  while files_copied_buffer_len < len(files_copied):
169  os.remove(files_copied.pop(0))
170 
171  current_lumi += 1
172  time.sleep(lumi_len)
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
Definition: print.cc:8
tuple group
Definition: watchdog.py:82