ATLAS Offline Software
Tree.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "./Tree.h"
6 #include <algorithm>
7 
8 Tree::Tree(const std::vector<std::size_t>& parents) :
9  m_parents(parents),
10  m_children(m_parents.size(),std::vector<std::size_t>()){
11  auto nParents = parents.size();
12 
13  // do not process node 0 to prevent inifinite loops
14  for(auto i = 1u; i < nParents; ++i){
15  m_children[m_parents[i]].push_back(i);
16  }
17 
18  auto nChildren = m_children.size();
19 
20  for(auto i = 0u; i < nChildren; ++i){
21  if(m_children[i].empty()){
22  m_leaves.push_back(i);
23  }
24  }
25 
26  for(auto i = 0u; i < nParents; ++i){
27  if (i == 0){continue;}
28  // tree numbering (tree[1] corresponds to Conditions[0]
29  if(parents[i] == 0){m_firstGeneration.push_back(i);}
30  }
31 }
32 
33 
34 std::size_t Tree::parent(std::size_t k) const {
35  return m_parents[k];
36 }
37 
38 
39 std::vector<std::size_t> Tree::siblings(std::size_t k) const {
40  return m_children[parent(k)];
41 }
42 
43 std::size_t Tree::size() const {
44  return m_parents.size();
45 }
46 
47 
48 const std::vector<std::size_t>& Tree::leaves() const {
49  return m_leaves;
50 }
51 
52 bool Tree::is_leaf(std::size_t c) const {
53  return std::find(m_leaves.begin(),
54  m_leaves.end(),
55  c) != m_leaves.end();
56 }
57 
58 
59 const std::vector<std::size_t>& Tree::firstGeneration() const {
60  return m_firstGeneration;
61 }
62 
63 
64 bool Tree::is_simple() const {
65  if (m_parents.empty()) {
66  return false;
67  } else {
68  return std::all_of(m_parents.begin(),
69  m_parents.end(),
70  [](const auto& e) {return e == 0;});
71  }
72 }
73 
74 
75 std::size_t Tree::depth(std::size_t n) const {
76  std::size_t depth{0u};
77  while (n != 0){
78  n = m_parents[n];
79  ++depth;
80  }
81  return depth;
82 }
83 
84 std::ostream& operator<< (std::ostream& out, const Tree& t){
85  out << "[";
86  auto n = t.size();
87  auto i = 0u;
88 
89  for(; i < n; ++i){out << t.parent(i) << " ";}
90 
91  out << "]";
92  return out;
93 }
94 
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Tree
Definition: Tree.h:18
Tree::siblings
std::vector< std::size_t > siblings(std::size_t) const
Definition: Tree.cxx:39
python.DecayParser.parents
parents
print ("==> buf:",buf)
Definition: DecayParser.py:31
Tree::is_simple
bool is_simple() const
Definition: Tree.cxx:64
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Tree::m_children
std::vector< std::vector< std::size_t > > m_children
Definition: Tree.h:48
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
Tree::m_parents
std::vector< std::size_t > m_parents
Definition: Tree.h:47
Tree::depth
std::size_t depth(std::size_t) const
Definition: Tree.cxx:75
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Tree::is_leaf
bool is_leaf(size_t) const
Definition: Tree.cxx:52
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
vector
Definition: MultiHisto.h:13
Tree::parent
std::size_t parent(std::size_t) const
Definition: Tree.cxx:34
Tree::m_leaves
std::vector< std::size_t > m_leaves
Definition: Tree.h:49
Tree::firstGeneration
const std::vector< std::size_t > & firstGeneration() const
Definition: Tree.cxx:59
Tree::Tree
Tree(const std::vector< std::size_t > &)
Definition: Tree.cxx:8
Tree.h
operator<<
std::ostream & operator<<(std::ostream &out, const Tree &t)
Definition: Tree.cxx:84
Tree::size
std::size_t size() const
Definition: Tree.cxx:43
Tree::m_firstGeneration
std::vector< std::size_t > m_firstGeneration
Definition: Tree.h:50
python.compressB64.c
def c
Definition: compressB64.py:93
fitman.k
k
Definition: fitman.py:528
Tree::leaves
const std::vector< std::size_t > & leaves() const
Definition: Tree.cxx:48