27from pprint
import PrettyPrinter
28pp = PrettyPrinter(indent=2)
31file_path_patterns = [
"/hsm/belle2/bdata/Data/Raw/e0003/r0495[5,6]*/**/*.root"]
34def bad_run_finder(filepath):
36 Returns True if a file path contains
'.bad' after the run number
38 return fnmatch.fnmatch(filepath,
"*r?????.bad/*")
43def from_raw_data_file_paths(file_path_patterns):
44 from caf.utils
import find_absolute_file_paths, parse_raw_data_iov
46 file_paths = find_absolute_file_paths(file_path_patterns)
49 file_paths = list(itertools.filterfalse(bad_run_finder, file_paths))
53 for file_path
in file_paths:
54 file_to_iov[file_path] = parse_raw_data_iov(file_path)
60def from_metadata_of_files(file_path_patterns):
62 from caf.utils
import make_file_to_iov_dictionary
64 def run_in_one_process():
66 Creates the file_to_iov dictionary but only one file at a time.
67 Uses bad_run_finder to filter out runs marked as bad
from our glob pattern.
69 return make_file_to_iov_dictionary(file_path_patterns, filterfalse=bad_run_finder)
71 def run_with_multiprocessing(max_processes):
73 Creates the file_to_iov dictionary but using a Pool object to control the number of subprocesses.
74 Note that even though we're using a ThreadPool, we aren't bound by the GIL because we are subprocessing to
75 run b2file-metadata-show
in each Thread.
77 Uses bad_run_finder to filter out runs marked
as bad
from our glob pattern.
79 from multiprocessing.pool
import ThreadPool
80 tp = ThreadPool(processes=max_processes)
81 mapping = make_file_to_iov_dictionary(file_path_patterns, polling_time=5, pool=tp, filterfalse=bad_run_finder)
86 return run_in_one_process()
91function_map = {
"filepath": from_raw_data_file_paths,
92 "metadata": from_metadata_of_files}
97 parser = argparse.ArgumentParser()
98 parser.add_argument(
"method", help=
"The method by which you want to create the mapping of file paths -> IoVs.",
99 choices=function_map.keys())
105 parser = get_argparser()
106 args = parser.parse_args()
109 file_to_iov = function_map[args.method](file_path_patterns)
111 print(
"Created the file to IoV map:")
112 pp.pprint(file_to_iov)
116 filename =
"file_iov_map.pkl"
117 with open(filename,
'bw')
as iov_map_file:
118 pickle.dump(file_to_iov, iov_map_file)
119 print(f
"Saved dictionary to the file '{filename}' for later use.")
125if __name__ ==
"__main__":