File indexing completed on 2024-11-26 02:34:35
0001
0002 '''
0003 Script prints out histogram names that are in one ROOT file but not in another.
0004
0005 Author: Albertas Gimbutas, Vilnius University (LT)
0006 e-mail: albertasgim@gmail.com
0007 '''
0008 from datetime import datetime, timedelta
0009 from optparse import OptionParser
0010
0011 def collect_directory_filenames(directory, names_list):
0012 """Adds current directory file (histogram) names to ``names_list``. Then
0013 recursively calls itself for every current directory sub-directories."""
0014 for key in directory.GetListOfKeys():
0015 subdir = directory.Get(key.GetName())
0016 if subdir:
0017 if subdir.IsFolder():
0018 collect_directory_filenames(subdir, names_list)
0019 else:
0020 filename = directory.GetPath().split(':')[1] + ': ' + subdir.GetName()
0021 names_list.add(filename)
0022
0023 def get_content(root_file_name):
0024 """Returns all file (histogram) names, which are found in <root_file_name>."""
0025 from ROOT import TFile
0026 root_file = TFile(root_file_name)
0027 root_directory = root_file.GetDirectory("DQMData")
0028 filename_set = set()
0029 collect_directory_filenames(root_directory, filename_set)
0030 root_file.Close()
0031 return filename_set
0032
0033 def dqm_diff(filename1, filename2):
0034 """Prints file (histogram) names that are in <file1> and not in <file2>."""
0035 print("Missing files:")
0036 content1 = get_content(filename1)
0037 content2 = get_content(filename2)
0038 printed = False
0039 for name in content1:
0040 if name not in content2:
0041 print(" ->", name)
0042 printed = True
0043 if not printed:
0044 print(" All files match.")
0045
0046
0047
0048 parser = OptionParser(usage='usage: %prog <root_file1> <root_file2> [options]')
0049 parser.add_option('-t', '--time', action='store_true', default=False,
0050 dest='show_exec_time', help='Show execution time.')
0051 (options, args) = parser.parse_args()
0052
0053
0054 if len(args) != 2:
0055 parser.error("You have to specify two root files. e.g. ``dqm_diff.py file1.root file2.root``.")
0056
0057
0058 start = datetime.now()
0059 dqm_diff(*args)
0060 if options.show_exec_time:
0061 print('Execution time:', str(timedelta(seconds=(datetime.now() - start).seconds)))