Belle II Software light-2406-ragdoll
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
14using namespace Belle2;
15
16DecayTree::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 // Build cache
27 } else
28 m_valid = false;
29 }
30}
31
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}
44
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}
59
61{
62 m_nodes_cache.push_back(&node);
63 for (auto& daughter : node.daughters) {
64 build_cache(daughter);
65 }
66}
67
68
69std::vector<DecayNode> DecayTree::build_tree(const std::string& decaystring, bool removeRadiativeGammaFlag)
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}
128
129bool DecayTree::find_decay(const DecayTree& tree) const
130{
131 return m_root_node.find_decay(tree.m_root_node);
132}
133
135{
136 return *(m_nodes_cache[n]);
137}
138
139const DecayNode& DecayTree::getDecayNode(unsigned int n) const
140{
141 return *(m_nodes_cache[n]);
142}
143
144
145std::string DecayTree::to_string() const
146{
147 return m_root_node.print_node();
148}
int getPDGCode() const
PDG code.
Definition: Const.h:473
static const ParticleType photon
photon particle
Definition: Const.h:673
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:45
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:139
std::vector< DecayNode > build_tree(const std::string &decaystring, bool removeRadiativeGammaFlag)
Recursively build a new tree.
Definition: DecayTree.cc:69
std::string to_string() const
Output string representation of DecayTree.
Definition: DecayTree.cc:145
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:129
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()
Default constructor.
Definition: DecayTree.h:43
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.
Definition: ClusterUtils.h:24