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