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
25namespace xAODMaker {
26
29 ATH_CHECK(m_eventStore.retrieve());
30 ATH_CHECK(m_metaDataSvc.retrieve());
32 ATH_CHECK(m_metaDataStore.retrieve());
33 ATH_CHECK(m_tagInfoMgr.retrieve());
34
35 // If DataHeader key not specified, try determining it
36 if (m_dataHeaderKey.empty()) {
37 const auto *parentAlg = dynamic_cast< const INamedInterface* >(parent());
38 if (parentAlg)
39 m_dataHeaderKey = parentAlg->name();
40 }
41
42 // Listen for the begin of an input file. Act after MetaDataSvc (prio 80) and
43 // TagInfoMgr (prio 50). That means the FileMetaDataTool be called first
44 ServiceHandle< IIncidentSvc > incidentSvc("IncidentSvc", name());
45 ATH_CHECK(incidentSvc.retrieve());
46 incidentSvc->addListener(this, "EndInputFile", 40);
47
48 // Create a fresh object to fill
49 ATH_MSG_DEBUG("Creating new xAOD::FileMetaData object to fill");
50 m_info = std::make_unique< xAOD::FileMetaData >();
51 m_aux = std::make_unique< xAOD::FileMetaDataAuxInfo >();
52 m_info->setStore(m_aux.get());
53
54 // FileMetaData has no content
55 m_filledNonEvent = false;
56 m_filledEvent = false;
57
58 // Return gracefully:
59 return StatusCode::SUCCESS;
60 }
61
62void
63 FileMetaDataCreatorTool::handle(const Incident& inc) {
64 // gracefully ignore unexpected incident types
65 if (inc.type() == "EndInputFile") {
66 // Lock the tool while we work on the FileMetaData
67 std::lock_guard lock(m_toolMutex);
68 m_hasInputFile = true;
69 if (!updateFromNonEvent().isSuccess())
70 ATH_MSG_DEBUG("Failed to fill FileMetaData with non-event info");
71 }
72 }
73
74StatusCode
76 return StatusCode::SUCCESS;
77 }
78
79StatusCode
81 return StatusCode::SUCCESS;
82 }
83
84StatusCode
86 return StatusCode::SUCCESS;
87 }
88
89StatusCode
91 std::lock_guard lock(m_toolMutex);
92
93 if (!m_filledNonEvent) {
94 ATH_MSG_DEBUG("Not writing empty or incomplete FileMetaData object");
95 return StatusCode::SUCCESS;
96 }
97
98 // Set metadata with content created for given stream
99 for (const std::string& key : m_metaDataSvc->getPerStreamKeysFor(m_key)) {
100 // Remove any existing objects with this key
101 if (!m_metaDataSvc->contains<xAOD::FileMetaData>(key)) {
102 auto info = std::make_unique<xAOD::FileMetaData>();
103 auto aux = std::make_unique<xAOD::FileMetaDataAuxInfo>();
104 info->setStore(aux.get());
105 ATH_CHECK(m_metaDataSvc->record(std::move(info), key));
106 ATH_CHECK(m_metaDataSvc->record(std::move(aux), key + "Aux."));
107 }
108
109 auto* output = m_metaDataSvc->tryRetrieve<xAOD::FileMetaData>(key);
110 if (output) {
111 // save event info that we've already had
112 float orig_mcProcID = -1;
113 std::vector<uint32_t> orig_runNumbers, orig_lumiBlocks;
114 if (!output->value(xAOD::FileMetaData::mcProcID, orig_mcProcID))
115 ATH_MSG_DEBUG("Could not get mcProcID");
116 if (!output->value("runNumbers", orig_runNumbers))
117 ATH_MSG_DEBUG("Could not get runNumbers");
118 if (!output->value("lumiBlocks", orig_lumiBlocks))
119 ATH_MSG_DEBUG("Could not get lumiBlocks");
120
121 // Replace content in store with content created for this stream
122 *output = *m_info;
123 ATH_MSG_DEBUG("FileMetaData payload replaced in store with content created for this stream");
124 if (!m_filledEvent) {
125 // restore original event info if it was not filled for this stream
126 ATH_MSG_DEBUG("Event information was not filled, restoring what we had");
127 if (!output->setValue(xAOD::FileMetaData::mcProcID, orig_mcProcID))
128 ATH_MSG_DEBUG("Could not set " << xAOD::FileMetaData::mcProcID << " to " << orig_mcProcID);
129 if (!output->setValue("runNumbers", orig_runNumbers))
130 ATH_MSG_DEBUG("Could not restore runNumbers");
131 if (!output->setValue("lumiBlocks", orig_lumiBlocks))
132 ATH_MSG_DEBUG("Could not restore lumiBlocks");
133 }
134 } else {
135 ATH_MSG_DEBUG("cannot copy FileMetaData payload to output");
136 }
137 }
138
139 return StatusCode::SUCCESS;
140}
141
142StatusCode
144 return StatusCode::SUCCESS;
145 }
146
147StatusCode
149 // Lock the tool while working with FileMetaData
150 std::lock_guard lock(m_toolMutex);
151
152 // Fill information from TagInfo and Simulation Parameters
153 if (!updateFromNonEvent().isSuccess())
154 ATH_MSG_DEBUG("Failed to fill FileMetaData with non-event info");
155
156 // Sanity check
157 if (!(m_info && m_aux)) {
158 ATH_MSG_DEBUG("No xAOD::FileMetaData object to fill");
159 return StatusCode::SUCCESS;
160 }
161
162 { // MC channel, run and/or lumi block numbers
163 const xAOD::EventInfo* eventInfo = nullptr;
164 StatusCode sc = StatusCode::FAILURE;
165
167 sc = m_eventStore->retrieve(eventInfo, m_eventInfoKey);
168 else if (m_eventStore->contains< xAOD::EventInfo >("Mc" + m_eventInfoKey))
169 sc = m_eventStore->retrieve(eventInfo, "Mc" + m_eventInfoKey);
170
171 if (eventInfo && sc.isSuccess()) {
172 addUniqueValue("runNumbers", eventInfo->runNumber());
173 addUniqueValue("lumiBlocks", eventInfo->lumiBlock());
174 // Return if object has already been filled
175 if (m_filledEvent) return StatusCode::SUCCESS;
176
177 try {
178 ATH_MSG_DEBUG("Retrieved " << m_eventInfoKey);
179
181 const float id = static_cast< float >(eventInfo->mcChannelNumber());
182
183 if (m_info->setValue(type, id))
184 ATH_MSG_DEBUG("setting " << type << " to " << id);
185 else
186 ATH_MSG_DEBUG("error setting " << type << " to " << id);
187 } catch (std::exception&) {
188 // Processing data not generated events
189 ATH_MSG_DEBUG("Failed to set " << xAOD::FileMetaData::mcProcID);
190 }
191 } else {
193 "Failed to retrieve " << m_eventInfoKey << " => cannot set "
195 << ", runNumbers, or lumiBlockNumbers");
196 }
197 }
198
199 m_filledEvent = true;
200
201 return StatusCode::SUCCESS;
202 }
203
204StatusCode
206
207 // Have we already done this?
208 if (m_filledNonEvent) return StatusCode::SUCCESS;
209
210 // Sanity check
211 if (!(m_info && m_aux)) {
212 ATH_MSG_DEBUG("No xAOD::FileMetaData object to fill");
213 return StatusCode::SUCCESS;
214 }
215
216 if (m_hasInputFile && m_tagInfoMgr->getInputTags().empty()) {
217 ATH_MSG_DEBUG("Deferring non-event fill due to eventless-input");
218 return StatusCode::SUCCESS;
219 }
220
222 m_tagInfoMgr->findTag("AtlasRelease"));
223
225 m_tagInfoMgr->findTag("AODFixVersion"));
226
227 set(xAOD::FileMetaData::amiTag, m_tagInfoMgr->findTag("AMITag"));
228
230
232 m_tagInfoMgr->findTag("IOVDbGlobalTag"));
233
234 set(xAOD::FileMetaData::beamType, m_tagInfoMgr->findTag("beam_type"));
235
236 set(xAOD::FileMetaData::mcCampaign, m_tagInfoMgr->findTag("mc_campaign"));
237
238 set(xAOD::FileMetaData::generatorsInfo, m_tagInfoMgr->findTag("generators"));
239
240 std::string beamEnergy = m_tagInfoMgr->findTag("beam_energy");
241 try {
243 std::stof(beamEnergy));
244 } catch (std::invalid_argument& e) {
245 ATH_MSG_DEBUG("beam energy \"" << beamEnergy << "\" tag could not be converted to float");
246 } catch (std::out_of_range& e) {
247 ATH_MSG_DEBUG("converted beam energy value (\"" << beamEnergy << "\") outside float range");
248 }
249
250 std::string dataYear = m_tagInfoMgr->findTag("data_year");
251 if (!dataYear.empty()) {
252 try {
254 static_cast<uint32_t>(std::stoul(dataYear)));
255 } catch (std::invalid_argument& e) {
256 ATH_MSG_DEBUG("data year \"" << dataYear << "\" tag could not be converted to unsigned long");
257 } catch (std::out_of_range& e) {
258 ATH_MSG_DEBUG("converted data year value (\"" << dataYear << "\") outside unsigned long range");
259 }
260 }
261
262 // Read simulation parameters
263 const IOVMetaDataContainer * simInfo = nullptr;
264 StatusCode sc = StatusCode::FAILURE;
266 sc = m_inputMetaDataStore->retrieve(simInfo, m_simInfoKey);
267 } else if (m_metaDataStore->contains< IOVMetaDataContainer >(m_simInfoKey)) {
268 sc = m_metaDataStore->retrieve(simInfo, m_simInfoKey);
269 }
270 const coral::AttributeList * attrList = nullptr;
271 if (simInfo && sc.isSuccess()) {
272 for (const CondAttrListCollection* payload : *simInfo->payloadContainer()) {
273 for (const auto& itr : *payload) {
274 attrList = &(itr.second);
275 }
276 }
277 }
278
279 bool simFlavourSet{};
280 bool isDataOverlaySet{};
281 if (attrList) {
282 { // set simulation flavor
283 std::string key = "SimulationFlavour";
284 if (attrList->exists(key)) {
285 std::string value = (*attrList)[key].data< std::string >();
286
287 // remap simulation flavor "default" to "FullSim"
288 if (value == "default")
289 value = "FullSim";
290
292 simFlavourSet = true;
293 }
294 }
295
296 { // set whether this is overlay
297 std::string key = "IsDataOverlay";
298 if (attrList->exists(key)) {
299 std::string attr = (*attrList)[key].data< std::string >();
300 set(xAOD::FileMetaData::isDataOverlay, attr == "True");
301 isDataOverlaySet = true;
302 }
303 }
304 }
305
306 if (!simFlavourSet || !isDataOverlaySet) {
308 "Failed to set "
311 << ". Trying to get them from input metadata store." );
312
313 for (const std::string& key : m_metaDataSvc->getPerStreamKeysFor(m_key)) {
314 const xAOD::FileMetaData* input = nullptr;
315 input = m_inputMetaDataStore->tryConstRetrieve< xAOD::FileMetaData >(key);
316 if (input) {
317 if (!simFlavourSet) {
318 std::string orig_simFlavour = "none";
319 if (!input->value(xAOD::FileMetaData::simFlavour, orig_simFlavour)) {
321 "Could not get xAOD::FileMetaData::simFlavour "
322 "from input metadata "
323 "store");
324 } else {
325 ATH_MSG_DEBUG("Retrieved from input metadata store: "
327 << orig_simFlavour);
328 set(xAOD::FileMetaData::simFlavour, orig_simFlavour);
329 }
330 }
331
332 if (!isDataOverlaySet) {
333 bool orig_isDataOverlay = false;
334 if (!input->value(xAOD::FileMetaData::isDataOverlay, orig_isDataOverlay)) {
336 "Could not get "
337 "xAOD::FileMetaData::isDataOverlay from input "
338 "metadata store");
339 } else {
340 ATH_MSG_DEBUG("Retrieved from input metadata store: "
342 << orig_isDataOverlay);
343 set(xAOD::FileMetaData::isDataOverlay, orig_isDataOverlay);
344 }
345 }
346 }
347 }
348 }
349
350 if (m_filledNonEvent) return StatusCode::SUCCESS;
351
352 { // get dataType
354 try {
355 if (m_info->setValue(type, m_dataHeaderKey.value()))
356 ATH_MSG_DEBUG("set " << type << " to " << m_dataHeaderKey.value());
357 else
358 ATH_MSG_DEBUG("error setting " << type << " to " << m_dataHeaderKey.value());
359 } catch (std::exception&) {
360 // This is unexpected
361 ATH_MSG_DEBUG("Failed to set " << xAOD::FileMetaData::dataType);
362 }
363 }
364
365 // FileMetaData object has been filled with non event info
366 m_filledNonEvent = true;
367
368 return StatusCode::SUCCESS;
369 }
370
371void
374 bool value) {
375 try {
376 if (m_info->setValue(key, value))
377 ATH_MSG_DEBUG("setting " << key << " to " << value);
378 else
379 ATH_MSG_DEBUG("error setting " << key << " to " << std::boolalpha << value
380 << std::noboolalpha);
381 } catch (std::exception&) {
382 // Processing data not generated events
383 ATH_MSG_DEBUG("Failed to set " << key);
384 }
385 }
386
387void
390 uint32_t value) {
391 try {
392 if (m_info->setValue(key, value))
393 ATH_MSG_DEBUG("setting " << key << " to " << value);
394 else
395 ATH_MSG_DEBUG("error setting " << key << " to " << value);
396 } catch (std::exception&) {
397 // Processing data not generated events
398 ATH_MSG_DEBUG("Failed to set " << key);
399 }
400 }
401
402void
405 float value) {
406 try {
407 if (m_info->setValue(key, value))
408 ATH_MSG_DEBUG("setting " << key << " to " << value);
409 else
410 ATH_MSG_DEBUG("error setting " << key << " to " << value);
411 } catch (std::exception&) {
412 // Processing data not generated events
413 ATH_MSG_DEBUG("Failed to set " << key);
414 }
415 }
416
417void
420 const std::string& value) {
421 if (value.empty()) return;
422 try {
423 if (m_info->setValue(key, value))
424 ATH_MSG_DEBUG("setting " << key << " to " << value);
425 else
426 ATH_MSG_DEBUG("error setting " << key << " to " << value);
427 } catch (std::exception&) {
428 // Processing data not generated events
429 ATH_MSG_DEBUG("Failed to set " << key);
430 }
431 }
432
434 const std::string& type, uint32_t value) {
435 try {
436 std::vector<uint32_t> list;
437 if (m_info->value(type, list)) {
438 ATH_MSG_DEBUG("retrieved existing list of " << type);
439 } else {
440 ATH_MSG_DEBUG("adding new list for " << type);
441 }
442 // we want a sorted list of unique values (without using std::set)
443 std::sort(list.begin(), list.end());
444 auto it = std::lower_bound(list.begin(), list.end(), value);
445 if (it == list.end() || (*it) != value) {
446 list.insert(it, value);
447 ATH_MSG_DEBUG("added " << value << " to list of " << type);
448 }
449 if (!m_info->setValue(type, list)) {
450 ATH_MSG_WARNING("error updating list for " + type);
451 }
452 } catch (std::exception& e) {
453 // Processing generated events not data
454 ATH_MSG_WARNING(e.what());
455 }
456}
457
458} // 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...
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 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
Handle BeginInputFile incident after MetaDataSvc.
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.