CMS 3D CMS Logo

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