CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/Utilities/RelMon/scripts/dqm_diff.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 from datetime import datetime, timedelta
00003 from optparse import OptionParser
00004 
00005 def collect_directory_filenames(directory, names_list):
00006     """Adds current directory file (histogram) names to ``names_list``. Then
00007     recursively calls itself for every current directory sub-directories."""
00008     for key in directory.GetListOfKeys():
00009         subdir = directory.Get(key.GetName())
00010         if subdir:
00011             if subdir.IsFolder():
00012                 collect_directory_filenames(subdir, names_list)
00013             else:
00014                 filename = directory.GetPath().split(':')[1] + ': ' + subdir.GetName()
00015                 names_list.add(filename)
00016 
00017 def get_content(root_file_name):
00018     """Returns all file (histogram) names, which are found in <root_file_name>."""
00019     from ROOT import TFile
00020     root_file = TFile(root_file_name)
00021     root_directory = root_file.GetDirectory("DQMData")
00022     filename_set = set()
00023     collect_directory_filenames(root_directory, filename_set)
00024     root_file.Close()
00025     return filename_set
00026 
00027 def dqm_diff(filename1, filename2):
00028     """Prints file (histogram) names that are in <file1> and not in <file2>."""
00029     print "Missing files:"
00030     content1 = get_content(filename1)
00031     content2 = get_content(filename2)
00032     printed = False
00033     for name in content1:
00034         if name not in content2:
00035             print "  ->", name
00036             printed = True
00037     if not printed:
00038         print "    All files match."
00039 
00040 
00041 ## Define commandline options
00042 parser = OptionParser(usage='usage: %prog <root_file1> <root_file2> [options]')
00043 parser.add_option('-t', '--time', action='store_true', default=False,
00044                     dest='show_exec_time', help='Show execution time.')
00045 (options, args) = parser.parse_args()
00046 
00047 ## Check for commandline option errors
00048 if len(args) != 2:
00049     parser.error("You have to specify two root files. e.g. ``dqm_diff.py file1.root file2.root``.")
00050 
00051 ## Execute the search of dismatches in two root fies.
00052 start = datetime.now()
00053 dqm_diff(*args)
00054 if options.show_exec_time:
00055     print 'Execution time:', str(timedelta(seconds=(datetime.now() - start).seconds))