2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5 #include "AsgMessaging/MessageCheck.h"
6 #include "AsgTools/CurrentContext.h"
9 namespace TrigCompositeUtils {
11 ANA_MSG_HEADER (msgFindLink)
14 * @brief Creates and right away records the Container CONT with the key.
16 * Returns the WriteHandle.
17 * If possible provide the context that comes via an argument to execute otherwise it will default to looking it up which is slower.
20 SG::WriteHandle<CONT> createAndStoreNoAux( const SG::WriteHandleKey<CONT>& key, const EventContext& ctx ) {
21 SG::WriteHandle<CONT> handle( key, ctx );
22 auto data = std::make_unique<CONT>() ;
23 if (handle.record( std::move( data ) ).isFailure()) {
24 throw std::runtime_error( "ERROR in TrigCompositeUtils::createAndStoreNoAux Unable to record " + key.key());
30 * @brief Creates and right away records the Container CONT with the key.
32 * Returns the WriteHandle.
33 * If possible provide the context that comes via an argument to execute otherwise it will default to looking it up which is slower.
35 template<class CONT, class AUX>
36 SG::WriteHandle<CONT> createAndStoreWithAux( const SG::WriteHandleKey<CONT>& key, const EventContext& ctx ) {
37 SG::WriteHandle<CONT> handle( key, ctx );
38 auto data = std::make_unique<CONT>() ;
39 auto aux = std::make_unique<AUX>() ;
40 data->setStore( aux.get() );
41 if (handle.record( std::move( data ), std::move( aux ) ).isFailure()) {
42 throw std::runtime_error( "ERROR in TrigCompositeUtils::createAndStoreWithAux Unable to record " + key.key());
49 findLinks(const Decision* start, const std::string& linkName, std::vector<LinkInfo<T>>& links, unsigned int behaviour, std::set<const Decision*>* fullyExploredFrom) {
50 using namespace msgFindLink;
51 std::vector<ElementLink<T>> featureLinks;
52 if (start->hasObjectCollectionLinks(linkName, ClassID_traits<T>::ID())) {
53 featureLinks = start->objectCollectionLinks<T>(linkName);
55 if (start->hasObjectLink(linkName, ClassID_traits<T>::ID())) {
56 featureLinks.push_back(start->objectLink<T>(linkName));
58 const bool linkFound = (featureLinks.size() > 0);
59 const std::vector<DecisionID> &ids = decisionIDs(start);
60 const EventContext& ctx = Gaudi::Hive::currentContext();
61 for (const ElementLink<T>& featureLink : featureLinks) {
62 // Check for duplicates
63 bool duplicate = false;
64 for (LinkInfo<T> &info : links)
66 if (info.link == featureLink) {
67 const auto A = decisionToElementLink(start, ctx);
68 const auto& B = info.link;
69 if (info.source != start
70 && linkName != initialRoIString() // Allow multiple nodes to reference the same initial ROI
71 && linkName != roiString() // Allow multiple nodes to reference the same updated ROI
72 && linkName != "l2cbroi" // Allow multiple nodes to reference the same l2cb ROI (muon specific use case)
73 && linkName != viewString() // Allow multiple nodes to reference the same view instance (i.e. re-use of EventViews)
74 && A.dataID().find("_probe") == std::string::npos // Allow re-use if one of the two locations is a tag-and-probe style probe component
75 && B.dataID().find("_probe") == std::string::npos
77 ANA_MSG_WARNING ("Found '" << linkName << "' by multiple paths, but "
78 << "the links are coming from different places in the navigation graph. " << std::endl
79 << "From A:" << A.dataID() << ":" << A.index() << " Dump:" << std::endl << *start << std::endl
80 << "From B:" << B.dataID() << ":" << B.index() << " Dump:" << std::endl << *(info.source));
82 if (behaviour & TrigDefs::fillDecisions) {
83 if (info.decisions.has_value())
84 info.decisions->insert(ids.begin(), ids.end());
86 info.decisions.emplace(ids.begin(), ids.end());
92 links.emplace_back(start, featureLink);
95 if (linkFound && (behaviour & TrigDefs::lastFeatureOfType)) {
98 // If not Early Exit, then recurse
99 for (const auto& seed : getLinkToPrevious(start)) {
100 #if TRIGCOMPUTILS_ENABLE_EARLY_EXIT == 1
101 if (fullyExploredFrom != nullptr) {
102 // We only need to recursivly explore back from each node in the graph once.
103 // We can keep a record of nodes which we have already explored, these we can safely skip over.
104 if (fullyExploredFrom->count(*seed) == 1) {
109 findLinks<T>(*seed, linkName, links, behaviour, fullyExploredFrom);
111 // Fully explored this node
112 if (fullyExploredFrom != nullptr) {
113 fullyExploredFrom->insert(start);
118 std::vector<LinkInfo<T>>
119 findLinks(const Decision* start, const std::string& linkName, unsigned int behaviour) {
120 std::vector<LinkInfo<T>> links;
121 std::set<const Decision*> fullyExploredFrom;
122 findLinks(start, linkName, links, behaviour, &fullyExploredFrom);
128 findLink(const Decision* start, const std::string& linkName, const bool suppressMultipleLinksWarning) {
129 using namespace msgFindLink;
130 // We use findLink in cases where there is only one link to be found, or if there are multiple then we
131 // only want the most recent.
132 // Hence we can supply TrigDefs::lastFeatureOfType. /--> parent3(link)
133 // We can still have more then one link found if there is a branch in the navigation. E.g. start --> parent1 --> parent2(link)
134 // If both parent2 and parent3 posessed an admisable ElementLink, then the warning below will trigger, and only one of the
135 // links will be returned (whichever of parent2 or parent3 happened to be the first seed of parent1).
136 std::vector<LinkInfo<T>> links = findLinks<T>(start, linkName, TrigDefs::lastFeatureOfType);
137 if (links.size() > 1 && !suppressMultipleLinksWarning) {
138 ANA_MSG_WARNING (links.size() << " links found for " << linkName
139 << " returning the first link, consider using findLinks.");
141 if (links.size() > 0) {
144 return LinkInfo<T>(); // invalid link
148 template<class CONTAINER>
149 void filterLinkVectorByContainerKey(const std::string& containerSGKey, std::vector<ElementLink<CONTAINER>>& vector) {
150 if (containerSGKey.empty()) {
153 auto it = std::remove_if(vector.begin(), vector.end(), [&](const ElementLink<CONTAINER>& el) {
154 return !std::regex_match( el.dataID(), std::regex(containerSGKey) );
156 // Collection has been re-ordered to put the bad elements at the end
157 vector.erase(it, vector.end());
161 template<class CONTAINER>
162 const std::vector< LinkInfo<CONTAINER> > recursiveGetFeaturesOfType(
163 const NavGraph& navGraph,
164 const std::string& containerSGKey,
165 const unsigned int behaviour,
166 const std::string& navElementLinkKey,
167 const DecisionIDContainer& chainIDs) {
169 std::vector< LinkInfo<CONTAINER> > features; // The return vector
170 std::set<const NavGraphNode*> fullyExploredFrom; // Lets us navigate more efficiently
172 // For each starting point through the navigation for a given chain-group
173 for (const NavGraphNode* finalNode : navGraph.finalNodes()) {
174 recursiveGetFeaturesOfTypeInternal<CONTAINER>(features,
187 template<class CONTAINER>
188 void recursiveGetFeaturesOfTypeInternal(
189 std::vector< LinkInfo<CONTAINER> >& features,
190 std::set<const NavGraphNode*>& fullyExploredFrom,
191 const NavGraphNode* navGraphNode,
192 const std::string& containerSGKey,
193 const unsigned int behaviour,
194 const std::string& navElementLinkKey,
195 const DecisionIDContainer& chainIDs) {
197 const Decision* decisionObj = navGraphNode->node();
198 const Decision* decisionObjChild = (navGraphNode->children().size() == 1 ? navGraphNode->children().at(0)->node() : nullptr);
199 const std::vector<DecisionID> &ids = decisionIDs(decisionObj);
200 std::vector<ElementLink<CONTAINER>> featureLinks;
202 // Look up what named links are available in the Decision Object
203 std::vector<std::string> availableLinkNames;
204 if (navElementLinkKey == "") {
205 const std::vector<std::string> getSingleLinkNames = decisionObj->getObjectNames<CONTAINER>();
206 const std::vector<std::string> getCollectionLinkNames = decisionObj->getObjectCollectionNames<CONTAINER>();
207 std::copy(getSingleLinkNames.begin(), getSingleLinkNames.end(), std::back_inserter(availableLinkNames));
208 std::copy(getCollectionLinkNames.begin(), getCollectionLinkNames.end(), std::back_inserter(availableLinkNames));
209 } else { // Just looking for an explicitly named feature
210 availableLinkNames.push_back( navElementLinkKey );
213 // Fetch the named links that we're interested in
214 for (const std::string& featureNameToGet : availableLinkNames) {
215 // This try block protects against ExcCLIDMismatch throws from
216 // features which do not derive from IParticle, when an IParticle interface is requested.
217 #ifndef XAOD_STANDALONE
220 // Slices may have added link collections. These links may have been to objects in different containers.
221 if (decisionObj->hasObjectCollectionLinks(featureNameToGet, ClassID_traits< CONTAINER >::ID())) {
222 std::vector<ElementLink<CONTAINER>> collectionLinks = decisionObj->objectCollectionLinks<CONTAINER>( featureNameToGet );
223 filterLinkVectorByContainerKey<CONTAINER>(containerSGKey, collectionLinks);
224 std::copy(collectionLinks.begin(), collectionLinks.end(), std::back_inserter(featureLinks));
226 #ifndef XAOD_STANDALONE
227 } catch (SG::ExcCLIDMismatch&) {
228 // This is in place to catch the exception caused by non-IParticle features when an IParticle interface is requested.
229 // We're fine to catch this silently and cary on looking at the next Decision object in the graph
233 // Slices may have added single links. Note: the framework-specified "feature" link is always a single link.
234 if (decisionObj->hasObjectLink(featureNameToGet, ClassID_traits< CONTAINER >::ID())) {
235 std::vector<ElementLink<CONTAINER>> singleEntryVector; // Filtering function operates on a vector
236 singleEntryVector.push_back( decisionObj->objectLink<CONTAINER>( featureNameToGet ) );
237 filterLinkVectorByContainerKey<CONTAINER>(containerSGKey, singleEntryVector);
238 std::copy(singleEntryVector.begin(), singleEntryVector.end(), std::back_inserter(featureLinks));
240 #ifndef XAOD_STANDALONE
241 } catch (SG::ExcCLIDMismatch&) {
242 // Silently. As above.
247 // Check if the Decsision object is active for a specific set of Chains-of-interest (as supplied by the TDT)
248 ActiveState state = ActiveState::UNSET;
249 if (chainIDs.size() > 0) {
250 // If we were given a list of chains to consider then we start assuming none passed this decisionObj
251 state = ActiveState::INACTIVE;
252 for (DecisionID id : chainIDs) {
253 if (std::count(decisionObj->decisions().begin(), decisionObj->decisions().end(), id) == 1) {
254 state = ActiveState::ACTIVE;
258 // But consider also the ComboHypo, this will have run immidiately after the Hypo and could have failed this chain due to combinatorics.
259 // This statement will only activate for the case that we are looking for "feature" edges (which is the default).
260 if (state == ActiveState::ACTIVE && decisionObjChild && decisionObjChild->name() == comboHypoAlgNodeName()) {
261 state = ActiveState::INACTIVE;
262 for (DecisionID id : chainIDs) {
263 if (std::count(decisionObjChild->decisions().begin(), decisionObjChild->decisions().end(), id) == 1) {
264 state = ActiveState::ACTIVE;
271 // Copy the fetched links into the return vector
272 for (const ElementLink<CONTAINER>& featureLink : featureLinks) {
273 typename std::vector<LinkInfo<CONTAINER>>::iterator vecIt = std::find_if(features.begin(), features.end(), [&](const auto& li) { return li.link == featureLink; } );
274 if (vecIt == features.end()) {
275 // Link did not already exist - add it to the output
276 features.emplace_back( decisionObj, featureLink, state);
278 // Link already existed - if the link's state in the return vector is INACTIVE but is ACTIVE here,
279 // then we need to change it to ACTIVE as this denotes one-or-more of the requested chains were active for the object.
280 if (vecIt->state == ActiveState::INACTIVE and state == ActiveState::ACTIVE) {
281 vecIt->state = ActiveState::ACTIVE;
283 if (behaviour & TrigDefs::fillDecisions) {
284 // Update the set of passed IDs
285 if (vecIt->decisions.has_value())
286 vecIt->decisions->insert(ids.begin(), ids.end());
288 vecIt->decisions.emplace(ids.begin(), ids.end());
293 // Stop processing this path through the navigation if the lastFeatureOfType flag is set
294 if (featureLinks.size() && (behaviour & TrigDefs::lastFeatureOfType)) {
298 // Recurse to decisionObj's seeds
299 for (const NavGraphNode* seedNavNode : navGraphNode->seeds()) {
300 #if TRIGCOMPUTILS_ENABLE_EARLY_EXIT == 1
301 if (fullyExploredFrom.count(seedNavNode) == 1) {
302 continue; // Already explored down from here
305 recursiveGetFeaturesOfTypeInternal<CONTAINER>(features,
314 // If we encounter this node again in the future, we don't need to explore it again as it's now fully explored.
316 fullyExploredFrom.insert( navGraphNode );
322 DecisionIDContainer passedDecisionIDs( const Decision* d, const T& required ) {
323 DecisionIDContainer passed;
324 for ( DecisionID id : decisionIDs( d ) ) {
325 if ( required.find(id) != required.end() ) {