ComPWA
Common Partial-Wave-Analysis Framework
TreeNode.hpp
Go to the documentation of this file.
1 // Copyright (c) 2013 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
9 
10 #ifndef COMPWA_FUNCTIONTREE_TREENODE_HPP_
11 #define COMPWA_FUNCTIONTREE_TREENODE_HPP_
12 
13 #include <complex>
14 #include <memory>
15 #include <string>
16 
20 
21 namespace ComPWA {
22 namespace FunctionTree {
23 
35 class TreeNode : public std::enable_shared_from_this<TreeNode>,
36  public ParObserver {
37 public:
40  TreeNode(std::shared_ptr<Strategy> strategy);
43  TreeNode(std::shared_ptr<Parameter> parameter,
44  std::shared_ptr<Strategy> strategy = nullptr);
45 
46  virtual ~TreeNode();
47 
48  void removeExpiredParents();
49 
50  void addNode(std::shared_ptr<TreeNode> node);
51  void addNodes(std::vector<std::shared_ptr<TreeNode>> nodes);
52  void addParent(std::weak_ptr<TreeNode> node);
53 
56  std::shared_ptr<Parameter> parameter();
57 
60  void fillParameters(ParameterList &list);
61 
63  void update();
64 
65  friend std::ostream &operator<<(std::ostream &os,
66  std::shared_ptr<TreeNode> p) {
67  return os << p->print(-1);
68  }
69 
73  std::string print(int level = -1, std::string prefix = "");
74 
75 private:
77  std::vector<std::weak_ptr<TreeNode>> Parents;
78 
80  std::vector<std::shared_ptr<TreeNode>> ChildNodes;
81 
83  std::shared_ptr<ComPWA::FunctionTree::Parameter> OutputParameter;
84 
86  bool HasChanged;
87 
90  std::shared_ptr<Strategy> Strat;
91 
94  std::shared_ptr<ComPWA::FunctionTree::Parameter> recalculate();
95 };
96 
97 std::shared_ptr<TreeNode> createLeaf(std::shared_ptr<Parameter> parameter);
98 
100 template <typename T,
101  typename = std::enable_if_t<
102  (std::is_same<int, T>::value || std::is_same<double, T>::value ||
103  std::is_same<std::complex<double>, T>::value ||
104  std::is_same<std::vector<int>, T>::value ||
105  std::is_same<std::vector<double>, T>::value ||
106  std::is_same<std::vector<std::complex<double>>, T>::value)>>
107 std::shared_ptr<TreeNode> createLeaf(const T &value, std::string name = "") {
108  auto leaf =
109  std::make_shared<TreeNode>(std::make_shared<Value<T>>(name, value));
110  return leaf;
111 }
112 
113 } // namespace FunctionTree
114 } // namespace ComPWA
115 
116 #endif
void fillParameters(ParameterList &list)
Fill ParameterList with parameters.
Definition: TreeNode.cpp:119
ParObserver class.
bool HasChanged
Node has changed and needs to call recalculate()
Definition: TreeNode.hpp:86
ParObserver Base class parameter observer.
Definition: ParObserver.hpp:23
std::string print(int level=-1, std::string prefix="")
Print node and its child nodes to std::string.
Definition: TreeNode.cpp:126
std::shared_ptr< Parameter > parameter()
Obtain parameter of node.
Definition: TreeNode.cpp:71
TreeNode(std::shared_ptr< Strategy > strategy)
Constructor for tree using a strategy.
Definition: TreeNode.cpp:15
ParameterList class.
TreeNode is the basic building block of the FunctionTree.
Definition: TreeNode.hpp:35
std::shared_ptr< Strategy > Strat
Node strategy.
Definition: TreeNode.hpp:90
This file contains Functions implementing the Strategy interface so they can be used inside a node of...
std::shared_ptr< ComPWA::FunctionTree::Parameter > OutputParameter
(cached) node value
Definition: TreeNode.hpp:83
void addNode(std::shared_ptr< TreeNode > node)
Definition: TreeNode.cpp:48
std::vector< std::weak_ptr< TreeNode > > Parents
List of parent nodes.
Definition: TreeNode.hpp:77
std::shared_ptr< TreeNode > createLeaf(std::shared_ptr< Parameter > parameter)
Definition: TreeNode.cpp:156
friend std::ostream & operator<<(std::ostream &os, std::shared_ptr< TreeNode > p)
Definition: TreeNode.hpp:65
void addParent(std::weak_ptr< TreeNode > node)
Definition: TreeNode.cpp:61
std::vector< std::shared_ptr< TreeNode > > ChildNodes
Definition: TreeNode.hpp:80
void addNodes(std::vector< std::shared_ptr< TreeNode >> nodes)
Definition: TreeNode.cpp:53
std::shared_ptr< ComPWA::FunctionTree::Parameter > recalculate()
Obtain parameter of node.
Definition: TreeNode.cpp:90
This class provides a list of parameters and values of different types.
void update()
Flags the node as modified. Should only be called from its child nodes.
Definition: TreeNode.cpp:65