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