CMS 3D CMS Logo

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