CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dqm_diff.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 '''
3 Script prints out histogram names that are in one ROOT file but not in another.
4 
5 Author: Albertas Gimbutas, Vilnius University (LT)
6 e-mail: albertasgim@gmail.com
7 '''
8 from datetime import datetime, timedelta
9 from optparse import OptionParser
10 
11 def collect_directory_filenames(directory, names_list):
12  """Adds current directory file (histogram) names to ``names_list``. Then
13  recursively calls itself for every current directory sub-directories."""
14  for key in directory.GetListOfKeys():
15  subdir = directory.Get(key.GetName())
16  if subdir:
17  if subdir.IsFolder():
18  collect_directory_filenames(subdir, names_list)
19  else:
20  filename = directory.GetPath().split(':')[1] + ': ' + subdir.GetName()
21  names_list.add(filename)
22 
23 def get_content(root_file_name):
24  """Returns all file (histogram) names, which are found in <root_file_name>."""
25  from ROOT import TFile
26  root_file = TFile(root_file_name)
27  root_directory = root_file.GetDirectory("DQMData")
28  filename_set = set()
29  collect_directory_filenames(root_directory, filename_set)
30  root_file.Close()
31  return filename_set
32 
33 def dqm_diff(filename1, filename2):
34  """Prints file (histogram) names that are in <file1> and not in <file2>."""
35  print "Missing files:"
36  content1 = get_content(filename1)
37  content2 = get_content(filename2)
38  printed = False
39  for name in content1:
40  if name not in content2:
41  print " ->", name
42  printed = True
43  if not printed:
44  print " All files match."
45 
46 
47 ## Define commandline options
48 parser = OptionParser(usage='usage: %prog <root_file1> <root_file2> [options]')
49 parser.add_option('-t', '--time', action='store_true', default=False,
50  dest='show_exec_time', help='Show execution time.')
51 (options, args) = parser.parse_args()
52 
53 ## Check for commandline option errors
54 if len(args) != 2:
55  parser.error("You have to specify two root files. e.g. ``dqm_diff.py file1.root file2.root``.")
56 
57 ## Execute the search of dismatches in two root fies.
58 start = datetime.now()
59 dqm_diff(*args)
60 if options.show_exec_time:
61  print 'Execution time:', str(timedelta(seconds=(datetime.now() - start).seconds))
def get_content
Definition: dqm_diff.py:23
def dqm_diff
Definition: dqm_diff.py:33
def collect_directory_filenames
Definition: dqm_diff.py:11
double split
Definition: MVATrainer.cc:139