Belle II Software  release-06-02-00
DecayTree.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <analysis/utility/DecayTree.h>
10 #include <framework/gearbox/Const.h>
11 
12 #include <stdexcept>
13 
14 using namespace Belle2;
15 
16 DecayTree::DecayTree(const std::string& decaystring, bool removeRadiativeGammaFlag) : m_i(0), m_token_count(0),
17  m_match_symbol_position(-1)
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  else
26  m_valid = false;
27  }
28 
29  // Build cache
30  if (m_valid) {
32  }
33 
34 }
35 
37  m_root_node(tree.m_root_node)
38 {
39  m_valid = tree.m_valid;
40  m_token_count = tree.m_token_count;
41  m_match_symbol_position = tree.m_match_symbol_position;
42  m_i = tree.m_i;
43 
44  if (m_valid) {
46  }
47 }
48 
50 {
51  m_valid = tree.m_valid;
52  m_token_count = tree.m_token_count;
53  m_match_symbol_position = tree.m_match_symbol_position;
54  m_root_node = tree.m_root_node;
55  m_i = tree.m_i;
56 
57  if (m_valid) {
59  }
60 
61  return *this;
62 }
63 
65 {
66  m_nodes_cache.push_back(&node);
67  for (auto& daughter : node.daughters) {
68  build_cache(daughter);
69  }
70 }
71 
72 
73 std::vector<DecayNode> DecayTree::build_tree(const std::string& decaystring, bool removeRadiativeGammaFlag)
74 {
75 
76  const unsigned int N = decaystring.size();
77  std::vector<DecayNode> nodes;
78  nodes.reserve(5);
79 
80  for (; m_i < N; ++m_i) {
81  // Skip ' ', '^' and "->"
82  if (decaystring[m_i] == ' ' or decaystring[m_i] == '>' or (m_i + 1 < N and decaystring[m_i] == '-'
83  and decaystring[m_i + 1] == '>')) {
84  continue;
85  }
86 
87  // Match symbol
88  if (decaystring[m_i] == '^') {
90  continue;
91  }
92 
93  // Handle start of subdecay of the form X ( ... )
94  if (decaystring[m_i] == '(') {
95  m_i++;
96  nodes.back().daughters = this->build_tree(decaystring, removeRadiativeGammaFlag);
97  continue;
98  }
99 
100  // Handle end of subdecay
101  if (decaystring[m_i] == ')') {
102  return nodes;
103  }
104 
105  // Search end of token
106  unsigned int j = m_i + 1;
107  for (; j < N; ++j) {
108  if (decaystring[j] == ' ' or decaystring[j] == ')' or decaystring[j] == '(')
109  break;
110  }
111 
112  // Parse token and add it as new node
113  try {
114  const int pdg = std::stoi(decaystring.substr(m_i, j - m_i));
115  if (removeRadiativeGammaFlag) {
116  if (nodes.size() < 2 or pdg != Const::photon.getPDGCode()) {
117  nodes.emplace_back(pdg);
118  m_token_count++;
119  }
120  } else {
121  nodes.emplace_back(pdg);
122  m_token_count++;
123  }
124  m_i = j - 1;
125  } catch (const std::invalid_argument&) {
126  // We ignore if the token cannot be parsed
127  }
128  }
129 
130  return nodes;
131 }
132 
133 bool DecayTree::find_decay(const DecayTree& tree) const
134 {
135  return m_root_node.find_decay(tree.m_root_node);
136 }
137 
139 {
140  return *(m_nodes_cache[n]);
141 }
142 
143 const DecayNode& DecayTree::getDecayNode(unsigned int n) const
144 {
145  return *(m_nodes_cache[n]);
146 }
147 
148 
149 std::string DecayTree::to_string() const
150 {
151  return m_root_node.print_node();
152 }
int getPDGCode() const
PDG code.
Definition: Const.h:354
static const ParticleType photon
photon particle
Definition: Const.h:554
DecayNode describes the decay of a particle identified by its pdg code, into list of daughters.
Definition: DecayNode.h:23
std::vector< DecayNode > daughters
daughter decay nodes
Definition: DecayNode.h:48
This is a helper class for the MCDecayFinderModule.
Definition: DecayTree.h:20
DecayTree & operator=(const DecayTree &tree)
Assign operator Required because we need to rebuild the node cache.
Definition: DecayTree.cc:49
unsigned int m_token_count
Count current tokens.
Definition: DecayTree.h:102
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
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
const DecayNode & getDecayNode(unsigned int n) const
Returns n-th node as const.
Definition: DecayTree.cc:143
std::vector< DecayNode > build_tree(const std::string &decaystring, bool removeRadiativeGammaFlag)
Recursively build a new tree.
Definition: DecayTree.cc:73
std::string to_string() const
Output string representation of DecayTree.
Definition: DecayTree.cc:149
int m_match_symbol_position
Position of the token with the match symbol ^.
Definition: DecayTree.h:103
bool find_decay(const DecayTree &tree) const
Check if the decay tree contains the given decay tree.
Definition: DecayTree.cc:133
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:64
DecayTree()
Default constructor.
Definition: DecayTree.h:46
std::string print_node(unsigned int indent=0) const
Output a single node.
Definition: DecayNode.cc:30
bool find_decay(const DecayNode &to_find) const
Check if the decay node contains the given decay tree.
Definition: DecayNode.cc:19
Abstract base class for different kinds of events.