Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:34:05

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import argparse
0005 import itertools
0006 import collections
0007 
0008 import Validation.RecoTrack.plotting.ntuple as ntuple
0009 
0010 def body(opts, ev1, ev2, printTrack):
0011     print(ev1.eventIdStr())
0012 
0013     tracks1 = ev1.tracks()
0014     tracks2 = ev2.tracks()
0015 
0016     singleTrack = (opts.track is not None)
0017     if singleTrack:
0018         tracks1 = [tracks1[opts.track]]
0019 
0020     diff = ntuple.diffTrackListsGeneric(printTrack, tracks1, tracks2, ignoreAdditionalLst2=singleTrack)
0021     if diff.hasDifference():
0022         print(str(diff))
0023         print()
0024 
0025 def inOrder(opts, ntpl1, ntpl2, *args, **kwargs):
0026     if opts.entry is not None:
0027         ev1 = ntpl1.getEvent(opts.entry)
0028         ev2 = ntpl2.getEvent(opts.entry)
0029 
0030         if ev1.eventId() != ev2.eventId():
0031             raise Exception("Events are out of order, entry %d file1 has %s and file %s. Use --outOfOrder option instead." % (ev1.entry(), ev1.eventIdStr(), ev2.eventIdStr()))
0032 
0033         body(opts, ev1, ev2, *args, **kwargs)
0034         return
0035 
0036     for i, (ev1, ev2) in enumerate(itertools.izip(ntpl1, ntpl2)):
0037         if opts.maxEvents >= 0 and i >= opts.maxEvents:
0038             break
0039 
0040         if ev1.eventId() != ev2.eventId():
0041             raise Exception("Events are out of order, entry %d file1 has %s and file %s. Use --outOfOrder option instead." % (ev1.entry(), ev1.eventIdStr(), ev2.eventIdStr()))
0042 
0043         body(opts, ev1, ev2, *args, **kwargs)
0044 
0045 
0046 def outOfOrder(opts, ntpl1, ntpl2, *args, **kwargs):
0047     if opts.entry is not None:
0048         raise Exception("--entry does not make sense with --outOfOrder")
0049 
0050     events2 = collections.OrderedDict()
0051     for ev2 in ntpl2:
0052         events2[ev2.eventIdStr()] = ev2.entry()
0053 
0054     for i, ev1 in enumerate(ntpl1):
0055         if opts.maxEvents >= 0 and i >= opts.maxEvents:
0056             break
0057 
0058         if not ev1.eventId() in events2:
0059             print("-", ev1.eventIdStr())
0060             continue
0061 
0062         ev2 = ntpl2.getEvent(events2[ev1.eventIdStr()])
0063         events2.remove(ev1.eventId())
0064 
0065         body(opts, ev1, ev2, *args, **kwargs)
0066 
0067 
0068     for eventIdStr in events2.iterkeys():
0069         print("+", eventIdStr)
0070 
0071 def main(opts):
0072     ntpl1 = ntuple.TrackingNtuple(opts.file1)
0073     ntpl2 = ntuple.TrackingNtuple(opts.file2)
0074 
0075     print("--- %s" % opts.file1)
0076     print("+++ %s" % opts.file2)
0077 
0078     printTrack = ntuple.TrackPrinter(trackingParticlePrinter=ntuple.TrackingParticlePrinter(parentage=False), diffForTwiki=opts.twiki)
0079 
0080     if opts.outOfOrder:
0081         outOfOrder(opts, ntpl1, ntpl2, printTrack)
0082     else:
0083         inOrder(opts, ntpl1, ntpl2, printTrack)
0084 
0085 if __name__ == "__main__":
0086     parser = argparse.ArgumentParser(description="Unified diff of two TrackingNtuple files (clusters i.e. hits and TrackingParticles are assumed to be the same")
0087     parser.add_argument("file1", type=str,
0088                         help="File1")
0089     parser.add_argument("file2", type=str,
0090                         help="File2")
0091     parser.add_argument("--outOfOrder", action="store_true",
0092                         help="Set this if events are in different order in the files")
0093     parser.add_argument("--twiki", action="store_true",
0094                         help="Additional twiki-friendly diff formatting")
0095     parser.add_argument("--maxEvents", type=int, default=-1,
0096                         help="Maximum number of events to process (default: -1 for all events)")
0097     parser.add_argument("--entry", type=int, default=None,
0098                         help="Make diff only for this entry")
0099     parser.add_argument("--track", type=int,
0100                         help="Make diff only for this track (indexing from FILE1; only if --entry is given)")
0101 
0102     opts = parser.parse_args()
0103 
0104     if opts.track is not None and opts.entry is None:
0105         parser.error("With --track need --entry, which was not given")
0106     main(opts)