CMS 3D CMS Logo

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