CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/PhysicsTools/PythonAnalysis/python/ParticleDecayDrawer.py

Go to the documentation of this file.
00001 # Benedikt Hegner, DESY
00002 # benedikt.hegner@cern.ch
00003 #
00004 # this tool is based on Luca Lista's tree drawer module
00005 
00006 
00007 class ParticleDecayDrawer(object):
00008     """Draws particle decay tree """
00009     
00010     def __init__(self):
00011         print "Init particleDecayDrawer"
00012         #  booleans: printP4 printPtEtaPhi printVertex   
00013         
00014     def _accept(self, candidate, skipList):
00015         if candidate in skipList: return False;
00016         return self._select(candidate)
00017         
00018     def _select(self, candidate):
00019         return candidate.status() == 3
00020          
00021     def _hasValidDaughters(self, candidate):
00022         nDaughters = candidate.numChildren()
00023         for i in xrange(nDaughters):
00024             if self._select(candidate.listChildren()[i]): return True
00025         return False        
00026 
00027     def _printP4(self, candidate):
00028         return " "
00029 
00030     def _decay(self, candidate, skipList):
00031 
00032           out = str()
00033           if candidate in skipList:
00034               return ""
00035           skipList.append(candidate)
00036     
00037           id = candidate.pdg_id()
00038           # here the part about the names :-(
00039           out += str(id) + self._printP4(candidate)
00040     
00041           validDau = 0
00042           nOfDaughters = candidate.numChildren()
00043           for i in xrange(nOfDaughters):
00044               if self._accept(candidate.listChildren()[i], skipList): validDau+=1
00045           if validDau == 0: return out
00046     
00047           out += " ->"
00048     
00049           for i in xrange(nOfDaughters):
00050               d = candidate.listChildren()[i]
00051               if self._accept(d, skipList):
00052                   decString = self._decay(d, skipList)
00053                   if ("->" in decString):  out += " ( %s ) " %decString
00054                   else:  out += " %s" %decString
00055           return out
00056 
00057     def draw(self, particles): 
00058         """ draw decay tree from list(HepMC.GenParticles)""" 
00059         skipList = []
00060         nodesList = []
00061         momsList = []  
00062         for particle in particles:
00063             if particle.numParents() > 1:
00064                 if self._select(particle):
00065                     skipList.append(particle)
00066                     nodesList.append(particle)
00067                     for j in xrange(particle.numParents()):
00068                         mom = particle.listParents()[j]
00069                         while (mom.mother()):# != None ):
00070                             mom = mom.mother()
00071                         if self._select(mom):
00072                             momsList.append(mom)
00073 
00074         print "-- decay --"  
00075         if len(momsList) > 0:
00076             if len(momsList) > 1:
00077                 for m in xrange(len(momsList)):
00078                     decString = self._decay( momsList[m], skipList)
00079                     if len(decString) > 0:
00080                        print "{ %s } " %decString
00081             else:
00082                 print self._decay(momsList[0], skipList)   
00083   
00084         if len(nodesList) > 0:
00085             print "-> "
00086             if len(nodesList) > 1:
00087                 for node in nodesList:
00088                    skipList.remove(node)
00089                    decString = self._decay(node, skipList)
00090                    if len(decString) > 0:
00091                        if "->" in decString:  print " ( %s ) " %decString
00092                        else:  print " " + decString
00093             else:
00094                 skipList.remove(nodesList[0])
00095                 print self._decay(nodesList[0], skipList)
00096           
00097         print
00098