ATLAS Offline Software
NavGraph.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "CxxUtils/sgkey_t.h"
8 
9 #ifndef XAOD_STANDALONE // Athena or AthAnalysis
11 #endif
12 
13 namespace TrigCompositeUtils {
14 
15  NavGraphNode::NavGraphNode(const Decision* me) : m_decisionObject(me)
16  {
17  }
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 
38  }
39 
40 
41  const Decision* NavGraphNode::node() const {
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());
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())) {
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 }
203 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TrigCompositeUtils::NavGraphNode::linksTo
bool linksTo(NavGraphNode *to)
Form an edge in the graph from this node to another one.
Definition: NavGraph.cxx:29
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
TrigCompositeUtils::NavGraph::recursivePrintNavPath
void recursivePrintNavPath(const NavGraphNode &nav, size_t level, MsgStream &log, MSG::Level msgLevel) const
@bried Internal helper function.
Definition: NavGraph.cxx:172
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
index
Definition: index.py:1
TrigCompositeUtils::NavGraph::finalNodes
const std::vector< NavGraphNode * > & finalNodes() const
Get all final nodes.
Definition: NavGraph.cxx:99
TrigCompositeUtils::NavGraphNode::seeds
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
skel.it
it
Definition: skel.GENtoEVGEN.py:407
TrigCompositeUtils::NavGraph::m_edges
size_t m_edges
Statistics on the number of edges, connecting the nodes in the graph.
Definition: NavGraph.h:214
CLIDRegistry::CLIDToTypeinfo
static const std::type_info * CLIDToTypeinfo(CLID clid)
Translate between CLID and type_info.
Definition: CLIDRegistry.cxx:136
TrigCompositeUtils::NavGraph::allNodes
std::vector< NavGraphNode * > allNodes()
Get all nodes.
Definition: NavGraph.cxx:103
TrigCompositeUtils::NavGraph::addNode
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
TrigCompositeUtils::NavGraph::printAllPaths
void printAllPaths(MsgStream &log, MSG::Level msgLevel=MSG::VERBOSE) const
Helper function.
Definition: NavGraph.cxx:165
TrigCompositeUtils::NavGraphNode::dropLinks
void dropLinks(NavGraphNode *node)
Forget about any graph edges to the supplied node.
Definition: NavGraph.cxx:35
TrigCompositeUtils::NavGraph::m_nodes
std::vector< std::unique_ptr< NavGraphNode > > m_nodes
Vector of unique pointers to nodes in the graph.
Definition: NavGraph.h:212
TrigCompositeUtils::NavGraph::edges
size_t edges() const
Definition: NavGraph.cxx:119
TrigCompositeUtils::NavGraphNode::NavGraphNode
NavGraphNode(const Decision *me)
Construct a NavGraphNode shadowing a node in the full xAOD navigation graph.
Definition: NavGraph.cxx:15
TrigCompositeUtils::NavGraphNode::children
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
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
TrigCompositeUtils::NavGraphNode::m_keepFlag
bool m_keepFlag
Keep this node when slimming the NavGraph.
Definition: NavGraph.h:100
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
TrigCompositeUtils.h
TrigCompositeUtils::NavGraph::nodes
size_t nodes() const
Definition: NavGraph.cxx:114
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigCompositeUtils::initialRoIString
const std::string & initialRoIString()
Definition: TrigCompositeUtilsRoot.cxx:882
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
test_pyathena.parent
parent
Definition: test_pyathena.py:15
TrigCompositeUtils::NavGraphNode::m_decisionObject
const Decision * m_decisionObject
The Decision object node which I shadow.
Definition: NavGraph.h:97
xAOD::TrigComposite_v1
Class used to describe composite objects in the HLT.
Definition: TrigComposite_v1.h:52
DeMoUpdate.toAdd
bool toAdd
Definition: DeMoUpdate.py:1304
TrigCompositeUtils::NavGraphNode::resetKeep
void resetKeep()
Reset the keep flag to false upon finishing thinning.
Definition: NavGraph.cxx:60
TrigCompositeUtils::featureString
const std::string & featureString()
Definition: TrigCompositeUtilsRoot.cxx:898
node::name
void name(const std::string &n)
Definition: node.h:37
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
TrigCompositeUtils::NavGraph::rewireNodeForRemoval
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
TrigCompositeUtils::NavGraphNode::node
const Decision * node() const
Return a const pointer to the Decision object node which this NavGraphNode is shadowing.
Definition: NavGraph.cxx:41
xAOD::TrigComposite_v1::index_type
uint32_t index_type
Definition: TrigComposite_v1.h:56
SG::sgkey_t
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition: CxxUtils/CxxUtils/sgkey_t.h:32
TrigCompositeUtils::NavGraph::m_nodePositionMap
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
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
CLIDRegistry.h
a static registry of CLID->typeName entries. NOT for general use. Use ClassIDSvc instead.
DeMoScan.index
string index
Definition: DeMoScan.py:362
TrigCompositeUtils::NavGraph::m_finalNodes
std::vector< NavGraphNode * > m_finalNodes
Entry points into the navigation graph.
Definition: NavGraph.h:213
sgkey_t.h
Define the type used for hashed StoreGate key+CLID pairs.
TrigCompositeUtils::NavGraphNode::getKeep
bool getKeep() const
Definition: NavGraph.cxx:65
TrigCompositeUtils::NavGraphNode
Transient utility class to represent a node in a graph (m_decisionObject), and a vector of edges (m_f...
Definition: NavGraph.h:20
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
TrigCompositeUtils::NavGraphNode::keep
void keep()
Flag this node as one to keep when the thin() operation is performed.
Definition: NavGraph.cxx:55
TrigCompositeUtils::NavGraphNode::m_filteredSeeds
std::vector< NavGraphNode * > m_filteredSeeds
My seeds (edges in the graph), filtered on per-chain requirements.
Definition: NavGraph.h:98
TrigCompositeUtils::NavGraphNode::addIfNotDuplicate
static bool addIfNotDuplicate(std::vector< NavGraphNode * > &container, NavGraphNode *toAdd)
Internal helper function.
Definition: NavGraph.cxx:19
TrigCompositeUtils
Definition: Event/xAOD/xAODTrigger/xAODTrigger/TrigComposite.h:19
NavGraph.h
TrigCompositeUtils::roiString
const std::string & roiString()
Definition: TrigCompositeUtilsRoot.cxx:890
TrigCompositeUtils::NavGraphNode::m_filteredChildren
std::vector< NavGraphNode * > m_filteredChildren
Two-way linking information, used when thinning the graph.
Definition: NavGraph.h:99
TrigCompositeUtils::viewString
const std::string & viewString()
Definition: TrigCompositeUtilsRoot.cxx:894
TrigCompositeUtils::NavGraph::thin
std::vector< const Decision * > thin()
Perform thinning.
Definition: NavGraph.cxx:123
node
Definition: node.h:21
TrigCompositeUtils::decisionToElementLink
ElementLink< DecisionContainer > decisionToElementLink(const Decision *d, const EventContext &ctx)
Takes a raw pointer to a Decision and returns an ElementLink to the Decision.
Definition: TrigCompositeUtilsRoot.cxx:122
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37