Belle II Software  release-05-01-25
DecayTree.cc
1 /* BASF2 (Belle Analysis Framework 2) *
2  * Copyright(C) 2016 - Belle II Collaboration *
3  * *
4  * Author: The Belle II Collaboration *
5  * Contributors: Thomas Keck *
6  * *
7  * This software is provided "as is" without any warranty. *
8  * *
9  **************************************************************************/
10 
11 #include <analysis/utility/DecayTree.h>
12 
13 #include <stdexcept>
14 
15 using namespace Belle2;
16 
17 DecayTree::DecayTree(const std::string& decaystring, bool removeRadiativeGammaFlag) : m_i(0), m_token_count(0),
18  m_match_symbol_position(-1)
19 {
20 
21  m_valid = decaystring.find("No match") == std::string::npos;
22  if (m_valid) {
23  const auto& root_nodes = this->build_tree(decaystring, removeRadiativeGammaFlag);
24  if (root_nodes.size() == 1)
25  m_root_node = root_nodes[0];
26  else
27  m_valid = false;
28  }
29 
30  // Build cache
31  if (m_valid) {
33  }
34 
35 }
36 
38  m_root_node(tree.m_root_node)
39 {
40  m_valid = tree.m_valid;
41  m_token_count = tree.m_token_count;
42  m_match_symbol_position = tree.m_match_symbol_position;
43  m_i = tree.m_i;
44 
45  if (m_valid) {
47  }
48 }
49 
51 {
52  m_valid = tree.m_valid;
53  m_token_count = tree.m_token_count;
54  m_match_symbol_position = tree.m_match_symbol_position;
55  m_root_node = tree.m_root_node;
56  m_i = tree.m_i;
57 
58  if (m_valid) {
60  }
61 
62  return *this;
63 }
64 
66 {
67  m_nodes_cache.push_back(&node);
68  for (auto& daughter : node.daughters) {
69  build_cache(daughter);
70  }
71 }
72 
73 
74 std::vector<DecayNode> DecayTree::build_tree(const std::string& decaystring, bool removeRadiativeGammaFlag)
75 {
76 
77  const unsigned int N = decaystring.size();
78  std::vector<DecayNode> nodes;
79  nodes.reserve(5);
80 
81  for (; m_i < N; ++m_i) {
82  // Skip ' ', '^' and "->"
83  if (decaystring[m_i] == ' ' or decaystring[m_i] == '>' or (m_i + 1 < N and decaystring[m_i] == '-'
84  and decaystring[m_i + 1] == '>')) {
85  continue;
86  }
87 
88  // Match symbol
89  if (decaystring[m_i] == '^') {
91  continue;
92  }
93 
94  // Handle start of subdecay of the form X ( ... )
95  if (decaystring[m_i] == '(') {
96  m_i++;
97  nodes.back().daughters = this->build_tree(decaystring, removeRadiativeGammaFlag);
98  continue;
99  }
100 
101  // Handle end of subdecay
102  if (decaystring[m_i] == ')') {
103  return nodes;
104  }
105 
106  // Search end of token
107  unsigned int j = m_i + 1;
108  for (; j < N; ++j) {
109  if (decaystring[j] == ' ' or decaystring[j] == ')' or decaystring[j] == '(')
110  break;
111  }
112 
113  // Parse token and add it as new node
114  try {
115  const int pdg = std::stoi(decaystring.substr(m_i, j - m_i));
116  if (removeRadiativeGammaFlag) {
117  if (nodes.size() < 2 or pdg != 22) {
118  nodes.emplace_back(pdg);
119  m_token_count++;
120  }
121  } else {
122  nodes.emplace_back(pdg);
123  m_token_count++;
124  }
125  m_i = j - 1;
126  } catch (const std::invalid_argument&) {
127  // We ignore if the token cannot be parsed
128  }
129  }
130 
131  return nodes;
132 }
133 
134 bool DecayTree::find_decay(const DecayTree& tree) const
135 {
136  return m_root_node.find_decay(tree.m_root_node);
137 }
138 
140 {
141  return *(m_nodes_cache[n]);
142 }
143 
144 const DecayNode& DecayTree::getDecayNode(unsigned int n) const
145 {
146  return *(m_nodes_cache[n]);
147 }
148 
149 
150 std::string DecayTree::to_string() const
151 {
152  return m_root_node.print_node();
153 }
Belle2::DecayTree::getDecayNode
const DecayNode & getDecayNode(unsigned int n) const
Returns n-th node as const.
Definition: DecayTree.cc:144
Belle2::DecayTree::m_valid
bool m_valid
True if the tree is valid (invalid can happen if is constructed from a node with "No match")
Definition: DecayTree.h:110
Belle2::DecayNode::print_node
std::string print_node(unsigned int indent=0) const
Output a single node.
Definition: DecayNode.cc:32
Belle2::DecayTree::to_string
std::string to_string() const
Output string representation of DecayTree.
Definition: DecayTree.cc:150
Belle2::DecayTree
This is a helper class for the MCDecayFinderModule.
Definition: DecayTree.h:30
Belle2::DecayTree::find_decay
bool find_decay(const DecayTree &tree) const
Check if the decay tree contains the given decay tree.
Definition: DecayTree.cc:134
Belle2::DecayTree::m_i
size_t m_i
Current position in the building of the DecayTree.
Definition: DecayTree.h:111
Belle2::DecayTree::build_tree
std::vector< DecayNode > build_tree(const std::string &decaystring, bool removeRadiativeGammaFlag)
Recursively build a new tree.
Definition: DecayTree.cc:74
Belle2::DecayTree::m_match_symbol_position
int m_match_symbol_position
Position of the token with the match symbol ^.
Definition: DecayTree.h:113
Belle2::DecayTree::DecayTree
DecayTree()
Default constructor.
Definition: DecayTree.h:56
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DecayNode::find_decay
bool find_decay(const DecayNode &to_find) const
Check if the decay node contains the given decay tree.
Definition: DecayNode.cc:21
Belle2::DecayNode
DecayNode describes the decay of a particle identified by its pdg code, into list of daughters.
Definition: DecayNode.h:35
Belle2::DecayTree::operator=
DecayTree & operator=(const DecayTree &tree)
Assign operator Required because we need to rebuild the node cache.
Definition: DecayTree.cc:50
Belle2::DecayNode::daughters
std::vector< DecayNode > daughters
daughter decay nodes
Definition: DecayNode.h:60
Belle2::DecayTree::m_nodes_cache
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:116
Belle2::DecayTree::build_cache
void build_cache(DecayNode &node)
Build nodes_cache in order of appearance in the decay string for fast access.
Definition: DecayTree.cc:65
Belle2::DecayTree::m_token_count
unsigned int m_token_count
Count current tokens.
Definition: DecayTree.h:112
Belle2::DecayTree::m_root_node
DecayNode m_root_node
root DecayNode
Definition: DecayTree.h:114