ATLAS Offline Software
Loading...
Searching...
No Matches
NavGraph.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
7#include "SGCore/sgkey_t.h"
8
9#ifndef XAOD_STANDALONE // Athena or AthAnalysis
11#endif
12
13namespace TrigCompositeUtils {
14
18
19 bool NavGraphNode::addIfNotDuplicate(std::vector<NavGraphNode*>& container, NavGraphNode* toAdd) {
20 const auto it = std::find(container.begin(), container.end(), toAdd);
21 if (it == container.end()) {
22 container.push_back(toAdd);
23 return true;
24 }
25 return false;
26 }
27
28
30 addIfNotDuplicate(to->m_filteredChildren, this);
31 return addIfNotDuplicate(m_filteredSeeds, to); // Return TRUE if a new edge is added
32 }
33
34
39
40
42 return m_decisionObject;
43 }
44
45
46 const std::vector<NavGraphNode*>& NavGraphNode::seeds() const {
47 return m_filteredSeeds;
48 }
49
50 const std::vector<NavGraphNode*>& NavGraphNode::children() const {
51 return m_filteredChildren;
52 }
53
54
56 m_keepFlag = true;
57 }
58
59
61 m_keepFlag = false;
62 }
63
64
65 bool NavGraphNode::getKeep() const {
66 return m_keepFlag;
67 }
68
69
70 // #################################################
71
72
73 void NavGraph::addNode(const Decision* node, const Decision* comingFrom) {
74 // m_nodes is a vector to preserve iteration ordering for stable output.
75 // m_nodePositionMap assures that there is no duplicated NavGraphNode
76 // with the same Decision pointer.
77 const auto& [nodeItr, newNode] = m_nodePositionMap.emplace(node, m_nodes.size());
78 if (newNode) {
79 m_nodes.emplace_back( std::make_unique<NavGraphNode>(node) );
80 }
81 NavGraphNode* nodeObj = m_nodes[nodeItr->second].get();
82
83 if (comingFrom == nullptr) { // Not coming from anywhere - hence a final node.
84 m_finalNodes.push_back( nodeObj );
85 } else {
86 const auto& [nodeItr, newNode] = m_nodePositionMap.emplace(comingFrom, m_nodes.size());
87 if (newNode) {
88 m_nodes.emplace_back( std::make_unique<NavGraphNode>(comingFrom) );
89 }
90 NavGraphNode* comingFromNodeObj = m_nodes[nodeItr->second].get();
91 const bool newEdge = comingFromNodeObj->linksTo( nodeObj );
92 if (newEdge) {
93 ++m_edges;
94 }
95 }
96 }
97
98
99 const std::vector<NavGraphNode*>& NavGraph::finalNodes() const {
100 return m_finalNodes;
101 }
102
103 std::vector<NavGraphNode*> NavGraph::allNodes() {
104 std::vector<NavGraphNode*> returnVec;
105 returnVec.reserve(m_nodes.size());
106 for (std::unique_ptr<NavGraphNode>& entry : m_nodes) {
107 NavGraphNode& nodeObj = *entry;
108 returnVec.push_back( &nodeObj );
109 }
110 return returnVec;
111 }
112
113
114 size_t NavGraph::nodes() const {
115 return m_nodes.size();
116 }
117
118
119 size_t NavGraph::edges() const {
120 return m_edges;
121 }
122
123 std::vector<const Decision*> NavGraph::thin() {
124 std::vector<const Decision*> returnVec;
125 std::vector<std::unique_ptr<NavGraphNode>>::iterator it;
126 for (it = m_nodes.begin(); it != m_nodes.end(); /*noop*/) {
127 if ((*it)->getKeep()) {
128 (*it)->resetKeep();
129 ++it;
130 } else {
131 returnVec.push_back((*it)->node());
132 rewireNodeForRemoval(*(*it));
133 it = m_nodes.erase(it);
134 }
135 }
136 return returnVec;
137 }
138
140 const std::vector<NavGraphNode*> myParents = toBeDeleted.seeds();
141 const std::vector<NavGraphNode*> myChildren = toBeDeleted.children();
142
143 // Perform the (potentially) many-to-many re-linking required to remove toBeDeleted from the graph
144 for (NavGraphNode* child : myChildren) {
145 for (NavGraphNode* parent : myParents) {
146 bool newEdge = child->linksTo(parent);
147 if (newEdge) {
148 ++m_edges;
149 }
150 }
151 }
152
153 // Remove the edges connecting to toBeDeleted from all of its children and all of parents
154 for (NavGraphNode* child : myChildren) {
155 child->dropLinks(&toBeDeleted);
156 --m_edges;
157 }
158 for (NavGraphNode* parent : myParents) {
159 parent->dropLinks(&toBeDeleted);
160 --m_edges;
161 }
162 }
163
164
165 void NavGraph::printAllPaths(MsgStream& log, MSG::Level msgLevel) const {
166 for (const NavGraphNode* finalNode : m_finalNodes) {
167 recursivePrintNavPath(*finalNode, 0, log, msgLevel);
168 }
169 }
170
171
172 void NavGraph::recursivePrintNavPath(const NavGraphNode& nav, size_t level, MsgStream& log, MSG::Level msgLevel) const {
173 const Decision* node = nav.node();
174 const ElementLink<DecisionContainer> nodeEL = decisionToElementLink( node, Gaudi::Hive::currentContext() );
175 std::stringstream ss;
176 for (size_t i = 0; i < level; ++i) {
177 ss << " ";
178 }
179
180 ss << "|-> " << nodeEL.dataID() << " #" << nodeEL.index() << " Name(" << node->name() << ") Passing(" << node->decisions().size() << ")";
181 if (nav.getKeep()) ss << " [KEEP]";
182 if (node->hasObjectLink(featureString())) {
183 SG::sgkey_t key;
184 uint32_t clid;
186 node->typelessGetObjectLink(featureString(), key, clid, index);
187#ifndef XAOD_STANDALONE // Athena or AthAnalysis
188 ss << " Feature(#" << index << ", " << CLIDRegistry::CLIDToTypeinfo(clid)->name() << ", " << key << ")";
189#else
190 ss << " Feature(#" << index << ", " << key << ")";
191#endif
192 }
193 if (node->hasObjectLink(viewString())) ss << " [View]";
194 if (node->hasObjectLink(roiString())) ss << " [RoI]";
195 if (node->hasObjectLink(initialRoIString())) ss << " [InitialRoI]";
196 log << msgLevel << ss.str() << endmsg;
197 for (const NavGraphNode* seed : nav.seeds()) {
198 recursivePrintNavPath(*seed, level + 1, log, msgLevel);
199 }
200 }
201
202}
#define endmsg
a static registry of CLID->typeName entries.
static Double_t ss
static const std::type_info * CLIDToTypeinfo(CLID clid)
Translate between CLID and type_info.
Transient utility class to represent a node in a graph (m_decisionObject), and a vector of edges (m_f...
Definition NavGraph.h:20
const std::vector< NavGraphNode * > & children() const
Return a vector of const pointers to the Decision object nodes which are the children of this NavGrap...
Definition NavGraph.cxx:50
std::vector< NavGraphNode * > m_filteredSeeds
My seeds (edges in the graph), filtered on per-chain requirements.
Definition NavGraph.h:98
const Decision * m_decisionObject
The Decision object node which I shadow.
Definition NavGraph.h:97
static bool addIfNotDuplicate(std::vector< NavGraphNode * > &container, NavGraphNode *toAdd)
Internal helper function.
Definition NavGraph.cxx:19
bool linksTo(NavGraphNode *to)
Form an edge in the graph from this node to another one.
Definition NavGraph.cxx:29
void dropLinks(NavGraphNode *node)
Forget about any graph edges to the supplied node.
Definition NavGraph.cxx:35
std::vector< NavGraphNode * > m_filteredChildren
Two-way linking information, used when thinning the graph.
Definition NavGraph.h:99
bool m_keepFlag
Keep this node when slimming the NavGraph.
Definition NavGraph.h:100
NavGraphNode(const Decision *me)
Construct a NavGraphNode shadowing a node in the full xAOD navigation graph.
Definition NavGraph.cxx:15
void resetKeep()
Reset the keep flag to false upon finishing thinning.
Definition NavGraph.cxx:60
void keep()
Flag this node as one to keep when the thin() operation is performed.
Definition NavGraph.cxx:55
const std::vector< NavGraphNode * > & seeds() const
Return a vector of const pointers to the Decision object nodes which this NavGraphNode seeds from.
Definition NavGraph.cxx:46
const Decision * node() const
Return a const pointer to the Decision object node which this NavGraphNode is shadowing.
Definition NavGraph.cxx:41
std::vector< NavGraphNode * > allNodes()
Get all nodes.
Definition NavGraph.cxx:103
void addNode(const Decision *node, const Decision *comingFrom=nullptr)
Add a new NavGraphNode which shadows the xAOD Decision object "node" from the full navigation graph.
Definition NavGraph.cxx:73
std::vector< NavGraphNode * > m_finalNodes
Entry points into the navigation graph.
Definition NavGraph.h:213
const std::vector< NavGraphNode * > & finalNodes() const
Get all final nodes.
Definition NavGraph.cxx:99
std::map< const Decision *, size_t > m_nodePositionMap
Map of Decision pointer and index of the node(that contains the Decision) in m_nodes.
Definition NavGraph.h:211
void printAllPaths(MsgStream &log, MSG::Level msgLevel=MSG::VERBOSE) const
Helper function.
Definition NavGraph.cxx:165
void recursivePrintNavPath(const NavGraphNode &nav, size_t level, MsgStream &log, MSG::Level msgLevel) const
@bried Internal helper function.
Definition NavGraph.cxx:172
std::vector< std::unique_ptr< NavGraphNode > > m_nodes
Vector of unique pointers to nodes in the graph.
Definition NavGraph.h:212
size_t m_edges
Statistics on the number of edges, connecting the nodes in the graph.
Definition NavGraph.h:214
std::vector< const Decision * > thin()
Perform thinning.
Definition NavGraph.cxx:123
void rewireNodeForRemoval(NavGraphNode &toBeDeleted)
Take all seeds (parents) of the supplied node and connect them to all the node's children.
Definition NavGraph.cxx:139
Definition node.h:24
void name(const std::string &n)
Definition node.h:40
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition sgkey_t.h:32
const std::string & viewString()
const std::string & roiString()
const std::string & featureString()
const std::string & initialRoIString()
ElementLink< DecisionContainer > decisionToElementLink(const Decision *d, const EventContext &ctx)
Takes a raw pointer to a Decision and returns an ElementLink to the Decision.
Definition index.py:1
DataModel_detail::iterator< DVL > remove(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, const T &value)
Specialization of remove for DataVector/List.