CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/FWCore/GuiBrowsers/python/Vispa/Views/RootCanvasView.py

Go to the documentation of this file.
00001 import logging
00002 
00003 from PyQt4.QtCore import SIGNAL,Qt,QCoreApplication
00004 from PyQt4.QtGui import QWidget
00005 
00006 from Vispa.Views.AbstractView import AbstractView
00007 from Vispa.Main.Exceptions import exception_traceback
00008 from Vispa.Share.BasicDataAccessor import BasicDataAccessor
00009 
00010 try:
00011     import ROOT
00012     import pxl.core,pxl.astro,pxl.hep
00013     import_root_error=None
00014 except Exception,e:
00015     import_root_error=(str(e),exception_traceback())
00016 
00017 from array import array
00018 
00019 class RootCanvasView(AbstractView, QWidget):
00020     
00021     LABEL = "&ROOT Canvas View"
00022     
00023     def __init__(self, parent=None):
00024         AbstractView.__init__(self)
00025         QWidget.__init__(self, parent)
00026         ROOT.gROOT.SetStyle('Plain')
00027         #ROOT.gStyle.SetPalette(1)
00028         self.canvas = ROOT.TCanvas()
00029         #self.canvas.SetEditable(False)
00030         #self.canvas = None 
00031         self._operationId = 0
00032         self._updatingFlag = 0
00033 
00034     def setDataAccessor(self, accessor):
00035         """ Sets the DataAccessor from which the data is read 
00036         You need to call updateContent() in order to make the changes visible.   
00037         """
00038         if not isinstance(accessor, BasicDataAccessor):
00039             raise TypeError(__name__ + " requires data accessor of type BasicDataAccessor.")
00040         #if not isinstance(accessor, RelativeDataAccessor):
00041         #    raise TypeError(__name__ + " requires data accessor of type RelativeDataAccessor.")
00042         AbstractView.setDataAccessor(self, accessor)
00043         
00044     def updateContent(self):
00045         """ Clear the view and refill it.
00046         """
00047         logging.debug(__name__ + ": updateContent")
00048         if import_root_error!=None:
00049             logging.error(__name__ + ": Could not import pxl and ROOT: "+import_root_error[1])
00050             QCoreApplication.instance().errorMessage("Could not import pxl and ROOT (see logfile for details):\n"+import_root_error[0])
00051             return
00052         self._updatingFlag +=1
00053         operationId = self._operationId
00054         #if self._dataAccessor:
00055         #objects = self._filter(self._dataObjects)
00056         objects = self.applyFilter(self.dataObjects())
00057         i = 0
00058         for object in objects:
00059             if operationId != self._operationId:
00060                 break
00061             self._plotscript(self.canvas, object) 
00062 
00063         self._updatingFlag -=1
00064         return operationId == self._operationId
00065   
00066     def _plotscript(self, canvas, object):
00067       ''' The actual plotting script - has to be replaced later with
00068       script from external file by user'''
00069       
00070       
00071       logging.debug(__name__ + ": _plotscript")
00072       canvas.cd()
00073       if isinstance(object, pxl.core.BasicContainer):
00074         self.basiccontainer = object
00075         abos = object.getObjectsOfType(pxl.astro.AstroBasicObject)
00076         logging.debug(__name__ + ": _plotscript: Plotting " + str(len(abos)) + " AstroObjects")
00077         lat = []
00078         lon = []
00079         for ao in abos:
00080           lat.append(ao.getLatitude())
00081           lon.append(ao.getLongitude())
00082         
00083         self.f = ROOT.TGraph(len(lat), array('f', lon), array('f', lat))
00084         #self.f.SetEditable(False) 
00085         self.f.SetTitle('')
00086         self.f.GetXaxis().SetTitle('Longitude')
00087         self.f.GetYaxis().SetTitle('Latitude')
00088         self.f.SetName('')
00089         self.f.Draw('ap')
00090         self.f.SetEditable(False)
00091         self.p = None
00092       elif isinstance(object, pxl.astro.RegionOfInterest) or isinstance(object, pxl.astro.UHECRSource):
00093         self.p = []
00094         self.p.append(ROOT.TMarker(object.getLongitude(), object.getLatitude(), 3))
00095         logging.debug(__name__ + 'DrawROI with ' + str(len(object.getSoftRelations().getContainer().items())) + ' UHECR')
00096         for key, item in object.getSoftRelations().getContainer().items():
00097           
00098           uhecr = pxl.astro.toUHECR(self.basiccontainer.getById(item))
00099           if isinstance(uhecr,pxl.astro.UHECR):
00100             self.p.append(ROOT.TMarker(uhecr.getLongitude(), uhecr.getLatitude(), 7))
00101             self.p[ - 1].SetMarkerSize(10)
00102             self.p[ - 1].SetMarkerColor(ROOT.kGreen + 1)
00103           else:
00104             uhecr = pxl.astro.toAstroObject(self.basiccontainer.getById(item))
00105             if isinstance(uhecr,pxl.astro.AstroObject):
00106               self.p.append(ROOT.TMarker(uhecr.getLongitude(), uhecr.getLatitude(), 7))
00107               self.p[ - 1].SetMarkerSize(8)
00108               self.p[ - 1].SetMarkerColor(ROOT.kOrange + 1)
00109 
00110         for x in self.p:
00111           x.Draw()
00112       
00113       elif isinstance(object, pxl.astro.AstroBasicObject):
00114         self.p = ROOT.TMarker(object.getLongitude(), object.getLatitude(), 7)
00115         self.p.SetMarkerSize(10)
00116         self.p.SetMarkerColor(ROOT.kRed)
00117         self.p.Draw()
00118       elif isinstance(object, pxl.hep.EventView):
00119         self.createLegoPlot(object)
00120       elif isinstance(object, pxl.core.Event):
00121         for eventview in object.getObjectsOfType(pxl.hep.EventView):
00122           self.createLegoPlot(object)
00123       elif isinstance(object, pxl.hep.Particle):
00124         self.p = ROOT.TMarker(object.getPhi(), object.getEta(), 20)
00125         if (object.getName() == "Muon"):
00126           h2 = ROOT.TH2F('muon-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00127           h2.SetFillColor(2)
00128           h2.Fill(object.getEta(), object.getPhi(), object.getPt())
00129           h2.DrawCopy('SAME LEGO A')
00130         elif (object.getName() == "Jet"):
00131           h2 = ROOT.TH2F('jet-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00132           h2.SetFillColor(4)
00133           h2.Fill(object.getEta(), object.getPhi(), object.getPt())
00134           h2.DrawCopy('SAME LEGO A')
00135         elif (object.getName() == "Electron"):
00136           h2 = ROOT.TH2F('electron-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00137           h2.SetFillColor(3)
00138           h2.Fill(object.getEta(), object.getPhi(), object.getPt())
00139           h2.DrawCopy('SAME LEGO A')
00140         elif (object.getName() == "MET"):
00141           h2 = ROOT.TH2F('met-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00142           h2.SetFillColor(6)
00143           h2.Fill(object.getEta(), object.getPhi(), object.getPt())
00144           h2.DrawCopy('SAME LEGO A')
00145         else:
00146           h2 = ROOT.TH2F('all-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00147           h2.SetFillColor(1)
00148           h2.Fill(object.getEta(), object.getPhi(), object.getPt())
00149           h2.DrawCopy('SAME LEGO A')
00150         self.p.Draw()
00151         
00152       canvas.Modified()
00153       canvas.Update()
00154 
00155     def createGraph(self, eventview):
00156       particles = eventview.getObjectsOfType(pxl.hep.Particle)
00157       logging.info("Display " + str(len(particles)) + " Particles.")
00158       if (len(particles) > 0):
00159         etas = []
00160         phis = []
00161         for particle in particles:
00162           if (particle.getEta() < 1000.):
00163             etas.append(particle.getEta())
00164             phis.append(particle.getPhi())
00165         self.f = ROOT.TGraph(len(etas), array('f', phis), array('f', etas))
00166         self.f.SetTitle('')
00167         self.f.GetXaxis().SetTitle('#Phi')
00168         self.f.GetXaxis().SetRangeUser(- 3.14156, 3.14156)
00169         self.f.GetYaxis().SetTitle('#eta')
00170         self.f.GetXaxis().SetRangeUser(- 5., 5.)
00171         self.f.SetMarkerSize(1)
00172         self.f.SetMarkerStyle(20)
00173         self.f.SetName('')
00174         self.f.Draw('ap')
00175         self.f.SetEditable(False)
00176         self.p = None
00177 
00178     def createLegoPlot(self, eventview):
00179       particles = eventview.getObjectsOfType(pxl.hep.Particle)
00180       logging.info("Display " + str(len(particles)) + " Particles")
00181       if (len(particles) > 0):
00182         self.f = ROOT.TH2F('eta-phi-plot', '', 50, - 4, 4, 50, - 3.141593, 3.141593)
00183         for particle in particles:
00184           self.f.Fill(particle.getEta(), particle.getPhi(), particle.getPt())
00185 
00186         self.f.SetTitle('')
00187         self.f.GetXaxis().SetTitle('#eta')
00188         self.f.GetYaxis().SetTitle('#Phi')
00189         self.f.GetZaxis().SetTitle('p_{T} (GeV)')
00190         self.f.SetName('')
00191         self.f.DrawCopy('LEGO2')
00192         self.p = None
00193     
00194     def cancel(self):
00195         """ Stop all running operations.
00196         """
00197         self._operationId += 1
00198 
00199     def mousePressEvent(self,event):
00200         QWidget.mousePressEvent(self,event)
00201         if event.button()==Qt.RightButton:
00202             self.emit(SIGNAL("mouseRightPressed"), event.globalPos())
00203 
00204     def isBusy(self):
00205         return self._updatingFlag>0