CMS 3D CMS Logo

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