19 from pprint
import PrettyPrinter
20 pp = PrettyPrinter(indent=2)
23 file_path_patterns = [
"/hsm/belle2/bdata/Data/Raw/e0003/r0495[5,6]*/**/*.root"]
29 def bad_run_finder(filepath):
31 Returns True if a file path contains '.bad' after the run number
33 return fnmatch.fnmatch(filepath,
"*r?????.bad/*")
38 def from_raw_data_file_paths(file_path_patterns):
39 from caf.utils
import find_absolute_file_paths, parse_raw_data_iov
41 file_paths = find_absolute_file_paths(file_path_patterns)
44 file_paths = list(itertools.filterfalse(bad_run_finder, file_paths))
48 for file_path
in file_paths:
49 file_to_iov[file_path] = parse_raw_data_iov(file_path)
55 def from_metadata_of_files(file_path_patterns):
57 from caf.utils
import make_file_to_iov_dictionary
59 def run_in_one_process():
61 Creates the file_to_iov dictionary but only one file at a time.
62 Uses bad_run_finder to filter out runs marked as bad from our glob pattern.
64 return make_file_to_iov_dictionary(file_path_patterns, filterfalse=bad_run_finder)
66 def run_with_multiprocessing(max_processes):
68 Creates the file_to_iov dictionary but using a Pool object to control the number of subprocesses.
69 Note that even though we're using a ThreadPool, we aren't bound by the GIL because we are subprocessing to
70 run b2file-metadata-show in each Thread.
72 Uses bad_run_finder to filter out runs marked as bad from our glob pattern.
74 from multiprocessing.pool
import ThreadPool
75 tp = ThreadPool(processes=max_processes)
76 mapping = make_file_to_iov_dictionary(file_path_patterns, polling_time=5, pool=tp, filterfalse=bad_run_finder)
81 return run_in_one_process()
86 function_map = {
"filepath": from_raw_data_file_paths,
87 "metadata": from_metadata_of_files}
92 parser = argparse.ArgumentParser()
93 parser.add_argument(
"method", help=
"The method by which you want to create the mapping of file paths -> IoVs.",
94 choices=function_map.keys())
100 parser = get_argparser()
101 args = parser.parse_args()
104 file_to_iov = function_map[args.method](file_path_patterns)
106 print(
"Created the file to IoV map:")
107 pp.pprint(file_to_iov)
111 filename =
"file_iov_map.pkl"
112 with open(filename,
'bw')
as iov_map_file:
113 pickle.dump(file_to_iov, iov_map_file)
114 print(
"Saved dictionary to the file '{}' for later use.".format(filename))
120 if __name__ ==
"__main__":