Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:34:06

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 from builtins import range
0005 import ROOT
0006 from array import array
0007 
0008 from Validation.RecoTrack.plotting.ntuple import *
0009 import analysis
0010 
0011 from math import sqrt, copysign, sin, cos, pi
0012 
0013 class EventPlotter(object):
0014 
0015     def __init__(self):
0016         self.plots_2D = []
0017     self.plots_3D = []
0018     c = ROOT.TColor()
0019         self.colors_G = [c.GetColor(0,255,0), c.GetColor(0,185,0), c.GetColor(50,255,50), \
0020                    c.GetColor(0,100,0), c.GetColor(50,155,0), c.GetColor(0,70,155), \
0021                c.GetColor(0,255,0), c.GetColor(0,255,0), c.GetColor(0,255,0)]
0022     self.colors_B = [c.GetColor(0,0,255), c.GetColor(0,0,155), c.GetColor(50,50,255), \
0023                    c.GetColor(0,0,80), c.GetColor(50,0,155), c.GetColor(0,70,155), \
0024                c.GetColor(0,0,255), c.GetColor(0,0,255), c.GetColor(0,0,255)]
0025     
0026 
0027     def Reset(self):
0028         self.plots_2D = []
0029     self.plots_3D = []
0030  
0031     def PlotEvent3DHits(self, event, flag="PSG"):
0032     '''
0033     Plots the 3D hits of an event.
0034     flag is an string which defines which hit types to plot
0035     (p for pixel, s for strip, g for glued)
0036     ''' 
0037     if('p' in flag or 'P' in flag):
0038         pixel_hits = event.pixelHits()
0039         pix_coords = []
0040         for hit in pixel_hits:
0041         pix_coords.append(hit.x())
0042         pix_coords.append(hit.y())
0043         pix_coords.append(hit.z())
0044         pix_plot = ROOT.TPolyMarker3D(len(pix_coords)/3, array('f', pix_coords), 1)
0045         pix_plot.SetMarkerColor(4)
0046 
0047     if('s' in flag or 'S' in flag):
0048         strip_hits = event.stripHits()
0049         str_coords = []
0050         for hit in strip_hits:
0051         str_coords.append(hit.x())
0052         str_coords.append(hit.y())
0053         str_coords.append(hit.z())
0054         str_plot = ROOT.TPolyMarker3D(len(str_coords)/3, array('f', str_coords), 1)
0055         str_plot.SetMarkerColor(2)
0056 
0057     if('g' in flag or 'G' in flag):
0058         glued_hits = event.gluedHits()
0059         glu_coords = []
0060         for hit in glued_hits:
0061         glu_coords.append(hit.x())
0062         glu_coords.append(hit.y())
0063         glu_coords.append(hit.z())
0064         glu_plot = ROOT.TPolyMarker3D(len(glu_coords)/3, array('f', glu_coords), 1)
0065         glu_plot.SetMarkerColor(3)        
0066         
0067     if('p' in flag or 'P' in flag): self.plots_3D.append(pix_plot)
0068     if('s' in flag or 'S' in flag): self.plots_3D.append(str_plot)
0069     if('g' in flag or 'G' in flag): self.plots_3D.append(glu_plot)  
0070 
0071     def PlotXY(self, event, limits=[-1000,1000], flag="PSG"):
0072     '''
0073     Plots the hits of an event in an XY plane.
0074     flag is an string which defines which hit types to plot
0075     (p for pixel, s for strip, g for glued)
0076     '''
0077     #ntuple.tree().GetBranch("pix_x")
0078     if('p' in flag or 'P' in flag):
0079         pixel_hits = event.pixelHits()
0080         pix_x = []
0081         pix_y = []
0082         for hit in pixel_hits:
0083         if(limits[0] < hit.z() < limits[1]):
0084             pix_x.append(hit.x())
0085             pix_y.append(hit.y())
0086         pix_plot = ROOT.TGraph(len(pix_x), array('f', pix_x), array('f', pix_y))
0087         pix_plot.SetMarkerColor(4)
0088 
0089     if('s' in flag or 'S' in flag):
0090         strip_hits = event.stripHits()
0091         str_x = []
0092         str_y = []
0093         for hit in strip_hits:
0094         if(limits[0] < hit.z() < limits[1]):
0095             str_x.append(hit.x())
0096             str_y.append(hit.y())
0097         str_plot = ROOT.TGraph(len(str_x), array('f', str_x), array('f', str_y))
0098         str_plot.SetMarkerColor(2)
0099 
0100     if('g' in flag or 'G' in flag):
0101         glued_hits = event.gluedHits()
0102         glu_x = []
0103         glu_y = []
0104         for hit in glued_hits:
0105         if(limits[0] < hit.z() < limits[1]):
0106             glu_x.append(hit.x())
0107             glu_y.append(hit.y())
0108         glu_plot = ROOT.TGraph(len(glu_x), array('f', glu_x), array('f', glu_y))
0109         glu_plot.SetMarkerColor(3)        
0110 
0111     plot = ROOT.TMultiGraph()
0112 
0113     if('p' in flag or 'P' in flag): plot.Add(pix_plot,"P")
0114     if('s' in flag or 'S' in flag): plot.Add(str_plot,"P")
0115     if('g' in flag or 'G' in flag): plot.Add(glu_plot,"P")
0116     self.plots_2D.append(plot)
0117 
0118     def PlotZY(self, event, limits=[-1000,1000], flag="PSG"):
0119     '''
0120     Plots the hits of an event in an ZR plane.
0121     flag is an string which defines which hit types to plot
0122     (p for pixel, s for strip, g for glued)
0123     '''
0124     #ntuple.tree().GetBranch("pix_x")
0125     if('p' in flag or 'P' in flag):
0126         pixel_hits = event.pixelHits()
0127         pix_z = []
0128         pix_y = []
0129         for hit in pixel_hits:
0130         if(limits[0] < hit.z() < limits[1]):
0131             pix_z.append(hit.z())
0132             pix_y.append(hit.y())
0133         pix_plot = ROOT.TGraph(len(pix_z), array('f', pix_z), array('f', pix_y))
0134         pix_plot.SetMarkerColor(4)
0135 
0136     if('s' in flag or 'S' in flag):
0137         strip_hits = event.stripHits()
0138         str_z = []
0139         str_y = []
0140         for hit in strip_hits:
0141         if(limits[0] < hit.z() < limits[1]):
0142             str_z.append(hit.z())
0143             str_y.append(hit.y())
0144         str_plot = ROOT.TGraph(len(str_z), array('f', str_z), array('f', str_y))
0145         str_plot.SetMarkerColor(2)
0146 
0147     if('g' in flag or 'G' in flag):
0148         glued_hits = event.gluedHits()
0149         glu_z = []
0150         glu_y = []
0151         for hit in glued_hits:
0152         if(limits[0] < hit.z() < limits[1]):
0153             glu_z.append(hit.z())
0154             glu_y.append(hit.y())
0155         glu_plot = ROOT.TGraph(len(glu_z), array('f', glu_z), array('f', glu_y))
0156         glu_plot.SetMarkerColor(3)        
0157 
0158     plot = ROOT.TMultiGraph()
0159 
0160     if('p' in flag or 'P' in flag): plot.Add(pix_plot,"P")
0161     if('s' in flag or 'S' in flag): plot.Add(str_plot,"P")
0162     if('g' in flag or 'G' in flag): plot.Add(glu_plot,"P")
0163     self.plots_2D.append(plot)
0164 
0165     def PlotTracksXY(self, tracks):
0166     plot = ROOT.TMultiGraph()
0167 
0168     for track in tracks:
0169         X = []; Y = [];
0170         for hit in track.hits():
0171         if(hit.isValid()):
0172             X.append(hit.x())
0173             Y.append(hit.y())
0174         plot.Add(ROOT.TGraph(len(X),array("f",X),array("f",Y)),"L")
0175 
0176     self.plots_2D.append(plot)
0177 
0178     def PlotTracksZY(self, tracks):
0179     plot = ROOT.TMultiGraph()
0180 
0181     for track in tracks:
0182         Y = []; Z = [];
0183         for hit in track.hits():
0184         if(hit.isValid()):
0185             Y.append(hit.y())
0186             Z.append(hit.z())
0187         plot.Add(ROOT.TGraph(len(Z),array("f",Z),array("f",Y)),"L")
0188 
0189     self.plots_2D.append(plot)    
0190    
0191     def PlotTracks3D(self, tracks): 
0192     for track in tracks:
0193         X = []; Y = []; Z = [];
0194         for hit in track.hits():
0195         if(hit.isValid()):
0196             X.append(hit.x())
0197             Y.append(hit.y())
0198             Z.append(hit.z())   
0199         if(X):
0200         self.plots_3D.append(ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z)))
0201     
0202     def PlotPixelTracks3D(self, tracks): 
0203     for track in tracks:
0204         X = []; Y = []; Z = [];
0205         for hit in track.pixelHits():
0206         if(hit.isValid()):
0207             X.append(hit.x())
0208             Y.append(hit.y())
0209             Z.append(hit.z())
0210         if(X):
0211         self.plots_3D.append(ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z)))
0212 
0213     def PlotPixelGluedTracks3D(self, tracks):
0214     for track in tracks:
0215         X = []; Y = []; Z = [];
0216         for hit in track.hits():
0217         if(hit.isValid() and hit.hitType != 1):
0218             X.append(hit.x())
0219             Y.append(hit.y())
0220             Z.append(hit.z())
0221         if(X):
0222         self.plots_3D.append(ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z)))
0223 
0224     def PlotTrack3D(self, track, color = 1): 
0225     '''Plots a single track and prints track info'''
0226     # Not so hasardous experimental edit:
0227     #hits = sorted([hit for hit in track.hits()], key = lambda hit: hit.index())
0228     #print [hit.index() for hit in hits]
0229     X = []; Y = []; Z = [];
0230     for hit in track.hits(): #hits: #track.hits():
0231         if(hit.isValid()):
0232         X.append(hit.x())
0233         Y.append(hit.y())
0234         Z.append(hit.z())   
0235     if(not X):
0236         print("Track has no valid points")
0237         return
0238     plot = ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z))
0239     plot.SetLineColor(color)
0240     self.plots_3D.append(plot)
0241 
0242     '''
0243     print "Track parameters:"
0244     print "px  : " + str(track.px())
0245     print "py  : " + str(track.py())
0246     print "pz  : " + str(track.pz())
0247     print "pt  : " + str(track.pt())
0248     print "eta : " + str(track.eta())
0249     print "phi : " + str(track.phi())
0250     print "dxy : " + str(track.dxy())
0251     print "dz  : " + str(track.dz())
0252     print "q   : " + str(track.q())
0253     '''
0254 
0255     def TrackHelix(self, track, color = 1, style = 0):
0256     '''Creates a THelix object which can be plotted with Draw() method.'''
0257     if isinstance(track, TrackingParticle):
0258         phi = track.pca_phi()
0259         dxy = track.pca_dxy()
0260         dz = track.pca_dz()
0261     else:
0262         phi = track.phi()
0263         dxy = track.dxy()
0264         dz = track.dz()
0265     xyz = array("d", [-dxy*ROOT.TMath.Sin(phi), dxy*ROOT.TMath.Cos(phi), dz])
0266     v = array("d", [track.px(), track.py(), track.pz()])
0267     w = 0.3*3.8*track.q()*0.01 #Angular frequency = 0.3*B*q*hattuvakio, close enough
0268     z_last = dz
0269     for hit in track.hits(): 
0270         if(hit.isValidHit()): z_last = hit.z()
0271     
0272     helix = ROOT.THelix(xyz, v, w, array("d", [dz, z_last]))
0273     helix.SetLineColor(color)
0274     if style == 1: helix.SetLineStyle(9)
0275     if style == 2: helix.SetLineStyle(7)
0276     return helix
0277 
0278     def Plot3DHelixes(self, tracks, color = 1, style = 0):  
0279     for track in tracks:
0280         if(track.hits()):
0281         self.plots_3D.append(self.TrackHelix(track, color, style))  
0282 
0283     def Plot3DHelix(self, track, color = 1, style = 0): 
0284     if(track.hits()):
0285         self.plots_3D.append(self.TrackHelix(track, color, style))
0286 
0287     def Plot3DHits(self, track, color = 1, style = 0):
0288     '''
0289     Plots the 3D hits from a track.
0290     ''' 
0291     pix_coords = []
0292     for hit in track.pixelHits():    
0293         if hit.isValid():
0294         pix_coords.append(hit.x())
0295         pix_coords.append(hit.y())
0296         pix_coords.append(hit.z())
0297         if pix_coords:
0298         pix_plot = ROOT.TPolyMarker3D(len(pix_coords)/3, array('f', pix_coords), 2)
0299         pix_plot.SetMarkerColor(color)
0300         if style == 1: pix_plot.SetMarkerStyle(5)
0301         if style == 2: pix_plot.SetMarkerStyle(4)
0302         self.plots_3D.append(pix_plot)
0303     
0304     for hit in track.gluedHits():
0305         if hit.isValid():
0306         x = hit.x(); y = hit.y(); z = hit.z()
0307         if hit.isBarrel():
0308             X = [x, x]
0309             Y = [y, y]
0310             Z = [z - sqrt(hit.zz()), z + sqrt(hit.zz())]
0311         else:
0312             X = [x - copysign(sqrt(hit.xx()),x), x + copysign(sqrt(hit.xx()),x)]
0313             Y = [y - copysign(sqrt(hit.yy()),y), y + copysign(sqrt(hit.yy()),y)]
0314             Z = [hit.z(), hit.z()]
0315         glu_plot = ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z))
0316         #glu_plot.SetLineStyle(2)    
0317         if style == 1: glu_plot.SetLineStyle(2)
0318         if style == 2: glu_plot.SetLineStyle(3)
0319         glu_plot.SetLineColor(color) 
0320         self.plots_3D.append(glu_plot)
0321 
0322     for hit in track.stripHits():
0323         if hit.isValid():
0324         x = hit.x(); y = hit.y(); z = hit.z()
0325         if hit.isBarrel():
0326             X = [x, x]
0327             Y = [y, y]
0328             Z = [z - 1.5*sqrt(hit.zz()), z + 1.5*sqrt(hit.zz())]
0329         else:
0330             X = [x - 1.5*copysign(sqrt(hit.xx()),x), x + 1.5*copysign(sqrt(hit.xx()),x)]
0331             Y = [y - 1.5*copysign(sqrt(hit.yy()),y), y + 1.5*copysign(sqrt(hit.yy()),y)]
0332             Z = [hit.z(), hit.z()]
0333         str_plot = ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z))
0334         if style == 1: str_plot.SetLineStyle(2)
0335         if style == 2: str_plot.SetLineStyle(3)
0336         str_plot.SetLineColor(color) 
0337         self.plots_3D.append(str_plot)
0338 
0339     def PlotVertex3D(self, vertex, color=1):
0340     plot = ROOT.TPolyMarker3D(1, array('f', [vertex.x(), vertex.y(), vertex.z()]),3)
0341     plot.SetMarkerColor(color)
0342         self.plots_3D.append(plot)
0343 
0344     def PlotPoint3D(self, point, color=1):
0345     plot = ROOT.TPolyMarker3D(1, array('f', [point[0], point[1], point[2]]),3)
0346     plot.SetMarkerColor(color)
0347         self.plots_3D.append(plot)
0348 
0349     def PlotTrackingFail(self, match):
0350     X = array('f', [match.last_loc[0], match.fail_loc[0]])
0351     Y = array('f', [match.last_loc[1], match.fail_loc[1]])
0352     Z = array('f', [match.last_loc[2], match.fail_loc[2]])
0353         plot = ROOT.TPolyLine3D(2, X, Y, Z)
0354     plot.SetLineWidth(3)
0355     plot.SetLineColor(2) 
0356         self.plots_3D.append(plot)
0357     self.PlotPoint3D(match.last_loc, 2)
0358 
0359     def PlotFakes_MatchedRecos(self, event, iterative = 1, reconstructed = 0):
0360         fakes = analysis.FindFakes(event)
0361     if iterative:
0362             # Plot fakes one by one
0363         for fake in fakes:
0364         self.Reset()
0365         self.Plot3DHelixes([fake],2)
0366         self.Plot3DHits(fake, 2)
0367 
0368                 # Plot real particle tracks which include fake tracks hits
0369         icol = 0
0370         particle_inds = []
0371         particles = []
0372         reco_inds = []
0373         recos = []
0374         for hit in fake.hits():
0375             if hit.isValid() and hit.nSimHits() >= 0:
0376             for simHit in hit.simHits():
0377                 particle = simHit.trackingParticle()
0378                 if particle.index() not in particle_inds:
0379                 particle_inds.append(particle.index())
0380                 particles.append(particle)
0381 
0382                 '''
0383                 self.Plot3DHelix(particle, ROOT.TColor().GetColor(0,255,0), 1) # kAzure color, maybe ;)
0384                 self.Plot3DHits(particle, 3+icol, 1) 
0385                 icol += 1
0386 
0387                 print "Number of matched tracks to real particle: " + str(particle.nMatchedTracks())
0388                 '''
0389                             # Plot reconstructed tracks of these real particle tracks
0390                 if reconstructed and particle.nMatchedTracks() > 0:
0391                 for info in particle.matchedTrackInfos():
0392                     track = info.track()
0393                     if track.index() not in reco_inds:
0394                     reco_inds.append(track.index())
0395                     recos.append(track)
0396 
0397                     '''
0398                     if particle.nMatchedTracks() == 1:
0399                     self.Plot3DHelix(track,1,2)
0400                     self.Plot3DHits(track,1,2)
0401                     else:
0402                     self.Plot3DHelix(track,5,2)
0403                     self.Plot3DHits(track,5,2)
0404                     '''
0405         icol =  0
0406                 for particle in particles:
0407             self.Plot3DHelix(particle, self.colors_G[icol], 1) # kAzure color, maybe ;)
0408             self.Plot3DHits(particle, self.colors_G[icol], 1) 
0409             icol += 1
0410         for track in recos:
0411             self.Plot3DHelix(track,1,2)
0412             self.Plot3DHits(track,1,2)
0413             '''
0414             if track.trackingParticle().nMatchedTracks() == 1:
0415             self.Plot3DHelix(track,1,2)
0416             self.Plot3DHits(track,1,2)
0417             else:
0418             self.Plot3DHelix(track,5,2)
0419             self.Plot3DHits(track,5,2)
0420             '''
0421 
0422             self.Draw()
0423             return
0424         # the following is useless by now
0425         # Plot all fakes at once (messy picture)
0426     '''
0427     self.Plot3DHelixes(fakes,2)
0428     for fake in fakes:
0429         self.Plot3DHits(fake, 2)
0430             # Plot real particle tracks which include fake tracks hits
0431         for hit in fake.hits():
0432         if hit.isValid() and hit.nMatchedTrackingParticles() >= 0:
0433             for info in hit.matchedTrackingParticleInfos():
0434             particle = info.trackingParticle()
0435             self.Plot3DHelix(particle,3,1)
0436             self.Plot3DHits(particle,3,1)
0437  
0438             print "Number of matched tracks to real particle: " + str(particle.nMatchedTracks())
0439             # Plot reconstructed tracks of these real particle tracks
0440             if reconstructed and particle.nMatchedTracks() > 0:
0441                 for info in particle.matchedTrackInfos():
0442                 track = info.track()    
0443                 if particle.nMatchedTracks() == 1:
0444                     self.Plot3DHelix(track,1,2)
0445                     self.Plot3DHits(track,1,2)
0446                 else:
0447                     self.Plot3DHelix(track,5,2)
0448                     self.Plot3DHits(track,5,2)
0449         '''
0450 
0451     def PlotFakes(self, event, reconstructed = 1, fake_filter = None, end_filter = None, last_filter = None, particle_end_filter = None):
0452     iterative = 1
0453 
0454         fakes = analysis.FindFakes(event)
0455     if iterative:
0456             # Plot fakes one by one
0457         for fake in fakes:
0458                 # Check for filter
0459         if fake_filter:
0460             info = analysis.FakeInfo(fake)
0461             if info.fake_class not in fake_filter:
0462             continue
0463 
0464         self.Reset()
0465         self.Plot3DHelixes([fake],2)
0466         self.Plot3DHits(fake, 2)
0467         #self.PlotVertex3D(vertex,2)
0468         fake_info = analysis.FakeInfo(fake)
0469         analysis.PrintTrackInfo(fake, None, 0, fake_info)
0470                 if fake_info.matches:
0471             for match in fake_info.matches:
0472             continue #self.PlotTrackingFail(match)
0473             #self.PlotPoint3D(fake_info.end_loc, 2)
0474 
0475                 # Find real particle tracks which include fake tracks hits
0476         icol = 0
0477         particle_inds = []
0478         particles = []
0479         reco_inds = []
0480         recos = []
0481         for hit in fake.hits():
0482             if hit.isValid() and hit.nMatchedTrackingParticles() >= 0:  
0483             for info in hit.matchedTrackingParticleInfos():
0484                 particle = info.trackingParticle()
0485                 if particle.index() not in particle_inds:
0486                 particle_inds.append(particle.index())
0487                 particles.append(particle)
0488                 if reconstructed and particle.nMatchedTracks() > 0:
0489                 for info in particle.matchedTrackInfos():
0490                     track = info.track()
0491                     if track.index() not in reco_inds:
0492                     reco_inds.append(track.index())
0493                     recos.append(track)
0494 
0495 
0496             # Find reconstructed tracks included in fakes hits 
0497             if hit.isValid() and reconstructed and hit.ntracks() > 0:
0498             for track in hit.tracks():
0499                 #track = info.track()
0500                 if (track.index() not in reco_inds) and track.index() != fake.index():
0501                 reco_inds.append(track.index())
0502                 recos.append(track)
0503  
0504                 # Plot the particles and reconstructed tracks
0505         icol =  0
0506         self.ParticleTest(particles, draw=False)
0507                 for particle in particles:
0508             self.Plot3DHelix(particle, self.colors_G[icol], 1)
0509             self.plots_3D[-1].SetLineStyle(5)
0510             self.Plot3DHits(particle, self.colors_G[icol], 1) 
0511             self.PlotVertex3D(particle.parentVertex(),self.colors_G[icol])
0512                     # EXPERIMENTAL LINE:
0513                     #self.PlotTrack3D(particle, self.colors_G[icol])
0514 
0515             for decay in particle.decayVertices():
0516             self.PlotVertex3D(decay, 5)
0517             analysis.PrintTrackInfo(particle, fake)
0518             icol += 1
0519         icol = 0
0520         for track in recos:
0521             self.Plot3DHelix(track,self.colors_B[icol],2)
0522             self.Plot3DHits(track,self.colors_B[icol],2)
0523             #self.PlotVertex3D(vertex,self.colors_B[icol])
0524             analysis.PrintTrackInfo(track, fake)
0525             icol += 1
0526             
0527         if hit.isValid() and hit.z() >= 0: self.PlotDetectorRange("p",4)
0528         elif hit.isValid() and hit.z() < 0: self.PlotDetectorRange("n",4)
0529         else: self.PlotDetectorRange("b",4)
0530 
0531                 print("****************************\n")
0532             self.Draw()
0533             return
0534 
0535     def DrawTrackTest_old(self, track):
0536         c3 = ROOT.TCanvas("3D Plot","3D Plot", 1200, 1200)
0537     self.PlotDetectorRange("b", 4)
0538     for i in range(8): self.plots_3D[i].Draw()
0539     axis = ROOT.TAxis3D()
0540     axis.Draw()
0541     for hit in track.hits():
0542         if hit.isValid(): 
0543         point = ROOT.TPolyMarker3D(1, array('f', [hit.x(), hit.y(), hit.z()]), 2)
0544         point.Draw()
0545         input("continue")
0546     #axis.SetAxisRange(-278,278,"Z")
0547     
0548     axis.ToggleZoom()
0549 
0550     def DrawTrackTest(self, track): 
0551     self.PlotDetectorRange("b", 4)  
0552     self.PlotTrack3D(track)
0553     #self.Draw()
0554     '''
0555     for hit in track.hits():
0556         if hit.isValid(): 
0557         
0558         point = ROOT.TPolyMarker3D(1, array('f', [hit.x(), hit.y(), hit.z()]), 2)
0559         self.plots_3D.append(point) 
0560         self.Draw()
0561         '''
0562 
0563     def ParticleTest(self, particles, draw=False):
0564     for particle in particles:
0565         print("\nPARTICLE " + str(particle.index()))
0566         for hit in particle.hits():
0567         tof = -1
0568         for info in hit.matchedTrackingParticleInfos():
0569             if info.trackingParticle().index() == particle.index():
0570             #if len(info.tof()): 
0571             tof = info.tof()
0572         print("Index: " + str(hit.index()) + ", Layer: " + str(hit.layerStr()) + ", TOF: " + str(tof) +\
0573             "     XY distance: " + str(sqrt(hit.x()**2 + hit.y()**2)) + ", Z: " + str(hit.z()))
0574         self.DrawTrackTest(particle)
0575         if draw:
0576         self.Draw()
0577         self.Reset()
0578  
0579     def PlotDetectorRange(self, area="b", plates = 6):
0580     '''
0581     Plots the detector schematic layout.
0582     Area  means which part to plot (p = positive side, n = negative side, b = both)
0583     Plates means how many detector circle schemes to plot per side.
0584     '''
0585         r = [17, 17, 57, 57, 112, 112]
0586     z = [30, 70, 70, 120, 120, 280]
0587     for i in range(plates):
0588         X = []; Y = []; Z = []; ZN = []
0589         for t in range(0,101):
0590         X.append(r[i]*cos(2*pi*t/100))
0591         Y.append(r[i]*sin(2*pi*t/100))
0592         Z.append(z[i]*1.0)
0593         ZN.append(-1.0*z[i])
0594         plot = ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",Z),"S")
0595         nplot = ROOT.TPolyLine3D(len(X),array("f",X),array("f",Y),array("f",ZN),"S")
0596         plot.SetLineStyle(3)
0597         nplot.SetLineStyle(3)
0598         if area == "p": self.plots_3D.append(plot)
0599         if area == "n": self.plots_3D.append(nplot)
0600         if area == "b":
0601         self.plots_3D.append(plot)
0602         self.plots_3D.append(nplot)
0603 
0604     def ClassHistogram(self, data, name = "Histogram", labels = False, ready = False):
0605         '''
0606     Draws histogram with data. Data is in form of a dictionary,
0607     labels assigned to amount of hits. If labels is defined, it
0608     defines the labels assigned to data.
0609     '''
0610     if ready:
0611         if labels:
0612         keys = [key for key in data]
0613         hist_labels = [labels[key] for key in keys]
0614         hist_data = [data[key] for key in keys]
0615         else:
0616         hist_labels = [key for key in data]
0617         hist_data = [data[key] for key in hist_labels]
0618     
0619     c1 = ROOT.TCanvas(name, name, 700, 500)
0620     c1.SetGrid()
0621     #c1.SetTopMargin(0.15)
0622     hist = ROOT.TH1F(name, name, 30,0,30)#, 3,0,3)
0623         hist.SetStats(0);
0624         hist.SetFillColor(38);
0625     hist.SetCanExtend(ROOT.TH1.kAllAxes);
0626     if ready: # cumulative results already calculated in data
0627         for i in range(len(hist_labels)):
0628         #hist.Fill(hist_labels[i],hist_data[i])
0629         [hist.Fill(hist_labels[i],1) for j in range(hist_data[i])]
0630     else: # data is in samples, not in cumulative results
0631         for entry in data: hist.Fill(entry,1)
0632     hist.LabelsDeflate()
0633     #hist.SetLabelSize(0.05)
0634     hist.Draw()
0635 
0636     input("Press any key to continue")
0637 
0638 
0639     def Draw(self):
0640     if self.plots_3D:
0641         c3 = ROOT.TCanvas("3D Plot","3D Plot", 1200, 1200)
0642         view = ROOT.TView3D()
0643         axis = ROOT.TAxis3D()
0644         axis.SetXTitle("x")
0645         axis.SetYTitle("y")
0646         axis.SetZTitle("z")
0647         #axis.GetZaxis().RotateTitle()
0648 
0649         for plot in self.plots_3D:
0650         if plot: plot.Draw()
0651         
0652         #view.DefineViewDirection(array("d",[1,1,1]),array("d",[0,0,0]),1.0,0.0,0.0,1.0,0.0,1.0)#,array("d",[-1,-1,-1]),array("d",[1,1,1]))
0653         axis.Draw()
0654         #view.SetAxisNDC(array("d",[0,0,0]),array("d",[0,1,0]),array("d",[0,0,0]),array("d",[0,0,1]),array("d",[0,0,0]),array("d",[1,0,0]))
0655     
0656         #view.SetPsi(30)
0657         #view.SetView(0,0,pi/2,ROOT.Long())
0658         #view.DefineViewDirection(0,0,pi/2)
0659         #view.RotateView(pi/2,pi/2)
0660         axis.ToggleZoom()
0661 
0662     if self.plots_2D:
0663         c2 = ROOT.TCanvas("2D Plot","2D Plot", 1200, 1200)
0664 
0665         for plot in self.plots_2D: plot.Draw("A") 
0666 
0667     input("Press any key to continue")
0668 
0669