29from pprint
import PrettyPrinter
32parser = argparse.ArgumentParser(
33 description=
"Make a mapping file of file paths -> IoV")
35 '--file_path_patterns',
36 default=
'/hsm/belle2/bdata/Data/Raw/e0003/r*/**/*.root',
38 help=
'Lets take some file patterns. We could have put wildcards in more places but this is enough for testing.')
39parser.add_argument(
'--output', default=
'file_iov_map.pkl', type=str,
40 help=
'Name of the output mapping file.')
45 help=
'Either take IoV from FileMetaData (option=metadata) or parse it from filepath (option=filepath).')
46args = parser.parse_args()
48file_path_patterns = [args.file_path_patterns, ]
49print(file_path_patterns)
54def from_raw_data_file_paths(file_path_patterns):
55 from caf.utils
import find_absolute_file_paths, parse_raw_data_iov
57 file_paths = find_absolute_file_paths(file_path_patterns)
61 for file_path
in file_paths:
62 file_to_iov[file_path] = parse_raw_data_iov(file_path)
68def from_metadata_of_files(file_path_patterns):
70 from caf.utils
import make_file_to_iov_dictionary
72 def run_in_one_process():
74 Creates the file_to_iov dictionary but only one file at a time.
76 return make_file_to_iov_dictionary(file_path_patterns)
78 def run_with_multiprocessing(max_processes):
80 Creates the file_to_iov dictionary but using a Pool object to control the number of subprocesses.
81 Note that even though we're using a ThreadPool, we aren't bound by the GIL because we are subprocessing to
82 run b2file-metadata-show
in each Thread.
84 from multiprocessing.pool
import ThreadPool
85 tp = ThreadPool(processes=max_processes)
86 mapping = make_file_to_iov_dictionary(
87 file_path_patterns, polling_time=5, pool=tp)
93 return run_with_multiprocessing(max_processes=6)
96if args.option ==
"metadata":
97 file_to_iov = from_metadata_of_files(file_path_patterns)
98elif args.option ==
"filepath":
99 file_to_iov = from_raw_data_file_paths(file_path_patterns)
101 print(
"That wasn't one of the available options for this script. Run it again with no arguments to see the options.")
104pp = PrettyPrinter(indent=2)
105pp.pprint(file_to_iov)
108with open(args.output,
'bw')
as iov_map_file:
109 pickle.dump(file_to_iov, iov_map_file)
110 print(
"Saved dictionary to a file for later use.")