ATLAS Offline Software
Loading...
Searching...
No Matches
FileMetaDataCreatorTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Local include(s):
7
8// System include(s):
9#include <algorithm>
10#include <functional>
11#include <memory>
12#include <stdexcept>
13#include <sstream>
14#include <utility>
15
16// Athena metadata EDM:
23
24
27static bool isFromSharedWriterWorker(const std::string& key) {
28 return key.find("__STREAM[") != std::string::npos;
29}
30
31namespace xAODMaker {
32
35 ATH_CHECK(m_eventStore.retrieve());
36 ATH_CHECK(m_metaDataSvc.retrieve());
38 ATH_CHECK(m_metaDataStore.retrieve());
39 ATH_CHECK(m_tagInfoMgr.retrieve());
40
41 // If DataHeader key not specified, try determining it
42 if (m_dataHeaderKey.empty()) {
43 const auto *parentAlg = dynamic_cast< const INamedInterface* >(parent());
44 if (parentAlg)
45 m_dataHeaderKey = parentAlg->name();
46 }
47
48 // Listen for the begin of an input file. Act after MetaDataSvc (prio 80) and
49 // TagInfoMgr (prio 50). That means the FileMetaDataTool be called first
50 ServiceHandle< IIncidentSvc > incidentSvc("IncidentSvc", name());
51 ATH_CHECK(incidentSvc.retrieve());
52 incidentSvc->addListener(this, "EndInputFile", 40);
53 incidentSvc->addListener(this, "PostFork", 40);
54
55 // Create a fresh object to fill
56 ATH_MSG_DEBUG("Creating new xAOD::FileMetaData object to fill");
57 m_info = std::make_unique< xAOD::FileMetaData >();
58 m_aux = std::make_unique< xAOD::FileMetaDataAuxInfo >();
59 m_info->setStore(m_aux.get());
60
61 // FileMetaData has no content
62 m_filledNonEvent = false;
63 m_filledEvent = false;
64
65 // Return gracefully:
66 return StatusCode::SUCCESS;
67 }
68
69void
70 FileMetaDataCreatorTool::handle(const Incident& inc) {
71 // gracefully ignore unexpected incident types
72 if (inc.type() == "EndInputFile") {
73 // Lock the tool while we work on the FileMetaData
74 std::lock_guard lock(m_toolMutex);
75 m_hasInputFile = true;
76 if (!updateFromNonEvent().isSuccess())
77 ATH_MSG_DEBUG("Failed to fill FileMetaData with non-event info");
78 } else if (inc.type() == "PostFork") {
79 // Drop event info inherited from the parent process
80 std::lock_guard lock(m_toolMutex);
81 if (m_info && m_filledEvent) {
82 if (!m_info->setValue("runNumbers", std::vector<uint32_t>{}) ||
83 !m_info->setValue("lumiBlocks", std::vector<uint32_t>{}))
84 ATH_MSG_WARNING("Error clearing event information from before the fork");
85 m_filledEvent = false;
86 }
87 }
88 }
89
90StatusCode
92 return StatusCode::SUCCESS;
93 }
94
95StatusCode
97 return StatusCode::SUCCESS;
98 }
99
100StatusCode
102 return StatusCode::SUCCESS;
103 }
104
105StatusCode
107 std::lock_guard lock(m_toolMutex);
108
109 if (!m_filledNonEvent) {
110 ATH_MSG_DEBUG("Not writing empty or incomplete FileMetaData object");
111 // Input FileMetaData is still written (e.g. pre-fork parent), so drop its event info
112 if (!m_filledEvent) {
113 for (const std::string& key : m_metaDataSvc->getPerStreamKeysFor(m_key)) {
114 if (isFromSharedWriterWorker(key)) {
115 continue;
116 }
117 auto* output = m_metaDataSvc->tryRetrieve<xAOD::FileMetaData>(key);
118 if (!output) {
119 continue;
120 }
121 if (!output->setValue("runNumbers", std::vector<uint32_t>{}) ||
122 !output->setValue("lumiBlocks", std::vector<uint32_t>{})) {
123 ATH_MSG_WARNING("Could not clear the event information of " << key);
124 } else {
125 ATH_MSG_DEBUG("Cleared the event information of " << key);
126 }
127 }
128 }
129 return StatusCode::SUCCESS;
130 }
131
132 // Set metadata with content created for given stream
133 for (const std::string& key : m_metaDataSvc->getPerStreamKeysFor(m_key)) {
134 // Remove any existing objects with this key
135 if (!m_metaDataSvc->contains<xAOD::FileMetaData>(key)) {
136 auto info = std::make_unique<xAOD::FileMetaData>();
137 auto aux = std::make_unique<xAOD::FileMetaDataAuxInfo>();
138 info->setStore(aux.get());
139 ATH_CHECK(m_metaDataSvc->record(std::move(info), key));
140 ATH_CHECK(m_metaDataSvc->record(std::move(aux), key + "Aux."));
141 }
142
143 auto* output = m_metaDataSvc->tryRetrieve<xAOD::FileMetaData>(key);
144 if (output) {
145 // save event info that we've already had
146 float orig_mcProcID = -1;
147 std::vector<uint32_t> orig_runNumbers, orig_lumiBlocks;
148 if (!output->value(xAOD::FileMetaData::mcProcID, orig_mcProcID))
149 ATH_MSG_DEBUG("Could not get mcProcID");
150 if (!output->value("runNumbers", orig_runNumbers))
151 ATH_MSG_DEBUG("Could not get runNumbers");
152 if (!output->value("lumiBlocks", orig_lumiBlocks))
153 ATH_MSG_DEBUG("Could not get lumiBlocks");
154
155 // Replace content in store with content created for this stream
156 *output = *m_info;
157 ATH_MSG_DEBUG("FileMetaData payload replaced in store with content created for this stream");
158 if (!m_filledEvent) {
159 // No events were written to this stream by this process: restore the original
160 // mcProcID, keep run/lumi info only if it came from a SharedWriter worker
161 ATH_MSG_DEBUG("Event information was not filled, restoring what we had");
162 if (!output->setValue(xAOD::FileMetaData::mcProcID, orig_mcProcID))
163 ATH_MSG_DEBUG("Could not set " << xAOD::FileMetaData::mcProcID << " to " << orig_mcProcID);
164 if (!isFromSharedWriterWorker(key)) {
165 orig_runNumbers.clear();
166 orig_lumiBlocks.clear();
167 }
168 if (!output->setValue("runNumbers", orig_runNumbers))
169 ATH_MSG_DEBUG("Could not set runNumbers");
170 if (!output->setValue("lumiBlocks", orig_lumiBlocks))
171 ATH_MSG_DEBUG("Could not set lumiBlocks");
172 }
173 } else {
174 ATH_MSG_DEBUG("cannot copy FileMetaData payload to output");
175 }
176 }
177
178 return StatusCode::SUCCESS;
179}
180
181StatusCode
183 return StatusCode::SUCCESS;
184 }
185
186StatusCode
188 // Lock the tool while working with FileMetaData
189 std::lock_guard lock(m_toolMutex);
190
191 // Fill information from TagInfo and Simulation Parameters
192 if (!updateFromNonEvent().isSuccess())
193 ATH_MSG_DEBUG("Failed to fill FileMetaData with non-event info");
194
195 // Sanity check
196 if (!(m_info && m_aux)) {
197 ATH_MSG_DEBUG("No xAOD::FileMetaData object to fill");
198 return StatusCode::SUCCESS;
199 }
200
201 // Skip events not written by this stream (no DataHeader)
202 if (!m_eventStore->contains<DataHeader>(m_dataHeaderKey)) {
203 ATH_MSG_DEBUG("Event was not written to " << m_dataHeaderKey.value()
204 << ", not adding it to the FileMetaData");
205 return StatusCode::SUCCESS;
206 }
207
208 { // MC channel, run and/or lumi block numbers
209 const xAOD::EventInfo* eventInfo = nullptr;
210 StatusCode sc = StatusCode::FAILURE;
211
212 if (m_eventStore->contains< xAOD::EventInfo >(m_eventInfoKey)) {
213 sc = m_eventStore->retrieve(eventInfo, m_eventInfoKey);
214 } else if (m_eventStore->contains< xAOD::EventInfo >("Mc" + m_eventInfoKey)) {
215 sc = m_eventStore->retrieve(eventInfo, "Mc" + m_eventInfoKey);
216 }
217
218 if (eventInfo && sc.isSuccess()) {
219 addUniqueValue("runNumbers", eventInfo->runNumber());
220 addUniqueValue("lumiBlocks", eventInfo->lumiBlock());
221 // Return if object has already been filled
222 if (m_filledEvent) return StatusCode::SUCCESS;
223
224 try {
225 ATH_MSG_DEBUG("Retrieved " << m_eventInfoKey);
226
228 const float id = static_cast< float >(eventInfo->mcChannelNumber());
229
230 if (m_info->setValue(type, id)) {
231 ATH_MSG_DEBUG("setting " << type << " to " << id);
232 } else {
233 ATH_MSG_DEBUG("error setting " << type << " to " << id);
234 }
235 } catch (std::exception&) {
236 // Processing data not generated events
237 ATH_MSG_DEBUG("Failed to set " << xAOD::FileMetaData::mcProcID);
238 }
239 } else {
241 "Failed to retrieve " << m_eventInfoKey << " => cannot set "
243 << ", runNumbers, or lumiBlockNumbers");
244 }
245 }
246
247 m_filledEvent = true;
248
249 return StatusCode::SUCCESS;
250 }
251
252StatusCode
254
255 // Have we already done this?
256 if (m_filledNonEvent) return StatusCode::SUCCESS;
257
258 // Sanity check
259 if (!(m_info && m_aux)) {
260 ATH_MSG_DEBUG("No xAOD::FileMetaData object to fill");
261 return StatusCode::SUCCESS;
262 }
263
264 if (m_hasInputFile && m_tagInfoMgr->getInputTags().empty()) {
265 ATH_MSG_DEBUG("Deferring non-event fill due to eventless-input");
266 return StatusCode::SUCCESS;
267 }
268
270 m_tagInfoMgr->findTag("AtlasRelease"));
271
273 m_tagInfoMgr->findTag("AODFixVersion"));
274
275 set(xAOD::FileMetaData::amiTag, m_tagInfoMgr->findTag("AMITag"));
276
278
280 m_tagInfoMgr->findTag("IOVDbGlobalTag"));
281
282 set(xAOD::FileMetaData::beamType, m_tagInfoMgr->findTag("beam_type"));
283
284 set(xAOD::FileMetaData::mcCampaign, m_tagInfoMgr->findTag("mc_campaign"));
285
286 set(xAOD::FileMetaData::generatorsInfo, m_tagInfoMgr->findTag("generators"));
287
288 std::string beamEnergy = m_tagInfoMgr->findTag("beam_energy");
289 try {
291 std::stof(beamEnergy));
292 } catch (std::invalid_argument& e) {
293 ATH_MSG_DEBUG("beam energy \"" << beamEnergy << "\" tag could not be converted to float");
294 } catch (std::out_of_range& e) {
295 ATH_MSG_DEBUG("converted beam energy value (\"" << beamEnergy << "\") outside float range");
296 }
297
298 std::string dataYear = m_tagInfoMgr->findTag("data_year");
299 if (!dataYear.empty()) {
300 try {
302 static_cast<uint32_t>(std::stoul(dataYear)));
303 } catch (std::invalid_argument& e) {
304 ATH_MSG_DEBUG("data year \"" << dataYear << "\" tag could not be converted to unsigned long");
305 } catch (std::out_of_range& e) {
306 ATH_MSG_DEBUG("converted data year value (\"" << dataYear << "\") outside unsigned long range");
307 }
308 }
309
310 // Read simulation parameters
311 const IOVMetaDataContainer * simInfo = nullptr;
312 StatusCode sc = StatusCode::FAILURE;
314 sc = m_inputMetaDataStore->retrieve(simInfo, m_simInfoKey);
315 } else if (m_metaDataStore->contains< IOVMetaDataContainer >(m_simInfoKey)) {
316 sc = m_metaDataStore->retrieve(simInfo, m_simInfoKey);
317 }
318 const coral::AttributeList * attrList = nullptr;
319 if (simInfo && sc.isSuccess()) {
320 for (const CondAttrListCollection* payload : *simInfo->payloadContainer()) {
321 for (const auto& itr : *payload) {
322 attrList = &(itr.second);
323 }
324 }
325 }
326
327 bool simFlavourSet{};
328 bool isDataOverlaySet{};
329 if (attrList) {
330 { // set simulation flavor
331 std::string key = "SimulationFlavour";
332 if (attrList->exists(key)) {
333 std::string value = (*attrList)[key].data< std::string >();
334
335 // remap simulation flavor "default" to "FullSim"
336 if (value == "default")
337 value = "FullSim";
338
340 simFlavourSet = true;
341 }
342 }
343
344 { // set whether this is overlay
345 std::string key = "IsDataOverlay";
346 if (attrList->exists(key)) {
347 std::string attr = (*attrList)[key].data< std::string >();
348 set(xAOD::FileMetaData::isDataOverlay, attr == "True");
349 isDataOverlaySet = true;
350 }
351 }
352 }
353
354 if (!simFlavourSet || !isDataOverlaySet) {
356 "Failed to set "
359 << ". Trying to get them from input metadata store." );
360
361 for (const std::string& key : m_metaDataSvc->getPerStreamKeysFor(m_key)) {
362 const xAOD::FileMetaData* input = nullptr;
363 input = m_inputMetaDataStore->tryConstRetrieve< xAOD::FileMetaData >(key);
364 if (input) {
365 if (!simFlavourSet) {
366 std::string orig_simFlavour = "none";
367 if (!input->value(xAOD::FileMetaData::simFlavour, orig_simFlavour)) {
369 "Could not get xAOD::FileMetaData::simFlavour "
370 "from input metadata "
371 "store");
372 } else {
373 ATH_MSG_DEBUG("Retrieved from input metadata store: "
375 << orig_simFlavour);
376 set(xAOD::FileMetaData::simFlavour, orig_simFlavour);
377 }
378 }
379
380 if (!isDataOverlaySet) {
381 bool orig_isDataOverlay = false;
382 if (!input->value(xAOD::FileMetaData::isDataOverlay, orig_isDataOverlay)) {
384 "Could not get "
385 "xAOD::FileMetaData::isDataOverlay from input "
386 "metadata store");
387 } else {
388 ATH_MSG_DEBUG("Retrieved from input metadata store: "
390 << orig_isDataOverlay);
391 set(xAOD::FileMetaData::isDataOverlay, orig_isDataOverlay);
392 }
393 }
394 }
395 }
396 }
397
398 if (m_filledNonEvent) return StatusCode::SUCCESS;
399
400 { // get dataType
402 try {
403 if (m_info->setValue(type, m_dataHeaderKey.value())) {
404 ATH_MSG_DEBUG("set " << type << " to " << m_dataHeaderKey.value());
405 } else {
406 ATH_MSG_DEBUG("error setting " << type << " to " << m_dataHeaderKey.value());
407 }
408 } catch (std::exception&) {
409 // This is unexpected
410 ATH_MSG_DEBUG("Failed to set " << xAOD::FileMetaData::dataType);
411 }
412 }
413
414 // FileMetaData object has been filled with non event info
415 m_filledNonEvent = true;
416
417 return StatusCode::SUCCESS;
418 }
419
420void
423 bool value) {
424 try {
425 if (m_info->setValue(key, value)) {
426 ATH_MSG_DEBUG("setting " << key << " to " << value);
427 } else {
428 ATH_MSG_DEBUG("error setting " << key << " to " << std::boolalpha << value
429 << std::noboolalpha);
430 }
431 } catch (std::exception&) {
432 // Processing data not generated events
433 ATH_MSG_DEBUG("Failed to set " << key);
434 }
435 }
436
437void
440 uint32_t value) {
441 try {
442 if (m_info->setValue(key, value)) {
443 ATH_MSG_DEBUG("setting " << key << " to " << value);
444 } else {
445 ATH_MSG_DEBUG("error setting " << key << " to " << value);
446 }
447 } catch (std::exception&) {
448 // Processing data not generated events
449 ATH_MSG_DEBUG("Failed to set " << key);
450 }
451 }
452
453void
456 float value) {
457 try {
458 if (m_info->setValue(key, value)) {
459 ATH_MSG_DEBUG("setting " << key << " to " << value);
460 } else {
461 ATH_MSG_DEBUG("error setting " << key << " to " << value);
462 }
463 } catch (std::exception&) {
464 // Processing data not generated events
465 ATH_MSG_DEBUG("Failed to set " << key);
466 }
467 }
468
469void
472 const std::string& value) {
473 if (value.empty()) return;
474 try {
475 if (m_info->setValue(key, value)) {
476 ATH_MSG_DEBUG("setting " << key << " to " << value);
477 } else {
478 ATH_MSG_DEBUG("error setting " << key << " to " << value);
479 }
480 } catch (std::exception&) {
481 // Processing data not generated events
482 ATH_MSG_DEBUG("Failed to set " << key);
483 }
484 }
485
487 const std::string& type, uint32_t value) {
488 try {
489 std::vector<uint32_t> list;
490 if (m_info->value(type, list)) {
491 ATH_MSG_DEBUG("retrieved existing list of " << type);
492 } else {
493 ATH_MSG_DEBUG("adding new list for " << type);
494 }
495 // we want a sorted list of unique values (without using std::set)
496 std::sort(list.begin(), list.end());
497 auto it = std::lower_bound(list.begin(), list.end(), value);
498 if (it == list.end() || (*it) != value) {
499 list.insert(it, value);
500 ATH_MSG_DEBUG("added " << value << " to list of " << type);
501 }
502 if (!m_info->setValue(type, list)) {
503 ATH_MSG_WARNING("error updating list for " + type);
504 }
505 } catch (std::exception& e) {
506 // Processing generated events not data
507 ATH_MSG_WARNING(e.what());
508 }
509}
510
511} // namespace xAODMaker
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_WARNING(x,...)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
static bool isFromSharedWriterWorker(const std::string &key)
Check if the object under this key was received by the SharedWriter from a worker Keep in sync with M...
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
static Double_t sc
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
This class is a collection of AttributeLists where each one is associated with a channel number.
This class provides the layout for summary information stored for data written to POOL.
Definition DataHeader.h:123
This class is a container for conditions data.
const IOVPayloadContainer * payloadContainer() const
Access to payload container.
std::unique_ptr< xAOD::FileMetaData > m_info
The object created for this output stream.
ServiceHandle< ITagInfoMgr > m_tagInfoMgr
Access to TagInfoMgr for tags.
void addUniqueValue(const std::string &type, uint32_t value)
helper function to add values to lists
StatusCode preStream() override
Called before actually streaming objects.
bool m_filledEvent
FileMetaData has been filled with event information.
StatusCode finalize() override
Called at the end of AthenaOutputStream::finalize() (via release()).
ServiceHandle< IAthMetaDataSvc > m_metaDataSvc
Use MetaDataSvc store interface to support output in EventService.
ServiceHandle< StoreGateSvc > m_metaDataStore
void set(const xAOD::FileMetaData::MetaDataType, bool)
helper method to update FileMetaDataProperty with some checks
StatusCode updateFromNonEvent()
Update from Simulation Parameters and TagInfo.
Gaudi::Property< std::string > m_simInfoKey
Read simulation parameters.
void handle(const Incident &) override
Calls updateFromNonEvent on EndInputFile Clears event information inherited from the parent process o...
ServiceHandle< StoreGateSvc > m_eventStore
DataHeader is produced by another OutputTool, so need StoreGateSvc.
ServiceHandle< StoreGateSvc > m_inputMetaDataStore
Access to the input metadata store.
Gaudi::Property< std::string > m_key
output key for produced xAOD::FileMetaData in MetaDataStore
bool m_filledNonEvent
FileMetaData has been filled with non-event info.
std::mutex m_toolMutex
creation of FileMetaData should happen on a single thread
Gaudi::Property< std::string > m_dataHeaderKey
Key for DataHeader in StoreGateSvc.
bool m_hasInputFile
Input file incident check.
Gaudi::Property< std::string > m_eventInfoKey
Key for xAOD::EventInfo to update MC channel number.
StatusCode postExecute() override
Fill the FileMetaData with event information.
StatusCode preFinalize() override
Write the FileMetaData object to the MetaDataStore via the MetaDataSvc.
StatusCode preExecute() override
Called at the beginning of AthenaOutputStream::execute().
std::unique_ptr< xAOD::FileMetaDataAuxInfo > m_aux
The auxiliary containing the created object.
uint32_t lumiBlock() const
The current event's luminosity block number.
uint32_t runNumber() const
The current event's run number.
uint32_t mcChannelNumber() const
The MC generator's channel number.
MetaDataType
Pre-defined metadata value types.
@ beamEnergy
Beam energy [float].
@ mcProcID
Same as mc_channel_number [float].
@ conditionsTag
Conditions version used for simulation/reconstruction [string].
@ dataType
Data type that's in the file [string].
@ generatorsInfo
Generators information [string].
@ AODFixVersion
Version of AODFix that was used on the file last [string].
@ mcCampaign
MC campaign [string].
@ amiTag
AMI tag used to process the file the last time [string].
@ dataYear
Data year [uint32_t].
@ beamType
Beam type [string].
@ simFlavour
Fast or Full sim [string].
@ productionRelease
Release that was used to make the file [string].
@ isDataOverlay
Used data overlay for backgrounds [bool].
@ geometryVersion
Geometry version [string].
::StatusCode StatusCode
StatusCode definition for legacy code.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
EventInfo_v1 EventInfo
Definition of the latest event info version.
FileMetaData_v1 FileMetaData
Declare the latest version of the class.