if 'setDescriptorTemplate' not in dir(DecayTreeTuple): print "Adding setDescriptorTemplate() to DecayTreeTuple because this DaVinci is too old" # Bored of typing decay descriptors and adding carat symbols? # Use some python string template magic to set your decay descriptor # and define your branches all in one go without excess typing! def setDescriptorTemplate(self, template): if 'Decay' not in dir(self): raise TypeError, ("You're trying to set the decay descriptor of something that doesn't have one, " + str(type(self))) if 'Branches' not in dir(self): raise TypeError, ("You're trying to define branches on something that doesn't support them, " + str(type(self))) from string import Template # The argument 'template' is a Python string template # e.g. "${D}[D0 -> ${kaon}K- ${pion}pi+]CC" # Here ["D", "kaon", "pion"] are the branch names you want dd = Template(template) # This parses the temlate to get the list of branch names, # i.e. ["D", "kaon", "pion"] particles = [y[1] if len(y[1]) else y[2] for y in dd.pattern.findall(dd.template) if len(y[1]) or len(y[2])] # To form the decay descriptor, we need to mark all the particles # except for the top-level particle, which is included by default mapping = { p : '^' for p in particles } mapping[particles[0]] = '' # Make the descriptor # "[D0 -> ^K- ^pi+]CC" self.Decay = dd.substitute(mapping) # Now make the branches branches = { } for p in particles: # Need a version of the descriptor where particle 'p' is marked but nothing else is. mapping = { q : '^' if p == q else '' for q in particles } branches[p] = dd.substitute(mapping) # Finally, add the branches to the DecayTreeTuple return self.addBranches(branches) DecayTreeTuple.setDescriptorTemplate = setDescriptorTemplate