Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:26:50

0001 #! /usr/bin/env python3
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 __future__ import print_function
0009 from datetime import datetime, timedelta
0010 from optparse import OptionParser
0011 
0012 def collect_directory_filenames(directory, names_list):
0013     """Adds current directory file (histogram) names to ``names_list``. Then
0014     recursively calls itself for every current directory sub-directories."""
0015     for key in directory.GetListOfKeys():
0016         subdir = directory.Get(key.GetName())
0017         if subdir:
0018             if subdir.IsFolder():
0019                 collect_directory_filenames(subdir, names_list)
0020             else:
0021                 filename = directory.GetPath().split(':')[1] + ': ' + subdir.GetName()
0022                 names_list.add(filename)
0023 
0024 def get_content(root_file_name):
0025     """Returns all file (histogram) names, which are found in <root_file_name>."""
0026     from ROOT import TFile
0027     root_file = TFile(root_file_name)
0028     root_directory = root_file.GetDirectory("DQMData")
0029     filename_set = set()
0030     collect_directory_filenames(root_directory, filename_set)
0031     root_file.Close()
0032     return filename_set
0033 
0034 def dqm_diff(filename1, filename2):
0035     """Prints file (histogram) names that are in <file1> and not in <file2>."""
0036     print("Missing files:")
0037     content1 = get_content(filename1)
0038     content2 = get_content(filename2)
0039     printed = False
0040     for name in content1:
0041         if name not in content2:
0042             print("  ->", name)
0043             printed = True
0044     if not printed:
0045         print("    All files match.")
0046 
0047 
0048 ## Define commandline options
0049 parser = OptionParser(usage='usage: %prog <root_file1> <root_file2> [options]')
0050 parser.add_option('-t', '--time', action='store_true', default=False,
0051                     dest='show_exec_time', help='Show execution time.')
0052 (options, args) = parser.parse_args()
0053 
0054 ## Check for commandline option errors
0055 if len(args) != 2:
0056     parser.error("You have to specify two root files. e.g. ``dqm_diff.py file1.root file2.root``.")
0057 
0058 ## Execute the search of dismatches in two root fies.
0059 start = datetime.now()
0060 dqm_diff(*args)
0061 if options.show_exec_time:
0062     print('Execution time:', str(timedelta(seconds=(datetime.now() - start).seconds)))