CMS 3D CMS Logo

printTrackingNtuple.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import argparse
4 
5 import Validation.RecoTrack.plotting.ntuple as ntuple
6 
7 
8 def findEvent(ntpl, event):
9  eventId = event.split(":")
10  if len(eventId) != 3:
11  raise Exception("Malformed eventId %s, is not run:lumi:event" % eventId)
12  eventId = (int(eventId[0]), int(eventId[1]), int(eventId[2]))
13 
14  for ev in ntpl:
15  if ev.eventId() == eventId:
16  return ev
17 
18  raise Exception("Did not find event %s from file %s" % (eventId, ntpl.file().GetPath()))
19 
20 def main(opts):
21  if opts.track is None and opts.trackingParticle is None and opts.seed is None and opts.pixelHit is None and opts.stripHit is None:
22  return
23 
24  ntpl = ntuple.TrackingNtuple(opts.file)
25 
26  if opts.entry is not None:
27  event = ntpl.getEvent(opts.entry)
28  print event.eventIdStr()
29  elif opts.event is not None:
30  event = findEvent(ntpl, opts.event)
31  print "Entry %d" % event.entry()
32 
33  hasHits = ntpl.hasHits()
34  hasSeeds = ntpl.hasSeeds()
35 
36  if not hasSeeds and opts.seed is not None:
37  print "Ntuple %s does not have seeds saved!" % opts.file
38  return
39  if not hasHits and (opts.pixelHit is not None or opts.stripHit is not None):
40  print "Ntuple %s does not have hits saved!" % opts.file
41  return
42 
43  seedArgs = dict(hits=hasHits, bestMatchingTrackingParticle=hasHits)
44  trackArgs = dict(hits=hasHits, bestMatchingTrackingParticle=hasHits)
45  tpArgs = dict(hits=hasHits, bestMatchingTrack=hasHits)
46  if not hasSeeds:
47  trackArgs["seedPrinter"] = None
48  tpArgs["seedPrinter"] = None
49  elif not hasHits:
50  trackArgs["seedPrinter"] = ntuple.SeedPrinter(**seedArgs)
51  tpArgs["seedPrinter"] = ntuple.SeedPrinter(**seedArgs)
52 
53  printSeed = ntuple.SeedPrinter(trackingParticles=True, trackingParticlePrinter=ntuple.TrackingParticlePrinter(**tpArgs), **seedArgs)
54  printTrack = ntuple.TrackPrinter(trackingParticlePrinter=ntuple.TrackingParticlePrinter(**tpArgs), **trackArgs)
55  printTrackingParticle = ntuple.TrackingParticlePrinter(trackPrinter=ntuple.TrackPrinter(**trackArgs), **tpArgs)
56 
57  if opts.track is not None:
58  trk = event.tracks()[opts.track]
59  printTrack(trk)
60 
61  if opts.trackingParticle is not None:
62  tp = event.trackingParticles()[opts.trackingParticle]
63  printTrackingParticle(tp)
64 
65  if opts.seed is not None:
66  seeds = event.seeds()
67  if opts.seedIteration is not None:
68  seed = seeds.seedForAlgo(getattr(ntuple.Algo, opts.seedIteration), opts.seed)
69  else:
70  seed = seeds[opts.seed]
71  printSeed(seed)
72 
73  if opts.pixelHit is not None:
74  hit = event.pixelHits()[opts.pixelHit]
75  print "Pixel hit %d tracks" % opts.pixelHit
76  for t in hit.tracks():
77  printTrack(t)
78  if hasSeeds:
79  print "Pixel hit %d seeds" % opts.pixelHit
80  for t in hit.seeds():
81  printSeed(s)
82 
83  if opts.stripHit is not None:
84  hit = event.stripHits()[opts.stripHit]
85  print "Strip hit %d tracks" % opts.stripHit
86  for t in hit.tracks():
87  printTrack(t)
88  if hasSeeds:
89  print "Strip hit %d seeds" % opts.stripHit
90  for t in hit.seeds():
91  printSeed(s)
92 
93 
94 if __name__ == "__main__":
95  parser = argparse.ArgumentParser(description="Print information from a TrackingNtuple file")
96  parser.add_argument("file", type=str,
97  help="Input file")
98 
99  parser.add_argument("--entry", type=int,
100  help="Entry in a file to print information for (conflicts with --event)")
101  parser.add_argument("--event", type=str,
102  help="Event in a file to print information for, in a format run:lumi:event (conflicts with --entry)")
103 
104  parser.add_argument("--track", type=int,
105  help="Index of a track to print information for")
106  parser.add_argument("--trackingParticle", type=int,
107  help="Index of a TrackingParticle to print information for")
108  parser.add_argument("--seed", type=int,
109  help="Index of a seed to print information for. If --seedIteration is specified, the index is within the iteration. Without --seedIteration it is used as a global index.")
110  parser.add_argument("--seedIteration", type=str,
111  help="Seed iteration, used optionally with --seed")
112  parser.add_argument("--pixelHit", type=int,
113  help="Index of a pixel hit")
114  parser.add_argument("--stripHit", type=int,
115  help="Index of a strip hit")
116 
117  opts = parser.parse_args()
118 
119  if opts.entry is None and opts.event is None:
120  parser.error("Need either --entry or --event, neither was given")
121  if opts.entry is not None and opts.event is not None:
122  parser.error("--entry and --event conflict, please give only one of them")
123 
124  main(opts)
125 
126 
def findEvent(ntpl, event)
Definition: main.py:1