Belle II Software development
DecayTree< T > Class Template Reference

This is a helper class for the MCDecayFinderModule. More...

#include <DecayTree.h>

Public Member Functions

 DecayTree ()
 Default constructor.
 
 ~DecayTree ()
 Destructor.
 
std::vector< DecayTree< T > * > getDaughters () const
 Return list of decay daughters.
 
T * getObj () const
 Return the decaying object itself, e.g.
 
void setObj (T *obj)
 Set the decaying object, e.g.
 
void append (DecayTree< T > *t)
 Add a daughter, which is a DecayTree itself.
 
 DecayTree (const std::string &decaystring, bool removeRadiativeGammaFlag=false)
 Create a new Decay tree from a decaystring.
 
 DecayTree (const DecayTree &tree)
 Copy constructor Required because we need to rebuild the node cache.
 
DecayTreeoperator= (const DecayTree &tree)
 Assign operator Required because we need to rebuild the node cache.
 
std::vector< DecayNodebuild_tree (const std::string &decaystring, bool removeRadiativeGammaFlag)
 Recursively build a new tree.
 
bool find_decay (const DecayTree &tree) const
 Check if the decay tree contains the given decay tree.
 
std::string to_string () const
 Output string representation of DecayTree.
 
int getMatchSymbolPosition () const
 Returns position of match symbol.
 
bool isValid () const
 Returns if the decay tree is valid (constructed from a decay string which has a match)
 
const DecayNodegetDecayNode (unsigned int n) const
 Returns n-th node as const.
 
DecayNodegetDecayNode (unsigned int n)
 Returns n-th node.
 
size_t getNumberOfDecayNodes () const
 Return number of nodes in this tree.
 

Private Member Functions

void build_cache (DecayNode &node)
 Build nodes_cache in order of appearance in the decay string for fast access.
 

Private Attributes

T * m_myObject
 The decaying object, e.g.
 
std::vector< DecayTree< T > * > m_daughters
 Decay daughters of m_myObject.
 
bool m_valid
 True if the tree is valid (invalid can happen if is constructed from a node with "No match")
 
size_t m_i
 Current position in the building of the DecayTree.
 
unsigned int m_token_count
 Count current tokens.
 
int m_match_symbol_position
 Position of the token with the match symbol ^.
 
DecayNode m_root_node
 root DecayNode
 
std::vector< DecayNode * > m_nodes_cache
 Vector of decay nodes of the particles in the order of their appearance in the decay string for fast access.
 

Detailed Description

template<class T>
class Belle2::DecayTree< T >

This is a helper class for the MCDecayFinderModule.

Consists of a tree of DecayNodes Can be constructed from the output of the ParticleMCDecayString module.

It is designed to hold MCParticle objects when trying to match a given DecayString.

Definition at line 20 of file DecayTree.h.

Constructor & Destructor Documentation

◆ DecayTree() [1/2]

DecayTree ( const std::string &  decaystring,
bool  removeRadiativeGammaFlag = false 
)
explicit

Create a new Decay tree from a decaystring.

Definition at line 16 of file DecayTree.cc.

16 : m_i(0), m_token_count(0),
18{
19
20 m_valid = decaystring.find("No match") == std::string::npos;
21 if (m_valid) {
22 const auto& root_nodes = this->build_tree(decaystring, removeRadiativeGammaFlag);
23 if (root_nodes.size() == 1) {
24 m_root_node = root_nodes[0];
25 // Build cache
27 } else
28 m_valid = false;
29 }
30}
unsigned int m_token_count
Count current tokens.
Definition: DecayTree.h:102
DecayNode m_root_node
root DecayNode
Definition: DecayTree.h:104
bool m_valid
True if the tree is valid (invalid can happen if is constructed from a node with "No match")
Definition: DecayTree.h:100
std::vector< DecayNode > build_tree(const std::string &decaystring, bool removeRadiativeGammaFlag)
Recursively build a new tree.
Definition: DecayTree.cc:69
int m_match_symbol_position
Position of the token with the match symbol ^.
Definition: DecayTree.h:103
size_t m_i
Current position in the building of the DecayTree.
Definition: DecayTree.h:101
void build_cache(DecayNode &node)
Build nodes_cache in order of appearance in the decay string for fast access.
Definition: DecayTree.cc:60

◆ DecayTree() [2/2]

DecayTree ( const DecayTree< T > &  tree)

Copy constructor Required because we need to rebuild the node cache.

Definition at line 32 of file DecayTree.cc.

32 :
33 m_root_node(tree.m_root_node)
34{
35 m_valid = tree.m_valid;
36 m_token_count = tree.m_token_count;
37 m_match_symbol_position = tree.m_match_symbol_position;
38 m_i = tree.m_i;
39
40 if (m_valid) {
42 }
43}

Member Function Documentation

◆ build_cache()

void build_cache ( DecayNode node)
private

Build nodes_cache in order of appearance in the decay string for fast access.

Parameters
nodecurrent node

Definition at line 60 of file DecayTree.cc.

61{
62 m_nodes_cache.push_back(&node);
63 for (auto& daughter : node.daughters) {
64 build_cache(daughter);
65 }
66}
std::vector< DecayNode > daughters
daughter decay nodes
Definition: DecayNode.h:48
std::vector< DecayNode * > m_nodes_cache
Vector of decay nodes of the particles in the order of their appearance in the decay string for fast ...
Definition: DecayTree.h:106

◆ build_tree()

std::vector< DecayNode > build_tree ( const std::string &  decaystring,
bool  removeRadiativeGammaFlag 
)

Recursively build a new tree.

