CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
edmOneToOneComparison.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 if __name__ == "__main__":
4  import optparse
5  parser = optparse.OptionParser("usage: %prog [options] config.txt file1.root [file2.root]\nVisit https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsToolsEdmOneToOneComparison\nfor full documentation.")
6  modeGroup = optparse.OptionGroup (parser, "Mode Conrols")
7  tupleGroup = optparse.OptionGroup (parser, "Tuple Controls")
8  optionsGroup = optparse.OptionGroup (parser, "Options")
9  # mode group
10  modeGroup.add_option ('--compare', dest='compare', action='store_true',
11  help='Compare tuple1 to tuple2')
12  modeGroup.add_option ('--saveAs', dest='saveAs', type='string',
13  help='Save tuple1 as GO Root file')
14  modeGroup.add_option ('--printTuple', dest='printTuple',
15  action='store_true',
16  help='Print out all events in tuple1')
17  modeGroup.add_option ('--interactive', dest='interactive',
18  action='store_true',
19  help='Loads files and prepares "event" '
20  'for interactive mode')
21  # tuple group
22  tupleGroup.add_option ('--tuple', dest='tuple', type='string',
23  default='',
24  help="Tuple type of 1st and 2nd tuple")
25  tupleGroup.add_option ('--tuple1', dest='tuple1', type='string',
26  default='reco',
27  help="Tuple type of 1st tuple")
28  tupleGroup.add_option ('--tuple2', dest='tuple2', type='string',
29  default='reco',
30  help="Tuple type of 2nd tuple")
31  tupleGroup.add_option ('--file', dest='file', type='string',
32  default="",
33  help="1st and 2nd tuple file (debugging only)")
34  tupleGroup.add_option ('--file1', dest='file1', type='string',
35  default="",
36  help="1st tuple file")
37  tupleGroup.add_option ('--file2', dest='file2', type='string',
38  default="",
39  help="2nd tuple file")
40  tupleGroup.add_option ('--numEvents', dest='numEvents', type='int',
41  default=0,
42  help="number of events for first and second file")
43  tupleGroup.add_option ('--numEvents1', dest='numEvents1', type='int',
44  default=0,
45  help="number of events for first file")
46  tupleGroup.add_option ('--numEvents2', dest='numEvents2', type='int',
47  default=0,
48  help="number of events for second file")
49  tupleGroup.add_option ('--alias', dest='alias', type='string',
50  action='append',
51  help="Change alias ('tuple:object:alias')")
52  tupleGroup.add_option ('--label', dest='label', type='string',
53  action='append',
54  help="Change label ('tuple^object^label')")
55  tupleGroup.add_option ('--changeVariable', dest='changeVar', type='string',
56  action='append',
57  help="Change variable filling "
58  "('tuple:objName:varName:def')")
59  # options group
60  optionsGroup.add_option ('--config', dest='config', type='string',
61  default='config.txt',
62  help="Configuration file (default: '%default')")
63  optionsGroup.add_option ('--printEvent', dest='printEvent',
64  action='store_true',
65  help='Prints loaded event to screen')
66  optionsGroup.add_option ('--printGlobal', dest='printGlobal',
67  action='store_true',
68  help='Prints out global information' +
69  ' (for development)')
70  optionsGroup.add_option ('--blur1', dest='blur', type='float',
71  default=0.,
72  help="Randomly changes values by 'BLUR' " +\
73  "from tuple1. For debugging only.")
74  optionsGroup.add_option ('--blurRate', dest='blurRate', type='float',
75  default=0.02,
76  help="Rate at which objects will be changed. " + \
77  "(%default default)")
78  optionsGroup.add_option ('--compRoot', dest='compRoot', type='string',
79  default='',
80  help="Write out root file for file comparisons")
81  optionsGroup.add_option ('--debug', dest='debug', action='store_true',
82  help="Print debugging information")
83  optionsGroup.add_option ('--relative', dest='relative',
84  action='store_true', default=True,
85  help='Precision is checked against relative difference')
86  optionsGroup.add_option ('--absolute', dest='relative',
87  action='store_false',
88  help='Precision is checked against absolute difference')
89  optionsGroup.add_option
90  parser.add_option_group (modeGroup)
91  parser.add_option_group (tupleGroup)
92  parser.add_option_group (optionsGroup)
93  (options, args) = parser.parse_args()
94  from Validation.Tools.GenObject import *
95  ROOT.gROOT.SetBatch()
96 
97  lenArgs = len (args)
98  if lenArgs >= 1:
99  options.config = args[0]
100  if lenArgs >= 2:
101  options.file1 = args[1]
102  if lenArgs == 3:
103  options.file2 = args[2]
104  if lenArgs > 3:
105  raise RuntimeError, "Too many arguments"
106 
107  # Here we go
108  random.seed( os.getpid() )
109  GenObject.loadConfigFile (options.config)
110  ROOT.gSystem.Load("libFWCoreFWLite.so")
111  ROOT.AutoLibraryLoader.enable()
112  # Let's parse any args
113  doubleColonRE = re.compile (r'(.+):(.+):(.+)')
114  if options.alias:
115  for arg in options.alias:
116  aliasMatch = doubleColonRE.match (arg)
117  if aliasMatch:
118  print "aM", aliasMatch
119  GenObject.changeAlias (aliasMatch.group (1),
120  aliasMatch.group (2),
121  aliasMatch.group (3))
122  continue
123  # if we're here, then we have an argument that we don't understand
124  raise RuntimeError, "Unknown alias format '%s'" % arg
125  tripleColonRE = re.compile (r'(.+):(.+):(.+):(.+)')
126  if options.changeVar:
127  for arg in options.changeVar:
128  changeMatch = tripleColonRE.match (arg)
129  if changeMatch:
130  GenObject.changeVariable (changeMatch.group (1),
131  changeMatch.group (2),
132  changeMatch.group (3),
133  changeMatch.group (4))
134  continue
135  # if we're here, then we have an argument that we don't understand
136  raise RuntimeError, "Unknown changeVar format '%s'" % arg
137  if options.label:
138  for label in options.label:
139  pieces = label.split('^')
140  if len (pieces) != 3:
141  raise RuntimeError, "Can't process label command '%s'" \
142  % options.label
143  GenObject.changeLabel (*pieces)
144  # We don't want to use options beyond the main code, so let thee
145  # kitchen sink know what we want
146  GenObject.setGlobalFlag ('printEvent', options.printEvent)
147  GenObject.setGlobalFlag ('debug', options.debug)
148  GenObject.setGlobalFlag ('relative', options.relative)
149  if options.blur:
150  GenObject.setGlobalFlag ('blur', options.blur)
151  GenObject.setGlobalFlag ('blurRate', options.blurRate)
152  # take care of any 'double' options now
153  if options.tuple:
154  options.tuple1 = options.tuple2 = options.tuple
155  if options.file:
156  options.file1 = options.file2 = options.file
157  if options.numEvents:
158  options.numEvents1 = options.numEvents2 = options.numEvents
159  if options.compare:
160  # Compare two files
161  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
162  options.numEvents1)
163  chain2 = GenObject.prepareTuple (options.tuple2, options.file2,
164  options.numEvents2)
165  problems = \
166  GenObject.compareTwoTrees (chain1, chain2,
167  diffOutputName = options.compRoot)
168  print "problems"
169  pprint.pprint (problems)
170  if options.saveAs:
171  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
172  options.numEvents1)
173  GenObject.saveTupleAs (chain1, options.saveAs)
174  if options.printTuple:
175  print "printing tuple"
176  GenObject.setGlobalFlag ('printEvent', True)
177  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
178  options.numEvents1)
179  GenObject.printTuple (chain1)
180  #GenObject.saveTupleAs (chain1, options.saveAs)
181  if options.printGlobal:
182  GenObject.printGlobal()
183  if options.interactive:
184  chain1 = chain2 = 0
185  if len (options.file1):
186  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
187  options.numEvents1)
188  if len (options.file2):
189  chain2 = GenObject.prepareTuple (options.tuple2, options.file2)
190  #############################################
191  ## Load and save command line history when ##
192  ## running interactively. ##
193  #############################################
194  import os, readline
195  import atexit
196  historyPath = os.path.expanduser("~/.pyhistory")
197 
198  def save_history (historyPath=historyPath):
199  import readline
200  readline.write_history_file(historyPath)
201  if os.path.exists(historyPath):
202  readline.read_history_file(historyPath)
203 
204  atexit.register(save_history)
205  readline.parse_and_bind("set show-all-if-ambiguous on")
206  readline.parse_and_bind("tab: complete")
207  if os.path.exists (historyPath) :
208  readline.read_history_file(historyPath)
209  readline.set_history_length(-1)
210