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