18#include "GaudiKernel/IIncidentSvc.h"
19#include "GaudiKernel/FileIncident.h"
20#include "GaudiKernel/EventIDBase.h"
21#include "GaudiKernel/EventIDRange.h"
32#include "CoralBase/AttributeListSpecification.h"
33#include "nlohmann/json.hpp"
37 const std::string& name,
38 const IInterface* parent)
39 : base_class(
type, name, parent)
67 incSvc->addListener(
this,
"FirstInputFile", 60);
80 std::map<std::string, std::map<std::string, std::string>> folderPayloads;
82 for (
const auto& [key, value] :
m_payloads.value()) {
84 size_t colonPos = key.find(
':');
85 if (colonPos == std::string::npos) {
86 ATH_MSG_ERROR(
"Invalid payload key format: " << key <<
" (expected 'folder:key')");
87 return StatusCode::FAILURE;
90 std::string folderName = key.substr(0, colonPos);
91 std::string paramName = key.substr(colonPos + 1);
92 folderPayloads[folderName][paramName] = value;
95 ATH_MSG_DEBUG(
"Processing " << folderPayloads.size() <<
" folder(s) for direct payload registration");
97 for (
const auto& [folderName, parameters] : folderPayloads) {
99 if (!parameters.contains(
"beginRun") || !parameters.contains(
"endRun")) {
100 ATH_MSG_ERROR(
"Payload for folder " << folderName <<
" missing beginRun or endRun");
101 return StatusCode::FAILURE;
104 unsigned int beginRun = std::stoul(parameters.at(
"beginRun"));
105 unsigned int endRun = std::stoul(parameters.at(
"endRun"));
108 std::map<std::string, std::string> filteredParams;
109 std::ranges::copy_if(parameters, std::inserter(filteredParams, filteredParams.end()),
110 [](
const auto& p) { return p.first !=
"beginRun" && p.first !=
"endRun"; });
112 ATH_MSG_DEBUG(
"Registering folder " << folderName <<
" with " << filteredParams.size()
113 <<
" parameters, IOV [" << beginRun <<
", " << endRun <<
"]");
120 coral::AttributeListSpecification* spec =
new coral::AttributeListSpecification();
121 for (
const auto& [key, value] : filteredParams) {
122 spec->extend(key,
"string");
125 coral::AttributeList attrList(*spec,
true);
126 for (
const auto& [key, value] : filteredParams) {
127 attrList[key].setValue(value);
130 auto payload = std::make_unique<CondAttrListCollection>(
true);
131 payload->addNewStart(
IOVTime(beginRun, 0));
133 payload->add(0, attrList);
135 ATH_MSG_DEBUG(
"Created payload with IOV [" << beginRun <<
", " << endRun <<
"]");
147 return(StatusCode::SUCCESS);
155 return StatusCode::SUCCESS;
162 const FileIncident* fileInc =
dynamic_cast<const FileIncident*
>(&inc);
163 if(!fileInc)
throw std::runtime_error(
"Unable to get FileName from FirstInputFile incident");
165 const std::string fileName = fileInc->fileName();
166 ATH_MSG_DEBUG(
"handle() " << inc.type() <<
" for " << fileName);
172 if(!
sc.isSuccess())
throw std::runtime_error(
"Could not process input file meta data");
182 return StatusCode::SUCCESS;
187 return StatusCode::SUCCESS;
195 return StatusCode::SUCCESS;
199 std::vector<std::string> iovMetaStrings;
203 ATH_MSG_WARNING(
"Could not find IOVMetaDataContainer for folder " << folderName <<
", skipping");
208 if (!jsonStr.empty()) {
209 iovMetaStrings.push_back(
"IOVMeta." + folderName +
"=" + jsonStr);
210 ATH_MSG_DEBUG(
"Serialized folder " << folderName <<
" (" << jsonStr.size() <<
" bytes JSON)");
214 if (iovMetaStrings.empty()) {
216 return StatusCode::SUCCESS;
221 if (
m_metaDataStore->contains<std::vector<std::string>>(
"IOVMetaDataStrings")) {
223 std::vector<std::string>* existingStrings =
nullptr;
225 existingStrings->insert(existingStrings->end(), iovMetaStrings.begin(), iovMetaStrings.end());
226 ATH_MSG_DEBUG(
"Appended " << iovMetaStrings.size() <<
" IOV metadata strings to existing collection");
229 auto iovMetaData = std::make_unique<std::vector<std::string>>(std::move(iovMetaStrings));
231 ATH_MSG_DEBUG(
"Stored " << iovMetaStrings.size() <<
" IOV metadata strings in MetaDataStore");
234 return StatusCode::SUCCESS;
239 return StatusCode::SUCCESS;
265 ATH_MSG_DEBUG(
"checkOverrideRunNumber: check if tag is set in jobOpts");
269 SmartIF<IProperty> appMgr{serviceLocator()->service(
"ApplicationMgr")};
271 ATH_MSG_ERROR(
"checkOverrideRunNumber: Cannot get ApplicationMgr ");
274 StringProperty property(
"EvtSel",
"");
275 StatusCode
sc = appMgr->getProperty(&property);
276 if (!
sc.isSuccess()) {
277 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get EvtSel: found " << property.value());
281 const std::string eventSelector =
property.value();
282 SmartIF<IProperty> evtSel{serviceLocator()->service(eventSelector)};
284 ATH_MSG_ERROR(
"checkOverrideRunNumber: Cannot get EventSelector " << eventSelector);
289 BooleanProperty overrideRunNumber(
"OverrideRunNumberFromInput",
false);
290 sc = evtSel->getProperty(&overrideRunNumber);
291 if (!
sc.isSuccess()) {
293 ATH_MSG_DEBUG(
"resetRunNumber: unable to get OverrideRunNumberFromInput property from EventSelector ");
299 IntegerProperty runNumber(
"RunNumber", 0);
300 sc = evtSel->getProperty(&runNumber);
301 if (!
sc.isSuccess()) {
302 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get RunNumber from EventSelector: found "
303 << runNumber.value());
308 IntegerProperty oldRunNumber(
"OldRunNumber", 0);
309 sc = evtSel->getProperty(&oldRunNumber);
310 if (!
sc.isSuccess()) {
311 ATH_MSG_ERROR(
"checkOverrideRunNumber: unable to get OldRunNumber from EventSelector: found "
312 << oldRunNumber.value());
320 else ATH_MSG_DEBUG(
"checkOverrideRunNumber: OverrideRunNumberFromInput not set for " << eventSelector);
330 std::string folderDescr =
"<timeStamp>run-event</timeStamp><addrHeader><address_header service_type=\"256\" clid=\"1238547719\" /> </addrHeader><typeName>CondAttrListCollection</typeName>" ;
339 const std::string& folderDescription)
const
342 std::scoped_lock guard(
m_mutex );
348 return(StatusCode::FAILURE);
351 ATH_MSG_DEBUG(
"IOVMetaDataContainer for folder " << folderName <<
" has been registered ");
354 return StatusCode::SUCCESS;
363 std::scoped_lock guard(
m_mutex );
370 ATH_MSG_DEBUG(
"Retrieved IOVMetaDataContainer from MetaDataStore for folder "
374 ATH_MSG_ERROR(
"addPayload: Could not find IOVMetaDataContainer in MetaDataStore for folder "
376 <<
". One must have previously called registerFolder. ");
377 return StatusCode::FAILURE;
386 bool success = cont->
merge(payload);
388 ATH_MSG_DEBUG(
"Added new payload for folder " << folderName);
393 <<
" (may be duplicate payload).");
401 if(payload && msgLvl(MSG::DEBUG)) {
402 std::ostringstream stream;
403 payload->dump(stream);
407 return StatusCode::SUCCESS;
420 ATH_MSG_DEBUG(
"begin modifyPayload for folder " << folderName);
423 bool modifyAttr =
false;
424 std::string attributeName;
427 for (
unsigned int i = 0; i < folders.size(); ++i) {
428 if (folderName == folders[i]) {
429 if (attrs.size() > i) {
430 attributeName = attrs[i];
432 ATH_MSG_DEBUG(
"modifyPayload: remove attribute " << attributeName);
439 ATH_MSG_DEBUG(
"modifyPayload: folder " << folderName <<
" OK ");
440 return StatusCode::SUCCESS;
443 bool iovSizeIsZero = coll->
iov_size() == 0;
456 unsigned int nchans = coll->
size();
457 bool hasChanNames = (coll->
name_size() == nchans);
458 for (
unsigned int ichan = 0; ichan < nchans; ++ichan) {
463 for (
unsigned int iatt = 0; iatt < oldAttrList.size(); ++iatt) {
465 if (attributeName == oldAttrList[iatt].specification().name()) {
466 ATH_MSG_DEBUG(
"modifyPayload: skipping attribute name " << oldAttrList[iatt].specification().name());
471 newAttrList.extend(oldAttrList[iatt].specification().name(),
472 oldAttrList[iatt].specification().
type());
473 const coral::Attribute& oldAttr = oldAttrList[iatt];
474 coral::Attribute& newAttr = newAttrList[oldAttrList[iatt].specification().name()];
478 << oldAttrList[iatt].specification().name() <<
" "
481 coll1->
add(chan, newAttrList);
482 if (!iovSizeIsZero) coll1->
add(chan, coll->
iovRange(chan));
483 if(hasChanNames)coll1->
add(chan, coll->
chanName(chan));
484 ATH_MSG_DEBUG(
"modifyPayload: copied attribute list for channel " << chan);
488 if (msgLvl(MSG::DEBUG)) {
489 std::ostringstream stream;
494 return StatusCode::SUCCESS;
510 ,
const std::string& folderDescription)
const
520 ATH_MSG_DEBUG(
"No IOVMetaDataContainer in MetaDataStore for folder " << folderName
521 <<
". Created a new instance");
523 if (!
sc.isSuccess()) {
524 ATH_MSG_ERROR(
"Could not record IOVMetaDataContainer in MetaDataStore for folder " << folderName);
530 ATH_MSG_DEBUG(
"IOVMetaDataContainer already in MetaDataStore for folder " << folderName);
532 if (!
sc.isSuccess()) {
533 ATH_MSG_ERROR(
"Could not retrieve IOVMetaDataContainer in MetaDataStore for folder " << folderName);
545 std::scoped_lock guard(
m_mutex );
547 ATH_MSG_DEBUG(
"processInputFileMetaData: file name " << fileName);
554 if (!
sc.isSuccess()) {
555 ATH_MSG_DEBUG(
"processInputFileMetaData: Could not retrieve IOVMetaDataContainer objects from InputMetaDataStore - cannot process input file meta data");
556 return StatusCode::SUCCESS;
559 ATH_MSG_DEBUG(
"processInputFileMetaData: Retrieved from IOVMetaDataContainer(s) from InputMetaDataStore");
562 unsigned int ncolls = 0;
563 unsigned int ndupColls = 0;
564 for (; cont != contEnd; ++cont) {
566 , cont->folderDescription());
570 std::list<SG::ObjectWithVersion<IOVMetaDataContainer> > allVersions;
572 if (!
sc.isSuccess()) {
580 ATH_MSG_DEBUG(
"processInputFileMetaData: New container: payload size " << payload->size() <<
" version key " << obj.versionedKey);
583 if (msgLvl(MSG::VERBOSE)) {
585 ATH_MSG_VERBOSE(
"Before merge, payload minRange for folder " << cont->folderName());
586 if (payloadMaster && payloadMaster->
size()) {
590 unsigned int iPayload = 0;
591 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
593 << (*itColl)->size());
603 if (msgLvl(MSG::DEBUG)) {
607 std::ostringstream stream;
608 for (; itColl1 != itCollEnd1; ++itColl1) (*itColl1)->dump(stream);
619 for (; itColl != itCollEnd; ++itColl) {
631 ATH_MSG_ERROR(
"processInputFileMetaData: Could not modify the payload for folder " << contMaster->
folderName());
632 return StatusCode::FAILURE;
636 if (!contMaster->
merge(coll)) {
648 ATH_MSG_DEBUG(
"processInputFileMetaData: Merged together containers for folder " << cont->folderName() <<
" ncoll/ndup "
649 << ncolls <<
" " << ndupColls);
653 if (payloadMaster && payloadMaster->
size()) {
658 if ((*itColl)->minRange().start().isTimestamp()) lastStop =
IOVTime(0);
660 bool hasError =
false;
661 for (; itColl != itCollEnd; ++itColl) {
662 if ((*itColl)->minRange().start() < lastStop) hasError =
true;
663 lastStop = (*itColl)->minRange().stop();
666 ATH_MSG_ERROR(
"processInputFileMetaData: error after merge of file meta data. " );
667 ATH_MSG_ERROR(
"processInputFileMetaData: Filename " << fileName);
669 ATH_MSG_ERROR(
"processInputFileMetaData: MinRange for meta data folders ");
670 unsigned int iPayload = 0;
671 itColl = payloadMaster->
begin();
672 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
673 ATH_MSG_ERROR(iPayload <<
" " << (*itColl)->minRange() <<
" " << (*itColl)->size());
679 if (msgLvl(MSG::VERBOSE)) {
681 ATH_MSG_VERBOSE(
"processInputFileMetaData: After merge, payload minRange ");
686 unsigned int iPayload = 0;
687 for (; itColl != itCollEnd; ++itColl, ++iPayload) {
689 << (*itColl)->size());
696 if (msgLvl(MSG::DEBUG)) {
697 ATH_MSG_DEBUG(
"processInputFileMetaData: Input payload " << cont->folderName());
698 std::ostringstream streamInp;
699 itColl = payload->begin();
700 itCollEnd = payload->end();
701 for (; itColl != itCollEnd; ++itColl) (*itColl)->dump(streamInp);
704 std::ostringstream streamOut;
707 for (; itColl != itCollEnd; ++itColl) (*itColl)->dump(streamOut);
713 ATH_MSG_DEBUG(
"processInputFileMetaData: Total number of attribute collections merged together " << ncolls
714 <<
" Number of duplicate collections " << ndupColls);
723 return StatusCode::SUCCESS;
732 if (!payloads || payloads->
size() == 0) {
737 using json = nlohmann::json;
740 jsonData[
"folder"] =
container->folderName();
741 jsonData[
"description"] =
container->folderDescription();
742 jsonData[
"iovs"] = json::array();
754 if (start.isRunEvent()) {
755 iov[
"range"][
"start"] = {{
"run", start.run()}, {
"event", start.event()}};
756 iov[
"range"][
"stop"] = {{
"run", stop.run()}, {
"event", stop.event()}};
758 iov[
"range"][
"start"] = {{
"timestamp", start.timestamp()}};
759 iov[
"range"][
"stop"] = {{
"timestamp", stop.timestamp()}};
763 iov[
"attrs"] = json::object();
764 for (
const auto& chanAttrPair : *coll) {
766 const coral::AttributeList& attrList = chanAttrPair.second;
768 std::string chanKey =
"chan" + std::to_string(chan);
769 iov[
"attrs"][chanKey] = json::object();
771 for (
const auto& attr : attrList) {
772 auto & thisAttribute = iov[
"attrs"][chanKey][attr.specification().name()];
774 const std::type_info&
type = attr.specification().
type();
775 if (
type ==
typeid(std::string)) {
776 thisAttribute = attr.data<std::string>();
777 }
else if (
type ==
typeid(
int)) {
778 thisAttribute = attr.data<
int>();
779 }
else if (
type ==
typeid(
unsigned int)) {
780 thisAttribute = attr.data<
unsigned int>();
781 }
else if (
type ==
typeid(
long)) {
782 thisAttribute = attr.data<
long>();
783 }
else if (
type ==
typeid(
unsigned long)) {
784 thisAttribute = attr.data<
unsigned long>();
785 }
else if (
type ==
typeid(
long long)) {
786 thisAttribute = attr.data<
long long>();
787 }
else if (
type ==
typeid(
unsigned long long)) {
788 thisAttribute = attr.data<
unsigned long long>();
789 }
else if (
type ==
typeid(
float)) {
790 thisAttribute = attr.data<
float>();
791 }
else if (
type ==
typeid(
double)) {
792 thisAttribute = attr.data<
double>();
793 }
else if (
type ==
typeid(
bool)) {
794 thisAttribute = attr.data<
bool>();
797 std::ostringstream oss;
798 attr.toOutputStream(oss);
799 thisAttribute = oss.str();
800 ATH_MSG_DEBUG(
"Attribute " << attr.specification().name() <<
" has unsupported type, converted to string: " << oss.str());
805 jsonData[
"iovs"].push_back(iov);
808 return jsonData.dump();
826 bool iovSizeIsZero = coll->
iov_size() == 0;
831 if (start.isRunEvent() && stop.isRunEvent()) {
840 return StatusCode::SUCCESS;
845 <<
" iovSizeIsZero: " << (
int)iovSizeIsZero
846 <<
" newRange " << newRange);
859 unsigned int nchans = coll->
size();
861 for (
unsigned int ichan = 0; ichan < nchans; ++ichan) {
864 coll->
add(chan, newRange);
865 ATH_MSG_DEBUG(
"overrideIOV: overriding the IOV of collection chan " << chan);
871 if (msgLvl(MSG::DEBUG)) {
872 ATH_MSG_DEBUG(
"overrideIOV: after overriding the IOV of collection");
873 std::ostringstream stream;
880 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
Hold mappings of ranges to condition objects.
a traits class that associates a CLID to a type T It also detects whether T inherits from Gaudi DataO...
Basic time unit for IOVSvc.
read-copy-update (RCU) style synchronization for Athena.
This class is a collection of AttributeLists where each one is associated with a channel number.
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
ChanNum chanNum(unsigned int index) const
channel number for index: (index = 0 to size-1)
void addNewStart(const IOVTime &start)
Add new start time to minRange - make sure that start is >= to new start.
name_size_type name_size() const
number of Chan/Name pairs
bool add(ChanNum chanNum, const AttributeList &attributeList)
Adding in chan/attrList pairs.
void addNewStop(const IOVTime &stop)
Add new stop time to minRange - make sure that stop is <= to new stop.
void dump() const
Dump our contents to std::cout.
const std::string & chanName(ChanNum chanNum) const
find name for particular channel
void resetMinRange()
Reset minRange according to the IOVs of the contained channels.
const IOVRange & minRange() const
Current minimal IOVRange.
size_type size() const
number of Chan/AttributeList pairs
iov_size_type iov_size() const
number of Chan/IOV pairs
coral::AttributeList AttributeList
const IOVRange & iovRange(ChanNum chanNum) const
IOVRange list for a given channel number.
This class is a container for the payload of conditions data.
size_type size() const
size of payload vector
payloadVec::const_iterator const_iterator
const_iterator begin() const
Begin of payload vector.
const_iterator end() const
End of payload vector.
const IOVTime & stop() const
bool isInRange(const IOVTime &t) const
const IOVTime & start() const
Basic time unit for IOVSvc.
static constexpr uint32_t MAXRUN
static constexpr uint32_t MAXEVENT
a const_iterator facade to DataHandle.
associate a data object with its VersionedKey The object is held by a ReadHandle to delay its retriev...
const std::string & key() const
Get the key string with which the current object was stored.
ElementLink_p1< typename GenerateELinkIndexType_p1< typename LINK::index_type >::type > type