Parameters
decaystringas outputted by the ParticleMCDecayString module (after splitting by | )
removeRadiativeGammaFlagremove radiative photons from decay string. Handle with care: In the decay B+ --> e+ nu_e gamma, the gamma would be removed although this might be your signal.

Definition at line 69 of file DecayTree.cc.

70{
71
72 const unsigned int N = decaystring.size();
73 std::vector<DecayNode> nodes;
74 nodes.reserve(5);
75
76 for (; m_i < N; ++m_i) {
77 // Skip ' ', '^' and "->"
78 if (decaystring[m_i] == ' ' or decaystring[m_i] == '>' or (m_i + 1 < N and decaystring[m_i] == '-'
79 and decaystring[m_i + 1] == '>')) {
80 continue;
81 }
82
83 // Match symbol
84 if (decaystring[m_i] == '^') {
86 continue;
87 }
88
89 // Handle start of subdecay of the form X ( ... )
90 if (decaystring[m_i] == '(') {
91 m_i++;
92 nodes.back().daughters = this->build_tree(decaystring, removeRadiativeGammaFlag);
93 continue;
94 }
95
96 // Handle end of subdecay
97 if (decaystring[m_i] == ')') {
98 return nodes;
99 }
100
101 // Search end of token
102 unsigned int j = m_i + 1;
103 for (; j < N; ++j) {
104 if (decaystring[j] == ' ' or decaystring[j] == ')' or decaystring[j] == '(')
105 break;
106 }
107
108 // Parse token and add it as new node
109 try {
110 const int pdg = std::stoi(decaystring.substr(m_i, j - m_i));
111 if (removeRadiativeGammaFlag) {
112 if (nodes.size() < 2 or pdg != Const::photon.getPDGCode()) {
113 nodes.emplace_back(pdg);
115 }
116 } else {
117 nodes.emplace_back(pdg);
119 }
120 m_i = j - 1;
121 } catch (const std::invalid_argument&) {
122 // We ignore if the token cannot be parsed
123 }
124 }
125
126 return nodes;
127}
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ParticleType photon
photon particle
Definition: Const.h:673

◆ find_decay()

bool find_decay ( const DecayTree< T > &  tree) const

Check if the decay tree contains the given decay tree.

Parameters
treeDecayTree describing the decay

Definition at line 129 of file DecayTree.cc.

130{
131 return m_root_node.find_decay(tree.m_root_node);
132}
bool find_decay(const DecayNode &to_find) const
Check if the decay node contains the given decay tree.
Definition: DecayNode.cc:19

◆ getDecayNode() [1/2]

DecayNode & getDecayNode ( unsigned int  n)

Returns n-th node.

Parameters
nnth node

Definition at line 134 of file DecayTree.cc.

135{
136 return *(m_nodes_cache[n]);
137}

◆ getDecayNode() [2/2]

const DecayNode & getDecayNode ( unsigned int  n) const

Returns n-th node as const.

Parameters
nnth node

Definition at line 139 of file DecayTree.cc.

140{
141 return *(m_nodes_cache[n]);
142}

◆ getMatchSymbolPosition()

int getMatchSymbolPosition ( ) const
inline

Returns position of match symbol.

Definition at line 68 of file DecayTree.h.

◆ getNumberOfDecayNodes()

size_t getNumberOfDecayNodes ( ) const
inline

Return number of nodes in this tree.

Definition at line 90 of file DecayTree.h.

90{ return m_nodes_cache.size(); }

◆ isValid()

bool isValid ( ) const
inline

Returns if the decay tree is valid (constructed from a decay string which has a match)

Definition at line 73 of file DecayTree.h.

73{ return m_valid; }

◆ operator=()

DecayTree & operator= ( const DecayTree< T > &  tree)

Assign operator Required because we need to rebuild the node cache.

Definition at line 45 of file DecayTree.cc.

46{
47 m_valid = tree.m_valid;
48 m_token_count = tree.m_token_count;
49 m_match_symbol_position = tree.m_match_symbol_position;
50 m_root_node = tree.m_root_node;
51 m_i = tree.m_i;
52
53 if (m_valid) {
55 }
56
57 return *this;
58}

◆ to_string()

std::string to_string ( ) const

Output string representation of DecayTree.

Definition at line 145 of file DecayTree.cc.

146{
147 return m_root_node.print_node();
148}
std::string print_node(unsigned int indent=0) const
Output a single node.
Definition: DecayNode.cc:30

Member Data Documentation

◆ m_daughters

std::vector< DecayTree<T>* > m_daughters
private

Decay daughters of m_myObject.

Definition at line 25 of file DecayTree.h.

◆ m_i

size_t m_i
private

Current position in the building of the DecayTree.

Definition at line 101 of file DecayTree.h.

◆ m_match_symbol_position

int m_match_symbol_position
private

Position of the token with the match symbol ^.

Definition at line 103 of file DecayTree.h.

◆ m_myObject

T* m_myObject
private

The decaying object, e.g.

MCParticle.

Definition at line 23 of file DecayTree.h.

◆ m_nodes_cache

std::vector<DecayNode*> m_nodes_cache
private

Vector of decay nodes of the particles in the order of their appearance in the decay string for fast access.

Definition at line 106 of file DecayTree.h.

◆ m_root_node

DecayNode m_root_node
private

root DecayNode

Definition at line 104 of file DecayTree.h.

◆ m_token_count

unsigned int m_token_count
private

Count current tokens.

Definition at line 102 of file DecayTree.h.

◆ m_valid

bool m_valid
private

True if the tree is valid (invalid can happen if is constructed from a node with "No match")

Definition at line 100 of file DecayTree.h.


The documentation for this class was generated from the following files: