ATLAS Offline Software
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 
18 namespace 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 
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 
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
python.root_lsr_rank.hashes
hashes
Definition: root_lsr_rank.py:34
xAODMaker::EventFormatStreamHelperTool::sgkey_t
SG::sgkey_t sgkey_t
Definition: EventFormatStreamHelperTool.h:31
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
xAOD::EventFormat_v1::get
const EventFormatElement * get(const std::string &key, bool quiet=false) const
Get the description of a given branch.
Definition: EventFormat_v1.cxx:91
xAODMaker::EventFormatStreamHelperTool::m_ignoreKeys
Gaudi::Property< std::vector< std::string > > m_ignoreKeys
StoreGate keys that should be ignored during the metadata collection.
Definition: EventFormatStreamHelperTool.h:82
xAODMaker::EventFormatStreamHelperTool::lookUpHash
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
Definition: EventFormatStreamHelperTool.cxx:143
xAODMaker::EventFormatStreamHelperTool::m_warnedCLIDsMutex
std::mutex m_warnedCLIDsMutex
Mutex for the m_warnedCLIDs variable.
Definition: EventFormatStreamHelperTool.h:121
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAODMaker::EventFormatStreamHelperTool::initialize
StatusCode initialize() override
Initialise the tool.
Definition: EventFormatStreamHelperTool.cxx:21
xAODMaker::EventFormatStreamHelperTool::postExecute
StatusCode postExecute() override
Called at the end of execute.
Definition: EventFormatStreamHelperTool.cxx:37
skel.it
it
Definition: skel.GENtoEVGEN.py:396
xAOD::EventFormatElement
Class describing one branch of the ROOT file.
Definition: EventFormatElement.h:39
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAODMaker
Definition: StoreGateSvc.h:72
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
EventFormatStreamHelperTool.h
DataHeaderElement
This class provides a persistent form for the TransientAddress.
Definition: DataHeader.h:36
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAODMaker::EventFormatStreamHelperTool::m_efMutex
std::mutex m_efMutex
Mutex for the m_ef variable.
Definition: EventFormatStreamHelperTool.h:124
calibdata.exception
exception
Definition: calibdata.py:496
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAODMaker::EventFormatStreamHelperTool::m_warnedCLIDs
std::set< CLID > m_warnedCLIDs
CLIDs about which warnings have already been printed.
Definition: EventFormatStreamHelperTool.h:119
xAODMaker::EventFormatStreamHelperTool::m_metadataStore
ServiceHandle< IAthMetaDataSvc > m_metadataStore
Use the metadata tool interface to store the EventFormat object.
Definition: EventFormatStreamHelperTool.h:66
DataHeader.h
This file contains the class definition for the DataHeader and DataHeaderElement classes.
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
EventFormat.h
xAOD::EventFormat_v1
Event format metadata for xAOD files.
Definition: EventFormat_v1.h:38
xAODMaker::EventFormatStreamHelperTool::m_dataHeaderKey
Gaudi::Property< std::string > m_dataHeaderKey
Definition: EventFormatStreamHelperTool.h:86
xAODMaker::EventFormatStreamHelperTool::m_key
Gaudi::Property< std::string > m_key
Definition: EventFormatStreamHelperTool.h:73
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ReadCalibFromCool.typeName
typeName
Definition: ReadCalibFromCool.py:477
xAOD::EventFormat_v1::add
void add(const EventFormatElement &element, bool updatePersistent=true)
Add the description of a new branch.
Definition: EventFormat_v1.cxx:43
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
xAODMaker::EventFormatStreamHelperTool::m_clidSvc
ServiceHandle< IClassIDSvc > m_clidSvc
Connection to the CLID service.
Definition: EventFormatStreamHelperTool.h:70
xAODMaker::EventFormatStreamHelperTool::collectFormatMetadata
StatusCode collectFormatMetadata()
Definition: EventFormatStreamHelperTool.cxx:48
xAOD::EventFormat_v1::exists
bool exists(const std::string &key) const
Check if a description exists about a given branch.
Definition: EventFormat_v1.cxx:65
xAODMaker::EventFormatStreamHelperTool::m_typeNames
Gaudi::Property< std::vector< std::string > > m_typeNames
Type names for which a metadata entry should be added.
Definition: EventFormatStreamHelperTool.h:77
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37