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 ('--strictPairing', dest='strictPairing',
84  action='store_true',
85  help="Objects are paired uniquely by order in collection")
86  optionsGroup.add_option ('--relative', dest='relative',
87  action='store_true', default=True,
88  help='Precision is checked against relative difference')
89  optionsGroup.add_option ('--absolute', dest='relative',
90  action='store_false',
91  help='Precision is checked against absolute difference')
92  optionsGroup.add_option
93  parser.add_option_group (modeGroup)
94  parser.add_option_group (tupleGroup)
95  parser.add_option_group (optionsGroup)
96  (options, args) = parser.parse_args()
97  from Validation.Tools.GenObject import *
98  ROOT.gROOT.SetBatch()
99 
100  lenArgs = len (args)
101  if lenArgs >= 1:
102  options.config = args[0]
103  if lenArgs >= 2:
104  options.file1 = args[1]
105  if lenArgs == 3:
106  options.file2 = args[2]
107  if lenArgs > 3:
108  raise RuntimeError, "Too many arguments"
109 
110  # Here we go
111  random.seed( os.getpid() )
112  GenObject.loadConfigFile (options.config)
113  ROOT.gSystem.Load("libFWCoreFWLite.so")
114  ROOT.FWLiteEnabler::enable()
115  # Let's parse any args
116  doubleColonRE = re.compile (r'(.+):(.+):(.+)')
117  if options.alias:
118  for arg in options.alias:
119  aliasMatch = doubleColonRE.match (arg)
120  if aliasMatch:
121  print "aM", aliasMatch
122  GenObject.changeAlias (aliasMatch.group (1),
123  aliasMatch.group (2),
124  aliasMatch.group (3))
125  continue
126  # if we're here, then we have an argument that we don't understand
127  raise RuntimeError, "Unknown alias format '%s'" % arg
128  tripleColonRE = re.compile (r'(.+):(.+):(.+):(.+)')
129  if options.changeVar:
130  for arg in options.changeVar:
131  changeMatch = tripleColonRE.match (arg)
132  if changeMatch:
133  GenObject.changeVariable (changeMatch.group (1),
134  changeMatch.group (2),
135  changeMatch.group (3),
136  changeMatch.group (4))
137  continue
138  # if we're here, then we have an argument that we don't understand
139  raise RuntimeError, "Unknown changeVar format '%s'" % arg
140  if options.label:
141  for label in options.label:
142  pieces = label.split('^')
143  if len (pieces) != 3:
144  raise RuntimeError, "Can't process label command '%s'" \
145  % options.label
146  GenObject.changeLabel (*pieces)
147  # We don't want to use options beyond the main code, so let the
148  # kitchen sink know what we want
149  GenObject.setGlobalFlag ('printEvent', options.printEvent)
150  GenObject.setGlobalFlag ('debug', options.debug)
151  GenObject.setGlobalFlag ('relative', options.relative)
152  GenObject.setGlobalFlag ('strictPairing', options.strictPairing)
153  if options.blur:
154  GenObject.setGlobalFlag ('blur', options.blur)
155  GenObject.setGlobalFlag ('blurRate', options.blurRate)
156  # take care of any 'double' options now
157  if options.tuple:
158  options.tuple1 = options.tuple2 = options.tuple
159  if options.file:
160  options.file1 = options.file2 = options.file
161  if options.numEvents:
162  options.numEvents1 = options.numEvents2 = options.numEvents
163  if options.compare:
164  # Compare two files
165  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
166  options.numEvents1)
167  chain2 = GenObject.prepareTuple (options.tuple2, options.file2,
168  options.numEvents2)
169  problems = \
170  GenObject.compareTwoTrees (chain1, chain2,
171  diffOutputName = options.compRoot)
172  print "Summary"
173  pprint.pprint (problems)
174  if options.saveAs:
175  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
176  options.numEvents1)
177  GenObject.saveTupleAs (chain1, options.saveAs)
178  if options.printTuple:
179  print "printing tuple"
180  GenObject.setGlobalFlag ('printEvent', True)
181  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
182  options.numEvents1)
183  GenObject.printTuple (chain1)
184  #GenObject.saveTupleAs (chain1, options.saveAs)
185  if options.printGlobal:
186  GenObject.printGlobal()
187  if options.interactive:
188  chain1 = chain2 = 0
189  if len (options.file1):
190  chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
191  options.numEvents1)
192  if len (options.file2):
193  chain2 = GenObject.prepareTuple (options.tuple2, options.file2)
194  #############################################
195  ## Load and save command line history when ##
196  ## running interactively. ##
197  #############################################
198  import os, readline
199  import atexit
200  historyPath = os.path.expanduser("~/.pyhistory")
201 
202  def save_history (historyPath=historyPath):
203  import readline
204  readline.write_history_file(historyPath)
205  if os.path.exists(historyPath):
206  readline.read_history_file(historyPath)
207 
208  atexit.register(save_history)
209  readline.parse_and_bind("set show-all-if-ambiguous on")
210  readline.parse_and_bind("tab: complete")
211  if os.path.exists (historyPath) :
212  readline.read_history_file(historyPath)
213  readline.set_history_length(-1)
214