CMS 3D CMS Logo

diffTrackingNtuple.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import argparse
4 import itertools
5 import collections
6 
7 import Validation.RecoTrack.plotting.ntuple as ntuple
8 
9 def body(opts, ev1, ev2, printTrack):
10  print ev1.eventIdStr()
11 
12  tracks1 = ev1.tracks()
13  tracks2 = ev2.tracks()
14 
15  singleTrack = (opts.track is not None)
16  if singleTrack:
17  tracks1 = [tracks1[opts.track]]
18 
19  diff = ntuple.diffTrackListsGeneric(printTrack, tracks1, tracks2, ignoreAdditionalLst2=singleTrack)
20  if diff.hasDifference():
21  print str(diff)
22  print
23 
24 def inOrder(opts, ntpl1, ntpl2, *args, **kwargs):
25  if opts.entry is not None:
26  ev1 = ntpl1.getEvent(opts.entry)
27  ev2 = ntpl2.getEvent(opts.entry)
28 
29  if ev1.eventId() != ev2.eventId():
30  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()))
31 
32  body(opts, ev1, ev2, *args, **kwargs)
33  return
34 
35  for i, (ev1, ev2) in enumerate(itertools.izip(ntpl1, ntpl2)):
36  if opts.maxEvents >= 0 and i >= opts.maxEvents:
37  break
38 
39  if ev1.eventId() != ev2.eventId():
40  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()))
41 
42  body(opts, ev1, ev2, *args, **kwargs)
43 
44 
45 def outOfOrder(opts, ntpl1, ntpl2, *args, **kwargs):
46  if opts.entry is not None:
47  raise Exception("--entry does not make sense with --outOfOrder")
48 
49  events2 = collections.OrderedDict()
50  for ev2 in ntpl2:
51  events2[ev2.eventIdStr()] = ev2.entry()
52 
53  for i, ev1 in enumerate(ntpl1):
54  if opts.maxEvents >= 0 and i >= opts.maxEvents:
55  break
56 
57  if not ev1.eventId() in events2:
58  print "-", ev1.eventIdStr()
59  continue
60 
61  ev2 = ntpl2.getEvent(events2[ev1.eventIdStr()])
62  events2.remove(ev1.eventId())
63 
64  body(opts, ev1, ev2, *args, **kwargs)
65 
66 
67  for eventIdStr in events2.iterkeys():
68  print "+", eventIdStr
69 
70 def main(opts):
71  ntpl1 = ntuple.TrackingNtuple(opts.file1)
72  ntpl2 = ntuple.TrackingNtuple(opts.file2)
73 
74  print "--- %s" % opts.file1
75  print "+++ %s" % opts.file2
76 
77  printTrack = ntuple.TrackPrinter(trackingParticlePrinter=ntuple.TrackingParticlePrinter(parentage=False), diffForTwiki=opts.twiki)
78 
79  if opts.outOfOrder:
80  outOfOrder(opts, ntpl1, ntpl2, printTrack)
81  else:
82  inOrder(opts, ntpl1, ntpl2, printTrack)
83 
84 if __name__ == "__main__":
85  parser = argparse.ArgumentParser(description="Unified diff of two TrackingNtuple files (clusters i.e. hits and TrackingParticles are assumed to be the same")
86  parser.add_argument("file1", type=str,
87  help="File1")
88  parser.add_argument("file2", type=str,
89  help="File2")
90  parser.add_argument("--outOfOrder", action="store_true",
91  help="Set this if events are in different order in the files")
92  parser.add_argument("--twiki", action="store_true",
93  help="Additional twiki-friendly diff formatting")
94  parser.add_argument("--maxEvents", type=int, default=-1,
95  help="Maximum number of events to process (default: -1 for all events)")
96  parser.add_argument("--entry", type=int, default=None,
97  help="Make diff only for this entry")
98  parser.add_argument("--track", type=int,
99  help="Make diff only for this track (indexing from FILE1; only if --entry is given)")
100 
101  opts = parser.parse_args()
102 
103  if opts.track is not None and opts.entry is None:
104  parser.error("With --track need --entry, which was not given")
105  main(opts)
def body(opts, ev1, ev2, printTrack)
Definition: main.py:1
def inOrder(opts, ntpl1, ntpl2, args, kwargs)
def outOfOrder(opts, ntpl1, ntpl2, args, kwargs)