ATLAS Offline Software
Loading...
Searching...
No Matches
EventFormatStreamHelperTool.cxx
Go to the documentation of this file.
1/* Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration */
2
3// Local include(s).
5
6// system includes
7#include <iterator>
8#include <memory>
9#include <regex>
10#include <utility>
11
12#include "GaudiKernel/Algorithm.h"
14
15// EDM include(s).
17
18namespace xAODMaker {
19
22 // Retrieve all needed components.
23 ATH_CHECK(m_metadataStore.retrieve());
24
25 if (m_dataHeaderKey.empty()) {
26 // find out name of stream we are working for
27 const Gaudi::Algorithm *parentAlg =
28 dynamic_cast< const Gaudi::Algorithm* >(parent());
29 if (parentAlg) m_dataHeaderKey = parentAlg->name();
30 }
31
32 // Return gracefully
33 return StatusCode::SUCCESS;
34 }
35
36StatusCode
38 // Notify the event format service that it should collect the metadata
39 // that it needs.
41 ATH_MSG_VERBOSE("Triggered metadata collection on: " << m_key.value());
42
43 // Return gracefully.
44 return StatusCode::SUCCESS;
45 }
46
47StatusCode
49 // Get the EventFormat object
50 xAOD::EventFormat * event_format =
51 m_metadataStore->tryRetrieve< xAOD::EventFormat >(m_key.value());
52
53 if (!event_format) {
54 auto p_event_format = std::make_unique< xAOD::EventFormat >();
55 event_format = p_event_format.get();
56 ATH_CHECK(m_metadataStore->record(std::move(p_event_format), m_key));
57 ATH_MSG_VERBOSE("Created new xAOD::EventFormat: " << m_key.value());
58 } else {
59 ATH_MSG_VERBOSE("Use existing xAOD::EventFormat: " << m_key.value());
60 }
61
62 // Grab output stream data header
64 if (!dataHeader.isValid()) {
65 // DataHeader won't exist if this event was rejected.
66 return StatusCode::SUCCESS;
67 }
68
69 // Loop over objects in output stream
70 for (const DataHeaderElement& elem : *dataHeader) {
71 // Caveat: assume branch name that Athena I/O is going to give to
72 // this object is the key name
73 const std::string& key = elem.getKey();
74 const CLID classID = elem.getPrimaryClassID();
75
76 // Skip objects that were set up to be ignored.
77 {
78 bool ignoreObject = false;
79 for (const std::string& ignorePattern : m_ignoreKeys.value()) {
80 if (std::regex_match(key, std::regex(ignorePattern))) {
81 ignoreObject = true;
82 break;
83 }
84 }
85 if (ignoreObject) continue;
86 }
87
88 // Get the type name of this object.
89 std::string typeName;
90 if (m_clidSvc->getTypeInfoNameOfID(classID, typeName).isFailure()) {
91 // Make sure that nobody else is using @c m_warnedCLIDs right now.
92 std::lock_guard< std::mutex > lock(m_warnedCLIDsMutex);
93
94 // Print a warning if this CLID didn't produce a warning yet:
95 if (m_warnedCLIDs.insert(classID).second)
96 ATH_MSG_WARNING("Couldn't get type name for CLID = " << classID );
97
98 continue;
99 }
100
101 // Now that we have the type name, check whether metadata for this type
102 // should be stored.
103 {
104 bool ignoreObject = true;
105 for (const std::string& typePattern : m_typeNames.value()) {
106 if (std::regex_match(typeName, std::regex(typePattern))) {
107 ignoreObject = false;
108 break;
109 }
110 }
111 if (ignoreObject) continue;
112 }
113
114 // Update the metadata object
115 try {
116 const sgkey_t hash =
117 lookUpHash(classID, elem.getClassIDs(), elem.getHashes());
118
119 // Make sure that nobody else is modifying @c m_ef or @c m_spool
120 // right now.
121 std::lock_guard< std::mutex > lock(m_efMutex);
122 // If we already know about this object, then don't bother.
123 if (event_format->exists(key)) continue;
124
125 // Add the info.
126 event_format->add(xAOD::EventFormatElement(key, typeName, "", hash));
127
128 // Tell the user what happened.
129 ATH_MSG_VERBOSE("Adding info: key = \"" << key
130 << ", typeName = \"" << typeName << "\""
131 << ", hash = 0x" << std::hex << std::setw(8)
132 << std::setfill('0') << hash);
133 } catch (const std::exception& e) {
134 ATH_MSG_WARNING(e.what());
135 }
136 }
137
138 // Return gracefully.
139 return StatusCode::SUCCESS;
140 }
141
144 CLID primaryClassID,
145 const std::set<CLID>& classIDs,
146 const std::vector<sgkey_t>& hashes) const {
147 // two collections of different size would cause undefined behaviour
148 if (classIDs.size() != hashes.size())
149 throw(std::runtime_error("CLID and hash sets not equal in size"));
150
151 auto it = classIDs.find(primaryClassID);
152
153 // bail if we don't find the primary class ID
154 if (it == classIDs.end())
155 throw(std::range_error("Primary class ID not in list of class IDs"));
156
157 return hashes[std::distance(classIDs.begin(), it)];
158 }
159
160} // namespace xAODMaker
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
This file contains the class definition for the DataHeader and DataHeaderElement classes.
uint32_t CLID
The Class ID type.
This class provides a persistent form for the TransientAddress.
Definition DataHeader.h:37
virtual bool isValid() override final
Can the handle be successfully dereferenced?
std::mutex m_efMutex
Mutex for the m_ef variable.
Gaudi::Property< std::vector< std::string > > m_typeNames
Type names for which a metadata entry should be added.
StatusCode postExecute() override
Called at the end of execute.
ServiceHandle< IAthMetaDataSvc > m_metadataStore
Use the metadata tool interface to store the EventFormat object.
std::set< CLID > m_warnedCLIDs
CLIDs about which warnings have already been printed.
Gaudi::Property< std::vector< std::string > > m_ignoreKeys
StoreGate keys that should be ignored during the metadata collection.
ServiceHandle< IClassIDSvc > m_clidSvc
Connection to the CLID service.
std::mutex m_warnedCLIDsMutex
Mutex for the m_warnedCLIDs variable.
StatusCode initialize() override
Initialise the tool.
sgkey_t lookUpHash(CLID primaryClassID, const std::set< CLID > &classIDs, const std::vector< sgkey_t > &hashes) const
look up hash corresponding to primary class ID
Class describing one branch of the ROOT file.
void add(const EventFormatElement &element, bool updatePersistent=true)
Add the description of a new branch.
bool exists(const std::string &key) const
Check if a description exists about a given branch.
const EventFormatElement * get(const std::string &key, bool quiet=false) const
Get the description of a given branch.
::StatusCode StatusCode
StatusCode definition for legacy code.
EventFormat_v1 EventFormat
Definition of the current event format version.
Definition EventFormat.h:16