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 
0006 import Validation.RecoTrack.plotting.ntuple as ntuple
0007 
0008 
0009 def findEvent(ntpl, event):
0010     eventId = event.split(":")
0011     if len(eventId) != 3:
0012         raise Exception("Malformed eventId %s, is not run:lumi:event" % eventId)
0013     eventId = (int(eventId[0]), int(eventId[1]), int(eventId[2]))
0014 
0015     for ev in ntpl:
0016         if ev.eventId() == eventId:
0017             return ev
0018 
0019     raise Exception("Did not find event %s from file %s" % (eventId, ntpl.file().GetPath()))
0020 
0021 def main(opts):
0022     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:
0023         return
0024 
0025     ntpl = ntuple.TrackingNtuple(opts.file)
0026 
0027     if opts.entry is not None:
0028         event = ntpl.getEvent(opts.entry)
0029         print(event.eventIdStr())
0030     elif opts.event is not None:
0031         event = findEvent(ntpl, opts.event)
0032         print("Entry %d" % event.entry())
0033 
0034     hasHits = ntpl.hasHits()
0035     hasSeeds = ntpl.hasSeeds()
0036 
0037     if not hasSeeds and opts.seed is not None:
0038         print("Ntuple %s does not have seeds saved!" % opts.file)
0039         return
0040     if not hasHits and (opts.pixelHit is not None or opts.stripHit is not None):
0041         print("Ntuple %s does not have hits saved!" % opts.file)
0042         return
0043 
0044     seedArgs = dict(hits=hasHits, bestMatchingTrackingParticle=hasHits)
0045     trackArgs = dict(hits=hasHits, bestMatchingTrackingParticle=hasHits)
0046     tpArgs = dict(hits=hasHits, bestMatchingTrack=hasHits)
0047     if not hasSeeds:
0048         trackArgs["seedPrinter"] = None
0049         tpArgs["seedPrinter"] = None
0050     elif not hasHits:
0051         trackArgs["seedPrinter"] = ntuple.SeedPrinter(**seedArgs)
0052         tpArgs["seedPrinter"] = ntuple.SeedPrinter(**seedArgs)
0053 
0054     printSeed = ntuple.SeedPrinter(trackingParticles=True, trackingParticlePrinter=ntuple.TrackingParticlePrinter(**tpArgs), **seedArgs)
0055     printTrack = ntuple.TrackPrinter(trackingParticlePrinter=ntuple.TrackingParticlePrinter(**tpArgs), **trackArgs)
0056     printTrackingParticle = ntuple.TrackingParticlePrinter(trackPrinter=ntuple.TrackPrinter(**trackArgs), **tpArgs)
0057 
0058     if opts.track is not None:
0059         trks = event.tracks()
0060         if opts.track >= len(trks):
0061             print("You requested track %d, but this event has only %d tracks" % (opts.track, len(trks)))
0062             return
0063         trk = trks[opts.track]
0064         printTrack(trk)
0065 
0066     if opts.trackingParticle is not None:
0067         tps = event.trackingParticles()
0068         if opts.trackingParticle >= len(tps):
0069             print("You requested TrackingParticle %d, but this event has ony %d TrackingParticles" % (opts.trackingParticle, len(tps)))
0070             return
0071         tp = tps[opts.trackingParticle]
0072         printTrackingParticle(tp)
0073 
0074     if opts.seed is not None:
0075         seeds = event.seeds()
0076         if opts.seedIteration is not None:
0077             algo = getattr(ntuple.Algo, opts.seedIteration)
0078             if opts.seed >= seeds.nSeedsForAlgo(algo):
0079                 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)))
0080                 return
0081             seed = seeds.seedForAlgo(algo, opts.seed)
0082         else:
0083             if opts.seed >= len(seeds):
0084                 print("You requested seed %d, but this event has only %d seeds" % (opts.seed, len(seeds)))
0085                 return
0086             seed = seeds[opts.seed]
0087         printSeed(seed)
0088 
0089     if opts.pixelHit is not None:
0090         hits = event.pixelHits()
0091         if opts.pixelHit >= len(hits):
0092             print("You requested pixel hit %d, but this event has only %d pixel hits" % (opts.pixelHit, len(hits)))
0093             return
0094 
0095         hit = hits[opts.pixelHit]
0096         print("Pixel hit %d tracks" % opts.pixelHit)
0097         for t in hit.tracks():
0098             printTrack(t)
0099         if hasSeeds:
0100             print("Pixel hit %d seeds" % opts.pixelHit)
0101             for s in hit.seeds():
0102                 printSeed(s)
0103 
0104     if opts.stripHit is not None:
0105         hits = event.stripHits()
0106         if opts.stripHit >= len(hits):
0107             print("You requested strip hit %d, but this event has only %d strip hits" % (opts.stripHit, len(hits)))
0108             return
0109         hit = hits[opts.stripHit]
0110         print("Strip hit %d tracks" % opts.stripHit)
0111         for t in hit.tracks():
0112             printTrack(t)
0113         if hasSeeds:
0114             print("Strip hit %d seeds" % opts.stripHit)
0115             for s in hit.seeds():
0116                 printSeed(s)
0117 
0118 
0119 if __name__ == "__main__":
0120     parser = argparse.ArgumentParser(description="Print information from a TrackingNtuple file")
0121     parser.add_argument("file", type=str,
0122                         help="Input file")
0123 
0124     parser.add_argument("--entry", type=int,
0125                         help="Entry in a file to print information for (conflicts with --event)")
0126     parser.add_argument("--event", type=str,
0127                         help="Event in a file to print information for, in a format run:lumi:event (conflicts with --entry)")
0128 
0129     parser.add_argument("--track", type=int,
0130                         help="Index of a track to print information for")
0131     parser.add_argument("--trackingParticle", type=int,
0132                         help="Index of a TrackingParticle to print information for")
0133     parser.add_argument("--seed", type=int,
0134                         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.")
0135     parser.add_argument("--seedIteration", type=str,
0136                         help="Seed iteration, used optionally with --seed")
0137     parser.add_argument("--pixelHit", type=int,
0138                         help="Index of a pixel hit")
0139     parser.add_argument("--stripHit", type=int,
0140                         help="Index of a strip hit")
0141 
0142     opts = parser.parse_args()
0143 
0144     if opts.entry is None and opts.event is None:
0145         parser.error("Need either --entry or --event, neither was given")
0146     if opts.entry is not None and opts.event is not None:
0147         parser.error("--entry and --event conflict, please give only one of them")
0148 
0149     main(opts)
0150 
0151