12 from DataFormats.FWLite
import Events, Handle
14 typeMap = {
'double' : [
'double',
'vector<double>'],
15 'int' : [
'int',
'vector<int>'],}
19 Special exception for a product not in file
29 isSimpleObject = (handleName.find(
'vector') == -1)
32 aux1 = event1.eventAuxiliary()
33 aux2 = event2.eventAuxiliary()
35 rle1 = (aux1.run(), aux1.luminosityBlock(), aux1.event())
36 rle2 = (aux2.run(), aux2.luminosityBlock(), aux2.event())
38 logging.debug(
"Comparing RLE #'s %s and %s" % (rle1, rle2))
41 raise RuntimeError(
"Run/Lumi/Events don't match: %s vs %s" % (rle1, rle2))
42 handle1 =
Handle(handleName)
43 handle2 =
Handle(handleName)
45 if event1.getByLabel(label, handle1)
and event2.getByLabel(label, handle2):
46 objects1 = handle1.product()
47 objects2 = handle1.product()
49 raise ProductNotFoundError(
"Product %s %s not found." % (handleName, label))
54 if options.blurRate
and options.blur
and random.random() < options.blurRate:
56 val1 += (random.random()-0.5) * options.blur
58 logging.error(
"Mismatch %s and %s in %s" % (val1, val2, aux2.event()))
61 logging.debug(
"Match of %s in %s" % (objects1[0], aux2.event()))
66 for val1, val2
in itertools.izip_longest(objects1, objects2):
68 if options.blurRate
and options.blur
and random.random() < options.blurRate:
70 val1 += (random.random()-0.5) * options.blur * val1
73 logging.error(
"Comparison problem %s != %s" % (val1, val2))
74 logging.debug(
"Compared %s elements" % count)
75 return (count, mismatch)
77 if __name__ ==
"__main__":
84 logging.basicConfig(level=logging.INFO)
86 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.")
87 modeGroup = optparse.OptionGroup (parser,
"Mode Conrols")
88 tupleGroup = optparse.OptionGroup (parser,
"Tuple Controls")
89 optionsGroup = optparse.OptionGroup (parser,
"Options")
91 modeGroup.add_option (
'--compare', dest=
'compare', action=
'store_true',
92 help=
'Compare tuple1 to tuple2')
94 tupleGroup.add_option (
'--numEvents', dest=
'numEvents', type=
'int',
96 help=
"number of events for first and second file")
98 tupleGroup.add_option (
'--label', dest=
'label', type=
'string',
100 help=
"Change label ('tuple^object^label')")
102 optionsGroup.add_option (
'--blur1', dest=
'blur', type=
'float',
104 help=
"Randomly changes values by 'BLUR' " +\
105 "from tuple1. For debugging only.")
106 optionsGroup.add_option (
'--blurRate', dest=
'blurRate', type=
'float',
108 help=
"Rate at which objects will be changed. " + \
109 "(%default default)")
111 parser.add_option_group (modeGroup)
112 parser.add_option_group (tupleGroup)
113 parser.add_option_group (optionsGroup)
114 (options, args) = parser.parse_args()
117 parser.error(
"Too many or too few arguments")
118 options.config = args[0]
119 options.file1 = args[1]
120 options.file2 = args[2]
123 tName, objName, lName = options.label[0].
split(
'^')
124 label = lName.split(
',')
126 ROOT.gROOT.SetBatch()
128 ROOT.gSystem.Load(
"libFWCoreFWLite.so")
129 ROOT.gSystem.Load(
"libDataFormatsFWLite.so")
130 ROOT.AutoLibraryLoader.enable()
132 chain1 = Events ([options.file1], forceEvent=
True)
133 chain2 = Events ([options.file2], forceEvent=
True)
135 if chain1.size() != chain1.size():
136 raise RuntimeError(
"Files have different #'s of events")
137 numEvents =
min(options.numEvents, chain1.size())
144 for handleName
in typeMap[objName]:
148 logging.info(
"Testing identity for handle=%s, label=%s" % (handleName, label))
150 for ev1, ev2, count
in itertools.izip(chain1, chain2, xrange(numEvents)):
151 evCount, evMismatch =
compareEvents(event1=ev1, event2=ev2, handleName=handleName, label=label, options=options)
152 totalCount += evCount
153 mismatches += evMismatch
154 logging.info(
"Compared %s events" % (count+1))
155 productsCompared += 1
157 plagerDict = {
'eventsCompared' : count+1}
158 plagerDict.update({
'count_%s' % objName : totalCount})
160 plagerDict.update({objName: {
'_var' : {handleName:mismatches}}})
162 pprint.pprint(plagerDict)
163 except ProductNotFoundError:
164 logging.info(
"No product found for handle=%s, label=%s" % (handleName, label))
166 logging.info(
"Total products compared: %s, %s/%s" % (productsCompared, mismatches, totalCount))
168 if not productsCompared:
169 print "Plager compatible message: not able to get any products"