Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:33:32

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import ROOT
0005 
0006 from Validation.RecoTrack.plotting.ntuple import *
0007 
0008 # The purpose of this file is to demonstrate mainly the links between
0009 # tracks, hits, seeds, and TrackingParticles.
0010 
0011 def main():
0012     ntuple = TrackingNtuple("trackingNtuple.root")
0013 
0014     tot_nevents = 0
0015     tot_pv_ntracks = 0
0016 
0017     tot_ntracks = 0
0018     tot_hptracks = 0
0019     tot_fakes = 0
0020     tot_fakes_ninvalidhits = 0
0021     tot_fakes_ninvalidhits_missing = 0
0022     tot_fakes_ninvalidhits_inactive = 0
0023     tot_fakes_npixhits = 0
0024     tot_fakes_nstrhits = 0
0025     tot_fakes_npixhits_true = 0
0026     tot_fakes_nstrhits_true = 0
0027     tot_fakes_npixhits_tps = 0
0028     tot_duplicates = 0
0029     tot_secondaries = 0
0030     tot_novertex = 0
0031 
0032     tot_tps = 0
0033     tot_recoed = 0
0034     tot_tp_dups = 0
0035     tot_tp_nonrecohits = 0.0
0036 
0037     tot_pix = 0
0038     tot_pix_ntracks = 0
0039     tot_pix_nseeds = 0
0040     tot_pix_eloss = 0.0
0041     tot_pix_signal = 0
0042     tot_pix_itpu = 0
0043     tot_pix_ootpu = 0
0044     tot_pix_noise = 0
0045     tot_str = 0
0046     tot_str_ntracks = 0
0047     tot_str_nseeds = 0
0048     tot_str_signal = 0
0049     tot_str_itpu = 0
0050     tot_str_ootpu = 0
0051     tot_str_noise = 0
0052     tot_glu = 0
0053     tot_glu_nseeds = 0
0054 
0055     tot_seeds = 0
0056     tot_seeds_true = 0
0057     tot_seeds_lowPtTriplet = 0
0058     tot_seeds_pixelhits = 0
0059     tot_seeds_striphits = 0
0060     tot_seeds_gluedhits = 0
0061     tot_seeds_notrack = 0
0062     tot_track_seeds_true = 0
0063 
0064     for event in ntuple:
0065         #print "Event", event.entry()
0066         tot_nevents += 1
0067 
0068         vertices = event.vertices()
0069         tot_pv_ntracks += vertices[0].nTracks()
0070 
0071         # links from TrackingParticles to tracks
0072         tps = event.trackingParticles()
0073         tot_tps += len(tps)
0074         neff = 0
0075         ndups = 0
0076         for tp in tps:
0077             if tp.nMatchedTracks() >= 1:
0078                 neff += 1
0079                 if tp.nMatchedTracks() > 1:
0080                     ndups += 1
0081 
0082                 # links from TrackingParticles to reco hits
0083                 #print "TP", tp.index()
0084                 #for hit in tp.hits():
0085                 #    print " %s %d x %f y %f tof %f" % (hit.layerStr(), hit.detId(), hit.x(), hit.y(), hit.tof())
0086 
0087             if ntuple.hasHits():
0088                 nhits = 0
0089                 nhits_noreco = 0
0090                 # links from TrackingParticles to SimHits
0091                 for hit in tp.simHits():
0092                     nhits += 1
0093                     # links from SimHits to RecHits
0094                     if hit.nRecHits() == 0:
0095                         nhits_noreco += 1
0096                 if nhits > 0:
0097                     tot_tp_nonrecohits += float(nhits_noreco)/nhits
0098 
0099         tot_recoed += neff
0100         tot_tp_dups += ndups
0101 
0102         # links from tracks to TrackingParticles
0103         tracks = event.tracks()
0104         ntracks = len(tracks) # also tracks.size() works
0105         tot_ntracks += ntracks
0106         nfakes = 0
0107         nfakes_invalidhits = 0
0108         nfakes_pixhits = 0
0109         nfakes_strhits = 0
0110         nfakes_pixhits_true = 0
0111         nfakes_strhits_true = 0
0112         nfakes_pixhits_tps = 0
0113         ndups = 0
0114         nsecondaries = 0
0115         for track in tracks:
0116             if track.isHP():
0117                 tot_hptracks += 1
0118 
0119             # link from track to vertex
0120             if not track.vertex().isValid():
0121                 tot_novertex += 1
0122 
0123             if track.nMatchedTrackingParticles() == 0:
0124                 #print "Track", track.index(), " is fake"
0125 
0126                 nfakes += 1
0127 
0128 
0129                 # links from tracks to hits
0130                 if ntuple.hasHits():
0131                     pix_simTrkIds = set()
0132 
0133                     #for hit in track.hits():
0134                     #    print hit.layerStr()
0135 
0136                     for hit in track.pixelHits():
0137                         nfakes_pixhits += 1
0138                         if hit.nSimHits() >= 1:
0139                             # links from hits to SimHits
0140                             nfakes_pixhits_true += 1
0141                             for simHit in hit.simHits():
0142                                 pix_simTrkIds.add(simHit.trackingParticle().index())
0143                     nfakes_pixhits_tps += len(pix_simTrkIds)
0144 
0145                     for hit in track.stripHits():
0146                         nfakes_strhits += 1
0147                         if hit.nSimHits() >= 1:
0148                             nfakes_strhits_true += 1
0149 
0150                     for hit in track.invalidHits():
0151                         tot_fakes_ninvalidhits += 1
0152                         if hit.type() == InvalidHit.Type.missing:
0153                             tot_fakes_ninvalidhits_missing += 1
0154                         elif hit.type() == InvalidHit.Type.inactive:
0155                             tot_fakes_ninvalidhits_inactive += 1
0156             else:
0157                 for tpInfo in track.matchedTrackingParticleInfos():
0158                     tp = tpInfo.trackingParticle()
0159                     if tp.nMatchedTracks() > 1:
0160                         ndups += 1
0161                         break
0162 
0163                     # TrackinParticle <-> TrackingVertex links
0164                     if tp.parentVertex().nSourceTrackingParticles() > 0:
0165                         nsecondaries += 1
0166 
0167         tot_fakes += nfakes
0168         tot_fakes_npixhits += nfakes_pixhits
0169         tot_fakes_nstrhits += nfakes_strhits
0170         tot_fakes_npixhits_true += nfakes_pixhits_true
0171         tot_fakes_nstrhits_true += nfakes_strhits_true
0172         tot_fakes_npixhits_tps += nfakes_pixhits_tps
0173         tot_duplicates += ndups
0174         tot_secondaries += nsecondaries
0175 
0176         # hits
0177         if ntuple.hasHits():
0178             # links from hits to tracks
0179             for hit in event.pixelHits():
0180                 tot_pix += 1
0181                 # hit -> track links
0182                 for track in hit.tracks():
0183                     tot_pix_ntracks += 1
0184 
0185                 if hit.simType() == HitSimType.Signal:
0186                     tot_pix_signal += 1
0187                 elif hit.simType() == HitSimType.ITPileup:
0188                     tot_pix_itpu += 1
0189                 elif hit.simType() == HitSimType.OOTPileup:
0190                     tot_pix_ootpu += 1
0191                 elif hit.simType() == HitSimType.Noise:
0192                     tot_pix_noise += 1
0193                 else:
0194                     raise Exception("Got unknown hit sim type value %d" % hit.simType())
0195 
0196                 # hit -> SimHit links
0197                 for simHit in hit.simHits():
0198                     tot_pix_eloss += simHit.eloss()
0199 
0200             for hit in event.stripHits():
0201                 tot_str += 1
0202                 # hit -> track links
0203                 for track in hit.tracks():
0204                     tot_str_ntracks += 1
0205 
0206                 if hit.simType() == HitSimType.Signal:
0207                     tot_str_signal += 1
0208                 elif hit.simType() == HitSimType.ITPileup:
0209                     tot_str_itpu += 1
0210                 elif hit.simType() == HitSimType.OOTPileup:
0211                     tot_str_ootpu += 1
0212                 elif hit.simType() == HitSimType.Noise:
0213                     tot_str_noise += 1
0214                 else:
0215                     raise Exception("Got unknown hit sim type value %d" % hit.simType())
0216 
0217             tot_glu += len(event.gluedHits())
0218 
0219             # hit -> seed links
0220             if ntuple.hasSeeds():
0221                 for hit in event.pixelHits():
0222                     for seed in hit.seeds():
0223                         tot_pix_nseeds += 1
0224                 for hit in event.stripHits():
0225                     for seed in hit.seeds():
0226                         tot_str_nseeds += 1
0227                 for hit in event.gluedHits():
0228                     for seed in hit.seeds():
0229                         tot_glu_nseeds += 1
0230 
0231         # seeds
0232         if ntuple.hasSeeds():
0233             seeds = event.seeds()
0234             nseeds = len(seeds)
0235             tot_seeds += nseeds
0236 
0237             # finding seeds of a particular iteration
0238             tot_seeds_lowPtTriplet += seeds.nSeedsForAlgo(5) # = lowPtTripletStep
0239 
0240             # links from seeds to TrackingParticles and tracks
0241             ntrue = 0
0242             for seed in seeds:
0243                 if seed.nMatchedTrackingParticles() >= 1:
0244                     ntrue += 1
0245 
0246                 if not seed.track().isValid():
0247                     tot_seeds_notrack += 1
0248             tot_seeds_true = ntrue
0249 
0250             # links from seeds to hits
0251             for seed in seeds:
0252                 for hit in seed.hits():
0253                     if isinstance(hit, PixelHit):
0254                         tot_seeds_pixelhits += 1
0255                     elif isinstance(hit, StripHit):
0256                         tot_seeds_striphits += 1
0257                     elif isinstance(hit, GluedHit):
0258                         tot_seeds_gluedhits += 1
0259 
0260             # links from tracks to seeds
0261             ntracktrue = 0
0262             for track in tracks:
0263                 seed = track.seed()
0264                 if seed.nMatchedTrackingParticles() >= 1:
0265                     ntracktrue += 1
0266             tot_track_seeds_true += ntracktrue
0267 
0268 
0269     print("Processed %d events" % tot_nevents)
0270     print("On average %f tracks from PV" % (float(tot_pv_ntracks)/tot_nevents))
0271     print("On average %f TrackingParticles" % (float(tot_tps)/tot_nevents))
0272     print(" with %f %% reconstructed" % (float(tot_recoed)/tot_tps * 100))
0273     print("  of which %f %% were reconstructed at least twice" % (float(tot_tp_dups)/tot_recoed * 100))
0274     print(" average fraction of non-recoed hits %f %%" % (tot_tp_nonrecohits/tot_tps * 100))
0275     print("On average %f tracks" % (float(tot_ntracks)/tot_nevents))
0276     print(" with %f %% being high purity" % (float(tot_hptracks)/tot_ntracks * 100))
0277     print(" with %f %% were not used in any vertex fit" % (float(tot_novertex)/tot_ntracks * 100))
0278     print(" with %f %% of true tracks being secondaries" % (float(tot_secondaries)/(tot_ntracks-tot_fakes) * 100))
0279     print(" with fake rate %f %%" % (float(tot_fakes)/tot_ntracks * 100))
0280     if tot_fakes_npixhits > 0:
0281         print("  on average %f %% of pixel hits are true" % (float(tot_fakes_npixhits_true)/tot_fakes_npixhits * 100))
0282         print("   pixel hits from %f TrackingParticles/track" % (float(tot_fakes_npixhits_tps)/tot_fakes))
0283         print("  on average %f %% of strip hits are true" % (float(tot_fakes_nstrhits_true)/tot_fakes_nstrhits * 100))
0284         print("  on average %f %% of hits are invalid" % (float(tot_fakes_ninvalidhits)/(tot_fakes_npixhits+tot_fakes_nstrhits) * 100))
0285         print("   of those, %f %% were missing" % (float(tot_fakes_ninvalidhits_missing)/tot_fakes_ninvalidhits * 100))
0286         print("   of those, %f %% were invalid" % (float(tot_fakes_ninvalidhits_inactive)/tot_fakes_ninvalidhits * 100))
0287     print(" with duplicate rate %f %%" % (float(tot_duplicates)/tot_ntracks * 100))
0288     if tot_seeds > 0:
0289         print(" of which %f %% had a true seed" % (float(tot_track_seeds_true)/tot_ntracks * 100))
0290         print("On average %f seeds" % (float(tot_seeds)/tot_nevents))
0291         print(" of which %f were from lowPtTripletStep" % (float(tot_seeds_lowPtTriplet)/tot_nevents))
0292         print(" of which %f %% were true" % (float(tot_seeds_true)/tot_seeds * 100))
0293         print(" of which %f %% did not produce a track" % (float(tot_seeds_notrack)/tot_seeds * 100))
0294         print(" on average %f pixel hits / seed" % (float(tot_seeds_pixelhits)/tot_seeds))
0295         print(" on average %f strip hits / seed" % (float(tot_seeds_striphits)/tot_seeds))
0296         print(" on average %f glued hits / seed" % (float(tot_seeds_gluedhits)/tot_seeds))
0297     if tot_pix > 0:
0298         print("On average %f pixel hits" % (float(tot_pix)/tot_nevents))
0299         print(" on average %f tracks per hit" % (float(tot_pix_ntracks)/tot_pix))
0300         print(" on average %f seeds per hit" % (float(tot_pix_nseeds)/tot_pix))
0301         print(" on average %f energy loss per hit" % (tot_pix_eloss/tot_pix))
0302         print(" on average %f %% from hard scatter" % (float(tot_pix_signal)/tot_pix*100))
0303         print(" on average %f %% from in-time pileup" % (float(tot_pix_itpu)/tot_pix*100))
0304         print(" on average %f %% from out-of-time pileup" % (float(tot_pix_ootpu)/tot_pix*100))
0305         print(" on average %f %% from noise" % (float(tot_pix_noise)/tot_pix*100))
0306         print("On average %f strip hits" % (float(tot_str)/tot_nevents))
0307         print(" on average %f tracks per hit" % (float(tot_str_ntracks)/tot_str))
0308         print(" on average %f seeds per hit" % (float(tot_str_nseeds)/tot_str))
0309         print(" on average %f %% from hard scatter" % (float(tot_str_signal)/tot_str*100))
0310         print(" on average %f %% from in-time pileup" % (float(tot_str_itpu)/tot_str*100))
0311         print(" on average %f %% from out-of-time pileup" % (float(tot_str_ootpu)/tot_str*100))
0312         print(" on average %f %% from noise" % (float(tot_str_noise)/tot_str*100))
0313         print("On average %f glued hits" % (float(tot_glu)/tot_nevents))
0314         print(" on average %f seeds per hit" % (float(tot_glu_nseeds)/tot_glu))
0315 
0316 
0317 if __name__ == "__main__":
0318     main()