Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:40

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