CMS 3D CMS Logo

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 __future__ import print_function
9 from datetime import datetime, timedelta
10 from optparse import OptionParser
11 
12 def collect_directory_filenames(directory, names_list):
13  """Adds current directory file (histogram) names to ``names_list``. Then
14  recursively calls itself for every current directory sub-directories."""
15  for key in directory.GetListOfKeys():
16  subdir = directory.Get(key.GetName())
17  if subdir:
18  if subdir.IsFolder():
19  collect_directory_filenames(subdir, names_list)
20  else:
21  filename = directory.GetPath().split(':')[1] + ': ' + subdir.GetName()
22  names_list.add(filename)
23 
24 def get_content(root_file_name):
25  """Returns all file (histogram) names, which are found in <root_file_name>."""
26  from ROOT import TFile
27  root_file = TFile(root_file_name)
28  root_directory = root_file.GetDirectory("DQMData")
29  filename_set = set()
30  collect_directory_filenames(root_directory, filename_set)
31  root_file.Close()
32  return filename_set
33 
34 def dqm_diff(filename1, filename2):
35  """Prints file (histogram) names that are in <file1> and not in <file2>."""
36  print("Missing files:")
37  content1 = get_content(filename1)
38  content2 = get_content(filename2)
39  printed = False
40  for name in content1:
41  if name not in content2:
42  print(" ->", name)
43  printed = True
44  if not printed:
45  print(" All files match.")
46 
47 
48 
49 parser = OptionParser(usage='usage: %prog <root_file1> <root_file2> [options]')
50 parser.add_option('-t', '--time', action='store_true', default=False,
51  dest='show_exec_time', help='Show execution time.')
52 (options, args) = parser.parse_args()
53 
54 
55 if len(args) != 2:
56  parser.error("You have to specify two root files. e.g. ``dqm_diff.py file1.root file2.root``.")
57 
58 
59 start = datetime.now()
60 dqm_diff(*args)
61 if options.show_exec_time:
62  print('Execution time:', str(timedelta(seconds=(datetime.now() - start).seconds)))
cms::dd::split
std::vector< std::string_view > split(std::string_view, const char *)
dqm_diff
Definition: dqm_diff.py:1
dqm_diff.dqm_diff
def dqm_diff(filename1, filename2)
Definition: dqm_diff.py:34
str
#define str(s)
Definition: TestProcessor.cc:48
dqm_diff.get_content
def get_content(root_file_name)
Definition: dqm_diff.py:24
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
dqm_diff.collect_directory_filenames
def collect_directory_filenames(directory, names_list)
Definition: dqm_diff.py:12