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 
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 
27  SG::ReadHandle<TrigConf::HLTMenu> hltMenuHandle = SG::makeHandle( m_HLTMenuKey );
28  ATH_CHECK( hltMenuHandle.isValid() );
29  m_sequencerToChainMap.clear();
30  m_algToSequencersMap.clear();
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 
57 StatusCode TrigCompositeUtils::AlgToChainTool::getAllChains(std::vector<TrigConf::Chain>& chainNames) const {
58  SG::ReadHandle<TrigConf::HLTMenu> hltMenuHandle = SG::makeHandle( m_HLTMenuKey );
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 
68 std::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 
90 std::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 
112 StatusCode 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_INFO("Caching the event store keys for event " << context.eventID().event_number());
119  eventStore->keys(static_cast<CLID>( ClassID_traits<TrigCompositeUtils::DecisionContainer>::ID() ), m_cachedEventStoreKeys);
120  m_cachedEventID = context.eventID().event_number();
121  }
122 
123  SG::ReadHandle<TrigConf::HLTMenu> hltMenuHandle = SG::makeHandle( m_HLTMenuKey, context );
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 
150 void TrigCompositeUtils::AlgToChainTool::cacheSGKeys(const EventContext& context) {
151  if (m_cachedEventID != context.eventID().event_number()){
152  ATH_MSG_INFO("Caching the event store keys for event " << context.eventID().event_number());
153  m_cachedEventStoreKeys = readSGKeys(context);
154  m_cachedEventID = context.eventID().event_number();
155  }
156 }
157 
158 std::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 
166 std::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 
177 std::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 
212 StatusCode 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] = activeChains;
232  }
233 
234  return StatusCode::SUCCESS;
235 }
236 
237 
238 
240  SG::ReadHandle<TrigConf::HLTMenu> hltMenuHandle = SG::makeHandle( m_HLTMenuKey, context );
241  ATH_CHECK( hltMenuHandle.isValid() );
242 
243  HLT::Identifier id = HLT::Identifier(decId);
245  id = getIDFromLeg(id);
246  }
247 
248  info.id = id;
249 
250  // Find chain with given id
251  TrigConf::HLTMenu::const_iterator chain = std::find_if(hltMenuHandle->begin(), hltMenuHandle->end(),
252  [&id](const TrigConf::Chain& c) {return c.namehash() == id;}
253  );
254 
255  if(chain == hltMenuHandle->end()){
256  ATH_MSG_WARNING("Chain " << id << " not found in the menu!");
257  info.name = id.name();
258  return StatusCode::SUCCESS;
259  }
260 
261  info.name = (*chain).name();
262  info.groups = (*chain).groups();
263 
264  // Check if chain passed - is in HLTPassRaw collection
265  IProxyDict* storeProxy = Atlas::getExtendedEventContext(context).proxy();
266  SmartIF<SGImplSvc> eventStore (storeProxy);
267  SG::ReadHandle<TrigCompositeUtils::DecisionContainer> dc = getDecisionFromStore(eventStore, "HLTNav_Summary");
268 
271  TrigCompositeUtils::decisionIDs( passRaw, chainsID );
272  info.isPassRaw = std::find(chainsID.begin(), chainsID.end(), id) != chainsID.end();
273 
274  return StatusCode::SUCCESS;
275 }
276 
277 
279  SG::DataProxy* dp = eventStore->proxy(
281 
283  if ( !dc.isValid() ) {
284  ATH_MSG_WARNING("Failed to retrieve " << key << " from event store.");
286  }
287 
288  return dc;
289 }
290 
291 
293  std::string filterName = "HLTNav_F" + sequenceName + "__";
294 
295  if (filterName.find("Trig") != std::string::npos){
296  filterName.replace(filterName.find("Trig"), 4, "");
297  }
298 
299  return filterName;
300 }
301 
302 #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:212
TileDCSDataPlotter.dp
dp
Definition: TileDCSDataPlotter.py:840
algorithm
std::string algorithm
Definition: hcg.cxx:82
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:150
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:47
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:19
TrigCompositeUtils::AlgToChainTool::~AlgToChainTool
virtual ~AlgToChainTool()
Definition: AlgToChainTool.cxx:16
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:239
TrigCompositeUtils::AlgToChainTool::ChainInfo
Definition: AlgToChainTool.h:29
TrigCompositeUtils::AlgToChainTool::createCollectionName
std::string createCollectionName(const std::string &sequenceName) const
Definition: AlgToChainTool.cxx:292
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?
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:57
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
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:166
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
TrigCompositeUtils::AlgToChainTool::getAllActiveSequences
StatusCode getAllActiveSequences(const EventContext &context, std::map< std::string, std::string > &algToSeq)
Retrieve algorithms and their active sequences.
Definition: AlgToChainTool.cxx:112
TrigCompositeUtils::AlgToChainTool::getDecisionFromStore
SG::ReadHandle< TrigCompositeUtils::DecisionContainer > getDecisionFromStore(SmartIF< SGImplSvc > &eventStore, const std::string &key) const
Definition: AlgToChainTool.cxx:278
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:26
TrigCompositeUtils::AlgToChainTool::AlgToChainTool
AlgToChainTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: AlgToChainTool.cxx:9
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:177
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:68
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
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:90
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:158
AlgToChainTool.h
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37