3 from __future__
import print_function
4 from builtins
import range
14 from DataFormats.FWLite
import Events, Handle
16 typeMap = {
'double' : [
'double',
'vector<double>'],
17 'int' : [
'int',
'vector<int>'],}
21 Special exception for a product not in file
31 isSimpleObject = (handleName.find(
'vector') == -1)
34 aux1 = event1.eventAuxiliary()
35 aux2 = event2.eventAuxiliary()
37 rle1 = (aux1.run(), aux1.luminosityBlock(), aux1.event())
38 rle2 = (aux2.run(), aux2.luminosityBlock(), aux2.event())
40 logging.debug(
"Comparing RLE #'s %s and %s" % (rle1, rle2))
43 raise RuntimeError(
"Run/Lumi/Events don't match: %s vs %s" % (rle1, rle2))
44 handle1 =
Handle(handleName)
45 handle2 =
Handle(handleName)
47 if event1.getByLabel(label, handle1)
and event2.getByLabel(label, handle2):
48 objects1 = handle1.product()
49 objects2 = handle1.product()
51 raise ProductNotFoundError(
"Product %s %s not found." % (handleName, label))
56 if options.blurRate
and options.blur
and random.random() < options.blurRate:
58 val1 += (random.random()-0.5) * options.blur
60 logging.error(
"Mismatch %s and %s in %s" % (val1, val2, aux2.event()))
63 logging.debug(
"Match of %s in %s" % (objects1[0], aux2.event()))
68 for val1, val2
in itertools.izip_longest(objects1, objects2):
70 if options.blurRate
and options.blur
and random.random() < options.blurRate:
72 val1 += (random.random()-0.5) * options.blur * val1
75 logging.error(
"Comparison problem %s != %s" % (val1, val2))
76 logging.debug(
"Compared %s elements" % count)
77 return (count, mismatch)
79 if __name__ ==
"__main__":
86 logging.basicConfig(level=logging.INFO)
88 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.")
89 modeGroup = optparse.OptionGroup (parser,
"Mode Conrols")
90 tupleGroup = optparse.OptionGroup (parser,
"Tuple Controls")
91 optionsGroup = optparse.OptionGroup (parser,
"Options")
93 modeGroup.add_option (
'--compare', dest=
'compare', action=
'store_true',
94 help=
'Compare tuple1 to tuple2')
96 tupleGroup.add_option (
'--numEvents', dest=
'numEvents', type=
'int',
98 help=
"number of events for first and second file")
100 tupleGroup.add_option (
'--label', dest=
'label', type=
'string',
102 help=
"Change label ('tuple^object^label')")
104 optionsGroup.add_option (
'--blur1', dest=
'blur', type=
'float',
106 help=
"Randomly changes values by 'BLUR' " +\
107 "from tuple1. For debugging only.")
108 optionsGroup.add_option (
'--blurRate', dest=
'blurRate', type=
'float',
110 help=
"Rate at which objects will be changed. " + \
111 "(%default default)")
113 parser.add_option_group (modeGroup)
114 parser.add_option_group (tupleGroup)
115 parser.add_option_group (optionsGroup)
116 (options, args) = parser.parse_args()
119 parser.error(
"Too many or too few arguments")
120 options.config = args[0]
121 options.file1 = args[1]
122 options.file2 = args[2]
125 tName, objName, lName = options.label[0].
split(
'^')
126 label = lName.split(
',')
128 ROOT.gROOT.SetBatch()
130 ROOT.gSystem.Load(
"libFWCoreFWLite.so")
131 ROOT.gSystem.Load(
"libDataFormatsFWLite.so")
132 ROOT.FWLiteEnabler.enable()
134 chain1 = Events ([options.file1], forceEvent=
True)
135 chain2 = Events ([options.file2], forceEvent=
True)
137 if chain1.size() != chain1.size():
138 raise RuntimeError(
"Files have different #'s of events")
139 numEvents =
min(options.numEvents, chain1.size())
146 for handleName
in typeMap[objName]:
150 logging.info(
"Testing identity for handle=%s, label=%s" % (handleName, label))
152 for ev1, ev2, count
in itertools.izip(chain1, chain2,
range(numEvents)):
153 evCount, evMismatch =
compareEvents(event1=ev1, event2=ev2, handleName=handleName, label=label, options=options)
154 totalCount += evCount
155 mismatches += evMismatch
156 logging.info(
"Compared %s events" % (count+1))
157 productsCompared += 1
159 plagerDict = {
'eventsCompared' : count+1}
160 plagerDict.update({
'count_%s' % objName : totalCount})
162 plagerDict.update({objName: {
'_var' : {handleName:mismatches}}})
164 pprint.pprint(plagerDict)
165 except ProductNotFoundError:
166 logging.info(
"No product found for handle=%s, label=%s" % (handleName, label))
168 logging.info(
"Total products compared: %s, %s/%s" % (productsCompared, mismatches, totalCount))
170 if not productsCompared:
171 print(
"Plager compatible message: not able to get any products")