ATLAS Offline Software
compare_metadata.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 import sys
6 import logging
7 import argparse
8 from PyUtils.MetaReader import read_metadata
9 from typing import Dict, List
10 
11 logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
12 
13 
15  metadata: Dict[str, any], keys_prefixes: List[str]
16 ) -> Dict[str, any]:
17  """
18  Filters a dictionary to include only keys that start with any of the specified prefixes.
19 
20  Args:
21  metadata (dict): The dictionary to filter.
22  keys_prefixes (list): A list of prefixes to include in the filtered dictionary.
23 
24  Returns:
25  dict: The filtered dictionary.
26  """
27  filtered_metadata: Dict[str, any] = {}
28  for key, value in metadata.items():
29  for prefix in keys_prefixes:
30  if key.startswith(prefix):
31  filtered_metadata[prefix] = value
32  break
33  return filtered_metadata
34 
35 
37  ttree_file_path: str,
38  rntuple_file_path: str,
39  keys_to_compare: List[str],
40  fmd_keys_to_compare: List[str],
41 ) -> int:
42  """
43  Compares metadata from two files to check if they are the same.
44 
45  Args:
46  ttree_file_path (str): The path to the first metadata file.
47  rntuple_file_path (str): The path to the second metadata file.
48  keys_to_compare (list): List of keys to compare in the metadata.
49  fmd_keys_to_compare (list): List of keys to compare in the 'FileMetaData' section.
50 
51  Returns:
52  int: 0 if metadata is the same, 1 if metadata is different.
53  """
54  try:
55  # Read metadata from the ttree file
56  ttree_metadata_full: Dict[str, any] = read_metadata(
57  ttree_file_path, mode="full"
58  )[ttree_file_path]
59  ttree_metadata_peeker: Dict[str, any] = read_metadata(
60  ttree_file_path, mode="peeker"
61  )[ttree_file_path]
62 
63  # Read metadata from the rntuple file
64  rntuple_metadata_full: Dict[str, any] = read_metadata(rntuple_file_path)[
65  rntuple_file_path
66  ]
67 
68  except KeyError as e:
69  logging.error(f"Error accessing metadata for file: {e}")
70  return 1
71 
72  # Filter metadata dictionaries based on the required keys
73  ttree_event_format_metadata: Dict[str, any] = filter_metadata(
74  ttree_metadata_full, ["EventFormat"]
75  )
76  ttree_peeker_metadata: Dict[str, any] = filter_metadata(
77  ttree_metadata_peeker, keys_to_compare
78  )
79  rntuple_metadata: Dict[str, any] = filter_metadata(
80  rntuple_metadata_full, keys_to_compare
81  )
82  rntuple_event_format_metadata: Dict[str, any] = filter_metadata(
83  rntuple_metadata_full, ["EventFormat"]
84  )
85 
86  # Further filter the 'FileMetaData' section of the metadata
87  ttree_peeker_metadata["FileMetaData"] = filter_metadata(
88  ttree_peeker_metadata.get("FileMetaData", {}), fmd_keys_to_compare
89  )
90  rntuple_metadata["FileMetaData"] = filter_metadata(
91  rntuple_metadata.get("FileMetaData", {}), fmd_keys_to_compare
92  )
93 
94  # Compare the filtered metadata
95  if (
96  ttree_peeker_metadata == rntuple_metadata
97  and ttree_event_format_metadata == rntuple_event_format_metadata
98  ):
99  logging.info(
100  f"Selected metadata keys ({keys_to_compare=}, {fmd_keys_to_compare=}) are the same"
101  )
102  return 0
103  else:
104  logging.info(
105  f"Selected metadata keys ({keys_to_compare=}, {fmd_keys_to_compare=}) are different"
106  )
107  return 1
108 
109 
110 def main():
111  parser = argparse.ArgumentParser(description="Compare metadata between two files.")
112  parser.add_argument(
113  "--ttree-file-path",
114  type=str,
115  required=True,
116  help="Path to the TTree metadata file.",
117  )
118  parser.add_argument(
119  "--rntuple-file-path",
120  type=str,
121  required=True,
122  help="Path to the RNTuple metadata file.",
123  )
124  parser.add_argument(
125  "--keys-to-compare",
126  type=str,
127  nargs="*",
128  default=[],
129  help="List of keys to compare in the metadata. Provide as space-separated values.",
130  )
131  parser.add_argument(
132  "--fmd-keys-to-compare",
133  type=str,
134  nargs="*",
135  default=[],
136  help="List of keys to compare in the 'FileMetaData' section. Provide as space-separated values.",
137  )
138 
139  args = parser.parse_args()
140 
141  sys.exit(
143  args.ttree_file_path,
144  args.rntuple_file_path,
145  args.keys_to_compare,
146  args.fmd_keys_to_compare,
147  )
148  )
149 
150 
151 if __name__ == "__main__":
152  main()
compare_metadata.main
def main()
Definition: compare_metadata.py:110
python.MetaReader.read_metadata
def read_metadata(filenames, file_type=None, mode='lite', promote=None, meta_key_filter=None, unique_tag_info_values=True, ignoreNonExistingLocalFiles=False)
Definition: MetaReader.py:53
compare_metadata
Definition: compare_metadata.py:1
compare_metadata.compare_metadata
int compare_metadata(str ttree_file_path, str rntuple_file_path, List[str] keys_to_compare, List[str] fmd_keys_to_compare)
Definition: compare_metadata.py:36
compare_metadata.filter_metadata
Dict[str, any] filter_metadata(Dict[str, any] metadata, List[str] keys_prefixes)
Definition: compare_metadata.py:14