CMS 3D CMS Logo

ParticleDecayDrawer.py
Go to the documentation of this file.
1 # Benedikt Hegner, DESY
2 # benedikt.hegner@cern.ch
3 #
4 # this tool is based on Luca Lista's tree drawer module
5 
6 
8  """Draws particle decay tree """
9 
10  def __init__(self):
11  print "Init particleDecayDrawer"
12  # booleans: printP4 printPtEtaPhi printVertex
13 
14  def _accept(self, candidate, skipList):
15  if candidate in skipList: return False;
16  return self._select(candidate)
17 
18  def _select(self, candidate):
19  return candidate.status() == 3
20 
21  def _hasValidDaughters(self, candidate):
22  nDaughters = candidate.numChildren()
23  for i in xrange(nDaughters):
24  if self._select(candidate.listChildren()[i]): return True
25  return False
26 
27  def _printP4(self, candidate):
28  return " "
29 
30  def _decay(self, candidate, skipList):
31 
32  out = str()
33  if candidate in skipList:
34  return ""
35  skipList.append(candidate)
36 
37  id = candidate.pdg_id()
38  # here the part about the names :-(
39  out += str(id) + self._printP4(candidate)
40 
41  validDau = 0
42  nOfDaughters = candidate.numChildren()
43  for i in xrange(nOfDaughters):
44  if self._accept(candidate.listChildren()[i], skipList): validDau+=1
45  if validDau == 0: return out
46 
47  out += " ->"
48 
49  for i in xrange(nOfDaughters):
50  d = candidate.listChildren()[i]
51  if self._accept(d, skipList):
52  decString = self._decay(d, skipList)
53  if ("->" in decString): out += " ( %s ) " %decString
54  else: out += " %s" %decString
55  return out
56 
57  def draw(self, particles):
58  """ draw decay tree from list(HepMC.GenParticles)"""
59  skipList = []
60  nodesList = []
61  momsList = []
62  for particle in particles:
63  if particle.numParents() > 1:
64  if self._select(particle):
65  skipList.append(particle)
66  nodesList.append(particle)
67  for j in xrange(particle.numParents()):
68  mom = particle.listParents()[j]
69  while (mom.mother()):# != None ):
70  mom = mom.mother()
71  if self._select(mom):
72  momsList.append(mom)
73 
74  print "-- decay --"
75  if len(momsList) > 0:
76  if len(momsList) > 1:
77  for m in xrange(len(momsList)):
78  decString = self._decay( momsList[m], skipList)
79  if len(decString) > 0:
80  print "{ %s } " %decString
81  else:
82  print self._decay(momsList[0], skipList)
83 
84  if len(nodesList) > 0:
85  print "-> "
86  if len(nodesList) > 1:
87  for node in nodesList:
88  skipList.remove(node)
89  decString = self._decay(node, skipList)
90  if len(decString) > 0:
91  if "->" in decString: print " ( %s ) " %decString
92  else: print " " + decString
93  else:
94  skipList.remove(nodesList[0])
95  print self._decay(nodesList[0], skipList)
96 
97  print
98 
#define str(s)