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  m_filteredSeeds(), m_filteredChildren(), m_keepFlag(false)
17  {
18  }
19 
20  bool NavGraphNode::addIfNotDuplicate(std::vector<NavGraphNode*>& container, NavGraphNode* toAdd) {
21  std::vector<NavGraphNode*>::iterator 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 
39  }
40 
41 
42  const Decision* NavGraphNode::node() const {
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  NavGraph::NavGraph() : m_nodePositionMap(), m_nodes(), m_finalNodes(), m_edges(0) {
75  }
76 
77 
78  void NavGraph::addNode(const Decision* node, const Decision* comingFrom) {
79  // m_node is a vector to preserve iteration ordering for stable output.
80  // m_nodePositionMap assures that there is no duplicated NavGraphNode
81  // with the same Decision pointer.
82  auto nodePairIt = m_nodePositionMap.insert(std::make_pair(node, m_nodes.size()));
83  if (nodePairIt.second) m_nodes.push_back( std::unique_ptr<NavGraphNode>(new NavGraphNode(node)) );
84  NavGraphNode& nodeObj = *m_nodes[nodePairIt.first->second];
85 
86  if (comingFrom == nullptr) { // Not coming from anywhere - hence a final node.
87  m_finalNodes.push_back( &nodeObj );
88  } else {
89  auto comingFromPairIt = m_nodePositionMap.insert( std::make_pair(comingFrom, m_nodes.size()) );
90  if (comingFromPairIt.second) m_nodes.push_back( std::unique_ptr<NavGraphNode>(new NavGraphNode(comingFrom)) );
91  NavGraphNode& comingFromNodeObj = *m_nodes[comingFromPairIt.first->second];
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());
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 
166  m_nodePositionMap.clear();
167  m_nodes.clear();
168  m_finalNodes.clear();
169  m_edges = 0;
170  }
171 
172  void NavGraph::printAllPaths(MsgStream& log, MSG::Level msgLevel) const {
173  for (const NavGraphNode* finalNode : m_finalNodes) {
174  recursivePrintNavPath(*finalNode, 0, log, msgLevel);
175  }
176  }
177 
178 
179  void NavGraph::recursivePrintNavPath(const NavGraphNode& nav, size_t level, MsgStream& log, MSG::Level msgLevel) const {
180  const Decision* node = nav.node();
182  std::stringstream ss;
183  for (size_t i = 0; i < level; ++i) {
184  ss << " ";
185  }
186 
187  ss << "|-> " << nodeEL.dataID() << " #" << nodeEL.index() << " Name(" << node->name() << ") Passing(" << node->decisions().size() << ")";
188  if (nav.getKeep()) ss << " [KEEP]";
189  if (node->hasObjectLink(featureString())) {
191  uint32_t clid;
192  uint16_t index;
193  node->typelessGetObjectLink(featureString(), key, clid, index);
194 #ifndef XAOD_STANDALONE // Athena or AthAnalysis
195  ss << " Feature(#" << index << ", " << CLIDRegistry::CLIDToTypeinfo(clid)->name() << ", " << key << ")";
196 #else
197  ss << " Feature(#" << index << ", " << key << ")";
198 #endif
199  }
200  if (node->hasObjectLink(viewString())) ss << " [View]";
201  if (node->hasObjectLink(roiString())) ss << " [RoI]";
202  if (node->hasObjectLink(initialRoIString())) ss << " [InitialRoI]";
203  log << msgLevel << ss.str() << endmsg;
204  for (const NavGraphNode* seed : nav.seeds()) {
205  recursivePrintNavPath(*seed, level + 1, log, msgLevel);
206  }
207  }
208 
209 }
210 
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:30
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:179
TrigCompositeUtils.h
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:100
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:47
skel.it
it
Definition: skel.GENtoEVGEN.py:423
TrigCompositeUtils::NavGraph::m_edges
size_t m_edges
Statistics on the number of edges, connecting the nodes in the graph.
Definition: NavGraph.h:219
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:104
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:78
TrigCompositeUtils::NavGraph::printAllPaths
void printAllPaths(MsgStream &log, MSG::Level msgLevel=MSG::VERBOSE) const
Helper function.
Definition: NavGraph.cxx:172
TrigCompositeUtils::NavGraphNode::dropLinks
void dropLinks(NavGraphNode *node)
Forget about any graph edges to the supplied node.
Definition: NavGraph.cxx:36
TrigCompositeUtils::NavGraph::m_nodes
std::vector< std::unique_ptr< NavGraphNode > > m_nodes
Vector of unique pointers to nodes in the graph.
Definition: NavGraph.h:217
TrigCompositeUtils::NavGraph::edges
size_t edges() const
Definition: NavGraph.cxx:120
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:51
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::NavGraph::nodes
size_t nodes() const
Definition: NavGraph.cxx:115
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigCompositeUtils::initialRoIString
const std::string & initialRoIString()
Definition: TrigCompositeUtilsRoot.cxx:868
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
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
TrigCompositeUtils::NavGraph::NavGraph
NavGraph()
Construct an empty NavGraph.
Definition: NavGraph.cxx:74
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:61
TrigCompositeUtils::featureString
const std::string & featureString()
Definition: TrigCompositeUtilsRoot.cxx:884
node::name
void name(const std::string &n)
Definition: node.h:38
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:140
TrigCompositeUtils::NavGraphNode::node
const Decision * node() const
Return a const pointer to the Decision object node which this NavGraphNode is shadowing.
Definition: NavGraph.cxx:42
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:216
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:364
TrigCompositeUtils::NavGraph::m_finalNodes
std::vector< NavGraphNode * > m_finalNodes
Entry points into the navigation graph.
Definition: NavGraph.h:218
sgkey_t.h
Define the type used for hashed StoreGate key+CLID pairs.
TrigCompositeUtils::NavGraphNode::getKeep
bool getKeep() const
Definition: NavGraph.cxx:66
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:56
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:20
TrigCompositeUtils
Definition: Event/xAOD/xAODTrigger/xAODTrigger/TrigComposite.h:19
NavGraph.h
TrigCompositeUtils::roiString
const std::string & roiString()
Definition: TrigCompositeUtilsRoot.cxx:876
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:880
TrigCompositeUtils::NavGraph::thin
std::vector< const Decision * > thin()
Perform thinning.
Definition: NavGraph.cxx:124
node
Definition: memory_hooks-stdcmalloc.h:74
TrigCompositeUtils::NavGraph::reset
void reset()
Resets the object, clearing any transient graph.
Definition: NavGraph.cxx:165
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37