ATLAS Offline Software
Loading...
Searching...
No Matches
AlgToChainTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#ifndef XAOD_STANDALONE
8
10 const std::string& name,
11 const IInterface* parent)
12 : AthAlgTool (type, name, parent)
13 {}
14
15
17
18
20 ATH_CHECK( m_HLTMenuKey.initialize() );
21
22 return StatusCode::SUCCESS;
23}
24
25
28 ATH_CHECK( hltMenuHandle.isValid() );
31
32 // Fill the maps
33 for ( const TrigConf::Chain& chain : *hltMenuHandle ) {
34 for ( const std::string& sequencer : chain.sequencers() ) {
35 // Skip empty steps = empty sequencers in the list
36 if (sequencer.empty()) continue;
37 m_sequencerToChainMap[sequencer].push_back(chain);
38 }
39 }
40
41 for ( const auto& sequencer : hltMenuHandle->sequencers() ) {
42 for ( const std::string& algorithm : sequencer.second ) {
43 // PassFilter is for empty steps - will never be associated with a chain
44 if (algorithm.starts_with( "PassFilter")) continue;
45
46 // Save just second part of algorithm ex. RoRSeqFilter/FFastCaloElectron -> FFastCaloElectron
47 m_algToSequencersMap[algorithm.substr(algorithm.find('/') + 1)]
48 .push_back(sequencer.first);
49 }
50 }
51
52 m_cachedEventID = 0; // Revet the cache identifier
53
54 return StatusCode::SUCCESS;
55}
56
57StatusCode TrigCompositeUtils::AlgToChainTool::getAllChains(std::vector<TrigConf::Chain>& chainNames) const {
59 ATH_CHECK( hltMenuHandle.isValid() );
60
61 for ( const TrigConf::Chain& chain : *hltMenuHandle ) {
62 chainNames.push_back(chain);
63 }
64 return StatusCode::SUCCESS;
65}
66
67
68std::vector<std::string> TrigCompositeUtils::AlgToChainTool::getChainsForAlg(const std::string& algorithmName) const {
69 std::vector<std::string> result;
70
71 try {
72 for ( const std::string& sequencer : m_algToSequencersMap.at(algorithmName) ) {
73 try {
74 for (const TrigConf::Chain& chain : m_sequencerToChainMap.at(sequencer)){
75 result.push_back(chain.name());
76 }
77 }
78 catch ( const std::out_of_range & ex ) {
79 ATH_MSG_DEBUG ( "Sequence " << sequencer << " is not part of the menu!" );
80 }
81 }
82 } catch ( const std::out_of_range & ex ) {
83 ATH_MSG_DEBUG ( "Algorithm " << algorithmName << " is not part of the menu!" );
84 }
85
86 return result;
87}
88
89
90std::set<std::string> TrigCompositeUtils::AlgToChainTool::getActiveChainsForAlg(const std::string& algorithmName, const EventContext& context) const {
91
92 std::set<std::string> allActiveChains;
93 try {
94 for ( const std::string& sequenceName : m_algToSequencersMap.at(algorithmName) ) {
95
96 std::string filterName = createCollectionName(sequenceName);
97 std::set<TrigCompositeUtils::DecisionID> activeChainsInSequence = retrieveActiveChains(context, filterName);
98
99 for ( const TrigCompositeUtils::DecisionID& id : activeChainsInSequence ) {
100 allActiveChains.insert( HLT::Identifier(id).name() );
101 }
102 }
103
104 } catch ( const std::out_of_range & ex ) {
105 ATH_MSG_DEBUG ( "Algorithm " << algorithmName << " is not part of the menu!" );
106 }
107
108 return allActiveChains;
109}
110
111
112StatusCode TrigCompositeUtils::AlgToChainTool::getAllActiveSequences( const EventContext& context, std::map<std::string, std::string>& algToSeq) {
113
114 // Retrieve EventStore and keys if not cached
115 IProxyDict* storeProxy = Atlas::getExtendedEventContext(context).proxy();
116 SmartIF<SGImplSvc> eventStore (storeProxy);
117 if (m_cachedEventID != context.eventID().event_number()){
118 ATH_MSG_DEBUG("Caching the event store keys for event " << context.eventID().event_number());
120 m_cachedEventID = context.eventID().event_number();
121 }
122
124 ATH_CHECK( hltMenuHandle.isValid() );
125 for (const auto& sequence : hltMenuHandle->sequencers()) {
126 std::string filterName = createCollectionName(sequence.first);
127
128 // Optimize
129 auto foundKey = std::find_if(m_cachedEventStoreKeys.begin(), m_cachedEventStoreKeys.end(), [&](const std::string& key) {
130 return key.starts_with( filterName);
131 });
132
133 if (foundKey != m_cachedEventStoreKeys.end() ){
134 // Check if sequence passed - if at least one DecisionObject was produced
135 for ( const TrigCompositeUtils::Decision* d : *getDecisionFromStore(eventStore, *foundKey) ) {
136 if (!d->decisions().empty()){
137 // Save algorithm to active sequence mapping
138 for (const std::string& alg : sequence.second) {
139 algToSeq[alg.substr(alg.find('/') + 1, alg.size())] = sequence.first;
140 }
141 break;
142 }
143 }
144 }
145 }
146
147 return StatusCode::SUCCESS;
148}
149
150void TrigCompositeUtils::AlgToChainTool::cacheSGKeys(const EventContext& context) {
151 if (m_cachedEventID != context.eventID().event_number()){
152 ATH_MSG_DEBUG("Caching the event store keys for event " << context.eventID().event_number());
154 m_cachedEventID = context.eventID().event_number();
155 }
156}
157
158std::vector<std::string> TrigCompositeUtils::AlgToChainTool::readSGKeys(const EventContext& context) const {
159 std::vector<std::string> keys;
160 IProxyDict* storeProxy = Atlas::getExtendedEventContext(context).proxy();
161 SmartIF<SGImplSvc> eventStore (storeProxy);
162 eventStore->keys(static_cast<CLID>( ClassID_traits<TrigCompositeUtils::DecisionContainer>::ID() ), keys);
163 return keys;
164}
165
166std::set<TrigCompositeUtils::DecisionID> TrigCompositeUtils::AlgToChainTool::retrieveActiveChains(const EventContext& context, const std::string& collectionName) const {
167
168 if (m_cachedEventID == context.eventID().event_number()) {
169 return retrieveActiveChainsForKeys(context, collectionName, m_cachedEventStoreKeys);
170 } else {
171 const std::vector<std::string> keys = readSGKeys(context);
172 return retrieveActiveChainsForKeys(context, collectionName, keys);
173 }
174}
175
176
177std::set<TrigCompositeUtils::DecisionID> TrigCompositeUtils::AlgToChainTool::retrieveActiveChainsForKeys(const EventContext& context, const std::string& collectionName, const std::vector<std::string>& keys) const {
178 std::set<TrigCompositeUtils::DecisionID> activeChainsID;
179
180 // Retrieve EventStore and keys if not cached
181 IProxyDict* storeProxy = Atlas::getExtendedEventContext(context).proxy();
182 SmartIF<SGImplSvc> eventStore (storeProxy);
183
184 // Retrieve active chains name hashes
185 for ( const std::string& key : keys ) {
186
187 // Look for given collection
188 if ( !collectionName.empty() && (!key.starts_with( collectionName)) ){
189 continue;
190 }
191
192 // Get data from any nav collection
193 if( collectionName.empty() && (!key.starts_with( "HLTNav") || key == "HLTNav_Summary") ) {
194 continue;
195 }
196
197 for ( const TrigCompositeUtils::Decision* d : *getDecisionFromStore(eventStore, key) ) {
199 TrigCompositeUtils::decisionIDs( d, chainsID );
200
201 // Save the active chains IDs
202 for (TrigCompositeUtils::DecisionID id : chainsID){
203 activeChainsID.insert(TrigCompositeUtils::getIDFromLeg(id));
204 }
205 }
206 }
207
208 return activeChainsID;
209}
210
211
212StatusCode TrigCompositeUtils::AlgToChainTool::getChainsForAllAlgs(const EventContext& context, std::map<std::string, std::set<std::string>>& algToChain) const {
213
214 // Look for chains which were active for any of the algorithms of the sequence
215 std::map<std::string, std::set<TrigCompositeUtils::DecisionID>> seqToActiveChains;
216 for (const auto& sequence : m_sequencerToChainMap) {
217 // Look for associated filters names with the sequence
218 std::string filterName = createCollectionName(sequence.first);
219 seqToActiveChains[sequence.first] = retrieveActiveChains(context, filterName);
220 }
221
222 for (const auto& algSeqPair : m_algToSequencersMap){
223 std::set<std::string> activeChains;
224 for (const std::string& sequenceName : algSeqPair.second){
225 // Save all active chains per sequences that algorithm was executed
226 for (TrigCompositeUtils::DecisionID chainId : seqToActiveChains.at(sequenceName)){
227 activeChains.insert(HLT::Identifier(chainId).name());
228 }
229 }
230
231 algToChain[algSeqPair.first] = std::move(activeChains);
232 }
233
234 return StatusCode::SUCCESS;
235}
236
237
238
239StatusCode TrigCompositeUtils::AlgToChainTool::getChainInfo(const EventContext& context, TrigCompositeUtils::DecisionID decId, ChainInfo& info) const {
241 ATH_CHECK( hltMenuHandle.isValid() );
242
243 HLT::Identifier id = getIDFromLeg(decId);
244 info.id = id;
245
246 // Find chain with given id
247 TrigConf::HLTMenu::const_iterator chain = std::find_if(hltMenuHandle->begin(), hltMenuHandle->end(),
248 [&id](const TrigConf::Chain& c) {return c.namehash() == id;}
249 );
250
251 if(chain == hltMenuHandle->end()){
252 ATH_MSG_WARNING("Chain " << id << " not found in the menu!");
253 info.name = id.name();
254 return StatusCode::SUCCESS;
255 }
256
257 info.name = (*chain).name();
258 info.groups = (*chain).groups();
259
260 // Check if chain passed - is in HLTPassRaw collection
261 IProxyDict* storeProxy = Atlas::getExtendedEventContext(context).proxy();
262 SmartIF<SGImplSvc> eventStore (storeProxy);
264
267 TrigCompositeUtils::decisionIDs( passRaw, chainsID );
268 info.isPassRaw = std::find(chainsID.begin(), chainsID.end(), id) != chainsID.end();
269
270 return StatusCode::SUCCESS;
271}
272
273
275 SG::DataProxy* dp = eventStore->proxy(
277
279 if ( !dc.isValid() ) {
280 ATH_MSG_WARNING("Failed to retrieve " << key << " from event store.");
282 }
283
284 return dc;
285}
286
287
288std::string TrigCompositeUtils::AlgToChainTool::createCollectionName(const std::string& sequenceName) const{
289 std::string filterName = "HLTNav_F" + sequenceName + "__";
290
291 if (filterName.find("Trig") != std::string::npos){
292 filterName.replace(filterName.find("Trig"), 4, "");
293 }
294
295 return filterName;
296}
297
298#endif // XAOD_STANDALONE
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
uint32_t CLID
The Class ID type.
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
virtual bool isValid() override final
Can the handle be successfully dereferenced?
std::string createCollectionName(const std::string &sequenceName) const
std::set< std::string > getActiveChainsForAlg(const std::string &algorithmName, const EventContext &context) const
Request set of active chains for given algorithm - dynamic lookup (navigation)
AlgToChainTool(const std::string &type, const std::string &name, const IInterface *parent)
SG::ReadHandleKey< TrigConf::HLTMenu > m_HLTMenuKey
std::vector< std::string > m_cachedEventStoreKeys
virtual StatusCode initialize() override
std::set< TrigCompositeUtils::DecisionID > retrieveActiveChainsForKeys(const EventContext &context, const std::string &collectionName, const std::vector< std::string > &keys) const
Request set of chains for given navigation collection and list of StoreGate keys.
SG::ReadHandle< TrigCompositeUtils::DecisionContainer > getDecisionFromStore(SmartIF< SGImplSvc > &eventStore, const std::string &key) const
void cacheSGKeys(const EventContext &context)
Cache the StoreGate keys.
virtual StatusCode start() override
StatusCode getChainsForAllAlgs(const EventContext &context, std::map< std::string, std::set< std::string > > &algToChain) const
Request set of chains for all algorithms in the menu.
std::map< std::string, std::vector< TrigConf::Chain > > m_sequencerToChainMap
StatusCode getChainInfo(const EventContext &context, TrigCompositeUtils::DecisionID id, ChainInfo &info) const
Retrieve chain information for gived chain id.
std::vector< std::string > readSGKeys(const EventContext &context) const
Read the keys from StoreGate.
std::vector< std::string > getChainsForAlg(const std::string &algorithmName) const
Request set of chains for given algorithm - static lookup (menu)
std::map< std::string, std::vector< std::string > > m_algToSequencersMap
StatusCode getAllChains(std::vector< TrigConf::Chain > &) const
Request all chains from the menu.
std::set< TrigCompositeUtils::DecisionID > retrieveActiveChains(const EventContext &context, const std::string &collectionName="") const
Read the cached or non cached SG kays and request set of chains for given navigation collection.
StatusCode getAllActiveSequences(const EventContext &context, std::map< std::string, std::string > &algToSeq)
Retrieve algorithms and their active sequences.
ConstIter< ptree, Chain > const_iterator
Iterator over the HLT chains.
Definition HLTMenu.h:50
std::string algorithm
Definition hcg.cxx:85
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
unsigned int DecisionID
HLT::Identifier getIDFromLeg(const HLT::Identifier &legIdentifier)
Generate the HLT::Identifier which corresponds to the chain name from the leg name.
std::set< DecisionID > DecisionIDContainer
const Decision * getTerminusNode(SG::ReadHandle< DecisionContainer > &container)
void decisionIDs(const Decision *d, DecisionIDContainer &destination)
Extracts DecisionIDs stored in the Decision object.